[TASK] Use name-resolution instead of strings where possible: 7
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / Unit / Page / PageRepositoryTest.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Tests\Unit\Page;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 /**
17 * Test case
18 *
19 * @author Christian Kuhn <lolli@schwarzbu.ch>
20 * @author Oliver Klee <typo3-coding@oliverklee.de>
21 */
22 class PageRepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
23
24 /**
25 * @var \TYPO3\CMS\Frontend\Page\PageRepository|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
26 */
27 protected $pageSelectObject;
28
29 /**
30 * Sets up this testcase
31 */
32 public function setUp() {
33 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array('exec_SELECTquery', 'sql_fetch_assoc', 'sql_free_result'));
34 $this->pageSelectObject = $this->getAccessibleMock(\TYPO3\CMS\Frontend\Page\PageRepository::class, array('getMultipleGroupsWhereClause'));
35 $this->pageSelectObject->expects($this->any())->method('getMultipleGroupsWhereClause')->will($this->returnValue(' AND 1=1'));
36 }
37
38 /**
39 * Tests whether the getPage Hook is called correctly.
40 *
41 * @test
42 */
43 public function isGetPageHookCalled() {
44 // Create a hook mock object
45 $className = uniqid('tx_coretest');
46 $getPageHookMock = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepositoryGetPageHookInterface::class, array('getPage_preProcess'), array(), $className);
47 // Register hook mock object
48 $GLOBALS['T3_VAR']['getUserObj'][$className] = $getPageHookMock;
49 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPage'][] = $className;
50 // Test if hook is called and register a callback method to check given arguments
51 $getPageHookMock->expects($this->once())->method('getPage_preProcess')->will($this->returnCallback(array($this, 'isGetPagePreProcessCalledCallback')));
52 $this->pageSelectObject->getPage(42, FALSE);
53 }
54
55 /**
56 * Handles the arguments that have been sent to the getPage_preProcess hook
57 */
58 public function isGetPagePreProcessCalledCallback() {
59 list($uid, $disableGroupAccessCheck, $parent) = func_get_args();
60 $this->assertEquals(42, $uid);
61 $this->assertFalse($disableGroupAccessCheck);
62 $this->assertTrue($parent instanceof \TYPO3\CMS\Frontend\Page\PageRepository);
63 }
64
65 /////////////////////////////////////////
66 // Tests concerning getPathFromRootline
67 /////////////////////////////////////////
68 /**
69 * @test
70 */
71 public function getPathFromRootLineForEmptyRootLineReturnsEmptyString() {
72 $this->assertEquals('', $this->pageSelectObject->getPathFromRootline(array()));
73 }
74
75 ///////////////////////////////
76 // Tests concerning getExtURL
77 ///////////////////////////////
78 /**
79 * @test
80 */
81 public function getExtUrlForDokType3AndUrlType1AddsHttpSchemeToUrl() {
82 $this->assertEquals('http://www.example.com', $this->pageSelectObject->getExtURL(array(
83 'doktype' => \TYPO3\CMS\Frontend\Page\PageRepository::DOKTYPE_LINK,
84 'urltype' => 1,
85 'url' => 'www.example.com'
86 )));
87 }
88
89 /**
90 * @test
91 */
92 public function getExtUrlForDokType3AndUrlType0PrependsSiteUrl() {
93 $this->assertEquals(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'hello/world/', $this->pageSelectObject->getExtURL(array(
94 'doktype' => \TYPO3\CMS\Frontend\Page\PageRepository::DOKTYPE_LINK,
95 'urltype' => 0,
96 'url' => 'hello/world/'
97 )));
98 }
99
100 /////////////////////////////////////////
101 // Tests concerning shouldFieldBeOverlaid
102 /////////////////////////////////////////
103 /**
104 * @test
105 * @dataProvider getShouldFieldBeOverlaidData
106 */
107 public function shouldFieldBeOverlaid($field, $table, $value, $expected, $comment = '') {
108 $GLOBALS['TCA']['fake_table']['columns'] = array(
109 'exclude' => array(
110 'l10n_mode' => 'exclude',
111 'config' => array('type' => 'input'),
112 ),
113 'mergeIfNotBlank' => array(
114 'l10n_mode' => 'mergeIfNotBlank',
115 'config' => array('type' => 'input'),
116 ),
117 'mergeIfNotBlank_group' => array(
118 'l10n_mode' => 'mergeIfNotBlank',
119 'config' => array('type' => 'group'),
120 ),
121 'default' => array(
122 // no l10n_mode set
123 'config' => array('type' => 'input'),
124 ),
125 'noCopy' => array(
126 'l10n_mode' => 'noCopy',
127 'config' => array('type' => 'input'),
128 ),
129 'prefixLangTitle' => array(
130 'l10n_mode' => 'prefixLangTitle',
131 'config' => array('type' => 'input'),
132 ),
133 );
134
135 $result = $this->pageSelectObject->_call('shouldFieldBeOverlaid', $table, $field, $value);
136 unset($GLOBALS['TCA']['fake_table']);
137
138 $this->assertSame($expected, $result, $comment);
139 }
140
141 /**
142 * Data provider for shouldFieldBeOverlaid
143 */
144 public function getShouldFieldBeOverlaidData() {
145 return array(
146 array('default', 'fake_table', 'foobar', TRUE, 'default is to merge non-empty string'),
147 array('default', 'fake_table', '', TRUE, 'default is to merge empty string'),
148
149 array('exclude', 'fake_table', '', FALSE, 'exclude field with empty string'),
150 array('exclude', 'fake_table', 'foobar', FALSE, 'exclude field with non-empty string'),
151
152 array('mergeIfNotBlank', 'fake_table', '', FALSE, 'mergeIfNotBlank is not merged with empty string'),
153 array('mergeIfNotBlank', 'fake_table', 0, TRUE, 'mergeIfNotBlank is merged with 0'),
154 array('mergeIfNotBlank', 'fake_table', '0', TRUE, 'mergeIfNotBlank is merged with "0"'),
155 array('mergeIfNotBlank', 'fake_table', 'foobar', TRUE, 'mergeIfNotBlank is merged with non-empty string'),
156
157 array('mergeIfNotBlank_group', 'fake_table', '', FALSE, 'mergeIfNotBlank on group is not merged empty string'),
158 array('mergeIfNotBlank_group', 'fake_table', 0, FALSE, 'mergeIfNotBlank on group is not merged with 0'),
159 array('mergeIfNotBlank_group', 'fake_table', '0', FALSE, 'mergeIfNotBlank on group is not merged with "0"'),
160 array('mergeIfNotBlank_group', 'fake_table', 'foobar', TRUE, 'mergeIfNotBlank on group is merged with non-empty string'),
161
162 array('noCopy', 'fake_table', 'foobar', TRUE, 'noCopy is merged with non-empty string'),
163 array('noCopy', 'fake_table', '', TRUE, 'noCopy is merged with empty string'),
164
165 array('prefixLangTitle', 'fake_table', 'foobar', TRUE, 'prefixLangTitle is merged with non-empty string'),
166 array('prefixLangTitle', 'fake_table', '', TRUE, 'prefixLangTitle is merged with empty string'),
167 );
168 }
169
170 ////////////////////////////////
171 // Tests concerning workspaces
172 ////////////////////////////////
173
174 /**
175 * @test
176 */
177 public function noPagesFromWorkspaceAreShownLive() {
178 // initialization
179 $wsid = 987654321;
180
181 // simulate calls from \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->fetch_the_id()
182 $this->pageSelectObject->versioningPreview = FALSE;
183 $this->pageSelectObject->versioningWorkspaceId = $wsid;
184 $this->pageSelectObject->init(FALSE);
185
186 // check SQL created by \TYPO3\CMS\Frontend\Page\PageRepository->getPage()
187 $GLOBALS['TYPO3_DB']->expects($this->once())
188 ->method('exec_SELECTquery')
189 ->with(
190 '*',
191 'pages',
192 $this->logicalAnd(
193 $this->logicalNot(
194 $this->stringContains('(pages.t3ver_wsid=0 or pages.t3ver_wsid=' . $wsid . ')')
195 ),
196 $this->stringContains('AND NOT pages.t3ver_state>0')
197 )
198 );
199
200 $this->pageSelectObject->getPage(1);
201
202 }
203
204 /**
205 * @test
206 */
207 public function previewShowsPagesFromLiveAndCurrentWorkspace() {
208 // initialization
209 $wsid = 987654321;
210
211 // simulate calls from \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->fetch_the_id()
212 $this->pageSelectObject->versioningPreview = TRUE;
213 $this->pageSelectObject->versioningWorkspaceId = $wsid;
214 $this->pageSelectObject->init(FALSE);
215
216 // check SQL created by \TYPO3\CMS\Frontend\Page\PageRepository->getPage()
217 $GLOBALS['TYPO3_DB']->expects($this->once())
218 ->method('exec_SELECTquery')
219 ->with(
220 '*',
221 'pages',
222 $this->stringContains('(pages.t3ver_wsid=0 or pages.t3ver_wsid=' . $wsid . ')')
223 );
224
225 $this->pageSelectObject->getPage(1);
226
227 }
228
229 ////////////////////////////////
230 // Tests concerning versioning
231 ////////////////////////////////
232
233 /**
234 * @test
235 */
236 public function enableFieldsHidesVersionedRecordsAndPlaceholders() {
237 $table = uniqid('aTable');
238 $GLOBALS['TCA'] = array(
239 $table => array(
240 'ctrl' => array(
241 'versioningWS' => 2
242 )
243 )
244 );
245
246 $this->pageSelectObject->versioningPreview = FALSE;
247 $this->pageSelectObject->init(FALSE);
248
249 $conditions = $this->pageSelectObject->enableFields($table);
250
251 $this->assertThat($conditions, $this->stringContains(' AND ' . $table . '.t3ver_state<=0'), 'Versioning placeholders');
252 $this->assertThat($conditions, $this->stringContains(' AND ' . $table . '.pid<>-1'), 'Records from page -1');
253 }
254
255 /**
256 * @test
257 */
258 public function enableFieldsDoesNotHidePlaceholdersInPreview() {
259 $table = uniqid('aTable');
260 $GLOBALS['TCA'] = array(
261 $table => array(
262 'ctrl' => array(
263 'versioningWS' => 2
264 )
265 )
266 );
267
268 $this->pageSelectObject->versioningPreview = TRUE;
269 $this->pageSelectObject->init(FALSE);
270
271 $conditions = $this->pageSelectObject->enableFields($table);
272
273 $this->assertThat($conditions, $this->logicalNot($this->stringContains(' AND ' . $table . '.t3ver_state<=0')), 'No versioning placeholders');
274 $this->assertThat($conditions, $this->stringContains(' AND ' . $table . '.pid<>-1'), 'Records from page -1');
275 }
276
277 /**
278 * @test
279 */
280 public function enableFieldsDoesFilterToCurrentAndLiveWorkspaceForRecordsInPreview() {
281 $table = uniqid('aTable');
282 $GLOBALS['TCA'] = array(
283 $table => array(
284 'ctrl' => array(
285 'versioningWS' => 2
286 )
287 )
288 );
289
290 $this->pageSelectObject->versioningPreview = TRUE;
291 $this->pageSelectObject->versioningWorkspaceId = 2;
292 $this->pageSelectObject->init(FALSE);
293
294 $conditions = $this->pageSelectObject->enableFields($table);
295
296 $this->assertThat($conditions, $this->stringContains(' AND (' . $table . '.t3ver_wsid=0 OR ' . $table . '.t3ver_wsid=2)'), 'No versioning placeholders');
297 }
298
299 /**
300 * @test
301 */
302 public function enableFieldsDoesNotHideVersionedRecordsWhenCheckingVersionOverlays() {
303 $table = uniqid('aTable');
304 $GLOBALS['TCA'] = array(
305 $table => array(
306 'ctrl' => array(
307 'versioningWS' => 2
308 )
309 )
310 );
311
312 $this->pageSelectObject->versioningPreview = TRUE;
313 $this->pageSelectObject->init(FALSE);
314
315 $conditions = $this->pageSelectObject->enableFields($table, -1, array(), TRUE );
316
317 $this->assertThat($conditions, $this->logicalNot($this->stringContains(' AND ' . $table . '.t3ver_state<=0')), 'No versioning placeholders');
318 $this->assertThat($conditions, $this->logicalNot($this->stringContains(' AND ' . $table . '.pid<>-1')), 'No ecords from page -1');
319 }
320
321
322 }