pictcode / lib / Cake / Test / Case / Controller / ScaffoldTest.php @ 635eef61
履歴 | 表示 | アノテート | ダウンロード (10.62 KB)
1 | 635eef61 | spyder1211 | <?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 Method name.
|
||
67 | * @return bool true
|
||
68 | */
|
||
69 | public function beforeScaffold($method) { |
||
70 | $this->set('scaffoldFields', array('title')); |
||
71 | return true; |
||
72 | } |
||
73 | |||
74 | } |
||
75 | |||
76 | /**
|
||
77 | * ScaffoldMockControllerWithError class
|
||
78 | *
|
||
79 | * @package Cake.Test.Case.Controller
|
||
80 | */
|
||
81 | class ScaffoldMockControllerWithError extends Controller { |
||
82 | |||
83 | /**
|
||
84 | * name property
|
||
85 | *
|
||
86 | * @var string
|
||
87 | */
|
||
88 | public $name = 'ScaffoldMock'; |
||
89 | |||
90 | /**
|
||
91 | * scaffold property
|
||
92 | *
|
||
93 | * @var mixed
|
||
94 | */
|
||
95 | public $scaffold; |
||
96 | |||
97 | /**
|
||
98 | * function beforeScaffold
|
||
99 | *
|
||
100 | * @param string $method Method name.
|
||
101 | * @return bool false
|
||
102 | */
|
||
103 | public function beforeScaffold($method) { |
||
104 | return false; |
||
105 | } |
||
106 | |||
107 | } |
||
108 | |||
109 | /**
|
||
110 | * TestScaffoldMock class
|
||
111 | *
|
||
112 | * @package Cake.Test.Case.Controller
|
||
113 | */
|
||
114 | class TestScaffoldMock extends Scaffold { |
||
115 | |||
116 | /**
|
||
117 | * Overload _scaffold
|
||
118 | *
|
||
119 | * @param CakeRequest $request Request object for scaffolding
|
||
120 | * @return void
|
||
121 | */
|
||
122 | protected function _scaffold(CakeRequest $request) { |
||
123 | $this->_params = $request; |
||
124 | } |
||
125 | |||
126 | /**
|
||
127 | * Get Params from the Controller.
|
||
128 | *
|
||
129 | * @return unknown
|
||
130 | */
|
||
131 | public function getParams() { |
||
132 | return $this->_params; |
||
133 | } |
||
134 | |||
135 | } |
||
136 | |||
137 | /**
|
||
138 | * Scaffold Test class
|
||
139 | *
|
||
140 | * @package Cake.Test.Case.Controller
|
||
141 | */
|
||
142 | class ScaffoldTest extends CakeTestCase { |
||
143 | |||
144 | /**
|
||
145 | * Controller property
|
||
146 | *
|
||
147 | * @var SecurityTestController
|
||
148 | */
|
||
149 | public $Controller; |
||
150 | |||
151 | /**
|
||
152 | * fixtures property
|
||
153 | *
|
||
154 | * @var array
|
||
155 | */
|
||
156 | public $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag'); |
||
157 | |||
158 | /**
|
||
159 | * setUp method
|
||
160 | *
|
||
161 | * @return void
|
||
162 | */
|
||
163 | public function setUp() { |
||
164 | parent::setUp();
|
||
165 | Configure::write('Config.language', 'eng'); |
||
166 | $request = new CakeRequest(null, false); |
||
167 | $this->Controller = new ScaffoldMockController($request); |
||
168 | $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader')); |
||
169 | } |
||
170 | |||
171 | /**
|
||
172 | * tearDown method
|
||
173 | *
|
||
174 | * @return void
|
||
175 | */
|
||
176 | public function tearDown() { |
||
177 | parent::tearDown();
|
||
178 | unset($this->Controller); |
||
179 | } |
||
180 | |||
181 | /**
|
||
182 | * Test the correct Generation of Scaffold Params.
|
||
183 | * This ensures that the correct action and view will be generated
|
||
184 | *
|
||
185 | * @return void
|
||
186 | */
|
||
187 | public function testScaffoldParams() { |
||
188 | $params = array( |
||
189 | 'plugin' => null, |
||
190 | 'pass' => array(), |
||
191 | 'form' => array(), |
||
192 | 'named' => array(), |
||
193 | 'url' => array('url' => 'admin/scaffold_mock/edit'), |
||
194 | 'controller' => 'scaffold_mock', |
||
195 | 'action' => 'admin_edit', |
||
196 | 'admin' => true, |
||
197 | ); |
||
198 | $this->Controller->request->base = ''; |
||
199 | $this->Controller->request->webroot = '/'; |
||
200 | $this->Controller->request->here = '/admin/scaffold_mock/edit'; |
||
201 | $this->Controller->request->addParams($params); |
||
202 | |||
203 | //set router.
|
||
204 | Router::setRequestInfo($this->Controller->request); |
||
205 | |||
206 | $this->Controller->constructClasses(); |
||
207 | $Scaffold = new TestScaffoldMock($this->Controller, $this->Controller->request); |
||
208 | $result = $Scaffold->getParams(); |
||
209 | $this->assertEquals('admin_edit', $result['action']); |
||
210 | } |
||
211 | |||
212 | /**
|
||
213 | * test that the proper names and variable values are set by Scaffold
|
||
214 | *
|
||
215 | * @return void
|
||
216 | */
|
||
217 | public function testScaffoldVariableSetting() { |
||
218 | $params = array( |
||
219 | 'plugin' => null, |
||
220 | 'pass' => array(), |
||
221 | 'form' => array(), |
||
222 | 'named' => array(), |
||
223 | 'url' => array('url' => 'admin/scaffold_mock/edit'), |
||
224 | 'controller' => 'scaffold_mock', |
||
225 | 'action' => 'admin_edit', |
||
226 | 'admin' => true, |
||
227 | ); |
||
228 | $this->Controller->request->base = ''; |
||
229 | $this->Controller->request->webroot = '/'; |
||
230 | $this->Controller->request->here = '/admin/scaffold_mock/edit'; |
||
231 | $this->Controller->request->addParams($params); |
||
232 | |||
233 | //set router.
|
||
234 | Router::setRequestInfo($this->Controller->request); |
||
235 | |||
236 | $this->Controller->constructClasses(); |
||
237 | $Scaffold = new TestScaffoldMock($this->Controller, $this->Controller->request); |
||
238 | $result = $Scaffold->controller->viewVars; |
||
239 | |||
240 | $this->assertEquals('Scaffold :: Admin Edit :: Scaffold Mock', $result['title_for_layout']); |
||
241 | $this->assertEquals('Scaffold Mock', $result['singularHumanName']); |
||
242 | $this->assertEquals('Scaffold Mock', $result['pluralHumanName']); |
||
243 | $this->assertEquals('ScaffoldMock', $result['modelClass']); |
||
244 | $this->assertEquals('id', $result['primaryKey']); |
||
245 | $this->assertEquals('title', $result['displayField']); |
||
246 | $this->assertEquals('scaffoldMock', $result['singularVar']); |
||
247 | $this->assertEquals('scaffoldMock', $result['pluralVar']); |
||
248 | $this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated'), $result['scaffoldFields']); |
||
249 | $this->assertArrayHasKey('plugin', $result['associations']['belongsTo']['User']); |
||
250 | } |
||
251 | |||
252 | /**
|
||
253 | * test that Scaffold overrides the view property even if its set to 'Theme'
|
||
254 | *
|
||
255 | * @return void
|
||
256 | */
|
||
257 | public function testScaffoldChangingViewProperty() { |
||
258 | $this->Controller->action = 'edit'; |
||
259 | $this->Controller->theme = 'TestTheme'; |
||
260 | $this->Controller->viewClass = 'Theme'; |
||
261 | $this->Controller->constructClasses(); |
||
262 | new TestScaffoldMock($this->Controller, $this->Controller->request); |
||
263 | |||
264 | $this->assertEquals('Scaffold', $this->Controller->viewClass); |
||
265 | } |
||
266 | |||
267 | /**
|
||
268 | * test that scaffold outputs flash messages when sessions are unset.
|
||
269 | *
|
||
270 | * @return void
|
||
271 | */
|
||
272 | public function testScaffoldFlashMessages() { |
||
273 | $params = array( |
||
274 | 'plugin' => null, |
||
275 | 'pass' => array(1), |
||
276 | 'form' => array(), |
||
277 | 'named' => array(), |
||
278 | 'url' => array('url' => 'scaffold_mock'), |
||
279 | 'controller' => 'scaffold_mock', |
||
280 | 'action' => 'edit', |
||
281 | ); |
||
282 | $this->Controller->request->base = ''; |
||
283 | $this->Controller->request->webroot = '/'; |
||
284 | $this->Controller->request->here = '/scaffold_mock/edit'; |
||
285 | $this->Controller->request->addParams($params); |
||
286 | |||
287 | //set router.
|
||
288 | Router::reload();
|
||
289 | Router::setRequestInfo($this->Controller->request); |
||
290 | $this->Controller->request->data = array( |
||
291 | 'ScaffoldMock' => array( |
||
292 | 'id' => 1, |
||
293 | 'title' => 'New title', |
||
294 | 'body' => 'new body' |
||
295 | ) |
||
296 | ); |
||
297 | $this->Controller->constructClasses(); |
||
298 | unset($this->Controller->Session); |
||
299 | |||
300 | ob_start(); |
||
301 | new Scaffold($this->Controller, $this->Controller->request); |
||
302 | $this->Controller->response->send(); |
||
303 | $result = ob_get_clean();
|
||
304 | $this->assertRegExp('/Scaffold Mock has been updated/', $result); |
||
305 | } |
||
306 | |||
307 | /**
|
||
308 | * test that habtm relationship keys get added to scaffoldFields.
|
||
309 | *
|
||
310 | * @return void
|
||
311 | */
|
||
312 | public function testHabtmFieldAdditionWithScaffoldForm() { |
||
313 | CakePlugin::unload();
|
||
314 | $params = array( |
||
315 | 'plugin' => null, |
||
316 | 'pass' => array(1), |
||
317 | 'form' => array(), |
||
318 | 'named' => array(), |
||
319 | 'url' => array('url' => 'scaffold_mock'), |
||
320 | 'controller' => 'scaffold_mock', |
||
321 | 'action' => 'edit', |
||
322 | ); |
||
323 | $this->Controller->request->base = ''; |
||
324 | $this->Controller->request->webroot = '/'; |
||
325 | $this->Controller->request->here = '/scaffold_mock/edit'; |
||
326 | $this->Controller->request->addParams($params); |
||
327 | |||
328 | //set router.
|
||
329 | Router::reload();
|
||
330 | Router::setRequestInfo($this->Controller->request); |
||
331 | |||
332 | $this->Controller->constructClasses(); |
||
333 | ob_start(); |
||
334 | $Scaffold = new Scaffold($this->Controller, $this->Controller->request); |
||
335 | $this->Controller->response->send(); |
||
336 | $result = ob_get_clean();
|
||
337 | $this->assertRegExp('/name="data\[ScaffoldTag\]\[ScaffoldTag\]"/', $result); |
||
338 | |||
339 | $result = $Scaffold->controller->viewVars; |
||
340 | $this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated', 'ScaffoldTag'), $result['scaffoldFields']); |
||
341 | } |
||
342 | |||
343 | /**
|
||
344 | * test that the proper names and variable values are set by Scaffold
|
||
345 | *
|
||
346 | * @return void
|
||
347 | */
|
||
348 | public function testEditScaffoldWithScaffoldFields() { |
||
349 | $request = new CakeRequest(null, false); |
||
350 | $this->Controller = new ScaffoldMockControllerWithFields($request); |
||
351 | $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader')); |
||
352 | |||
353 | $params = array( |
||
354 | 'plugin' => null, |
||
355 | 'pass' => array(1), |
||
356 | 'form' => array(), |
||
357 | 'named' => array(), |
||
358 | 'url' => array('url' => 'scaffold_mock/edit'), |
||
359 | 'controller' => 'scaffold_mock', |
||
360 | 'action' => 'edit', |
||
361 | ); |
||
362 | $this->Controller->request->base = ''; |
||
363 | $this->Controller->request->webroot = '/'; |
||
364 | $this->Controller->request->here = '/scaffold_mock/edit'; |
||
365 | $this->Controller->request->addParams($params); |
||
366 | |||
367 | //set router.
|
||
368 | Router::reload();
|
||
369 | Router::setRequestInfo($this->Controller->request); |
||
370 | |||
371 | $this->Controller->constructClasses(); |
||
372 | ob_start(); |
||
373 | new Scaffold($this->Controller, $this->Controller->request); |
||
374 | $this->Controller->response->send(); |
||
375 | $result = ob_get_clean();
|
||
376 | |||
377 | $this->assertNotRegExp('/textarea name="data\[ScaffoldMock\]\[body\]" cols="30" rows="6" id="ScaffoldMockBody"/', $result); |
||
378 | } |
||
379 | |||
380 | /**
|
||
381 | * test in case of scaffold error
|
||
382 | *
|
||
383 | * @return void
|
||
384 | */
|
||
385 | public function testScaffoldError() { |
||
386 | $request = new CakeRequest(null, false); |
||
387 | $this->Controller = new ScaffoldMockControllerWithError($request); |
||
388 | $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader')); |
||
389 | |||
390 | $params = array( |
||
391 | 'plugin' => null, |
||
392 | 'pass' => array(1), |
||
393 | 'form' => array(), |
||
394 | 'named' => array(), |
||
395 | 'url' => array('url' => 'scaffold_mock/edit'), |
||
396 | 'controller' => 'scaffold_mock', |
||
397 | 'action' => 'edit', |
||
398 | ); |
||
399 | $this->Controller->request->base = ''; |
||
400 | $this->Controller->request->webroot = '/'; |
||
401 | $this->Controller->request->here = '/scaffold_mock/edit'; |
||
402 | $this->Controller->request->addParams($params); |
||
403 | |||
404 | //set router.
|
||
405 | Router::reload();
|
||
406 | Router::setRequestInfo($this->Controller->request); |
||
407 | |||
408 | $this->Controller->constructClasses(); |
||
409 | ob_start(); |
||
410 | new Scaffold($this->Controller, $this->Controller->request); |
||
411 | $this->Controller->response->send(); |
||
412 | $result = ob_get_clean();
|
||
413 | |||
414 | $this->assertRegExp('/Scaffold Error/', $result); |
||
415 | } |
||
416 | } |