Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/controller/controller.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 * ControllerTest file | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The MIT 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://cakephp.org CakePHP Project | |
15 * @package cake | |
16 * @subpackage cake.tests.cases.libs.controller | |
17 * @since CakePHP(tm) v 1.2.0.5436 | |
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) | |
19 */ | |
20 App::import('Controller', 'Controller', false); | |
21 App::import('Component', 'Security'); | |
22 App::import('Component', 'Cookie'); | |
23 | |
24 /** | |
25 * AppController class | |
26 * | |
27 * @package cake | |
28 * @subpackage cake.tests.cases.libs.controller | |
29 */ | |
30 if (!class_exists('AppController')) { | |
31 /** | |
32 * AppController class | |
33 * | |
34 * @package cake | |
35 * @subpackage cake.tests.cases.libs.controller | |
36 */ | |
37 class AppController extends Controller { | |
38 /** | |
39 * helpers property | |
40 * | |
41 * @var array | |
42 * @access public | |
43 */ | |
44 var $helpers = array('Html', 'Javascript'); | |
45 /** | |
46 * uses property | |
47 * | |
48 * @var array | |
49 * @access public | |
50 */ | |
51 var $uses = array('ControllerPost'); | |
52 /** | |
53 * components property | |
54 * | |
55 * @var array | |
56 * @access public | |
57 */ | |
58 var $components = array('Cookie'); | |
59 } | |
60 } elseif (!defined('APP_CONTROLLER_EXISTS')) { | |
61 define('APP_CONTROLLER_EXISTS', true); | |
62 } | |
63 | |
64 /** | |
65 * ControllerPost class | |
66 * | |
67 * @package cake | |
68 * @subpackage cake.tests.cases.libs.controller | |
69 */ | |
70 class ControllerPost extends CakeTestModel { | |
71 | |
72 /** | |
73 * name property | |
74 * | |
75 * @var string 'ControllerPost' | |
76 * @access public | |
77 */ | |
78 var $name = 'ControllerPost'; | |
79 | |
80 /** | |
81 * useTable property | |
82 * | |
83 * @var string 'posts' | |
84 * @access public | |
85 */ | |
86 var $useTable = 'posts'; | |
87 | |
88 /** | |
89 * invalidFields property | |
90 * | |
91 * @var array | |
92 * @access public | |
93 */ | |
94 var $invalidFields = array('name' => 'error_msg'); | |
95 | |
96 /** | |
97 * lastQuery property | |
98 * | |
99 * @var mixed null | |
100 * @access public | |
101 */ | |
102 var $lastQuery = null; | |
103 | |
104 /** | |
105 * beforeFind method | |
106 * | |
107 * @param mixed $query | |
108 * @access public | |
109 * @return void | |
110 */ | |
111 function beforeFind($query) { | |
112 $this->lastQuery = $query; | |
113 } | |
114 | |
115 /** | |
116 * find method | |
117 * | |
118 * @param mixed $type | |
119 * @param array $options | |
120 * @access public | |
121 * @return void | |
122 */ | |
123 function find($type, $options = array()) { | |
124 if ($type == 'popular') { | |
125 $conditions = array($this->name . '.' . $this->primaryKey .' > ' => '1'); | |
126 $options = Set::merge($options, compact('conditions')); | |
127 return parent::find('all', $options); | |
128 } | |
129 return parent::find($type, $options); | |
130 } | |
131 } | |
132 | |
133 /** | |
134 * ControllerPostsController class | |
135 * | |
136 * @package cake | |
137 * @subpackage cake.tests.cases.libs.controller | |
138 */ | |
139 class ControllerCommentsController extends AppController { | |
140 | |
141 /** | |
142 * name property | |
143 * | |
144 * @var string 'ControllerPost' | |
145 * @access public | |
146 */ | |
147 var $name = 'ControllerComments'; | |
148 } | |
149 | |
150 /** | |
151 * ControllerComment class | |
152 * | |
153 * @package cake | |
154 * @subpackage cake.tests.cases.libs.controller | |
155 */ | |
156 class ControllerComment extends CakeTestModel { | |
157 | |
158 /** | |
159 * name property | |
160 * | |
161 * @var string 'ControllerComment' | |
162 * @access public | |
163 */ | |
164 var $name = 'Comment'; | |
165 | |
166 /** | |
167 * useTable property | |
168 * | |
169 * @var string 'comments' | |
170 * @access public | |
171 */ | |
172 var $useTable = 'comments'; | |
173 | |
174 /** | |
175 * data property | |
176 * | |
177 * @var array | |
178 * @access public | |
179 */ | |
180 var $data = array('name' => 'Some Name'); | |
181 | |
182 /** | |
183 * alias property | |
184 * | |
185 * @var string 'ControllerComment' | |
186 * @access public | |
187 */ | |
188 var $alias = 'ControllerComment'; | |
189 } | |
190 | |
191 /** | |
192 * ControllerAlias class | |
193 * | |
194 * @package cake | |
195 * @subpackage cake.tests.cases.libs.controller | |
196 */ | |
197 class ControllerAlias extends CakeTestModel { | |
198 | |
199 /** | |
200 * name property | |
201 * | |
202 * @var string 'ControllerAlias' | |
203 * @access public | |
204 */ | |
205 var $name = 'ControllerAlias'; | |
206 | |
207 /** | |
208 * alias property | |
209 * | |
210 * @var string 'ControllerSomeAlias' | |
211 * @access public | |
212 */ | |
213 var $alias = 'ControllerSomeAlias'; | |
214 | |
215 /** | |
216 * useTable property | |
217 * | |
218 * @var string 'posts' | |
219 * @access public | |
220 */ | |
221 var $useTable = 'posts'; | |
222 } | |
223 | |
224 /** | |
225 * ControllerPaginateModel class | |
226 * | |
227 * @package cake | |
228 * @subpackage cake.tests.cases.libs.controller | |
229 */ | |
230 class ControllerPaginateModel extends CakeTestModel { | |
231 | |
232 /** | |
233 * name property | |
234 * | |
235 * @var string | |
236 * @access public | |
237 */ | |
238 var $name = 'ControllerPaginateModel'; | |
239 | |
240 /** | |
241 * useTable property | |
242 * | |
243 * @var string' | |
244 * @access public | |
245 */ | |
246 var $useTable = 'comments'; | |
247 | |
248 /** | |
249 * paginate method | |
250 * | |
251 * @return void | |
252 * @access public | |
253 */ | |
254 function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra) { | |
255 $this->extra = $extra; | |
256 } | |
257 | |
258 /** | |
259 * paginateCount | |
260 * | |
261 * @access public | |
262 * @return void | |
263 */ | |
264 function paginateCount($conditions, $recursive, $extra) { | |
265 $this->extraCount = $extra; | |
266 } | |
267 } | |
268 | |
269 /** | |
270 * NameTest class | |
271 * | |
272 * @package cake | |
273 * @subpackage cake.tests.cases.libs.controller | |
274 */ | |
275 class NameTest extends CakeTestModel { | |
276 | |
277 /** | |
278 * name property | |
279 * @var string 'Name' | |
280 * @access public | |
281 */ | |
282 var $name = 'Name'; | |
283 | |
284 /** | |
285 * useTable property | |
286 * @var string 'names' | |
287 * @access public | |
288 */ | |
289 var $useTable = 'comments'; | |
290 | |
291 /** | |
292 * alias property | |
293 * | |
294 * @var string 'ControllerComment' | |
295 * @access public | |
296 */ | |
297 var $alias = 'Name'; | |
298 } | |
299 | |
300 /** | |
301 * TestController class | |
302 * | |
303 * @package cake | |
304 * @subpackage cake.tests.cases.libs.controller | |
305 */ | |
306 class TestController extends AppController { | |
307 | |
308 /** | |
309 * name property | |
310 * @var string 'Name' | |
311 * @access public | |
312 */ | |
313 var $name = 'TestController'; | |
314 | |
315 /** | |
316 * helpers property | |
317 * | |
318 * @var array | |
319 * @access public | |
320 */ | |
321 var $helpers = array('Session', 'Xml'); | |
322 | |
323 /** | |
324 * components property | |
325 * | |
326 * @var array | |
327 * @access public | |
328 */ | |
329 var $components = array('Security'); | |
330 | |
331 /** | |
332 * uses property | |
333 * | |
334 * @var array | |
335 * @access public | |
336 */ | |
337 var $uses = array('ControllerComment', 'ControllerAlias'); | |
338 | |
339 /** | |
340 * index method | |
341 * | |
342 * @param mixed $testId | |
343 * @param mixed $test2Id | |
344 * @access public | |
345 * @return void | |
346 */ | |
347 function index($testId, $test2Id) { | |
348 $this->data['testId'] = $testId; | |
349 $this->data['test2Id'] = $test2Id; | |
350 } | |
351 } | |
352 | |
353 /** | |
354 * TestComponent class | |
355 * | |
356 * @package cake | |
357 * @subpackage cake.tests.cases.libs.controller | |
358 */ | |
359 class TestComponent extends Object { | |
360 | |
361 /** | |
362 * beforeRedirect method | |
363 * | |
364 * @access public | |
365 * @return void | |
366 */ | |
367 function beforeRedirect() { | |
368 } | |
369 /** | |
370 * initialize method | |
371 * | |
372 * @access public | |
373 * @return void | |
374 */ | |
375 function initialize(&$controller) { | |
376 } | |
377 | |
378 /** | |
379 * startup method | |
380 * | |
381 * @access public | |
382 * @return void | |
383 */ | |
384 function startup(&$controller) { | |
385 } | |
386 /** | |
387 * shutdown method | |
388 * | |
389 * @access public | |
390 * @return void | |
391 */ | |
392 function shutdown(&$controller) { | |
393 } | |
394 /** | |
395 * beforeRender callback | |
396 * | |
397 * @return void | |
398 */ | |
399 function beforeRender(&$controller) { | |
400 if ($this->viewclass) { | |
401 $controller->view = $this->viewclass; | |
402 } | |
403 } | |
404 } | |
405 | |
406 /** | |
407 * AnotherTestController class | |
408 * | |
409 * @package cake | |
410 * @subpackage cake.tests.cases.libs.controller | |
411 */ | |
412 class AnotherTestController extends AppController { | |
413 | |
414 /** | |
415 * name property | |
416 * @var string 'Name' | |
417 * @access public | |
418 */ | |
419 var $name = 'AnotherTest'; | |
420 /** | |
421 * uses property | |
422 * | |
423 * @var array | |
424 * @access public | |
425 */ | |
426 var $uses = null; | |
427 } | |
428 | |
429 /** | |
430 * ControllerTest class | |
431 * | |
432 * @package cake | |
433 * @subpackage cake.tests.cases.libs.controller | |
434 */ | |
435 class ControllerTest extends CakeTestCase { | |
436 | |
437 /** | |
438 * fixtures property | |
439 * | |
440 * @var array | |
441 * @access public | |
442 */ | |
443 var $fixtures = array('core.post', 'core.comment', 'core.name'); | |
444 | |
445 /** | |
446 * endTest | |
447 * | |
448 * @access public | |
449 * @return void | |
450 */ | |
451 function endTest() { | |
452 App::build(); | |
453 } | |
454 | |
455 /** | |
456 * testLoadModel method | |
457 * | |
458 * @access public | |
459 * @return void | |
460 */ | |
461 function testLoadModel() { | |
462 $Controller =& new Controller(); | |
463 | |
464 $this->assertFalse(isset($Controller->ControllerPost)); | |
465 | |
466 $result = $Controller->loadModel('ControllerPost'); | |
467 $this->assertTrue($result); | |
468 $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost')); | |
469 $this->assertTrue(in_array('ControllerPost', $Controller->modelNames)); | |
470 | |
471 ClassRegistry::flush(); | |
472 unset($Controller); | |
473 } | |
474 | |
475 /** | |
476 * testConstructClasses method | |
477 * | |
478 * @access public | |
479 * @return void | |
480 */ | |
481 function testConstructClasses() { | |
482 $Controller =& new Controller(); | |
483 $Controller->modelClass = 'ControllerPost'; | |
484 $Controller->passedArgs[] = '1'; | |
485 $Controller->constructClasses(); | |
486 $this->assertEqual($Controller->ControllerPost->id, 1); | |
487 | |
488 unset($Controller); | |
489 | |
490 $Controller =& new Controller(); | |
491 $Controller->uses = array('ControllerPost', 'ControllerComment'); | |
492 $Controller->passedArgs[] = '1'; | |
493 $Controller->constructClasses(); | |
494 $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost')); | |
495 $this->assertTrue(is_a($Controller->ControllerComment, 'ControllerComment')); | |
496 | |
497 $this->assertEqual($Controller->ControllerComment->name, 'Comment'); | |
498 | |
499 unset($Controller); | |
500 | |
501 App::build(array('plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS))); | |
502 | |
503 $Controller =& new Controller(); | |
504 $Controller->uses = array('TestPlugin.TestPluginPost'); | |
505 $Controller->constructClasses(); | |
506 | |
507 $this->assertEqual($Controller->modelClass, 'TestPluginPost'); | |
508 $this->assertTrue(isset($Controller->TestPluginPost)); | |
509 $this->assertTrue(is_a($Controller->TestPluginPost, 'TestPluginPost')); | |
510 | |
511 unset($Controller); | |
512 } | |
513 | |
514 /** | |
515 * testAliasName method | |
516 * | |
517 * @access public | |
518 * @return void | |
519 */ | |
520 function testAliasName() { | |
521 $Controller =& new Controller(); | |
522 $Controller->uses = array('NameTest'); | |
523 $Controller->constructClasses(); | |
524 | |
525 $this->assertEqual($Controller->NameTest->name, 'Name'); | |
526 $this->assertEqual($Controller->NameTest->alias, 'Name'); | |
527 | |
528 unset($Controller); | |
529 } | |
530 | |
531 /** | |
532 * testPersistent method | |
533 * | |
534 * @access public | |
535 * @return void | |
536 */ | |
537 function testPersistent() { | |
538 Configure::write('Cache.disable', false); | |
539 $Controller =& new Controller(); | |
540 $Controller->modelClass = 'ControllerPost'; | |
541 $Controller->persistModel = true; | |
542 $Controller->constructClasses(); | |
543 $this->assertTrue(file_exists(CACHE . 'persistent' . DS .'controllerpost.php')); | |
544 $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost')); | |
545 @unlink(CACHE . 'persistent' . DS . 'controllerpost.php'); | |
546 @unlink(CACHE . 'persistent' . DS . 'controllerpostregistry.php'); | |
547 | |
548 unset($Controller); | |
549 Configure::write('Cache.disable', true); | |
550 } | |
551 | |
552 /** | |
553 * testPaginate method | |
554 * | |
555 * @access public | |
556 * @return void | |
557 */ | |
558 function testPaginate() { | |
559 $Controller =& new Controller(); | |
560 $Controller->uses = array('ControllerPost', 'ControllerComment'); | |
561 $Controller->passedArgs[] = '1'; | |
562 $Controller->params['url'] = array(); | |
563 $Controller->constructClasses(); | |
564 | |
565 $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); | |
566 $this->assertEqual($results, array(1, 2, 3)); | |
567 | |
568 $results = Set::extract($Controller->paginate('ControllerComment'), '{n}.ControllerComment.id'); | |
569 $this->assertEqual($results, array(1, 2, 3, 4, 5, 6)); | |
570 | |
571 $Controller->modelClass = null; | |
572 | |
573 $Controller->uses[0] = 'Plugin.ControllerPost'; | |
574 $results = Set::extract($Controller->paginate(), '{n}.ControllerPost.id'); | |
575 $this->assertEqual($results, array(1, 2, 3)); | |
576 | |
577 $Controller->passedArgs = array('page' => '-1'); | |
578 $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); | |
579 $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1); | |
580 $this->assertEqual($results, array(1, 2, 3)); | |
581 | |
582 $Controller->passedArgs = array('sort' => 'ControllerPost.id', 'direction' => 'asc'); | |
583 $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); | |
584 $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1); | |
585 $this->assertEqual($results, array(1, 2, 3)); | |
586 | |
587 $Controller->passedArgs = array('sort' => 'ControllerPost.id', 'direction' => 'desc'); | |
588 $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); | |
589 $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1); | |
590 $this->assertEqual($results, array(3, 2, 1)); | |
591 | |
592 $Controller->passedArgs = array('sort' => 'id', 'direction' => 'desc'); | |
593 $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); | |
594 $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1); | |
595 $this->assertEqual($results, array(3, 2, 1)); | |
596 | |
597 $Controller->passedArgs = array('sort' => 'NotExisting.field', 'direction' => 'desc'); | |
598 $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); | |
599 $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1, 'Invalid field in query %s'); | |
600 $this->assertEqual($results, array(1, 2, 3)); | |
601 | |
602 $Controller->passedArgs = array('sort' => 'ControllerPost.author_id', 'direction' => 'allYourBase'); | |
603 $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); | |
604 $this->assertEqual($Controller->ControllerPost->lastQuery['order'][0], array('ControllerPost.author_id' => 'asc')); | |
605 $this->assertEqual($results, array(1, 3, 2)); | |
606 | |
607 $Controller->passedArgs = array('page' => '1 " onclick="alert(\'xss\');">'); | |
608 $Controller->paginate = array('limit' => 1); | |
609 $Controller->paginate('ControllerPost'); | |
610 $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1, 'XSS exploit opened %s'); | |
611 $this->assertIdentical($Controller->params['paging']['ControllerPost']['options']['page'], 1, 'XSS exploit opened %s'); | |
612 | |
613 $Controller->passedArgs = array(); | |
614 $Controller->paginate = array('limit' => 0); | |
615 $Controller->paginate('ControllerPost'); | |
616 $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1); | |
617 $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3); | |
618 $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false); | |
619 $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true); | |
620 | |
621 $Controller->passedArgs = array(); | |
622 $Controller->paginate = array('limit' => 'garbage!'); | |
623 $Controller->paginate('ControllerPost'); | |
624 $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1); | |
625 $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3); | |
626 $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false); | |
627 $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true); | |
628 | |
629 $Controller->passedArgs = array(); | |
630 $Controller->paginate = array('limit' => '-1'); | |
631 $Controller->paginate('ControllerPost'); | |
632 $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1); | |
633 $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3); | |
634 $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false); | |
635 $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true); | |
636 } | |
637 | |
638 /** | |
639 * testPaginateExtraParams method | |
640 * | |
641 * @access public | |
642 * @return void | |
643 */ | |
644 function testPaginateExtraParams() { | |
645 $Controller =& new Controller(); | |
646 $Controller->uses = array('ControllerPost', 'ControllerComment'); | |
647 $Controller->passedArgs[] = '1'; | |
648 $Controller->params['url'] = array(); | |
649 $Controller->constructClasses(); | |
650 | |
651 $Controller->passedArgs = array('page' => '-1', 'contain' => array('ControllerComment')); | |
652 $result = $Controller->paginate('ControllerPost'); | |
653 $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1); | |
654 $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(1, 2, 3)); | |
655 $this->assertTrue(!isset($Controller->ControllerPost->lastQuery['contain'])); | |
656 | |
657 $Controller->passedArgs = array('page' => '-1'); | |
658 $Controller->paginate = array('ControllerPost' => array('contain' => array('ControllerComment'))); | |
659 $result = $Controller->paginate('ControllerPost'); | |
660 $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1); | |
661 $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(1, 2, 3)); | |
662 $this->assertTrue(isset($Controller->ControllerPost->lastQuery['contain'])); | |
663 | |
664 $Controller->paginate = array('ControllerPost' => array('popular', 'fields' => array('id', 'title'))); | |
665 $result = $Controller->paginate('ControllerPost'); | |
666 $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(2, 3)); | |
667 $this->assertEqual($Controller->ControllerPost->lastQuery['conditions'], array('ControllerPost.id > ' => '1')); | |
668 | |
669 $Controller->passedArgs = array('limit' => 12); | |
670 $Controller->paginate = array('limit' => 30); | |
671 $result = $Controller->paginate('ControllerPost'); | |
672 $paging = $Controller->params['paging']['ControllerPost']; | |
673 | |
674 $this->assertEqual($Controller->ControllerPost->lastQuery['limit'], 12); | |
675 $this->assertEqual($paging['options']['limit'], 12); | |
676 | |
677 $Controller =& new Controller(); | |
678 $Controller->uses = array('ControllerPaginateModel'); | |
679 $Controller->params['url'] = array(); | |
680 $Controller->constructClasses(); | |
681 $Controller->paginate = array( | |
682 'ControllerPaginateModel' => array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id') | |
683 ); | |
684 $result = $Controller->paginate('ControllerPaginateModel'); | |
685 $expected = array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id'); | |
686 $this->assertEqual($Controller->ControllerPaginateModel->extra, $expected); | |
687 $this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected); | |
688 | |
689 $Controller->paginate = array( | |
690 'ControllerPaginateModel' => array('foo', 'contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id') | |
691 ); | |
692 $Controller->paginate('ControllerPaginateModel'); | |
693 $expected = array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id', 'type' => 'foo'); | |
694 $this->assertEqual($Controller->ControllerPaginateModel->extra, $expected); | |
695 $this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected); | |
696 } | |
697 | |
698 /** | |
699 * testPaginateFieldsDouble method | |
700 * | |
701 * @return void | |
702 * @access public | |
703 */ | |
704 function testPaginateFieldsDouble(){ | |
705 $Controller =& new Controller(); | |
706 $Controller->uses = array('ControllerPost'); | |
707 $Controller->params['url'] = array(); | |
708 $Controller->constructClasses(); | |
709 | |
710 $Controller->paginate = array( | |
711 'fields' => array( | |
712 'ControllerPost.id', | |
713 'radians(180.0) as floatvalue' | |
714 ), | |
715 'order' => array('ControllerPost.created'=>'DESC'), | |
716 'limit' => 1, | |
717 'page' => 1, | |
718 'recursive' => -1 | |
719 ); | |
720 $conditions = array(); | |
721 $result = $Controller->paginate('ControllerPost',$conditions); | |
722 $expected = array( | |
723 array( | |
724 'ControllerPost' => array( | |
725 'id' => 3, | |
726 ), | |
727 0 => array( | |
728 'floatvalue' => '3.14159265358979', | |
729 ), | |
730 ), | |
731 ); | |
732 $this->assertEqual($result, $expected); | |
733 } | |
734 | |
735 | |
736 /** | |
737 * testPaginatePassedArgs method | |
738 * | |
739 * @return void | |
740 * @access public | |
741 */ | |
742 function testPaginatePassedArgs() { | |
743 $Controller =& new Controller(); | |
744 $Controller->uses = array('ControllerPost'); | |
745 $Controller->passedArgs[] = array('1', '2', '3'); | |
746 $Controller->params['url'] = array(); | |
747 $Controller->constructClasses(); | |
748 | |
749 $Controller->paginate = array( | |
750 'fields' => array(), | |
751 'order' => '', | |
752 'limit' => 5, | |
753 'page' => 1, | |
754 'recursive' => -1 | |
755 ); | |
756 $conditions = array(); | |
757 $Controller->paginate('ControllerPost',$conditions); | |
758 | |
759 $expected = array( | |
760 'fields' => array(), | |
761 'order' => '', | |
762 'limit' => 5, | |
763 'page' => 1, | |
764 'recursive' => -1, | |
765 'conditions' => array() | |
766 ); | |
767 $this->assertEqual($Controller->params['paging']['ControllerPost']['options'],$expected); | |
768 } | |
769 | |
770 /** | |
771 * Test that special paginate types are called and that the type param doesn't leak out into defaults or options. | |
772 * | |
773 * @return void | |
774 */ | |
775 function testPaginateSpecialType() { | |
776 $Controller =& new Controller(); | |
777 $Controller->uses = array('ControllerPost', 'ControllerComment'); | |
778 $Controller->passedArgs[] = '1'; | |
779 $Controller->params['url'] = array(); | |
780 $Controller->constructClasses(); | |
781 | |
782 $Controller->paginate = array('ControllerPost' => array('popular', 'fields' => array('id', 'title'))); | |
783 $result = $Controller->paginate('ControllerPost'); | |
784 | |
785 $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(2, 3)); | |
786 $this->assertEqual($Controller->ControllerPost->lastQuery['conditions'], array('ControllerPost.id > ' => '1')); | |
787 $this->assertFalse(isset($Controller->params['paging']['ControllerPost']['defaults'][0])); | |
788 $this->assertFalse(isset($Controller->params['paging']['ControllerPost']['options'][0])); | |
789 } | |
790 | |
791 /** | |
792 * testDefaultPaginateParams method | |
793 * | |
794 * @access public | |
795 * @return void | |
796 */ | |
797 function testDefaultPaginateParams() { | |
798 $Controller =& new Controller(); | |
799 $Controller->modelClass = 'ControllerPost'; | |
800 $Controller->params['url'] = array(); | |
801 $Controller->paginate = array('order' => 'ControllerPost.id DESC'); | |
802 $Controller->constructClasses(); | |
803 $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id'); | |
804 $this->assertEqual($Controller->params['paging']['ControllerPost']['defaults']['order'], 'ControllerPost.id DESC'); | |
805 $this->assertEqual($Controller->params['paging']['ControllerPost']['options']['order'], 'ControllerPost.id DESC'); | |
806 $this->assertEqual($results, array(3, 2, 1)); | |
807 } | |
808 | |
809 /** | |
810 * test paginate() and virtualField interactions | |
811 * | |
812 * @return void | |
813 */ | |
814 function testPaginateOrderVirtualField() { | |
815 $Controller =& new Controller(); | |
816 $Controller->uses = array('ControllerPost', 'ControllerComment'); | |
817 $Controller->params['url'] = array(); | |
818 $Controller->constructClasses(); | |
819 $Controller->ControllerPost->virtualFields = array( | |
820 'offset_test' => 'ControllerPost.id + 1' | |
821 ); | |
822 | |
823 $Controller->paginate = array( | |
824 'fields' => array('id', 'title', 'offset_test'), | |
825 'order' => array('offset_test' => 'DESC') | |
826 ); | |
827 $result = $Controller->paginate('ControllerPost'); | |
828 $this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(4, 3, 2)); | |
829 | |
830 $Controller->passedArgs = array('sort' => 'offset_test', 'direction' => 'asc'); | |
831 $result = $Controller->paginate('ControllerPost'); | |
832 $this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(2, 3, 4)); | |
833 } | |
834 | |
835 /** | |
836 * testFlash method | |
837 * | |
838 * @access public | |
839 * @return void | |
840 */ | |
841 function testFlash() { | |
842 $Controller =& new Controller(); | |
843 $Controller->flash('this should work', '/flash'); | |
844 $result = $Controller->output; | |
845 | |
846 $expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
847 <html xmlns="http://www.w3.org/1999/xhtml"> | |
848 <head> | |
849 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
850 <title>this should work</title> | |
851 <style><!-- | |
852 P { text-align:center; font:bold 1.1em sans-serif } | |
853 A { color:#444; text-decoration:none } | |
854 A:HOVER { text-decoration: underline; color:#44E } | |
855 --></style> | |
856 </head> | |
857 <body> | |
858 <p><a href="/flash">this should work</a></p> | |
859 </body> | |
860 </html>'; | |
861 $result = str_replace(array("\t", "\r\n", "\n"), "", $result); | |
862 $expected = str_replace(array("\t", "\r\n", "\n"), "", $expected); | |
863 $this->assertEqual($result, $expected); | |
864 | |
865 App::build(array('views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS))); | |
866 $Controller =& new Controller(); | |
867 $Controller->flash('this should work', '/flash', 1, 'ajax2'); | |
868 $result = $Controller->output; | |
869 $this->assertPattern('/Ajax!/', $result); | |
870 App::build(); | |
871 } | |
872 | |
873 /** | |
874 * testControllerSet method | |
875 * | |
876 * @access public | |
877 * @return void | |
878 */ | |
879 function testControllerSet() { | |
880 $Controller =& new Controller(); | |
881 $Controller->set('variable_with_underscores', null); | |
882 $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars)); | |
883 | |
884 $Controller->viewVars = array(); | |
885 $viewVars = array('ModelName' => array('id' => 1, 'name' => 'value')); | |
886 $Controller->set($viewVars); | |
887 $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars)); | |
888 | |
889 $Controller->viewVars = array(); | |
890 $Controller->set('variable_with_underscores', 'value'); | |
891 $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars)); | |
892 | |
893 $Controller->viewVars = array(); | |
894 $viewVars = array('ModelName' => 'name'); | |
895 $Controller->set($viewVars); | |
896 $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars)); | |
897 | |
898 $Controller->set('title', 'someTitle'); | |
899 $this->assertIdentical($Controller->viewVars['title'], 'someTitle'); | |
900 $this->assertTrue(empty($Controller->pageTitle)); | |
901 | |
902 $Controller->viewVars = array(); | |
903 $expected = array('ModelName' => 'name', 'ModelName2' => 'name2'); | |
904 $Controller->set(array('ModelName', 'ModelName2'), array('name', 'name2')); | |
905 $this->assertIdentical($Controller->viewVars, $expected); | |
906 | |
907 $Controller->viewVars = array(); | |
908 $Controller->set(array(3 => 'three', 4 => 'four')); | |
909 $Controller->set(array(1 => 'one', 2 => 'two')); | |
910 $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two'); | |
911 $this->assertEqual($Controller->viewVars, $expected); | |
912 | |
913 } | |
914 | |
915 /** | |
916 * testRender method | |
917 * | |
918 * @access public | |
919 * @return void | |
920 */ | |
921 function testRender() { | |
922 App::build(array( | |
923 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS) | |
924 ), true); | |
925 | |
926 $Controller =& new Controller(); | |
927 $Controller->viewPath = 'posts'; | |
928 | |
929 $result = $Controller->render('index'); | |
930 $this->assertPattern('/posts index/', $result); | |
931 | |
932 $result = $Controller->render('/elements/test_element'); | |
933 $this->assertPattern('/this is the test element/', $result); | |
934 | |
935 $Controller = new TestController(); | |
936 $Controller->constructClasses(); | |
937 $Controller->ControllerComment->validationErrors = array('title' => 'tooShort'); | |
938 $expected = $Controller->ControllerComment->validationErrors; | |
939 | |
940 ClassRegistry::flush(); | |
941 $Controller->viewPath = 'posts'; | |
942 $result = $Controller->render('index'); | |
943 $View = ClassRegistry::getObject('view'); | |
944 $this->assertTrue(isset($View->validationErrors['ControllerComment'])); | |
945 $this->assertEqual($expected, $View->validationErrors['ControllerComment']); | |
946 | |
947 $Controller->ControllerComment->validationErrors = array(); | |
948 ClassRegistry::flush(); | |
949 App::build(); | |
950 } | |
951 | |
952 /** | |
953 * test that a component beforeRender can change the controller view class. | |
954 * | |
955 * @return void | |
956 */ | |
957 function testComponentBeforeRenderChangingViewClass() { | |
958 $core = App::core('views'); | |
959 App::build(array( | |
960 'views' => array( | |
961 TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS, | |
962 $core[0] | |
963 ) | |
964 ), true); | |
965 $Controller =& new Controller(); | |
966 $Controller->uses = array(); | |
967 $Controller->components = array('Test'); | |
968 $Controller->constructClasses(); | |
969 $Controller->Test->viewclass = 'Theme'; | |
970 $Controller->viewPath = 'posts'; | |
971 $Controller->theme = 'test_theme'; | |
972 $result = $Controller->render('index'); | |
973 $this->assertPattern('/default test_theme layout/', $result); | |
974 App::build(); | |
975 } | |
976 | |
977 /** | |
978 * testToBeInheritedGuardmethods method | |
979 * | |
980 * @access public | |
981 * @return void | |
982 */ | |
983 function testToBeInheritedGuardmethods() { | |
984 $Controller =& new Controller(); | |
985 $this->assertTrue($Controller->_beforeScaffold('')); | |
986 $this->assertTrue($Controller->_afterScaffoldSave('')); | |
987 $this->assertTrue($Controller->_afterScaffoldSaveError('')); | |
988 $this->assertFalse($Controller->_scaffoldError('')); | |
989 } | |
990 | |
991 /** | |
992 * testRedirect method | |
993 * | |
994 * @access public | |
995 * @return void | |
996 */ | |
997 function testRedirect() { | |
998 $codes = array( | |
999 100 => "Continue", | |
1000 101 => "Switching Protocols", | |
1001 200 => "OK", | |
1002 201 => "Created", | |
1003 202 => "Accepted", | |
1004 203 => "Non-Authoritative Information", | |
1005 204 => "No Content", | |
1006 205 => "Reset Content", | |
1007 206 => "Partial Content", | |
1008 300 => "Multiple Choices", | |
1009 301 => "Moved Permanently", | |
1010 302 => "Found", | |
1011 303 => "See Other", | |
1012 304 => "Not Modified", | |
1013 305 => "Use Proxy", | |
1014 307 => "Temporary Redirect", | |
1015 400 => "Bad Request", | |
1016 401 => "Unauthorized", | |
1017 402 => "Payment Required", | |
1018 403 => "Forbidden", | |
1019 404 => "Not Found", | |
1020 405 => "Method Not Allowed", | |
1021 406 => "Not Acceptable", | |
1022 407 => "Proxy Authentication Required", | |
1023 408 => "Request Time-out", | |
1024 409 => "Conflict", | |
1025 410 => "Gone", | |
1026 411 => "Length Required", | |
1027 412 => "Precondition Failed", | |
1028 413 => "Request Entity Too Large", | |
1029 414 => "Request-URI Too Large", | |
1030 415 => "Unsupported Media Type", | |
1031 416 => "Requested range not satisfiable", | |
1032 417 => "Expectation Failed", | |
1033 500 => "Internal Server Error", | |
1034 501 => "Not Implemented", | |
1035 502 => "Bad Gateway", | |
1036 503 => "Service Unavailable", | |
1037 504 => "Gateway Time-out" | |
1038 ); | |
1039 | |
1040 Mock::generatePartial('Controller', 'MockController', array('header')); | |
1041 Mock::generate('TestComponent', 'MockTestComponent'); | |
1042 Mock::generate('TestComponent', 'MockTestBComponent'); | |
1043 | |
1044 App::import('Helper', 'Cache'); | |
1045 | |
1046 foreach ($codes as $code => $msg) { | |
1047 $MockController =& new MockController(); | |
1048 $MockController->Component =& new Component(); | |
1049 $MockController->Component->init($MockController); | |
1050 $MockController->expectAt(0, 'header', array("HTTP/1.1 {$code} {$msg}")); | |
1051 $MockController->expectAt(1, 'header', array('Location: http://cakephp.org')); | |
1052 $MockController->expectCallCount('header', 2); | |
1053 $MockController->redirect('http://cakephp.org', (int)$code, false); | |
1054 $this->assertFalse($MockController->autoRender); | |
1055 } | |
1056 foreach ($codes as $code => $msg) { | |
1057 $MockController =& new MockController(); | |
1058 $MockController->Component =& new Component(); | |
1059 $MockController->Component->init($MockController); | |
1060 $MockController->expectAt(0, 'header', array("HTTP/1.1 {$code} {$msg}")); | |
1061 $MockController->expectAt(1, 'header', array('Location: http://cakephp.org')); | |
1062 $MockController->expectCallCount('header', 2); | |
1063 $MockController->redirect('http://cakephp.org', $msg, false); | |
1064 $this->assertFalse($MockController->autoRender); | |
1065 } | |
1066 | |
1067 $MockController =& new MockController(); | |
1068 $MockController->Component =& new Component(); | |
1069 $MockController->Component->init($MockController); | |
1070 $MockController->expectAt(0, 'header', array('Location: http://www.example.org/users/login')); | |
1071 $MockController->expectCallCount('header', 1); | |
1072 $MockController->redirect('http://www.example.org/users/login', null, false); | |
1073 | |
1074 $MockController =& new MockController(); | |
1075 $MockController->Component =& new Component(); | |
1076 $MockController->Component->init($MockController); | |
1077 $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently')); | |
1078 $MockController->expectAt(1, 'header', array('Location: http://www.example.org/users/login')); | |
1079 $MockController->expectCallCount('header', 2); | |
1080 $MockController->redirect('http://www.example.org/users/login', 301, false); | |
1081 | |
1082 $MockController =& new MockController(); | |
1083 $MockController->components = array('MockTest'); | |
1084 $MockController->Component =& new Component(); | |
1085 $MockController->Component->init($MockController); | |
1086 $MockController->MockTest->setReturnValue('beforeRedirect', null); | |
1087 $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently')); | |
1088 $MockController->expectAt(1, 'header', array('Location: http://cakephp.org')); | |
1089 $MockController->expectCallCount('header', 2); | |
1090 $MockController->redirect('http://cakephp.org', 301, false); | |
1091 | |
1092 $MockController =& new MockController(); | |
1093 $MockController->components = array('MockTest'); | |
1094 $MockController->Component =& new Component(); | |
1095 $MockController->Component->init($MockController); | |
1096 $MockController->MockTest->setReturnValue('beforeRedirect', 'http://book.cakephp.org'); | |
1097 $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently')); | |
1098 $MockController->expectAt(1, 'header', array('Location: http://book.cakephp.org')); | |
1099 $MockController->expectCallCount('header', 2); | |
1100 $MockController->redirect('http://cakephp.org', 301, false); | |
1101 | |
1102 $MockController =& new MockController(); | |
1103 $MockController->components = array('MockTest'); | |
1104 $MockController->Component =& new Component(); | |
1105 $MockController->Component->init($MockController); | |
1106 $MockController->MockTest->setReturnValue('beforeRedirect', false); | |
1107 $MockController->expectNever('header'); | |
1108 $MockController->redirect('http://cakephp.org', 301, false); | |
1109 | |
1110 $MockController =& new MockController(); | |
1111 $MockController->components = array('MockTest', 'MockTestB'); | |
1112 $MockController->Component =& new Component(); | |
1113 $MockController->Component->init($MockController); | |
1114 $MockController->MockTest->setReturnValue('beforeRedirect', 'http://book.cakephp.org'); | |
1115 $MockController->MockTestB->setReturnValue('beforeRedirect', 'http://bakery.cakephp.org'); | |
1116 $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently')); | |
1117 $MockController->expectAt(1, 'header', array('Location: http://bakery.cakephp.org')); | |
1118 $MockController->expectCallCount('header', 2); | |
1119 $MockController->redirect('http://cakephp.org', 301, false); | |
1120 } | |
1121 | |
1122 /** | |
1123 * testMergeVars method | |
1124 * | |
1125 * @access public | |
1126 * @return void | |
1127 */ | |
1128 function testMergeVars() { | |
1129 if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) { | |
1130 return; | |
1131 } | |
1132 | |
1133 $TestController =& new TestController(); | |
1134 $TestController->constructClasses(); | |
1135 | |
1136 $testVars = get_class_vars('TestController'); | |
1137 $appVars = get_class_vars('AppController'); | |
1138 | |
1139 $components = is_array($appVars['components']) | |
1140 ? array_merge($appVars['components'], $testVars['components']) | |
1141 : $testVars['components']; | |
1142 if (!in_array('Session', $components)) { | |
1143 $components[] = 'Session'; | |
1144 } | |
1145 $helpers = is_array($appVars['helpers']) | |
1146 ? array_merge($appVars['helpers'], $testVars['helpers']) | |
1147 : $testVars['helpers']; | |
1148 $uses = is_array($appVars['uses']) | |
1149 ? array_merge($appVars['uses'], $testVars['uses']) | |
1150 : $testVars['uses']; | |
1151 | |
1152 $this->assertEqual(count(array_diff_assoc(Set::normalize($TestController->helpers), Set::normalize($helpers))), 0); | |
1153 $this->assertEqual(count(array_diff($TestController->uses, $uses)), 0); | |
1154 $this->assertEqual(count(array_diff_assoc(Set::normalize($TestController->components), Set::normalize($components))), 0); | |
1155 | |
1156 $expected = array('ControllerComment', 'ControllerAlias', 'ControllerPost'); | |
1157 $this->assertEqual($expected, $TestController->uses, '$uses was merged incorrectly, AppController models should be last.'); | |
1158 | |
1159 | |
1160 $TestController =& new AnotherTestController(); | |
1161 $TestController->constructClasses(); | |
1162 | |
1163 $appVars = get_class_vars('AppController'); | |
1164 $testVars = get_class_vars('AnotherTestController'); | |
1165 | |
1166 | |
1167 $this->assertTrue(in_array('ControllerPost', $appVars['uses'])); | |
1168 $this->assertNull($testVars['uses']); | |
1169 | |
1170 $this->assertFalse(isset($TestController->ControllerPost)); | |
1171 | |
1172 | |
1173 $TestController =& new ControllerCommentsController(); | |
1174 $TestController->constructClasses(); | |
1175 | |
1176 $appVars = get_class_vars('AppController'); | |
1177 $testVars = get_class_vars('ControllerCommentsController'); | |
1178 | |
1179 | |
1180 $this->assertTrue(in_array('ControllerPost', $appVars['uses'])); | |
1181 $this->assertEqual(array('ControllerPost'), $testVars['uses']); | |
1182 | |
1183 $this->assertTrue(isset($TestController->ControllerPost)); | |
1184 $this->assertTrue(isset($TestController->ControllerComment)); | |
1185 } | |
1186 | |
1187 /** | |
1188 * test that options from child classes replace those in the parent classes. | |
1189 * | |
1190 * @access public | |
1191 * @return void | |
1192 */ | |
1193 function testChildComponentOptionsSupercedeParents() { | |
1194 if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) { | |
1195 return; | |
1196 } | |
1197 $TestController =& new TestController(); | |
1198 $expected = array('foo'); | |
1199 $TestController->components = array('Cookie' => $expected); | |
1200 $TestController->constructClasses(); | |
1201 $this->assertEqual($TestController->components['Cookie'], $expected); | |
1202 } | |
1203 | |
1204 /** | |
1205 * Ensure that __mergeVars is not being greedy and merging with | |
1206 * AppController when you make an instance of Controller | |
1207 * | |
1208 * @return void | |
1209 */ | |
1210 function testMergeVarsNotGreedy() { | |
1211 $Controller =& new Controller(); | |
1212 $Controller->components = array(); | |
1213 $Controller->uses = array(); | |
1214 $Controller->constructClasses(); | |
1215 | |
1216 $this->assertFalse(isset($Controller->Session)); | |
1217 } | |
1218 | |
1219 /** | |
1220 * testReferer method | |
1221 * | |
1222 * @access public | |
1223 * @return void | |
1224 */ | |
1225 function testReferer() { | |
1226 $Controller =& new Controller(); | |
1227 $_SERVER['HTTP_REFERER'] = 'http://cakephp.org'; | |
1228 $result = $Controller->referer(null, false); | |
1229 $expected = 'http://cakephp.org'; | |
1230 $this->assertIdentical($result, $expected); | |
1231 | |
1232 $_SERVER['HTTP_REFERER'] = ''; | |
1233 $result = $Controller->referer('http://cakephp.org', false); | |
1234 $expected = 'http://cakephp.org'; | |
1235 $this->assertIdentical($result, $expected); | |
1236 | |
1237 $_SERVER['HTTP_REFERER'] = ''; | |
1238 $referer = array( | |
1239 'controller' => 'pages', | |
1240 'action' => 'display', | |
1241 'home' | |
1242 ); | |
1243 $result = $Controller->referer($referer, false); | |
1244 $expected = 'http://' . env('HTTP_HOST') . '/pages/display/home'; | |
1245 $this->assertIdentical($result, $expected); | |
1246 | |
1247 $_SERVER['HTTP_REFERER'] = ''; | |
1248 $result = $Controller->referer(null, false); | |
1249 $expected = '/'; | |
1250 $this->assertIdentical($result, $expected); | |
1251 | |
1252 $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'/some/path'; | |
1253 $result = $Controller->referer(null, false); | |
1254 $expected = '/some/path'; | |
1255 $this->assertIdentical($result, $expected); | |
1256 | |
1257 $Controller->webroot .= '/'; | |
1258 $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'/some/path'; | |
1259 $result = $Controller->referer(null, false); | |
1260 $expected = '/some/path'; | |
1261 $this->assertIdentical($result, $expected); | |
1262 | |
1263 $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'some/path'; | |
1264 $result = $Controller->referer(null, false); | |
1265 $expected = '/some/path'; | |
1266 $this->assertIdentical($result, $expected); | |
1267 | |
1268 $Controller->webroot = '/recipe/'; | |
1269 | |
1270 $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'recipes/add'; | |
1271 $result = $Controller->referer(); | |
1272 $expected = '/recipes/add'; | |
1273 $this->assertIdentical($result, $expected); | |
1274 } | |
1275 | |
1276 /** | |
1277 * testSetAction method | |
1278 * | |
1279 * @access public | |
1280 * @return void | |
1281 */ | |
1282 function testSetAction() { | |
1283 $TestController =& new TestController(); | |
1284 $TestController->setAction('index', 1, 2); | |
1285 $expected = array('testId' => 1, 'test2Id' => 2); | |
1286 $this->assertidentical($TestController->data, $expected); | |
1287 } | |
1288 | |
1289 /** | |
1290 * testUnimplementedIsAuthorized method | |
1291 * | |
1292 * @access public | |
1293 * @return void | |
1294 */ | |
1295 function testUnimplementedIsAuthorized() { | |
1296 $TestController =& new TestController(); | |
1297 $TestController->isAuthorized(); | |
1298 $this->assertError(); | |
1299 } | |
1300 | |
1301 /** | |
1302 * testValidateErrors method | |
1303 * | |
1304 * @access public | |
1305 * @return void | |
1306 */ | |
1307 function testValidateErrors() { | |
1308 $TestController =& new TestController(); | |
1309 $TestController->constructClasses(); | |
1310 $this->assertFalse($TestController->validateErrors()); | |
1311 $this->assertEqual($TestController->validate(), 0); | |
1312 | |
1313 $TestController->ControllerComment->invalidate('some_field', 'error_message'); | |
1314 $TestController->ControllerComment->invalidate('some_field2', 'error_message2'); | |
1315 $comment =& new ControllerComment(); | |
1316 $comment->set('someVar', 'data'); | |
1317 $result = $TestController->validateErrors($comment); | |
1318 $expected = array('some_field' => 'error_message', 'some_field2' => 'error_message2'); | |
1319 $this->assertIdentical($result, $expected); | |
1320 $this->assertEqual($TestController->validate($comment), 2); | |
1321 } | |
1322 | |
1323 /** | |
1324 * test that validateErrors works with any old model. | |
1325 * | |
1326 * @return void | |
1327 */ | |
1328 function testValidateErrorsOnArbitraryModels() { | |
1329 $TestController =& new TestController(); | |
1330 | |
1331 $Post = new ControllerPost(); | |
1332 $Post->validate = array('title' => 'notEmpty'); | |
1333 $Post->set('title', ''); | |
1334 $result = $TestController->validateErrors($Post); | |
1335 | |
1336 $expected = array('title' => 'This field cannot be left blank'); | |
1337 $this->assertEqual($result, $expected); | |
1338 } | |
1339 | |
1340 /** | |
1341 * testPostConditions method | |
1342 * | |
1343 * @access public | |
1344 * @return void | |
1345 */ | |
1346 function testPostConditions() { | |
1347 $Controller =& new Controller(); | |
1348 | |
1349 | |
1350 $data = array( | |
1351 'Model1' => array('field1' => '23'), | |
1352 'Model2' => array('field2' => 'string'), | |
1353 'Model3' => array('field3' => '23'), | |
1354 ); | |
1355 $expected = array( | |
1356 'Model1.field1' => '23', | |
1357 'Model2.field2' => 'string', | |
1358 'Model3.field3' => '23', | |
1359 ); | |
1360 $result = $Controller->postConditions($data); | |
1361 $this->assertIdentical($result, $expected); | |
1362 | |
1363 | |
1364 $data = array(); | |
1365 $Controller->data = array( | |
1366 'Model1' => array('field1' => '23'), | |
1367 'Model2' => array('field2' => 'string'), | |
1368 'Model3' => array('field3' => '23'), | |
1369 ); | |
1370 $expected = array( | |
1371 'Model1.field1' => '23', | |
1372 'Model2.field2' => 'string', | |
1373 'Model3.field3' => '23', | |
1374 ); | |
1375 $result = $Controller->postConditions($data); | |
1376 $this->assertIdentical($result, $expected); | |
1377 | |
1378 | |
1379 $data = array(); | |
1380 $Controller->data = array(); | |
1381 $result = $Controller->postConditions($data); | |
1382 $this->assertNull($result); | |
1383 | |
1384 | |
1385 $data = array(); | |
1386 $Controller->data = array( | |
1387 'Model1' => array('field1' => '23'), | |
1388 'Model2' => array('field2' => 'string'), | |
1389 'Model3' => array('field3' => '23'), | |
1390 ); | |
1391 $ops = array( | |
1392 'Model1.field1' => '>', | |
1393 'Model2.field2' => 'LIKE', | |
1394 'Model3.field3' => '<=', | |
1395 ); | |
1396 $expected = array( | |
1397 'Model1.field1 >' => '23', | |
1398 'Model2.field2 LIKE' => "%string%", | |
1399 'Model3.field3 <=' => '23', | |
1400 ); | |
1401 $result = $Controller->postConditions($data, $ops); | |
1402 $this->assertIdentical($result, $expected); | |
1403 } | |
1404 | |
1405 /** | |
1406 * testRequestHandlerPrefers method | |
1407 * | |
1408 * @access public | |
1409 * @return void | |
1410 */ | |
1411 function testRequestHandlerPrefers(){ | |
1412 Configure::write('debug', 2); | |
1413 $Controller =& new Controller(); | |
1414 $Controller->components = array("RequestHandler"); | |
1415 $Controller->modelClass='ControllerPost'; | |
1416 $Controller->params['url']['ext'] = 'rss'; | |
1417 $Controller->constructClasses(); | |
1418 $Controller->Component->initialize($Controller); | |
1419 $Controller->beforeFilter(); | |
1420 $Controller->Component->startup($Controller); | |
1421 | |
1422 $this->assertEqual($Controller->RequestHandler->prefers(), 'rss'); | |
1423 unset($Controller); | |
1424 } | |
1425 | |
1426 /** | |
1427 * testControllerHttpCodes method | |
1428 * | |
1429 * @access public | |
1430 * @return void | |
1431 */ | |
1432 function testControllerHttpCodes() { | |
1433 $Controller =& new Controller(); | |
1434 $result = $Controller->httpCodes(); | |
1435 $this->assertEqual(count($result), 39); | |
1436 | |
1437 $result = $Controller->httpCodes(100); | |
1438 $expected = array(100 => 'Continue'); | |
1439 $this->assertEqual($result, $expected); | |
1440 | |
1441 $codes = array( | |
1442 1337 => 'Undefined Unicorn', | |
1443 1729 => 'Hardy-Ramanujan Located' | |
1444 ); | |
1445 | |
1446 $result = $Controller->httpCodes($codes); | |
1447 $this->assertTrue($result); | |
1448 $this->assertEqual(count($Controller->httpCodes()), 41); | |
1449 | |
1450 $result = $Controller->httpCodes(1337); | |
1451 $expected = array(1337 => 'Undefined Unicorn'); | |
1452 $this->assertEqual($result, $expected); | |
1453 | |
1454 $codes = array(404 => 'Sorry Bro'); | |
1455 $result = $Controller->httpCodes($codes); | |
1456 $this->assertTrue($result); | |
1457 $this->assertEqual(count($Controller->httpCodes()), 41); | |
1458 | |
1459 $result = $Controller->httpCodes(404); | |
1460 $expected = array(404 => 'Sorry Bro'); | |
1461 $this->assertEqual($result, $expected); | |
1462 } | |
1463 | |
1464 /** | |
1465 * Tests that the startup process calls the correct functions | |
1466 * | |
1467 * @access public | |
1468 * @return void | |
1469 */ | |
1470 function testStartupProcess() { | |
1471 Mock::generatePartial('AnotherTestController','MockedController', array('beforeFilter', 'afterFilter')); | |
1472 Mock::generate('TestComponent', 'MockTestComponent', array('startup', 'initialize')); | |
1473 $MockedController =& new MockedController(); | |
1474 $MockedController->components = array('MockTest'); | |
1475 $MockedController->Component =& new Component(); | |
1476 $MockedController->Component->init($MockedController); | |
1477 $MockedController->expectCallCount('beforeFilter', 1); | |
1478 $MockedController->MockTest->expectCallCount('initialize', 1); | |
1479 $MockedController->MockTest->expectCallCount('startup', 1); | |
1480 $MockedController->startupProcess(); | |
1481 } | |
1482 /** | |
1483 * Tests that the shutdown process calls the correct functions | |
1484 * | |
1485 * @access public | |
1486 * @return void | |
1487 */ | |
1488 function testShutdownProcess() { | |
1489 Mock::generate('TestComponent', 'MockTestComponent', array('shutdown')); | |
1490 $MockedController =& new MockedController(); | |
1491 $MockedController->components = array('MockTest'); | |
1492 $MockedController->Component =& new Component(); | |
1493 $MockedController->Component->init($MockedController); | |
1494 $MockedController->expectCallCount('afterFilter', 1); | |
1495 $MockedController->MockTest->expectCallCount('shutdown', 1); | |
1496 $MockedController->shutdownProcess(); | |
1497 } | |
1498 } |