Extbase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / MVC / Web / Routing / URIBuilder_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 class Tx_Extbase_MVC_Web_Routing_URIBuilder_testcase extends Tx_Extbase_Base_testcase {
29
30 protected $tsfeBackup;
31
32 public function setUp() {
33 $this->tsfeBackup = $GLOBALS['TSFE'];
34 $GLOBALS['TSFE'] = $this->getMock('tslib_fe', array(), array(), '', FALSE);
35 }
36
37 public function tearDown() {
38 $GLOBALS['TSFE'] = $this->tsfeBackup;
39 }
40
41 /**
42 * @test
43 */
44 public function uriForPrefixesArgumentsWithExtensionAndPluginNameAndSetsControllerArgument() {
45 $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
46
47 $expectedArguments = array('tx_someextension_someplugin' => array('foo' => 'bar', 'baz' => array('extbase' => 'fluid'), 'controller' => 'SomeController'));
48 $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, $expectedArguments, 0, FALSE, TRUE, '', FALSE);
49 $URIBuilder->URIFor(NULL, NULL, array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')), 'SomeController', 'SomeExtension', 'SomePlugin');
50 }
51
52 /**
53 * @test
54 */
55 public function additionalArgumentsOverruleArguments() {
56 $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
57
58 $arguments = array('foo' => 'bar', 'baz' => array('extbase' => 'fluid'));
59 $additionalArguments = array('tx_someextension_someplugin' => array('foo' => 'overruled'), 'additionalParam' => 'additionalValue');
60 $expectedArguments = array('tx_someextension_someplugin' => array('foo' => 'overruled', 'baz' => array('extbase' => 'fluid'), 'controller' => 'SomeController'), 'additionalParam' => 'additionalValue');
61 $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, $expectedArguments, 0, FALSE, TRUE, '', FALSE);
62 $URIBuilder->URIFor(NULL, NULL, $arguments, 'SomeController', 'SomeExtension', 'SomePlugin', 0, FALSE, TRUE, '', FALSE, $additionalArguments);
63 }
64
65 /**
66 * @test
67 */
68 public function uriForForwardsAllParametersToTypolinkURI() {
69 $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
70
71 $expectedArguments = array('tx_someextension_someplugin' => array('action' => 'SomeAction', 'controller' => 'SomeController'));
72 $URIBuilder->expects($this->once())->method('typolinkURI')->with(123, $expectedArguments, 2, TRUE, FALSE, 'SomeSection', TRUE);
73 $URIBuilder->URIFor(123, 'SomeAction', array(), 'SomeController', 'SomeExtension', 'SomePlugin', 2, TRUE, FALSE, 'SomeSection', TRUE);
74 }
75
76 /**
77 * @test
78 */
79 public function uriForSetsActionArgument() {
80 $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
81
82 $expectedArguments = array('tx_someextension_someplugin' => array('action' => 'SomeAction', 'controller' => 'SomeController'));
83 $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, $expectedArguments, 0, FALSE, TRUE, '', FALSE);
84 $URIBuilder->URIFor(NULL, 'SomeAction', array(), 'SomeController', 'SomeExtension', 'SomePlugin');
85 }
86
87 /**
88 * @test
89 */
90 public function uriForSetsControllerFromRequestIfControllerIsNotSet() {
91 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request');
92 $mockRequest->expects($this->once())->method('getControllerName')->will($this->returnValue('SomeControllerFromRequest'));
93
94 $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
95 $URIBuilder->setRequest($mockRequest);
96
97 $expectedArguments = array('tx_someextension_someplugin' => array('controller' => 'SomeControllerFromRequest'));
98 $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, $expectedArguments, 0, FALSE, TRUE, '', FALSE);
99 $URIBuilder->URIFor(NULL, NULL, array(), NULL, 'SomeExtension', 'SomePlugin');
100 }
101
102 /**
103 * @test
104 */
105 public function uriForSetsExtensionNameFromRequestIfExtensionNameIsNotSet() {
106 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request');
107 $mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('SomeExtensionNameFromRequest'));
108
109 $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
110 $URIBuilder->setRequest($mockRequest);
111
112 $expectedArguments = array('tx_someextensionnamefromrequest_someplugin' => array('controller' => 'SomeController'));
113 $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, $expectedArguments, 0, FALSE, TRUE, '', FALSE);
114 $URIBuilder->URIFor(NULL, NULL, array(), 'SomeController', NULL, 'SomePlugin');
115 }
116
117 /**
118 * @test
119 */
120 public function uriForSetsPluginNameFromRequestIfPluginNameIsNotSet() {
121 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request');
122 $mockRequest->expects($this->once())->method('getPluginName')->will($this->returnValue('SomePluginNameFromRequest'));
123
124 $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
125 $URIBuilder->setRequest($mockRequest);
126
127 $expectedArguments = array('tx_someextension_somepluginnamefromrequest' => array('controller' => 'SomeController'));
128 $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, $expectedArguments, 0, FALSE, TRUE, '', FALSE);
129 $URIBuilder->URIFor(NULL, NULL, array(), 'SomeController', 'SomeExtension');
130 }
131
132 /**
133 * @test
134 */
135 public function uriForCallsConvertDomainObjectsToIdentityArraysAfterArgumentsHaveBeenMerged() {
136 $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI', 'convertDomainObjectsToIdentityArrays'));
137
138 $arguments = array('foo' => 'bar', 'baz' => array('extbase' => 'fluid'));
139 $additionalArguments = array('tx_someextension_someplugin' => array('foo' => 'overruled'), 'additionalParam' => 'additionalValue');
140 $expectedArguments = array('tx_someextension_someplugin' => array('foo' => 'overruled', 'baz' => array('extbase' => 'fluid'), 'controller' => 'SomeController'), 'additionalParam' => 'additionalValue');
141 $URIBuilder->expects($this->once())->method('convertDomainObjectsToIdentityArrays')->with($expectedArguments)->will($this->returnValue(array()));;
142 $URIBuilder->URIFor(NULL, NULL, $arguments, 'SomeController', 'SomeExtension', 'SomePlugin', 0, FALSE, TRUE, '', FALSE, $additionalArguments);
143 }
144
145 /**
146 * @test
147 */
148 public function convertDomainObjectsToIdentityArraysConvertsDomainObjects() {
149 $mockDomainObject1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_DomainObject_AbstractEntity'), array('dummy'));
150 $mockDomainObject1->_set('uid', '123');
151
152 $mockDomainObject2 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_DomainObject_AbstractEntity'), array('dummy'));
153 $mockDomainObject2->_set('uid', '321');
154
155 $URIBuilder = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Web_Routing_URIBuilder'), array('dummy'));
156
157 $expectedResult = array('foo' => array('bar' => 'baz'), 'domainObject1' => array('uid' => '123'), 'second' => array('domainObject2' => array('uid' => '321')));
158 $actualResult = $URIBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'domainObject1' => $mockDomainObject1, 'second' => array('domainObject2' => $mockDomainObject2)));
159
160 $this->assertEquals($expectedResult, $actualResult);
161 }
162
163 /**
164 * @test
165 */
166 public function typolinkURILinksToCurrentPageIfPageUidIsNotSet() {
167 $mockContentObject = $this->getMock('tslib_cObj');
168 $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
169
170 $GLOBALS['TSFE']->id = 123;
171 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1);
172 $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
173
174 $URIBuilder->typolinkURI();
175 }
176
177 /**
178 * @test
179 */
180 public function typolinkURILinksToPageUidIfSet() {
181 $mockContentObject = $this->getMock('tslib_cObj');
182 $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
183
184 $expectedConfiguration = array('parameter' => 321, 'useCacheHash' => 1);
185 $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
186
187 $URIBuilder->typolinkURI(321);
188 }
189
190 /**
191 * @test
192 */
193 public function typolinkURILinksProperlySetsAdditionalArguments() {
194 $mockContentObject = $this->getMock('tslib_cObj');
195 $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
196
197 $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&foo=bar&baz%5Bextbase%5D=fluid');
198 $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
199
200 $URIBuilder->typolinkURI(123, array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')));
201 }
202
203 /**
204 * @test
205 */
206 public function typolinkURIConsidersPageType() {
207 $mockContentObject = $this->getMock('tslib_cObj');
208 $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
209
210 $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
211 $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
212
213 $URIBuilder->typolinkURI(123, array(), 2);
214 }
215
216 /**
217 * @test
218 */
219 public function typolinkURIDisablesCacheHashIfNoCacheIsSet() {
220 $mockContentObject = $this->getMock('tslib_cObj');
221 $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
222
223 $expectedConfiguration = array('parameter' => 123, 'no_cache' => 1);
224 $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
225
226 $URIBuilder->typolinkURI(123, array(), 0, TRUE, TRUE);
227 }
228
229 /**
230 * @test
231 */
232 public function cacheHashCanBeDisabled() {
233 $mockContentObject = $this->getMock('tslib_cObj');
234 $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
235
236 $expectedConfiguration = array('parameter' => 123);
237 $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
238
239 $URIBuilder->typolinkURI(123, array(), 0, FALSE, FALSE);
240 }
241
242 /**
243 * @test
244 */
245 public function typolinkURIConsidersSection() {
246 $mockContentObject = $this->getMock('tslib_cObj');
247 $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
248
249 $expectedConfiguration = array('parameter' => 123, 'section' => 'SomeSection');
250 $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
251
252 $URIBuilder->typolinkURI(123, array(), 0, FALSE, FALSE, 'SomeSection');
253 }
254
255 /**
256 * @test
257 */
258 public function typolinkURIConsidersLinkAccessRestrictedPages() {
259 $mockContentObject = $this->getMock('tslib_cObj');
260 $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
261
262 $expectedConfiguration = array('parameter' => 123, 'linkAccessRestrictedPages' => 1);
263 $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
264
265 $URIBuilder->typolinkURI(123, array(), 0, FALSE, FALSE, '', TRUE);
266 }
267 }
268 ?>