[~CONFIGURATION] Extbase: Set lockType to empty string instead of 'system'.
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / MVC / Controller / AbstractController_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_Controller_AbstractController_testcase extends Tx_Extbase_BaseTestCase {
29
30 /**
31 * @test
32 */
33 public function initializeObjectSetsCurrentPackage() {
34 $extensionName = uniqid('Test');
35 $controller = $this->getMock('Tx_Extbase_MVC_Controller_AbstractController', array(), array(), 'Tx_' . $extensionName . '_Controller');
36 $this->assertSame($extensionName, $this->readAttribute($controller, 'extensionName'));
37 }
38
39 /**
40 * @test
41 * @expectedException Tx_Extbase_MVC_Exception_UnsupportedRequestType
42 */
43 public function processRequestWillThrowAnExceptionIfTheGivenRequestIsNotSupported() {
44 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
45 $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
46
47 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('mapRequestArgumentsToControllerArguments'), array(), '', FALSE);
48 $controller->_set('supportedRequestTypes', array('Tx_Something_Request'));
49 $controller->processRequest($mockRequest, $mockResponse);
50 }
51
52 /**
53 * @test
54 */
55 public function processRequestSetsTheDispatchedFlagOfTheRequest() {
56 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
57 $mockRequest->expects($this->once())->method('setDispatched')->with(TRUE);
58
59 $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
60
61 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('initializeArguments', 'initializeControllerArgumentsBaseValidators', 'mapRequestArgumentsToControllerArguments'), array());
62 $controller->processRequest($mockRequest, $mockResponse);
63 }
64
65 /**
66 * @test
67 * @expectedException Tx_Extbase_MVC_Exception_StopAction
68 */
69 public function forwardThrowsAStopActionException() {
70 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
71 $mockRequest->expects($this->once())->method('setDispatched')->with(FALSE);
72 $mockRequest->expects($this->once())->method('setControllerActionName')->with('foo');
73
74 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
75 $controller->_set('request', $mockRequest);
76 $controller->_call('forward', 'foo');
77 }
78
79 /**
80 * @test
81 * @expectedException Tx_Extbase_MVC_Exception_StopAction
82 */
83 public function forwardSetsControllerAndArgumentsAtTheRequestObjectIfTheyAreSpecified() {
84 $arguments = array('foo' => 'bar');
85
86 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
87 $mockRequest->expects($this->once())->method('setControllerActionName')->with('foo');
88 $mockRequest->expects($this->once())->method('setControllerName')->with('Bar');
89 $mockRequest->expects($this->once())->method('setControllerExtensionName')->with('Baz');
90 $mockRequest->expects($this->once())->method('setArguments')->with($arguments);
91
92 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
93 $controller->_set('request', $mockRequest);
94 $controller->_call('forward', 'foo', 'Bar', 'Baz', $arguments);
95 }
96
97 /**
98 * @test
99 */
100 public function redirectRedirectsToTheSpecifiedAction() {
101 $arguments = array('foo' => 'bar');
102
103 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
104 $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
105
106 $mockUriBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_UriBuilder');
107 $mockUriBuilder->expects($this->once())->method('reset')->will($this->returnValue($mockUriBuilder));
108 $mockUriBuilder->expects($this->once())->method('setTargetPageUid')->with(123)->will($this->returnValue($mockUriBuilder));
109 $mockUriBuilder->expects($this->once())->method('uriFor')->with('theActionName', $arguments, 'TheControllerName', 'TheExtensionName')->will($this->returnValue('the uri'));
110
111 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('redirectToURI'), array(), '', FALSE);
112 $controller->expects($this->once())->method('redirectToURI')->with('the uri');
113 $controller->_set('request', $mockRequest);
114 $controller->_set('response', $mockResponse);
115 $controller->_set('uriBuilder', $mockUriBuilder);
116 $controller->_call('redirect', 'theActionName', 'TheControllerName', 'TheExtensionName', $arguments, 123);
117 }
118
119 /**
120 * @test
121 * @expectedException Tx_Extbase_MVC_Exception_StopAction
122 */
123 public function throwStatusSetsTheSpecifiedStatusHeaderAndStopsTheCurrentAction() {
124 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
125 $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
126 $mockResponse->expects($this->once())->method('setStatus')->with(404, 'File Really Not Found');
127 $mockResponse->expects($this->once())->method('setContent')->with('<h1>All wrong!</h1><p>Sorry, the file does not exist.</p>');
128
129 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
130 $controller->_set('request', $mockRequest);
131 $controller->_set('response', $mockResponse);
132 $controller->_call('throwStatus', 404, 'File Really Not Found', '<h1>All wrong!</h1><p>Sorry, the file does not exist.</p>');
133 }
134
135 /**
136 * @test
137 */
138 public function initializeControllerArgumentsBaseValidatorsRegistersValidatorsDeclaredInTheArgumentModels() {
139 $mockValidators = array(
140 'foo' => $this->getMock('Tx_Extbase_Validation_Validator_ValidatorInterface'),
141 );
142
143 $mockValidatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array(), array(), '', FALSE);
144 $mockValidatorResolver->expects($this->at(0))->method('getBaseValidatorConjunction')->with('FooType')->will($this->returnValue($mockValidators['foo']));
145 $mockValidatorResolver->expects($this->at(1))->method('getBaseValidatorConjunction')->with('BarType')->will($this->returnValue(NULL));
146
147 $mockArgumentFoo = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('foo'));
148 $mockArgumentFoo->expects($this->once())->method('getDataType')->will($this->returnValue('FooType'));
149 $mockArgumentFoo->expects($this->once())->method('setValidator')->with($mockValidators['foo']);
150
151 $mockArgumentBar = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('bar'));
152 $mockArgumentBar->expects($this->once())->method('getDataType')->will($this->returnValue('BarType'));
153 $mockArgumentBar->expects($this->never())->method('setValidator');
154
155 $mockArguments = new Tx_Extbase_MVC_Controller_Arguments();
156 $mockArguments->addArgument($mockArgumentFoo);
157 $mockArguments->addArgument($mockArgumentBar);
158
159 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
160 $controller->_set('arguments', $mockArguments);
161 $controller->injectValidatorResolver($mockValidatorResolver);
162 $controller->_call('initializeControllerArgumentsBaseValidators');
163 }
164
165 /**
166 * @test
167 */
168 public function mapRequestArgumentsToControllerArgumentsPreparesInformationAndValidatorsAndMapsAndValidates() {
169 $mockValidator = new Tx_Extbase_MVC_Controller_ArgumentsValidator(); // FIXME see original FLOW3 code
170
171 $mockArgumentFoo = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('foo'));
172 $mockArgumentFoo->expects($this->any())->method('getName')->will($this->returnValue('foo'));
173 $mockArgumentBar = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array(), array('bar'));
174 $mockArgumentBar->expects($this->any())->method('getName')->will($this->returnValue('bar'));
175
176 $mockArguments = new Tx_Extbase_MVC_Controller_Arguments();
177 $mockArguments->addArgument($mockArgumentFoo);
178 $mockArguments->addArgument($mockArgumentBar);
179
180 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
181 $mockRequest->expects($this->once())->method('getArguments')->will($this->returnValue(array('requestFoo', 'requestBar')));
182
183 $mockMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults');
184
185 $mockPropertyMapper = $this->getMock('Tx_Extbase_Property_Mapper', array(), array(), '', FALSE);
186 $mockPropertyMapper->expects($this->once())->method('mapAndValidate')
187 ->with(array('foo', 'bar'), array('requestFoo', 'requestBar'), $mockArguments, array(), $mockValidator)
188 ->will($this->returnValue(TRUE));
189 $mockPropertyMapper->expects($this->once())->method('getMappingResults')->will($this->returnValue($mockMappingResults));
190
191 $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('dummy'), array(), '', FALSE);
192
193 $controller->_set('arguments', $mockArguments);
194 $controller->_set('request', $mockRequest);
195 $controller->_set('propertyMapper', $mockPropertyMapper);
196 $controller->_set('objectManager', $mockObjectManager);
197
198 $controller->_call('mapRequestArgumentsToControllerArguments');
199
200 $this->assertSame($mockMappingResults, $controller->_get('argumentsMappingResults'));
201 // $this->assertTrue(in_array('Tx_Extbase_Validation_Validator_ObjectValidatorInterface', class_implements($controller->_get('argumentsMappingResults'))));
202 }
203
204 }
205 ?>