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