[FEATURE] HMENU - sectionIndex: where clause should be configurable
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tests / tslib / class.tslib_menuTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Stefan Galinski <stefan.galinski@gmail.com>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Testcase for the "tslib_menu" class in the TYPO3 Core.
27 *
28 * @package TYPO3
29 * @subpackage tslib
30 *
31 * @author Stefan Galinski <stefan.galinski@gmail.com>
32 */
33 class tslib_menuTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
34 /**
35 * @var tslib_menu
36 */
37 private $fixture = NULL;
38
39 /**
40 * @var array
41 */
42 private $backupGlobalVariables = array();
43
44 public function setUp() {
45 $proxy = $this->buildAccessibleProxy('tslib_menu');
46 $this->fixture = new $proxy;
47
48 $backupGlobalVariables['TYPO3_DB'] = $GLOBALS['TYPO3_DB'];
49 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_db');
50
51 $backupGlobalVariables['TSFE'] = $GLOBALS['TSFE'];
52 $GLOBALS['TSFE'] = $this->getMock('tslib_fe');
53 $GLOBALS['TSFE']->cObj = new tslib_cObj();
54 }
55
56 public function tearDown() {
57 foreach ($this->backupGlobalVariables as $key => $data) {
58 $GLOBALS[$key] = $data;
59 }
60
61 unset($this->fixture);
62 }
63
64 ////////////////////////////////
65 // Tests concerning sectionIndex
66 ////////////////////////////////
67
68 /**
69 * Prepares a test for the method sectionIndex
70 *
71 * @return void
72 */
73 protected function prepareSectionIndexTest() {
74 $this->fixture->sys_page = $this->getMock('t3lib_pageSelect');
75 $this->fixture->parent_cObj = $this->getMock('tslib_cObj');
76 }
77
78 /**
79 * @test
80 */
81 public function sectionIndexReturnsEmptyArrayIfTheRequestedPageCouldNotBeFetched() {
82 $this->prepareSectionIndexTest();
83
84 $this->fixture->sys_page->expects($this->once())->method('getPage')
85 ->will($this->returnValue(NULL));
86
87 $result = $this->fixture->_call('sectionIndex', 'field');
88 $this->assertEquals($result, array());
89 }
90
91 /**
92 * @test
93 */
94 public function sectionIndexUsesTheInternalIdIfNoPageIdWasGiven() {
95 $this->prepareSectionIndexTest();
96
97 $this->fixture->id = 10;
98 $this->fixture->sys_page->expects($this->once())->method('getPage')
99 ->will($this->returnValue(NULL))->with(10);
100
101 $result = $this->fixture->_call('sectionIndex', 'field');
102 $this->assertEquals($result, array());
103 }
104
105 /**
106 * @test
107 * @expectedException UnexpectedValueException
108 */
109 public function sectionIndexThrowsAnExceptionIfTheInternalQueryFails() {
110 $this->prepareSectionIndexTest();
111
112 $this->fixture->sys_page->expects($this->once())->method('getPage')
113 ->will($this->returnValue(array()));
114
115 $this->fixture->parent_cObj->expects($this->once())->method('exec_getQuery')
116 ->will($this->returnValue(0));
117
118 $this->fixture->_call('sectionIndex', 'field');
119 }
120
121 /**
122 * @test
123 */
124 public function sectionIndexReturnsOverlaidRowBasedOnTheLanguageOfTheGivenPage() {
125 $this->prepareSectionIndexTest();
126
127 $this->fixture->mconf['sectionIndex.']['type'] = 'all';
128 $GLOBALS['TSFE']->sys_language_contentOL = 1;
129
130 $this->fixture->sys_page->expects($this->once())->method('getPage')
131 ->will($this->returnValue(array('_PAGES_OVERLAY_LANGUAGE' => 1)));
132
133 $this->fixture->parent_cObj->expects($this->once())->method('exec_getQuery')
134 ->will($this->returnValue(1));
135
136 $GLOBALS['TYPO3_DB']->expects($this->exactly(2))->method('sql_fetch_assoc')
137 ->will($this->onConsecutiveCalls(
138 $this->returnValue(array('uid' => 0, 'header' => 'NOT_OVERLAID')),
139 $this->returnValue(FALSE))
140 );
141
142 $this->fixture->sys_page->expects($this->once())->method('getRecordOverlay')
143 ->will($this->returnValue(array('uid' => 0, 'header' => 'OVERLAID')));
144
145 $result = $this->fixture->_call('sectionIndex', 'field');
146 $this->assertEquals($result[0]['title'], 'OVERLAID');
147 }
148
149 /**
150 * @return array
151 */
152 public function sectionIndexFiltersDataProvider() {
153 return array(
154 'unfiltered fields' => array(
155 1, array(
156 'sectionIndex' => 1,
157 'header' => 'foo',
158 'header_layout' => 1,
159 ),
160 ),
161 'with unset section index' => array(
162 0, array(
163 'sectionIndex' => 0,
164 'header' => 'foo',
165 'header_layout' => 1,
166 ),
167 ),
168 'with unset header' => array(
169 0, array(
170 'sectionIndex' => 1,
171 'header' => '',
172 'header_layout' => 1,
173 ),
174 ),
175 'with header layout 100' => array(
176 0, array(
177 'sectionIndex' => 1,
178 'header' => 'foo',
179 'header_layout' => 100,
180 ),
181 ),
182 );
183 }
184
185 /**
186 * @test
187 * @dataProvider sectionIndexFiltersDataProvider
188 * @param integer $expectedAmount
189 * @param array $dataRow
190 */
191 public function sectionIndexFilters($expectedAmount, array $dataRow) {
192 $this->prepareSectionIndexTest();
193
194 $this->fixture->mconf['sectionIndex.']['type'] = 'header';
195
196 $this->fixture->sys_page->expects($this->once())->method('getPage')
197 ->will($this->returnValue(array()));
198
199 $this->fixture->parent_cObj->expects($this->once())->method('exec_getQuery')
200 ->will($this->returnValue(1));
201
202 $GLOBALS['TYPO3_DB']->expects($this->exactly(2))->method('sql_fetch_assoc')
203 ->will($this->onConsecutiveCalls(
204 $this->returnValue($dataRow),
205 $this->returnValue(FALSE))
206 );
207
208 $result = $this->fixture->_call('sectionIndex', 'field');
209 $this->assertCount($expectedAmount, $result);
210 }
211
212 /**
213 * @return array
214 */
215 public function sectionIndexQueriesWithDifferentColPosDataProvider() {
216 return array(
217 'no configuration' => array(
218 array(),
219 'colPos=0'
220 ),
221 'with useColPos 2' => array(
222 array('useColPos' => 2),
223 'colPos=2'
224 ),
225 'with useColPos -1' => array(
226 array('useColPos' => -1),
227 ''
228 ),
229 'with stdWrap useColPos' => array(
230 array(
231 'useColPos.' => array(
232 'wrap' => '2|',
233 ),
234 ),
235 'colPos=2'
236 ),
237 );
238 }
239
240 /**
241 * @test
242 * @dataProvider sectionIndexQueriesWithDifferentColPosDataProvider
243 * @param array $configuration
244 * @param string $whereClausePrefix
245 */
246 public function sectionIndexQueriesWithDifferentColPos($configuration, $whereClausePrefix) {
247 $this->prepareSectionIndexTest();
248
249 $this->fixture->sys_page->expects($this->once())->method('getPage')
250 ->will($this->returnValue(array()));
251
252 $this->fixture->mconf['sectionIndex.'] = $configuration;
253 $queryConfiguration = array(
254 'pidInList' => 12,
255 'orderBy' => 'field',
256 'languageField' =>'sys_language_uid',
257 'where' => $whereClausePrefix,
258 );
259
260 $this->fixture->parent_cObj->expects($this->once())->method('exec_getQuery')
261 ->with('tt_content', $queryConfiguration)
262 ->will($this->returnValue(1));
263
264 $this->fixture->_call('sectionIndex', 'field', 12);
265 }
266 }
267
268 ?>