960ec4e9ae33ab623e5535657b8473bf3a791e25
[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 /**
18 * Test case
19 */
20 class PageRepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * @var \TYPO3\CMS\Frontend\Page\PageRepository|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
24 */
25 protected $pageSelectObject;
26
27 protected $defaultTcaForPages = array(
28 'ctrl' => array(
29 'label' => 'title',
30 'tstamp' => 'tstamp',
31 'sortby' => 'sorting',
32 'type' => 'doktype',
33 'versioningWS' => true,
34 'origUid' => 't3_origuid',
35 'delete' => 'deleted',
36 'enablecolumns' => array(
37 'disabled' => 'hidden',
38 'starttime' => 'starttime',
39 'endtime' => 'endtime',
40 'fe_group' => 'fe_group'
41 ),
42 ),
43 'columns' => array()
44 );
45
46 /**
47 * Sets up this testcase
48 */
49 protected function setUp()
50 {
51 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array('sql_fetch_assoc', 'sql_free_result', 'exec_SELECTgetSingleRow'));
52 $this->pageSelectObject = $this->getAccessibleMock(\TYPO3\CMS\Frontend\Page\PageRepository::class, array('getMultipleGroupsWhereClause'));
53 $this->pageSelectObject->expects($this->any())->method('getMultipleGroupsWhereClause')->will($this->returnValue(' AND 1=1'));
54 }
55
56 /**
57 * Tests whether the getPage Hook is called correctly.
58 *
59 * @test
60 */
61 public function isGetPageHookCalled()
62 {
63 // Create a hook mock object
64 $className = $this->getUniqueId('tx_coretest');
65 $getPageHookMock = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepositoryGetPageHookInterface::class, array('getPage_preProcess'), array(), $className);
66 // Register hook mock object
67 $GLOBALS['T3_VAR']['getUserObj'][$className] = $getPageHookMock;
68 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPage'][] = $className;
69 // Test if hook is called and register a callback method to check given arguments
70 $getPageHookMock->expects($this->once())->method('getPage_preProcess')->will($this->returnCallback(array($this, 'isGetPagePreProcessCalledCallback')));
71 $this->pageSelectObject->getPage(42, false);
72 }
73
74 /**
75 * Handles the arguments that have been sent to the getPage_preProcess hook
76 */
77 public function isGetPagePreProcessCalledCallback()
78 {
79 list($uid, $disableGroupAccessCheck, $parent) = func_get_args();
80 $this->assertEquals(42, $uid);
81 $this->assertFalse($disableGroupAccessCheck);
82 $this->assertTrue($parent instanceof \TYPO3\CMS\Frontend\Page\PageRepository);
83 }
84
85 /////////////////////////////////////////
86 // Tests concerning getPathFromRootline
87 /////////////////////////////////////////
88 /**
89 * @test
90 */
91 public function getPathFromRootLineForEmptyRootLineReturnsEmptyString()
92 {
93 $this->assertEquals('', $this->pageSelectObject->getPathFromRootline(array()));
94 }
95
96 ///////////////////////////////
97 // Tests concerning getExtURL
98 ///////////////////////////////
99 /**
100 * @test
101 */
102 public function getExtUrlForDokType3AndUrlType1AddsHttpSchemeToUrl()
103 {
104 $this->assertEquals('http://www.example.com', $this->pageSelectObject->getExtURL(array(
105 'doktype' => \TYPO3\CMS\Frontend\Page\PageRepository::DOKTYPE_LINK,
106 'urltype' => 1,
107 'url' => 'www.example.com'
108 )));
109 }
110
111 /**
112 * @test
113 */
114 public function getExtUrlForDokType3AndUrlType0PrependsSiteUrl()
115 {
116 $this->assertEquals(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'hello/world/', $this->pageSelectObject->getExtURL(array(
117 'doktype' => \TYPO3\CMS\Frontend\Page\PageRepository::DOKTYPE_LINK,
118 'urltype' => 0,
119 'url' => 'hello/world/'
120 )));
121 }
122
123 /////////////////////////////////////////
124 // Tests concerning shouldFieldBeOverlaid
125 /////////////////////////////////////////
126 /**
127 * @test
128 * @dataProvider getShouldFieldBeOverlaidData
129 */
130 public function shouldFieldBeOverlaid($field, $table, $value, $expected, $comment = '')
131 {
132 $GLOBALS['TCA']['fake_table']['columns'] = array(
133 'exclude' => array(
134 'l10n_mode' => 'exclude',
135 'config' => array('type' => 'input'),
136 ),
137 'mergeIfNotBlank' => array(
138 'l10n_mode' => 'mergeIfNotBlank',
139 'config' => array('type' => 'input'),
140 ),
141 'mergeIfNotBlank_group' => array(
142 'l10n_mode' => 'mergeIfNotBlank',
143 'config' => array('type' => 'group'),
144 ),
145 'default' => array(
146 // no l10n_mode set
147 'config' => array('type' => 'input'),
148 ),
149 'noCopy' => array(
150 'l10n_mode' => 'noCopy',
151 'config' => array('type' => 'input'),
152 ),
153 'prefixLangTitle' => array(
154 'l10n_mode' => 'prefixLangTitle',
155 'config' => array('type' => 'input'),
156 ),
157 );
158
159 $result = $this->pageSelectObject->_call('shouldFieldBeOverlaid', $table, $field, $value);
160 unset($GLOBALS['TCA']['fake_table']);
161
162 $this->assertSame($expected, $result, $comment);
163 }
164
165 /**
166 * Data provider for shouldFieldBeOverlaid
167 */
168 public function getShouldFieldBeOverlaidData()
169 {
170 return array(
171 array('default', 'fake_table', 'foobar', true, 'default is to merge non-empty string'),
172 array('default', 'fake_table', '', true, 'default is to merge empty string'),
173
174 array('exclude', 'fake_table', '', false, 'exclude field with empty string'),
175 array('exclude', 'fake_table', 'foobar', false, 'exclude field with non-empty string'),
176
177 array('mergeIfNotBlank', 'fake_table', '', false, 'mergeIfNotBlank is not merged with empty string'),
178 array('mergeIfNotBlank', 'fake_table', 0, true, 'mergeIfNotBlank is merged with 0'),
179 array('mergeIfNotBlank', 'fake_table', '0', true, 'mergeIfNotBlank is merged with "0"'),
180 array('mergeIfNotBlank', 'fake_table', 'foobar', true, 'mergeIfNotBlank is merged with non-empty string'),
181
182 array('mergeIfNotBlank_group', 'fake_table', '', false, 'mergeIfNotBlank on group is not merged empty string'),
183 array('mergeIfNotBlank_group', 'fake_table', 0, false, 'mergeIfNotBlank on group is not merged with 0'),
184 array('mergeIfNotBlank_group', 'fake_table', '0', false, 'mergeIfNotBlank on group is not merged with "0"'),
185 array('mergeIfNotBlank_group', 'fake_table', 'foobar', true, 'mergeIfNotBlank on group is merged with non-empty string'),
186
187 array('noCopy', 'fake_table', 'foobar', true, 'noCopy is merged with non-empty string'),
188 array('noCopy', 'fake_table', '', true, 'noCopy is merged with empty string'),
189
190 array('prefixLangTitle', 'fake_table', 'foobar', true, 'prefixLangTitle is merged with non-empty string'),
191 array('prefixLangTitle', 'fake_table', '', true, 'prefixLangTitle is merged with empty string'),
192 );
193 }
194
195 ////////////////////////////////
196 // Tests concerning workspaces
197 ////////////////////////////////
198
199 /**
200 * @test
201 */
202 public function noPagesFromWorkspaceAreShownLive()
203 {
204 // initialization
205 $wsid = 987654321;
206 $GLOBALS['TCA'] = array(
207 'pages' => $this->defaultTcaForPages
208 );
209
210 // simulate calls from \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->fetch_the_id()
211 $this->pageSelectObject->versioningPreview = false;
212 $this->pageSelectObject->versioningWorkspaceId = $wsid;
213 $this->pageSelectObject->init(false);
214
215 // check SQL created by \TYPO3\CMS\Frontend\Page\PageRepository->getPage()
216 $GLOBALS['TYPO3_DB']->expects($this->at(2))
217 ->method('exec_SELECTgetSingleRow')
218 ->with(
219 '*',
220 'pages',
221 $this->logicalAnd(
222 $this->logicalNot(
223 $this->stringContains('(pages.t3ver_wsid=0 or pages.t3ver_wsid=' . $wsid . ')')
224 ),
225 $this->stringContains('AND pages.t3ver_state<=0')
226 )
227 );
228
229 $this->pageSelectObject->getPage(1);
230 }
231
232 /**
233 * @test
234 */
235 public function previewShowsPagesFromLiveAndCurrentWorkspace()
236 {
237 // initialization
238 $wsid = 987654321;
239 $GLOBALS['TCA'] = array(
240 'pages' => $this->defaultTcaForPages
241 );
242
243 // simulate calls from \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->fetch_the_id()
244 $this->pageSelectObject->versioningPreview = true;
245 $this->pageSelectObject->versioningWorkspaceId = $wsid;
246 $this->pageSelectObject->init(false);
247
248 // check SQL created by \TYPO3\CMS\Frontend\Page\PageRepository->getPage()
249 $GLOBALS['TYPO3_DB']->expects($this->at(2))
250 ->method('exec_SELECTgetSingleRow')
251 ->with(
252 '*',
253 'pages',
254 $this->stringContains('(pages.t3ver_wsid=0 or pages.t3ver_wsid=' . $wsid . ')')
255 );
256
257 $this->pageSelectObject->getPage(1);
258 }
259
260 ////////////////////////////////
261 // Tests concerning versioning
262 ////////////////////////////////
263
264 /**
265 * @test
266 */
267 public function enableFieldsHidesVersionedRecordsAndPlaceholders()
268 {
269 $table = $this->getUniqueId('aTable');
270 $GLOBALS['TCA'] = array(
271 'pages' => $this->defaultTcaForPages,
272 $table => array(
273 'ctrl' => array(
274 'versioningWS' => true
275 )
276 )
277 );
278
279 $this->pageSelectObject->versioningPreview = false;
280 $this->pageSelectObject->init(false);
281
282 $conditions = $this->pageSelectObject->enableFields($table);
283
284 $this->assertThat($conditions, $this->stringContains(' AND ' . $table . '.t3ver_state<=0'), 'Versioning placeholders');
285 $this->assertThat($conditions, $this->stringContains(' AND ' . $table . '.pid<>-1'), 'Records from page -1');
286 }
287
288 /**
289 * @test
290 */
291 public function enableFieldsDoesNotHidePlaceholdersInPreview()
292 {
293 $table = $this->getUniqueId('aTable');
294 $GLOBALS['TCA'] = array(
295 'pages' => $this->defaultTcaForPages,
296 $table => array(
297 'ctrl' => array(
298 'versioningWS' => true
299 )
300 )
301 );
302
303 $this->pageSelectObject->versioningPreview = true;
304 $this->pageSelectObject->init(false);
305
306 $conditions = $this->pageSelectObject->enableFields($table);
307
308 $this->assertThat($conditions, $this->logicalNot($this->stringContains(' AND ' . $table . '.t3ver_state<=0')), 'No versioning placeholders');
309 $this->assertThat($conditions, $this->stringContains(' AND ' . $table . '.pid<>-1'), 'Records from page -1');
310 }
311
312 /**
313 * @test
314 */
315 public function enableFieldsDoesFilterToCurrentAndLiveWorkspaceForRecordsInPreview()
316 {
317 $table = $this->getUniqueId('aTable');
318 $GLOBALS['TCA'] = array(
319 'pages' => $this->defaultTcaForPages,
320 $table => array(
321 'ctrl' => array(
322 'versioningWS' => true
323 )
324 )
325 );
326
327 $this->pageSelectObject->versioningPreview = true;
328 $this->pageSelectObject->versioningWorkspaceId = 2;
329 $this->pageSelectObject->init(false);
330
331 $conditions = $this->pageSelectObject->enableFields($table);
332
333 $this->assertThat($conditions, $this->stringContains(' AND (' . $table . '.t3ver_wsid=0 OR ' . $table . '.t3ver_wsid=2)'), 'No versioning placeholders');
334 }
335
336 /**
337 * @test
338 */
339 public function enableFieldsDoesNotHideVersionedRecordsWhenCheckingVersionOverlays()
340 {
341 $table = $this->getUniqueId('aTable');
342 $GLOBALS['TCA'] = array(
343 'pages' => $this->defaultTcaForPages,
344 $table => array(
345 'ctrl' => array(
346 'versioningWS' => true
347 )
348 )
349 );
350
351 $this->pageSelectObject->versioningPreview = true;
352 $this->pageSelectObject->init(false);
353
354 $conditions = $this->pageSelectObject->enableFields($table, -1, array(), true);
355
356 $this->assertThat($conditions, $this->logicalNot($this->stringContains(' AND ' . $table . '.t3ver_state<=0')), 'No versioning placeholders');
357 $this->assertThat($conditions, $this->logicalNot($this->stringContains(' AND ' . $table . '.pid<>-1')), 'No ecords from page -1');
358 }
359 }