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