| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* ScaffoldTest file
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
|
|
14 |
* @package Cake.Test.Case.Controller
|
|
|
15 |
* @since CakePHP(tm) v 1.2.0.5436
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('Router', 'Routing');
|
|
|
20 |
App::uses('Controller', 'Controller');
|
|
|
21 |
App::uses('Scaffold', 'Controller');
|
|
|
22 |
App::uses('ScaffoldView', 'View');
|
|
|
23 |
App::uses('AppModel', 'Model');
|
|
|
24 |
|
|
|
25 |
require_once dirname(dirname(__FILE__)) . DS . 'Model' . DS . 'models.php';
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* ScaffoldMockController class
|
|
|
29 |
*
|
|
|
30 |
* @package Cake.Test.Case.Controller
|
|
|
31 |
*/
|
|
|
32 |
class ScaffoldMockController extends Controller {
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* scaffold property
|
|
|
36 |
*
|
|
|
37 |
* @var mixed
|
|
|
38 |
*/
|
|
|
39 |
public $scaffold;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* ScaffoldMockControllerWithFields class
|
|
|
44 |
*
|
|
|
45 |
* @package Cake.Test.Case.Controller
|
|
|
46 |
*/
|
|
|
47 |
class ScaffoldMockControllerWithFields extends Controller {
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* name property
|
|
|
51 |
*
|
|
|
52 |
* @var string
|
|
|
53 |
*/
|
|
|
54 |
public $name = 'ScaffoldMock';
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* scaffold property
|
|
|
58 |
*
|
|
|
59 |
* @var mixed
|
|
|
60 |
*/
|
|
|
61 |
public $scaffold;
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* function beforeScaffold
|
|
|
65 |
*
|
|
|
66 |
* @param string method
|
|
|
67 |
*/
|
|
|
68 |
public function beforeScaffold($method) {
|
|
|
69 |
$this->set('scaffoldFields', array('title'));
|
|
|
70 |
return true;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* TestScaffoldMock class
|
|
|
77 |
*
|
|
|
78 |
* @package Cake.Test.Case.Controller
|
|
|
79 |
*/
|
|
|
80 |
class TestScaffoldMock extends Scaffold {
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Overload _scaffold
|
|
|
84 |
*
|
|
|
85 |
* @param unknown_type $params
|
|
|
86 |
*/
|
|
|
87 |
protected function _scaffold(CakeRequest $request) {
|
|
|
88 |
$this->_params = $request;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Get Params from the Controller.
|
|
|
93 |
*
|
|
|
94 |
* @return unknown
|
|
|
95 |
*/
|
|
|
96 |
public function getParams() {
|
|
|
97 |
return $this->_params;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Scaffold Test class
|
|
|
104 |
*
|
|
|
105 |
* @package Cake.Test.Case.Controller
|
|
|
106 |
*/
|
|
|
107 |
class ScaffoldTest extends CakeTestCase {
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Controller property
|
|
|
111 |
*
|
|
|
112 |
* @var SecurityTestController
|
|
|
113 |
*/
|
|
|
114 |
public $Controller;
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* fixtures property
|
|
|
118 |
*
|
|
|
119 |
* @var array
|
|
|
120 |
*/
|
|
|
121 |
public $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag');
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* setUp method
|
|
|
125 |
*
|
|
|
126 |
* @return void
|
|
|
127 |
*/
|
|
|
128 |
public function setUp() {
|
|
|
129 |
parent::setUp();
|
|
|
130 |
Configure::write('Config.language', 'eng');
|
|
|
131 |
$request = new CakeRequest(null, false);
|
|
|
132 |
$this->Controller = new ScaffoldMockController($request);
|
|
|
133 |
$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* tearDown method
|
|
|
138 |
*
|
|
|
139 |
* @return void
|
|
|
140 |
*/
|
|
|
141 |
public function tearDown() {
|
|
|
142 |
parent::tearDown();
|
|
|
143 |
unset($this->Controller);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Test the correct Generation of Scaffold Params.
|
|
|
148 |
* This ensures that the correct action and view will be generated
|
|
|
149 |
*
|
|
|
150 |
* @return void
|
|
|
151 |
*/
|
|
|
152 |
public function testScaffoldParams() {
|
|
|
153 |
$params = array(
|
|
|
154 |
'plugin' => null,
|
|
|
155 |
'pass' => array(),
|
|
|
156 |
'form' => array(),
|
|
|
157 |
'named' => array(),
|
|
|
158 |
'url' => array('url' => 'admin/scaffold_mock/edit'),
|
|
|
159 |
'controller' => 'scaffold_mock',
|
|
|
160 |
'action' => 'admin_edit',
|
|
|
161 |
'admin' => true,
|
|
|
162 |
);
|
|
|
163 |
$this->Controller->request->base = '';
|
|
|
164 |
$this->Controller->request->webroot = '/';
|
|
|
165 |
$this->Controller->request->here = '/admin/scaffold_mock/edit';
|
|
|
166 |
$this->Controller->request->addParams($params);
|
|
|
167 |
|
|
|
168 |
//set router.
|
|
|
169 |
Router::setRequestInfo($this->Controller->request);
|
|
|
170 |
|
|
|
171 |
$this->Controller->constructClasses();
|
|
|
172 |
$Scaffold = new TestScaffoldMock($this->Controller, $this->Controller->request);
|
|
|
173 |
$result = $Scaffold->getParams();
|
|
|
174 |
$this->assertEquals('admin_edit', $result['action']);
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* test that the proper names and variable values are set by Scaffold
|
|
|
179 |
*
|
|
|
180 |
* @return void
|
|
|
181 |
*/
|
|
|
182 |
public function testScaffoldVariableSetting() {
|
|
|
183 |
$params = array(
|
|
|
184 |
'plugin' => null,
|
|
|
185 |
'pass' => array(),
|
|
|
186 |
'form' => array(),
|
|
|
187 |
'named' => array(),
|
|
|
188 |
'url' => array('url' => 'admin/scaffold_mock/edit'),
|
|
|
189 |
'controller' => 'scaffold_mock',
|
|
|
190 |
'action' => 'admin_edit',
|
|
|
191 |
'admin' => true,
|
|
|
192 |
);
|
|
|
193 |
$this->Controller->request->base = '';
|
|
|
194 |
$this->Controller->request->webroot = '/';
|
|
|
195 |
$this->Controller->request->here = '/admin/scaffold_mock/edit';
|
|
|
196 |
$this->Controller->request->addParams($params);
|
|
|
197 |
|
|
|
198 |
//set router.
|
|
|
199 |
Router::setRequestInfo($this->Controller->request);
|
|
|
200 |
|
|
|
201 |
$this->Controller->constructClasses();
|
|
|
202 |
$Scaffold = new TestScaffoldMock($this->Controller, $this->Controller->request);
|
|
|
203 |
$result = $Scaffold->controller->viewVars;
|
|
|
204 |
|
|
|
205 |
$this->assertEquals('Scaffold :: Admin Edit :: Scaffold Mock', $result['title_for_layout']);
|
|
|
206 |
$this->assertEquals('Scaffold Mock', $result['singularHumanName']);
|
|
|
207 |
$this->assertEquals('Scaffold Mock', $result['pluralHumanName']);
|
|
|
208 |
$this->assertEquals('ScaffoldMock', $result['modelClass']);
|
|
|
209 |
$this->assertEquals('id', $result['primaryKey']);
|
|
|
210 |
$this->assertEquals('title', $result['displayField']);
|
|
|
211 |
$this->assertEquals('scaffoldMock', $result['singularVar']);
|
|
|
212 |
$this->assertEquals('scaffoldMock', $result['pluralVar']);
|
|
|
213 |
$this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated'), $result['scaffoldFields']);
|
|
|
214 |
$this->assertArrayHasKey('plugin', $result['associations']['belongsTo']['User']);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* test that Scaffold overrides the view property even if its set to 'Theme'
|
|
|
219 |
*
|
|
|
220 |
* @return void
|
|
|
221 |
*/
|
|
|
222 |
public function testScaffoldChangingViewProperty() {
|
|
|
223 |
$this->Controller->action = 'edit';
|
|
|
224 |
$this->Controller->theme = 'TestTheme';
|
|
|
225 |
$this->Controller->viewClass = 'Theme';
|
|
|
226 |
$this->Controller->constructClasses();
|
|
|
227 |
new TestScaffoldMock($this->Controller, $this->Controller->request);
|
|
|
228 |
|
|
|
229 |
$this->assertEquals('Scaffold', $this->Controller->viewClass);
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
/**
|
|
|
233 |
* test that scaffold outputs flash messages when sessions are unset.
|
|
|
234 |
*
|
|
|
235 |
* @return void
|
|
|
236 |
*/
|
|
|
237 |
public function testScaffoldFlashMessages() {
|
|
|
238 |
$params = array(
|
|
|
239 |
'plugin' => null,
|
|
|
240 |
'pass' => array(1),
|
|
|
241 |
'form' => array(),
|
|
|
242 |
'named' => array(),
|
|
|
243 |
'url' => array('url' => 'scaffold_mock'),
|
|
|
244 |
'controller' => 'scaffold_mock',
|
|
|
245 |
'action' => 'edit',
|
|
|
246 |
);
|
|
|
247 |
$this->Controller->request->base = '';
|
|
|
248 |
$this->Controller->request->webroot = '/';
|
|
|
249 |
$this->Controller->request->here = '/scaffold_mock/edit';
|
|
|
250 |
$this->Controller->request->addParams($params);
|
|
|
251 |
|
|
|
252 |
//set router.
|
|
|
253 |
Router::reload();
|
|
|
254 |
Router::setRequestInfo($this->Controller->request);
|
|
|
255 |
$this->Controller->request->data = array(
|
|
|
256 |
'ScaffoldMock' => array(
|
|
|
257 |
'id' => 1,
|
|
|
258 |
'title' => 'New title',
|
|
|
259 |
'body' => 'new body'
|
|
|
260 |
)
|
|
|
261 |
);
|
|
|
262 |
$this->Controller->constructClasses();
|
|
|
263 |
unset($this->Controller->Session);
|
|
|
264 |
|
|
|
265 |
ob_start();
|
|
|
266 |
new Scaffold($this->Controller, $this->Controller->request);
|
|
|
267 |
$this->Controller->response->send();
|
|
|
268 |
$result = ob_get_clean();
|
|
|
269 |
$this->assertRegExp('/Scaffold Mock has been updated/', $result);
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
/**
|
|
|
273 |
* test that habtm relationship keys get added to scaffoldFields.
|
|
|
274 |
*
|
|
|
275 |
* @return void
|
|
|
276 |
*/
|
|
|
277 |
public function testHabtmFieldAdditionWithScaffoldForm() {
|
|
|
278 |
CakePlugin::unload();
|
|
|
279 |
$params = array(
|
|
|
280 |
'plugin' => null,
|
|
|
281 |
'pass' => array(1),
|
|
|
282 |
'form' => array(),
|
|
|
283 |
'named' => array(),
|
|
|
284 |
'url' => array('url' => 'scaffold_mock'),
|
|
|
285 |
'controller' => 'scaffold_mock',
|
|
|
286 |
'action' => 'edit',
|
|
|
287 |
);
|
|
|
288 |
$this->Controller->request->base = '';
|
|
|
289 |
$this->Controller->request->webroot = '/';
|
|
|
290 |
$this->Controller->request->here = '/scaffold_mock/edit';
|
|
|
291 |
$this->Controller->request->addParams($params);
|
|
|
292 |
|
|
|
293 |
//set router.
|
|
|
294 |
Router::reload();
|
|
|
295 |
Router::setRequestInfo($this->Controller->request);
|
|
|
296 |
|
|
|
297 |
$this->Controller->constructClasses();
|
|
|
298 |
ob_start();
|
|
|
299 |
$Scaffold = new Scaffold($this->Controller, $this->Controller->request);
|
|
|
300 |
$this->Controller->response->send();
|
|
|
301 |
$result = ob_get_clean();
|
|
|
302 |
$this->assertRegExp('/name="data\[ScaffoldTag\]\[ScaffoldTag\]"/', $result);
|
|
|
303 |
|
|
|
304 |
$result = $Scaffold->controller->viewVars;
|
|
|
305 |
$this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated', 'ScaffoldTag'), $result['scaffoldFields']);
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
/**
|
|
|
309 |
* test that the proper names and variable values are set by Scaffold
|
|
|
310 |
*
|
|
|
311 |
* @return void
|
|
|
312 |
*/
|
|
|
313 |
public function testEditScaffoldWithScaffoldFields() {
|
|
|
314 |
$request = new CakeRequest(null, false);
|
|
|
315 |
$this->Controller = new ScaffoldMockControllerWithFields($request);
|
|
|
316 |
$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
317 |
|
|
|
318 |
$params = array(
|
|
|
319 |
'plugin' => null,
|
|
|
320 |
'pass' => array(1),
|
|
|
321 |
'form' => array(),
|
|
|
322 |
'named' => array(),
|
|
|
323 |
'url' => array('url' => 'scaffold_mock/edit'),
|
|
|
324 |
'controller' => 'scaffold_mock',
|
|
|
325 |
'action' => 'edit',
|
|
|
326 |
);
|
|
|
327 |
$this->Controller->request->base = '';
|
|
|
328 |
$this->Controller->request->webroot = '/';
|
|
|
329 |
$this->Controller->request->here = '/scaffold_mock/edit';
|
|
|
330 |
$this->Controller->request->addParams($params);
|
|
|
331 |
|
|
|
332 |
//set router.
|
|
|
333 |
Router::reload();
|
|
|
334 |
Router::setRequestInfo($this->Controller->request);
|
|
|
335 |
|
|
|
336 |
$this->Controller->constructClasses();
|
|
|
337 |
ob_start();
|
|
|
338 |
new Scaffold($this->Controller, $this->Controller->request);
|
|
|
339 |
$this->Controller->response->send();
|
|
|
340 |
$result = ob_get_clean();
|
|
|
341 |
|
|
|
342 |
$this->assertNotRegExp('/textarea name="data\[ScaffoldMock\]\[body\]" cols="30" rows="6" id="ScaffoldMockBody"/', $result);
|
|
|
343 |
}
|
|
|
344 |
|
|
|
345 |
}
|