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