Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/controller/scaffold.test.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * ScaffoldTest file | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing> | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The Open Group Test Suite License | |
11 * Redistributions of files must retain the above copyright notice. | |
12 * | |
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests | |
15 * @package cake | |
16 * @subpackage cake.tests.cases.libs.controller | |
17 * @since CakePHP(tm) v 1.2.0.5436 | |
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License | |
19 */ | |
20 App::import('Core', 'Scaffold'); | |
21 | |
22 /** | |
23 * ScaffoldMockController class | |
24 * | |
25 * @package cake | |
26 * @subpackage cake.tests.cases.libs.controller | |
27 */ | |
28 class ScaffoldMockController extends Controller { | |
29 | |
30 /** | |
31 * name property | |
32 * | |
33 * @var string 'ScaffoldMock' | |
34 * @access public | |
35 */ | |
36 var $name = 'ScaffoldMock'; | |
37 | |
38 /** | |
39 * scaffold property | |
40 * | |
41 * @var mixed | |
42 * @access public | |
43 */ | |
44 var $scaffold; | |
45 } | |
46 | |
47 /** | |
48 * ScaffoldMockControllerWithFields class | |
49 * | |
50 * @package cake | |
51 * @subpackage cake.tests.cases.libs.controller | |
52 */ | |
53 class ScaffoldMockControllerWithFields extends Controller { | |
54 | |
55 /** | |
56 * name property | |
57 * | |
58 * @var string 'ScaffoldMock' | |
59 * @access public | |
60 */ | |
61 var $name = 'ScaffoldMock'; | |
62 | |
63 /** | |
64 * scaffold property | |
65 * | |
66 * @var mixed | |
67 * @access public | |
68 */ | |
69 var $scaffold; | |
70 | |
71 /** | |
72 * function _beforeScaffold | |
73 * | |
74 * @param string method | |
75 */ | |
76 function _beforeScaffold($method) { | |
77 $this->set('scaffoldFields', array('title')); | |
78 return true; | |
79 } | |
80 } | |
81 | |
82 /** | |
83 * TestScaffoldMock class | |
84 * | |
85 * @package cake | |
86 * @subpackage cake.tests.cases.libs.controller | |
87 */ | |
88 class TestScaffoldMock extends Scaffold { | |
89 | |
90 /** | |
91 * Overload __scaffold | |
92 * | |
93 * @param unknown_type $params | |
94 */ | |
95 function __scaffold($params) { | |
96 $this->_params = $params; | |
97 } | |
98 | |
99 /** | |
100 * Get Params from the Controller. | |
101 * | |
102 * @return unknown | |
103 */ | |
104 function getParams() { | |
105 return $this->_params; | |
106 } | |
107 } | |
108 | |
109 /** | |
110 * ScaffoldMock class | |
111 * | |
112 * @package cake | |
113 * @subpackage cake.tests.cases.libs.controller | |
114 */ | |
115 class ScaffoldMock extends CakeTestModel { | |
116 | |
117 /** | |
118 * useTable property | |
119 * | |
120 * @var string 'posts' | |
121 * @access public | |
122 */ | |
123 var $useTable = 'articles'; | |
124 | |
125 /** | |
126 * belongsTo property | |
127 * | |
128 * @var array | |
129 * @access public | |
130 */ | |
131 var $belongsTo = array( | |
132 'User' => array( | |
133 'className' => 'ScaffoldUser', | |
134 'foreignKey' => 'user_id', | |
135 ) | |
136 ); | |
137 | |
138 /** | |
139 * hasMany property | |
140 * | |
141 * @var array | |
142 * @access public | |
143 */ | |
144 var $hasMany = array( | |
145 'Comment' => array( | |
146 'className' => 'ScaffoldComment', | |
147 'foreignKey' => 'article_id', | |
148 ) | |
149 ); | |
150 /** | |
151 * hasAndBelongsToMany property | |
152 * | |
153 * @var string | |
154 */ | |
155 var $hasAndBelongsToMany = array( | |
156 'ScaffoldTag' => array( | |
157 'className' => 'ScaffoldTag', | |
158 'foreignKey' => 'something_id', | |
159 'associationForeignKey' => 'something_else_id', | |
160 'joinTable' => 'join_things' | |
161 ) | |
162 ); | |
163 } | |
164 | |
165 /** | |
166 * ScaffoldUser class | |
167 * | |
168 * @package cake | |
169 * @subpackage cake.tests.cases.libs.controller | |
170 */ | |
171 class ScaffoldUser extends CakeTestModel { | |
172 | |
173 /** | |
174 * useTable property | |
175 * | |
176 * @var string 'posts' | |
177 * @access public | |
178 */ | |
179 var $useTable = 'users'; | |
180 | |
181 /** | |
182 * hasMany property | |
183 * | |
184 * @var array | |
185 * @access public | |
186 */ | |
187 var $hasMany = array( | |
188 'Article' => array( | |
189 'className' => 'ScaffoldMock', | |
190 'foreignKey' => 'article_id', | |
191 ) | |
192 ); | |
193 } | |
194 | |
195 /** | |
196 * ScaffoldComment class | |
197 * | |
198 * @package cake | |
199 * @subpackage cake.tests.cases.libs.controller | |
200 */ | |
201 class ScaffoldComment extends CakeTestModel { | |
202 | |
203 /** | |
204 * useTable property | |
205 * | |
206 * @var string 'posts' | |
207 * @access public | |
208 */ | |
209 var $useTable = 'comments'; | |
210 | |
211 /** | |
212 * belongsTo property | |
213 * | |
214 * @var array | |
215 * @access public | |
216 */ | |
217 var $belongsTo = array( | |
218 'Article' => array( | |
219 'className' => 'ScaffoldMock', | |
220 'foreignKey' => 'article_id', | |
221 ) | |
222 ); | |
223 } | |
224 | |
225 /** | |
226 * ScaffoldTag class | |
227 * | |
228 * @package cake | |
229 * @subpackage cake.tests.cases.libs.controller | |
230 */ | |
231 class ScaffoldTag extends CakeTestModel { | |
232 /** | |
233 * useTable property | |
234 * | |
235 * @var string 'posts' | |
236 * @access public | |
237 */ | |
238 var $useTable = 'tags'; | |
239 } | |
240 /** | |
241 * TestScaffoldView class | |
242 * | |
243 * @package cake | |
244 * @subpackage cake.tests.cases.libs.controller | |
245 */ | |
246 class TestScaffoldView extends ScaffoldView { | |
247 | |
248 /** | |
249 * testGetFilename method | |
250 * | |
251 * @param mixed $action | |
252 * @access public | |
253 * @return void | |
254 */ | |
255 function testGetFilename($action) { | |
256 return $this->_getViewFileName($action); | |
257 } | |
258 } | |
259 | |
260 /** | |
261 * ScaffoldViewTest class | |
262 * | |
263 * @package cake | |
264 * @subpackage cake.tests.cases.libs.controller | |
265 */ | |
266 class ScaffoldViewTest extends CakeTestCase { | |
267 | |
268 /** | |
269 * fixtures property | |
270 * | |
271 * @var array | |
272 * @access public | |
273 */ | |
274 var $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag'); | |
275 | |
276 /** | |
277 * startTest method | |
278 * | |
279 * @access public | |
280 * @return void | |
281 */ | |
282 function startTest() { | |
283 $this->Controller =& new ScaffoldMockController(); | |
284 | |
285 App::build(array( | |
286 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS), | |
287 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS) | |
288 )); | |
289 } | |
290 | |
291 /** | |
292 * endTest method | |
293 * | |
294 * @access public | |
295 * @return void | |
296 */ | |
297 function endTest() { | |
298 unset($this->Controller); | |
299 | |
300 App::build(); | |
301 } | |
302 | |
303 /** | |
304 * testGetViewFilename method | |
305 * | |
306 * @access public | |
307 * @return void | |
308 */ | |
309 function testGetViewFilename() { | |
310 $_admin = Configure::read('Routing.prefixes'); | |
311 Configure::write('Routing.prefixes', array('admin')); | |
312 | |
313 $this->Controller->action = 'index'; | |
314 $ScaffoldView =& new TestScaffoldView($this->Controller); | |
315 $result = $ScaffoldView->testGetFilename('index'); | |
316 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'index.ctp'; | |
317 $this->assertEqual($result, $expected); | |
318 | |
319 $result = $ScaffoldView->testGetFilename('edit'); | |
320 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'edit.ctp'; | |
321 $this->assertEqual($result, $expected); | |
322 | |
323 $result = $ScaffoldView->testGetFilename('add'); | |
324 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'edit.ctp'; | |
325 $this->assertEqual($result, $expected); | |
326 | |
327 $result = $ScaffoldView->testGetFilename('view'); | |
328 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'view.ctp'; | |
329 $this->assertEqual($result, $expected); | |
330 | |
331 $result = $ScaffoldView->testGetFilename('admin_index'); | |
332 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'index.ctp'; | |
333 $this->assertEqual($result, $expected); | |
334 | |
335 $result = $ScaffoldView->testGetFilename('admin_view'); | |
336 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'view.ctp'; | |
337 $this->assertEqual($result, $expected); | |
338 | |
339 $result = $ScaffoldView->testGetFilename('admin_edit'); | |
340 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'edit.ctp'; | |
341 $this->assertEqual($result, $expected); | |
342 | |
343 $result = $ScaffoldView->testGetFilename('admin_add'); | |
344 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'edit.ctp'; | |
345 $this->assertEqual($result, $expected); | |
346 | |
347 $result = $ScaffoldView->testGetFilename('error'); | |
348 $expected = 'cake' . DS . 'libs' . DS . 'view' . DS . 'errors' . DS . 'scaffold_error.ctp'; | |
349 $this->assertEqual($result, $expected); | |
350 | |
351 $Controller =& new ScaffoldMockController(); | |
352 $Controller->scaffold = 'admin'; | |
353 $Controller->viewPath = 'posts'; | |
354 $Controller->action = 'admin_edit'; | |
355 $ScaffoldView =& new TestScaffoldView($Controller); | |
356 $result = $ScaffoldView->testGetFilename('admin_edit'); | |
357 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' .DS . 'views' . DS . 'posts' . DS . 'scaffold.edit.ctp'; | |
358 $this->assertEqual($result, $expected); | |
359 | |
360 $result = $ScaffoldView->testGetFilename('edit'); | |
361 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' .DS . 'views' . DS . 'posts' . DS . 'scaffold.edit.ctp'; | |
362 $this->assertEqual($result, $expected); | |
363 | |
364 $Controller =& new ScaffoldMockController(); | |
365 $Controller->scaffold = 'admin'; | |
366 $Controller->viewPath = 'tests'; | |
367 $Controller->plugin = 'test_plugin'; | |
368 $Controller->action = 'admin_add'; | |
369 $ScaffoldView =& new TestScaffoldView($Controller); | |
370 $result = $ScaffoldView->testGetFilename('admin_add'); | |
371 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' | |
372 . DS .'test_plugin' . DS . 'views' . DS . 'tests' . DS . 'scaffold.edit.ctp'; | |
373 $this->assertEqual($result, $expected); | |
374 | |
375 $result = $ScaffoldView->testGetFilename('add'); | |
376 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' | |
377 . DS .'test_plugin' . DS . 'views' . DS . 'tests' . DS . 'scaffold.edit.ctp'; | |
378 $this->assertEqual($result, $expected); | |
379 | |
380 Configure::write('Routing.prefixes', $_admin); | |
381 } | |
382 | |
383 /** | |
384 * test getting the view file name for themed scaffolds. | |
385 * | |
386 * @return void | |
387 */ | |
388 function testGetViewFileNameWithTheme() { | |
389 $this->Controller->action = 'index'; | |
390 $this->Controller->viewPath = 'posts'; | |
391 $this->Controller->theme = 'test_theme'; | |
392 $ScaffoldView =& new TestScaffoldView($this->Controller); | |
393 | |
394 $result = $ScaffoldView->testGetFilename('index'); | |
395 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS | |
396 . 'themed' . DS . 'test_theme' . DS . 'posts' . DS . 'scaffold.index.ctp'; | |
397 $this->assertEqual($result, $expected); | |
398 } | |
399 | |
400 /** | |
401 * test default index scaffold generation | |
402 * | |
403 * @access public | |
404 * @return void | |
405 */ | |
406 function testIndexScaffold() { | |
407 $this->Controller->action = 'index'; | |
408 $this->Controller->here = '/scaffold_mock'; | |
409 $this->Controller->webroot = '/'; | |
410 $params = array( | |
411 'plugin' => null, | |
412 'pass' => array(), | |
413 'form' => array(), | |
414 'named' => array(), | |
415 'url' => array('url' =>'scaffold_mock'), | |
416 'controller' => 'scaffold_mock', | |
417 'action' => 'index', | |
418 ); | |
419 //set router. | |
420 Router::reload(); | |
421 Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/'))); | |
422 $this->Controller->params = $params; | |
423 $this->Controller->controller = 'scaffold_mock'; | |
424 $this->Controller->base = '/'; | |
425 $this->Controller->constructClasses(); | |
426 ob_start(); | |
427 new Scaffold($this->Controller, $params); | |
428 $result = ob_get_clean(); | |
429 | |
430 $this->assertPattern('#<h2>Scaffold Mock</h2>#', $result); | |
431 $this->assertPattern('#<table cellpadding="0" cellspacing="0">#', $result); | |
432 | |
433 $this->assertPattern('#<a href="/scaffold_users/view/1">1</a>#', $result); //belongsTo links | |
434 $this->assertPattern('#<li><a href="/scaffold_mock/add">New Scaffold Mock</a></li>#', $result); | |
435 $this->assertPattern('#<li><a href="/scaffold_users">List Scaffold Users</a></li>#', $result); | |
436 $this->assertPattern('#<li><a href="/scaffold_comments/add">New Comment</a></li>#', $result); | |
437 } | |
438 | |
439 /** | |
440 * test default view scaffold generation | |
441 * | |
442 * @access public | |
443 * @return void | |
444 */ | |
445 function testViewScaffold() { | |
446 $this->Controller->action = 'view'; | |
447 $this->Controller->here = '/scaffold_mock'; | |
448 $this->Controller->webroot = '/'; | |
449 $params = array( | |
450 'plugin' => null, | |
451 'pass' => array(1), | |
452 'form' => array(), | |
453 'named' => array(), | |
454 'url' => array('url' =>'scaffold_mock'), | |
455 'controller' => 'scaffold_mock', | |
456 'action' => 'view', | |
457 ); | |
458 //set router. | |
459 Router::reload(); | |
460 Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/'))); | |
461 $this->Controller->params = $params; | |
462 $this->Controller->controller = 'scaffold_mock'; | |
463 $this->Controller->base = '/'; | |
464 $this->Controller->constructClasses(); | |
465 | |
466 ob_start(); | |
467 new Scaffold($this->Controller, $params); | |
468 $result = ob_get_clean(); | |
469 | |
470 $this->assertPattern('/<h2>View Scaffold Mock<\/h2>/', $result); | |
471 $this->assertPattern('/<dl>/', $result); | |
472 //TODO: add specific tests for fields. | |
473 $this->assertPattern('/<a href="\/scaffold_users\/view\/1">1<\/a>/', $result); //belongsTo links | |
474 $this->assertPattern('/<li><a href="\/scaffold_mock\/edit\/1">Edit Scaffold Mock<\/a>\s<\/li>/', $result); | |
475 $this->assertPattern('/<li><a href="\/scaffold_mock\/delete\/1"[^>]*>Delete Scaffold Mock<\/a>\s*<\/li>/', $result); | |
476 //check related table | |
477 $this->assertPattern('/<div class="related">\s*<h3>Related Scaffold Comments<\/h3>\s*<table cellpadding="0" cellspacing="0">/', $result); | |
478 $this->assertPattern('/<li><a href="\/scaffold_comments\/add">New Comment<\/a><\/li>/', $result); | |
479 $this->assertNoPattern('/<th>JoinThing<\/th>/', $result); | |
480 } | |
481 | |
482 /** | |
483 * test default view scaffold generation | |
484 * | |
485 * @access public | |
486 * @return void | |
487 */ | |
488 function testEditScaffold() { | |
489 $this->Controller->action = 'edit'; | |
490 $this->Controller->here = '/scaffold_mock'; | |
491 $this->Controller->webroot = '/'; | |
492 $params = array( | |
493 'plugin' => null, | |
494 'pass' => array(1), | |
495 'form' => array(), | |
496 'named' => array(), | |
497 'url' => array('url' =>'scaffold_mock'), | |
498 'controller' => 'scaffold_mock', | |
499 'action' => 'edit', | |
500 ); | |
501 //set router. | |
502 Router::reload(); | |
503 Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/'))); | |
504 $this->Controller->params = $params; | |
505 $this->Controller->controller = 'scaffold_mock'; | |
506 $this->Controller->base = '/'; | |
507 $this->Controller->constructClasses(); | |
508 ob_start(); | |
509 new Scaffold($this->Controller, $params); | |
510 $result = ob_get_clean(); | |
511 | |
512 $this->assertPattern('/<form id="ScaffoldMockEditForm" method="post" action="\/scaffold_mock\/edit\/1"/', $result); | |
513 $this->assertPattern('/<legend>Edit Scaffold Mock<\/legend>/', $result); | |
514 | |
515 $this->assertPattern('/input type="hidden" name="data\[ScaffoldMock\]\[id\]" value="1" id="ScaffoldMockId"/', $result); | |
516 $this->assertPattern('/select name="data\[ScaffoldMock\]\[user_id\]" id="ScaffoldMockUserId"/', $result); | |
517 $this->assertPattern('/input name="data\[ScaffoldMock\]\[title\]" type="text" maxlength="255" value="First Article" id="ScaffoldMockTitle"/', $result); | |
518 $this->assertPattern('/input name="data\[ScaffoldMock\]\[published\]" type="text" maxlength="1" value="Y" id="ScaffoldMockPublished"/', $result); | |
519 $this->assertPattern('/textarea name="data\[ScaffoldMock\]\[body\]" cols="30" rows="6" id="ScaffoldMockBody"/', $result); | |
520 $this->assertPattern('/<li><a href="\/scaffold_mock\/delete\/1"[^>]*>Delete<\/a>\s*<\/li>/', $result); | |
521 } | |
522 | |
523 /** | |
524 * Test Admin Index Scaffolding. | |
525 * | |
526 * @access public | |
527 * @return void | |
528 */ | |
529 function testAdminIndexScaffold() { | |
530 $_backAdmin = Configure::read('Routing.prefixes'); | |
531 | |
532 Configure::write('Routing.prefixes', array('admin')); | |
533 $params = array( | |
534 'plugin' => null, | |
535 'pass' => array(), | |
536 'form' => array(), | |
537 'named' => array(), | |
538 'prefix' => 'admin', | |
539 'url' => array('url' =>'admin/scaffold_mock'), | |
540 'controller' => 'scaffold_mock', | |
541 'action' => 'admin_index', | |
542 'admin' => 1, | |
543 ); | |
544 //reset, and set router. | |
545 Router::reload(); | |
546 Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/admin/scaffold_mock', 'webroot' => '/'))); | |
547 $this->Controller->params = $params; | |
548 $this->Controller->controller = 'scaffold_mock'; | |
549 $this->Controller->base = '/'; | |
550 $this->Controller->action = 'admin_index'; | |
551 $this->Controller->here = '/tests/admin/scaffold_mock'; | |
552 $this->Controller->webroot = '/'; | |
553 $this->Controller->scaffold = 'admin'; | |
554 $this->Controller->constructClasses(); | |
555 | |
556 ob_start(); | |
557 $Scaffold = new Scaffold($this->Controller, $params); | |
558 $result = ob_get_clean(); | |
559 | |
560 $this->assertPattern('/<h2>Scaffold Mock<\/h2>/', $result); | |
561 $this->assertPattern('/<table cellpadding="0" cellspacing="0">/', $result); | |
562 //TODO: add testing for table generation | |
563 $this->assertPattern('/<li><a href="\/admin\/scaffold_mock\/add">New Scaffold Mock<\/a><\/li>/', $result); | |
564 | |
565 Configure::write('Routing.prefixes', $_backAdmin); | |
566 } | |
567 | |
568 /** | |
569 * Test Admin Index Scaffolding. | |
570 * | |
571 * @access public | |
572 * @return void | |
573 */ | |
574 function testAdminEditScaffold() { | |
575 $_backAdmin = Configure::read('Routing.prefixes'); | |
576 | |
577 Configure::write('Routing.prefixes', array('admin')); | |
578 $params = array( | |
579 'plugin' => null, | |
580 'pass' => array(), | |
581 'form' => array(), | |
582 'named' => array(), | |
583 'prefix' => 'admin', | |
584 'url' => array('url' =>'admin/scaffold_mock/edit'), | |
585 'controller' => 'scaffold_mock', | |
586 'action' => 'admin_edit', | |
587 'admin' => 1, | |
588 ); | |
589 //reset, and set router. | |
590 Router::reload(); | |
591 Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/admin/scaffold_mock/edit', 'webroot' => '/'))); | |
592 $this->Controller->params = $params; | |
593 $this->Controller->controller = 'scaffold_mock'; | |
594 $this->Controller->base = '/'; | |
595 $this->Controller->action = 'admin_index'; | |
596 $this->Controller->here = '/tests/admin/scaffold_mock'; | |
597 $this->Controller->webroot = '/'; | |
598 $this->Controller->scaffold = 'admin'; | |
599 $this->Controller->constructClasses(); | |
600 | |
601 ob_start(); | |
602 $Scaffold = new Scaffold($this->Controller, $params); | |
603 $result = ob_get_clean(); | |
604 | |
605 $this->assertPattern('#admin/scaffold_mock/edit/1#', $result); | |
606 $this->assertPattern('#Scaffold Mock#', $result); | |
607 | |
608 Configure::write('Routing.prefixes', $_backAdmin); | |
609 } | |
610 | |
611 /** | |
612 * Test Admin Index Scaffolding. | |
613 * | |
614 * @access public | |
615 * @return void | |
616 */ | |
617 function testMultiplePrefixScaffold() { | |
618 $_backAdmin = Configure::read('Routing.prefixes'); | |
619 | |
620 Configure::write('Routing.prefixes', array('admin', 'member')); | |
621 $params = array( | |
622 'plugin' => null, | |
623 'pass' => array(), | |
624 'form' => array(), | |
625 'named' => array(), | |
626 'prefix' => 'member', | |
627 'url' => array('url' =>'member/scaffold_mock'), | |
628 'controller' => 'scaffold_mock', | |
629 'action' => 'member_index', | |
630 'member' => 1, | |
631 ); | |
632 //reset, and set router. | |
633 Router::reload(); | |
634 Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/member/scaffold_mock', 'webroot' => '/'))); | |
635 $this->Controller->params = $params; | |
636 $this->Controller->controller = 'scaffold_mock'; | |
637 $this->Controller->base = '/'; | |
638 $this->Controller->action = 'member_index'; | |
639 $this->Controller->here = '/tests/member/scaffold_mock'; | |
640 $this->Controller->webroot = '/'; | |
641 $this->Controller->scaffold = 'member'; | |
642 $this->Controller->constructClasses(); | |
643 | |
644 ob_start(); | |
645 $Scaffold = new Scaffold($this->Controller, $params); | |
646 $result = ob_get_clean(); | |
647 | |
648 $this->assertPattern('/<h2>Scaffold Mock<\/h2>/', $result); | |
649 $this->assertPattern('/<table cellpadding="0" cellspacing="0">/', $result); | |
650 //TODO: add testing for table generation | |
651 $this->assertPattern('/<li><a href="\/member\/scaffold_mock\/add">New Scaffold Mock<\/a><\/li>/', $result); | |
652 | |
653 Configure::write('Routing.prefixes', $_backAdmin); | |
654 } | |
655 | |
656 } | |
657 | |
658 /** | |
659 * Scaffold Test class | |
660 * | |
661 * @package cake | |
662 * @subpackage cake.tests.cases.libs.controller | |
663 */ | |
664 class ScaffoldTest extends CakeTestCase { | |
665 | |
666 /** | |
667 * Controller property | |
668 * | |
669 * @var SecurityTestController | |
670 * @access public | |
671 */ | |
672 var $Controller; | |
673 | |
674 /** | |
675 * fixtures property | |
676 * | |
677 * @var array | |
678 * @access public | |
679 */ | |
680 var $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag'); | |
681 /** | |
682 * startTest method | |
683 * | |
684 * @access public | |
685 * @return void | |
686 */ | |
687 function startTest() { | |
688 $this->Controller =& new ScaffoldMockController(); | |
689 } | |
690 | |
691 /** | |
692 * endTest method | |
693 * | |
694 * @access public | |
695 * @return void | |
696 */ | |
697 function endTest() { | |
698 unset($this->Controller); | |
699 } | |
700 | |
701 /** | |
702 * Test the correct Generation of Scaffold Params. | |
703 * This ensures that the correct action and view will be generated | |
704 * | |
705 * @access public | |
706 * @return void | |
707 */ | |
708 function testScaffoldParams() { | |
709 $this->Controller->action = 'admin_edit'; | |
710 $this->Controller->here = '/admin/scaffold_mock/edit'; | |
711 $this->Controller->webroot = '/'; | |
712 $params = array( | |
713 'plugin' => null, | |
714 'pass' => array(), | |
715 'form' => array(), | |
716 'named' => array(), | |
717 'url' => array('url' =>'admin/scaffold_mock/edit'), | |
718 'controller' => 'scaffold_mock', | |
719 'action' => 'admin_edit', | |
720 'admin' => true, | |
721 ); | |
722 //set router. | |
723 Router::setRequestInfo(array($params, array('base' => '/', 'here' => 'admin/scaffold_mock', 'webroot' => '/'))); | |
724 | |
725 $this->Controller->params = $params; | |
726 $this->Controller->controller = 'scaffold_mock'; | |
727 $this->Controller->base = '/'; | |
728 $this->Controller->constructClasses(); | |
729 $Scaffold =& new TestScaffoldMock($this->Controller, $params); | |
730 $result = $Scaffold->getParams(); | |
731 $this->assertEqual($result['action'], 'admin_edit'); | |
732 } | |
733 | |
734 /** | |
735 * test that the proper names and variable values are set by Scaffold | |
736 * | |
737 * @return void | |
738 */ | |
739 function testScaffoldVariableSetting() { | |
740 $this->Controller->action = 'admin_edit'; | |
741 $this->Controller->here = '/admin/scaffold_mock/edit'; | |
742 $this->Controller->webroot = '/'; | |
743 $params = array( | |
744 'plugin' => null, | |
745 'pass' => array(), | |
746 'form' => array(), | |
747 'named' => array(), | |
748 'url' => array('url' =>'admin/scaffold_mock/edit'), | |
749 'controller' => 'scaffold_mock', | |
750 'action' => 'admin_edit', | |
751 'admin' => true, | |
752 ); | |
753 //set router. | |
754 Router::setRequestInfo(array($params, array('base' => '/', 'here' => 'admin/scaffold_mock', 'webroot' => '/'))); | |
755 | |
756 $this->Controller->params = $params; | |
757 $this->Controller->controller = 'scaffold_mock'; | |
758 $this->Controller->base = '/'; | |
759 $this->Controller->constructClasses(); | |
760 $Scaffold =& new TestScaffoldMock($this->Controller, $params); | |
761 $result = $Scaffold->controller->viewVars; | |
762 | |
763 $this->assertEqual($result['title_for_layout'], 'Scaffold :: Admin Edit :: Scaffold Mock'); | |
764 $this->assertEqual($result['singularHumanName'], 'Scaffold Mock'); | |
765 $this->assertEqual($result['pluralHumanName'], 'Scaffold Mock'); | |
766 $this->assertEqual($result['modelClass'], 'ScaffoldMock'); | |
767 $this->assertEqual($result['primaryKey'], 'id'); | |
768 $this->assertEqual($result['displayField'], 'title'); | |
769 $this->assertEqual($result['singularVar'], 'scaffoldMock'); | |
770 $this->assertEqual($result['pluralVar'], 'scaffoldMock'); | |
771 $this->assertEqual($result['scaffoldFields'], array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated')); | |
772 } | |
773 function getTests() { | |
774 return array('start', 'startCase', 'testScaffoldChangingViewProperty', 'endCase', 'end'); | |
775 } | |
776 | |
777 /** | |
778 * test that Scaffold overrides the view property even if its set to 'Theme' | |
779 * | |
780 * @return void | |
781 */ | |
782 function testScaffoldChangingViewProperty() { | |
783 $this->Controller->action = 'edit'; | |
784 $this->Controller->theme = 'test_theme'; | |
785 $this->Controller->view = 'Theme'; | |
786 $this->Controller->constructClasses(); | |
787 $Scaffold =& new TestScaffoldMock($this->Controller, array()); | |
788 | |
789 $this->assertEqual($this->Controller->view, 'Scaffold'); | |
790 } | |
791 | |
792 /** | |
793 * test that scaffold outputs flash messages when sessions are unset. | |
794 * | |
795 * @return void | |
796 */ | |
797 function testScaffoldFlashMessages() { | |
798 $this->Controller->action = 'edit'; | |
799 $this->Controller->here = '/scaffold_mock'; | |
800 $this->Controller->webroot = '/'; | |
801 $params = array( | |
802 'plugin' => null, | |
803 'pass' => array(1), | |
804 'form' => array(), | |
805 'named' => array(), | |
806 'url' => array('url' =>'scaffold_mock'), | |
807 'controller' => 'scaffold_mock', | |
808 'action' => 'edit', | |
809 ); | |
810 //set router. | |
811 Router::reload(); | |
812 Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/'))); | |
813 $this->Controller->params = $params; | |
814 $this->Controller->controller = 'scaffold_mock'; | |
815 $this->Controller->base = '/'; | |
816 $this->Controller->data = array( | |
817 'ScaffoldMock' => array( | |
818 'id' => 1, | |
819 'title' => 'New title', | |
820 'body' => 'new body' | |
821 ) | |
822 ); | |
823 $this->Controller->constructClasses(); | |
824 unset($this->Controller->Session); | |
825 | |
826 ob_start(); | |
827 new Scaffold($this->Controller, $params); | |
828 $result = ob_get_clean(); | |
829 $this->assertPattern('/Scaffold Mock has been updated/', $result); | |
830 } | |
831 /** | |
832 * test that habtm relationship keys get added to scaffoldFields. | |
833 * | |
834 * @see http://code.cakephp.org/tickets/view/48 | |
835 * @return void | |
836 */ | |
837 function testHabtmFieldAdditionWithScaffoldForm() { | |
838 $this->Controller->action = 'edit'; | |
839 $this->Controller->here = '/scaffold_mock'; | |
840 $this->Controller->webroot = '/'; | |
841 $params = array( | |
842 'plugin' => null, | |
843 'pass' => array(1), | |
844 'form' => array(), | |
845 'named' => array(), | |
846 'url' => array('url' =>'scaffold_mock'), | |
847 'controller' => 'scaffold_mock', | |
848 'action' => 'edit', | |
849 ); | |
850 //set router. | |
851 Router::reload(); | |
852 Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/'))); | |
853 $this->Controller->params = $params; | |
854 $this->Controller->controller = 'scaffold_mock'; | |
855 $this->Controller->base = '/'; | |
856 $this->Controller->constructClasses(); | |
857 ob_start(); | |
858 $Scaffold = new Scaffold($this->Controller, $params); | |
859 $result = ob_get_clean(); | |
860 $this->assertPattern('/name="data\[ScaffoldTag\]\[ScaffoldTag\]"/', $result); | |
861 | |
862 $result = $Scaffold->controller->viewVars; | |
863 $this->assertEqual($result['scaffoldFields'], array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated', 'ScaffoldTag')); | |
864 } | |
865 /** | |
866 * test that the proper names and variable values are set by Scaffold | |
867 * | |
868 * @return void | |
869 */ | |
870 function testEditScaffoldWithScaffoldFields() { | |
871 $this->Controller = new ScaffoldMockControllerWithFields(); | |
872 $this->Controller->action = 'edit'; | |
873 $this->Controller->here = '/scaffold_mock'; | |
874 $this->Controller->webroot = '/'; | |
875 $params = array( | |
876 'plugin' => null, | |
877 'pass' => array(1), | |
878 'form' => array(), | |
879 'named' => array(), | |
880 'url' => array('url' =>'scaffold_mock'), | |
881 'controller' => 'scaffold_mock', | |
882 'action' => 'edit', | |
883 ); | |
884 //set router. | |
885 Router::reload(); | |
886 Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/'))); | |
887 $this->Controller->params = $params; | |
888 $this->Controller->controller = 'scaffold_mock'; | |
889 $this->Controller->base = '/'; | |
890 $this->Controller->constructClasses(); | |
891 ob_start(); | |
892 new Scaffold($this->Controller, $params); | |
893 $result = ob_get_clean(); | |
894 | |
895 $this->assertNoPattern('/textarea name="data\[ScaffoldMock\]\[body\]" cols="30" rows="6" id="ScaffoldMockBody"/', $result); | |
896 } | |
897 } |