13cf7cd761d69f4755e5219941322e50162d5dfe
[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 * Copyright notice
6 *
7 * (c) 2012-2013 Stefan Galinski <stefan.galinski@gmail.com>
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 * Testcase for TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject
28 *
29 * @author Stefan Galinski <stefan.galinski@gmail.com>
30 */
31 class AbstractMenuContentObjectTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
32
33 /**
34 * A backup of the global database
35 *
36 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
37 */
38 protected $databaseBackup = NULL;
39
40 /**
41 * @var \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject
42 */
43 protected $fixture = NULL;
44
45 /**
46 * Set up this testcase
47 */
48 public function setUp() {
49 $proxy = $this->buildAccessibleProxy('TYPO3\\CMS\\Frontend\\ContentObject\\Menu\\AbstractMenuContentObject');
50 $this->fixture = new $proxy();
51 $this->databaseBackup = $GLOBALS['TYPO3_DB'];
52 $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');
53 $GLOBALS['TSFE'] = $this->getMock('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', array(), array($GLOBALS['TYPO3_CONF_VARS'], 1, 1));
54 $GLOBALS['TSFE']->cObj = new \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer();
55 }
56
57 /**
58 * Tear down this testcase
59 */
60 public function tearDown() {
61 $GLOBALS['TYPO3_DB'] = $this->databaseBackup;
62 }
63
64 ////////////////////////////////
65 // Tests concerning sectionIndex
66 ////////////////////////////////
67 /**
68 * Prepares a test for the method sectionIndex
69 *
70 * @return void
71 */
72 protected function prepareSectionIndexTest() {
73 $this->fixture->sys_page = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
74 $this->fixture->parent_cObj = $this->getMock('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
75 }
76
77 /**
78 * @test
79 */
80 public function sectionIndexReturnsEmptyArrayIfTheRequestedPageCouldNotBeFetched() {
81 $this->prepareSectionIndexTest();
82 $this->fixture->sys_page->expects($this->once())->method('getPage')->will($this->returnValue(NULL));
83 $result = $this->fixture->_call('sectionIndex', 'field');
84 $this->assertEquals($result, array());
85 }
86
87 /**
88 * @test
89 */
90 public function sectionIndexUsesTheInternalIdIfNoPageIdWasGiven() {
91 $this->prepareSectionIndexTest();
92 $this->fixture->id = 10;
93 $this->fixture->sys_page->expects($this->once())->method('getPage')->will($this->returnValue(NULL))->with(10);
94 $result = $this->fixture->_call('sectionIndex', 'field');
95 $this->assertEquals($result, array());
96 }
97
98 /**
99 * @test
100 * @expectedException UnexpectedValueException
101 */
102 public function sectionIndexThrowsAnExceptionIfTheInternalQueryFails() {
103 $this->prepareSectionIndexTest();
104 $this->fixture->sys_page->expects($this->once())->method('getPage')->will($this->returnValue(array()));
105 $this->fixture->parent_cObj->expects($this->once())->method('exec_getQuery')->will($this->returnValue(0));
106 $this->fixture->_call('sectionIndex', 'field');
107 }
108
109 /**
110 * @test
111 */
112 public function sectionIndexReturnsOverlaidRowBasedOnTheLanguageOfTheGivenPage() {
113 $this->prepareSectionIndexTest();
114 $this->fixture->mconf['sectionIndex.']['type'] = 'all';
115 $GLOBALS['TSFE']->sys_language_contentOL = 1;
116 $this->fixture->sys_page->expects($this->once())->method('getPage')->will($this->returnValue(array('_PAGES_OVERLAY_LANGUAGE' => 1)));
117 $this->fixture->parent_cObj->expects($this->once())->method('exec_getQuery')->will($this->returnValue(1));
118 $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)));
119 $this->fixture->sys_page->expects($this->once())->method('getRecordOverlay')->will($this->returnValue(array('uid' => 0, 'header' => 'OVERLAID')));
120 $result = $this->fixture->_call('sectionIndex', 'field');
121 $this->assertEquals($result[0]['title'], 'OVERLAID');
122 }
123
124 /**
125 * @return array
126 */
127 public function sectionIndexFiltersDataProvider() {
128 return array(
129 'unfiltered fields' => array(
130 1,
131 array(
132 'sectionIndex' => 1,
133 'header' => 'foo',
134 'header_layout' => 1
135 )
136 ),
137 'with unset section index' => array(
138 0,
139 array(
140 'sectionIndex' => 0,
141 'header' => 'foo',
142 'header_layout' => 1
143 )
144 ),
145 'with unset header' => array(
146 0,
147 array(
148 'sectionIndex' => 1,
149 'header' => '',
150 'header_layout' => 1
151 )
152 ),
153 'with header layout 100' => array(
154 0,
155 array(
156 'sectionIndex' => 1,
157 'header' => 'foo',
158 'header_layout' => 100
159 )
160 )
161 );
162 }
163
164 /**
165 * @test
166 * @dataProvider sectionIndexFiltersDataProvider
167 * @param integer $expectedAmount
168 * @param array $dataRow
169 */
170 public function sectionIndexFilters($expectedAmount, array $dataRow) {
171 $this->prepareSectionIndexTest();
172 $this->fixture->mconf['sectionIndex.']['type'] = 'header';
173 $this->fixture->sys_page->expects($this->once())->method('getPage')->will($this->returnValue(array()));
174 $this->fixture->parent_cObj->expects($this->once())->method('exec_getQuery')->will($this->returnValue(1));
175 $GLOBALS['TYPO3_DB']->expects($this->exactly(2))->method('sql_fetch_assoc')->will($this->onConsecutiveCalls($this->returnValue($dataRow), $this->returnValue(FALSE)));
176 $result = $this->fixture->_call('sectionIndex', 'field');
177 $this->assertCount($expectedAmount, $result);
178 }
179
180 /**
181 * @return array
182 */
183 public function sectionIndexQueriesWithDifferentColPosDataProvider() {
184 return array(
185 'no configuration' => array(
186 array(),
187 'colPos=0'
188 ),
189 'with useColPos 2' => array(
190 array('useColPos' => 2),
191 'colPos=2'
192 ),
193 'with useColPos -1' => array(
194 array('useColPos' => -1),
195 ''
196 ),
197 'with stdWrap useColPos' => array(
198 array(
199 'useColPos.' => array(
200 'wrap' => '2|'
201 )
202 ),
203 'colPos=2'
204 )
205 );
206 }
207
208 /**
209 * @test
210 * @dataProvider sectionIndexQueriesWithDifferentColPosDataProvider
211 * @param array $configuration
212 * @param string $whereClausePrefix
213 */
214 public function sectionIndexQueriesWithDifferentColPos($configuration, $whereClausePrefix) {
215 $this->prepareSectionIndexTest();
216 $this->fixture->sys_page->expects($this->once())->method('getPage')->will($this->returnValue(array()));
217 $this->fixture->mconf['sectionIndex.'] = $configuration;
218 $queryConfiguration = array(
219 'pidInList' => 12,
220 'orderBy' => 'field',
221 'languageField' => 'sys_language_uid',
222 'where' => $whereClausePrefix
223 );
224 $this->fixture->parent_cObj->expects($this->once())->method('exec_getQuery')->with('tt_content', $queryConfiguration)->will($this->returnValue(1));
225 $this->fixture->_call('sectionIndex', 'field', 12);
226 }
227
228 }
229
230
231 ?>