d47367b4651e7c1a4bcbef1b4ba5c538f26921a2
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / Unit / ContentObject / Menu / AbstractMenuContentObjectTest.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu;
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 */
21 class AbstractMenuContentObjectTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
22
23 /**
24 * @var \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject
25 */
26 protected $fixture = NULL;
27
28 /**
29 * Set up this testcase
30 */
31 protected function setUp() {
32 $proxy = $this->buildAccessibleProxy(\TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject::class);
33 $this->fixture = new $proxy();
34 $GLOBALS['TYPO3_DB'] = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
35 $GLOBALS['TSFE'] = $this->getMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class, array(), array($GLOBALS['TYPO3_CONF_VARS'], 1, 1));
36 $GLOBALS['TSFE']->cObj = new \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer();
37 $GLOBALS['TSFE']->page = array();
38 }
39
40 ////////////////////////////////
41 // Tests concerning sectionIndex
42 ////////////////////////////////
43 /**
44 * Prepares a test for the method sectionIndex
45 *
46 * @return void
47 */
48 protected function prepareSectionIndexTest() {
49 $this->fixture->sys_page = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepository::class);
50 $this->fixture->parent_cObj = $this->getMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
51 }
52
53 /**
54 * @test
55 */
56 public function sectionIndexReturnsEmptyArrayIfTheRequestedPageCouldNotBeFetched() {
57 $this->prepareSectionIndexTest();
58 $this->fixture->sys_page->expects($this->once())->method('getPage')->will($this->returnValue(NULL));
59 $result = $this->fixture->_call('sectionIndex', 'field');
60 $this->assertEquals($result, array());
61 }
62
63 /**
64 * @test
65 */
66 public function sectionIndexUsesTheInternalIdIfNoPageIdWasGiven() {
67 $this->prepareSectionIndexTest();
68 $this->fixture->id = 10;
69 $this->fixture->sys_page->expects($this->once())->method('getPage')->will($this->returnValue(NULL))->with(10);
70 $result = $this->fixture->_call('sectionIndex', 'field');
71 $this->assertEquals($result, array());
72 }
73
74 /**
75 * @test
76 * @expectedException UnexpectedValueException
77 */
78 public function sectionIndexThrowsAnExceptionIfTheInternalQueryFails() {
79 $this->prepareSectionIndexTest();
80 $this->fixture->sys_page->expects($this->once())->method('getPage')->will($this->returnValue(array()));
81 $this->fixture->parent_cObj->expects($this->once())->method('exec_getQuery')->will($this->returnValue(0));
82 $this->fixture->_call('sectionIndex', 'field');
83 }
84
85 /**
86 * @test
87 */
88 public function sectionIndexReturnsOverlaidRowBasedOnTheLanguageOfTheGivenPage() {
89 $this->prepareSectionIndexTest();
90 $this->fixture->mconf['sectionIndex.']['type'] = 'all';
91 $GLOBALS['TSFE']->sys_language_contentOL = 1;
92 $this->fixture->sys_page->expects($this->once())->method('getPage')->will($this->returnValue(array('_PAGES_OVERLAY_LANGUAGE' => 1)));
93 $this->fixture->parent_cObj->expects($this->once())->method('exec_getQuery')->will($this->returnValue(1));
94 $GLOBALS['TYPO3_DB']->expects($this->exactly(2))->method('sql_fetch_assoc')->will($this->onConsecutiveCalls($this->returnValue(array('uid' => 0, 'header' => 'NOT_OVERLAID')), $this->returnValue(FALSE)));
95 $this->fixture->sys_page->expects($this->once())->method('getRecordOverlay')->will($this->returnValue(array('uid' => 0, 'header' => 'OVERLAID')));
96 $result = $this->fixture->_call('sectionIndex', 'field');
97 $this->assertEquals($result[0]['title'], 'OVERLAID');
98 }
99
100 /**
101 * @return array
102 */
103 public function sectionIndexFiltersDataProvider() {
104 return array(
105 'unfiltered fields' => array(
106 1,
107 array(
108 'sectionIndex' => 1,
109 'header' => 'foo',
110 'header_layout' => 1
111 )
112 ),
113 'with unset section index' => array(
114 0,
115 array(
116 'sectionIndex' => 0,
117 'header' => 'foo',
118 'header_layout' => 1
119 )
120 ),
121 'with unset header' => array(
122 0,
123 array(
124 'sectionIndex' => 1,
125 'header' => '',
126 'header_layout' => 1
127 )
128 ),
129 'with header layout 100' => array(
130 0,
131 array(
132 'sectionIndex' => 1,
133 'header' => 'foo',
134 'header_layout' => 100
135 )
136 )
137 );
138 }
139
140 /**
141 * @test
142 * @dataProvider sectionIndexFiltersDataProvider
143 * @param int $expectedAmount
144 * @param array $dataRow
145 */
146 public function sectionIndexFilters($expectedAmount, array $dataRow) {
147 $this->prepareSectionIndexTest();
148 $this->fixture->mconf['sectionIndex.']['type'] = 'header';
149 $this->fixture->sys_page->expects($this->once())->method('getPage')->will($this->returnValue(array()));
150 $this->fixture->parent_cObj->expects($this->once())->method('exec_getQuery')->will($this->returnValue(1));
151 $GLOBALS['TYPO3_DB']->expects($this->exactly(2))->method('sql_fetch_assoc')->will($this->onConsecutiveCalls($this->returnValue($dataRow), $this->returnValue(FALSE)));
152 $result = $this->fixture->_call('sectionIndex', 'field');
153 $this->assertCount($expectedAmount, $result);
154 }
155
156 /**
157 * @return array
158 */
159 public function sectionIndexQueriesWithDifferentColPosDataProvider() {
160 return array(
161 'no configuration' => array(
162 array(),
163 'colPos=0'
164 ),
165 'with useColPos 2' => array(
166 array('useColPos' => 2),
167 'colPos=2'
168 ),
169 'with useColPos -1' => array(
170 array('useColPos' => -1),
171 ''
172 ),
173 'with stdWrap useColPos' => array(
174 array(
175 'useColPos.' => array(
176 'wrap' => '2|'
177 )
178 ),
179 'colPos=2'
180 )
181 );
182 }
183
184 /**
185 * @test
186 * @dataProvider sectionIndexQueriesWithDifferentColPosDataProvider
187 * @param array $configuration
188 * @param string $whereClausePrefix
189 */
190 public function sectionIndexQueriesWithDifferentColPos($configuration, $whereClausePrefix) {
191 $this->prepareSectionIndexTest();
192 $this->fixture->sys_page->expects($this->once())->method('getPage')->will($this->returnValue(array()));
193 $this->fixture->mconf['sectionIndex.'] = $configuration;
194 $queryConfiguration = array(
195 'pidInList' => 12,
196 'orderBy' => 'field',
197 'languageField' => 'sys_language_uid',
198 'where' => $whereClausePrefix
199 );
200 $this->fixture->parent_cObj->expects($this->once())->method('exec_getQuery')->with('tt_content', $queryConfiguration)->will($this->returnValue(1));
201 $this->fixture->_call('sectionIndex', 'field', 12);
202 }
203
204 ////////////////////////////////////
205 // Tests concerning menu item states
206 ////////////////////////////////////
207 /**
208 * @return array
209 */
210 public function ifsubHasToCheckExcludeUidListDataProvider() {
211 return array(
212 'none excluded' => array (
213 array(12, 34, 56),
214 '1, 23, 456',
215 TRUE
216 ),
217 'one excluded' => array (
218 array(1, 234, 567),
219 '1, 23, 456',
220 TRUE
221 ),
222 'three excluded' => array (
223 array(1, 23, 456),
224 '1, 23, 456',
225 FALSE
226 ),
227 'empty excludeList' => array (
228 array(1, 123, 45),
229 '',
230 TRUE
231 ),
232 'empty menu' => array (
233 array(),
234 '1, 23, 456',
235 FALSE
236 ),
237 );
238
239 }
240
241 /**
242 * @test
243 * @dataProvider ifsubHasToCheckExcludeUidListDataProvider
244 * @param array $menuItems
245 * @param string $excludeUidList
246 * @param bool $expectedResult
247 */
248 public function ifsubHasToCheckExcludeUidList($menuItems, $excludeUidList, $expectedResult) {
249 $menu = array();
250 foreach ($menuItems as $page) {
251 $menu[] = array('uid' => $page);
252 }
253
254 $this->prepareSectionIndexTest();
255 $this->fixture->parent_cObj = $this->getMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, array());
256
257 $this->fixture->sys_page->expects($this->once())->method('getMenu')->will($this->returnValue($menu));
258 $this->fixture->menuArr = array(
259 0 => array('uid' => 1)
260 );
261 $this->fixture->conf['excludeUidList'] = $excludeUidList;
262
263 $this->assertEquals($expectedResult, $this->fixture->isItemState('IFSUB', 0));
264 }
265
266 }