7
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://cakephp.org CakePHP Project
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)
20
App::import('Controller', 'Controller', false);
21
App::import('Component', 'Security');
22
App::import('Component', 'Cookie');
28
* @subpackage cake.tests.cases.libs.controller
30
if (!class_exists('AppController')) {
35
* @subpackage cake.tests.cases.libs.controller
37
class AppController extends Controller {
44
var $helpers = array('Html', 'Javascript');
51
var $uses = array('ControllerPost');
58
var $components = array('Cookie');
60
} elseif (!defined('APP_CONTROLLER_EXISTS')) {
61
define('APP_CONTROLLER_EXISTS', true);
65
* ControllerPost class
68
* @subpackage cake.tests.cases.libs.controller
70
class ControllerPost extends CakeTestModel {
75
* @var string 'ControllerPost'
78
var $name = 'ControllerPost';
86
var $useTable = 'posts';
89
* invalidFields property
94
var $invalidFields = array('name' => 'error_msg');
102
var $lastQuery = null;
107
* @param mixed $query
111
function beforeFind($query) {
112
$this->lastQuery = $query;
119
* @param array $options
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);
129
return parent::find($type, $options);
134
* ControllerPostsController class
137
* @subpackage cake.tests.cases.libs.controller
139
class ControllerCommentsController extends AppController {
144
* @var string 'ControllerPost'
147
var $name = 'ControllerComments';
151
* ControllerComment class
154
* @subpackage cake.tests.cases.libs.controller
156
class ControllerComment extends CakeTestModel {
161
* @var string 'ControllerComment'
164
var $name = 'Comment';
169
* @var string 'comments'
172
var $useTable = 'comments';
180
var $data = array('name' => 'Some Name');
185
* @var string 'ControllerComment'
188
var $alias = 'ControllerComment';
192
* ControllerAlias class
195
* @subpackage cake.tests.cases.libs.controller
197
class ControllerAlias extends CakeTestModel {
202
* @var string 'ControllerAlias'
205
var $name = 'ControllerAlias';
210
* @var string 'ControllerSomeAlias'
213
var $alias = 'ControllerSomeAlias';
218
* @var string 'posts'
221
var $useTable = 'posts';
225
* ControllerPaginateModel class
228
* @subpackage cake.tests.cases.libs.controller
230
class ControllerPaginateModel extends CakeTestModel {
238
var $name = 'ControllerPaginateModel';
246
var $useTable = 'comments';
254
function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra) {
255
$this->extra = $extra;
264
function paginateCount($conditions, $recursive, $extra) {
265
$this->extraCount = $extra;
273
* @subpackage cake.tests.cases.libs.controller
275
class NameTest extends CakeTestModel {
286
* @var string 'names'
289
var $useTable = 'comments';
294
* @var string 'ControllerComment'
301
* TestController class
304
* @subpackage cake.tests.cases.libs.controller
306
class TestController extends AppController {
313
var $name = 'TestController';
321
var $helpers = array('Session', 'Xml');
324
* components property
329
var $components = array('Security');
337
var $uses = array('ControllerComment', 'ControllerAlias');
342
* @param mixed $testId
343
* @param mixed $test2Id
347
function index($testId, $test2Id) {
348
$this->data['testId'] = $testId;
349
$this->data['test2Id'] = $test2Id;
354
* TestComponent class
357
* @subpackage cake.tests.cases.libs.controller
359
class TestComponent extends Object {
362
* beforeRedirect method
367
function beforeRedirect() {
375
function initialize(&$controller) {
384
function startup(&$controller) {
392
function shutdown(&$controller) {
395
* beforeRender callback
399
function beforeRender(&$controller) {
400
if ($this->viewclass) {
401
$controller->view = $this->viewclass;
407
* AnotherTestController class
410
* @subpackage cake.tests.cases.libs.controller
412
class AnotherTestController extends AppController {
419
var $name = 'AnotherTest';
430
* ControllerTest class
433
* @subpackage cake.tests.cases.libs.controller
435
class ControllerTest extends CakeTestCase {
443
var $fixtures = array('core.post', 'core.comment', 'core.name');
456
* testLoadModel method
461
function testLoadModel() {
462
$Controller =& new Controller();
464
$this->assertFalse(isset($Controller->ControllerPost));
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));
471
ClassRegistry::flush();
476
* testConstructClasses method
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);
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'));
497
$this->assertEqual($Controller->ControllerComment->name, 'Comment');
501
App::build(array('plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)));
503
$Controller =& new Controller();
504
$Controller->uses = array('TestPlugin.TestPluginPost');
505
$Controller->constructClasses();
507
$this->assertEqual($Controller->modelClass, 'TestPluginPost');
508
$this->assertTrue(isset($Controller->TestPluginPost));
509
$this->assertTrue(is_a($Controller->TestPluginPost, 'TestPluginPost'));
515
* testAliasName method
520
function testAliasName() {
521
$Controller =& new Controller();
522
$Controller->uses = array('NameTest');
523
$Controller->constructClasses();
525
$this->assertEqual($Controller->NameTest->name, 'Name');
526
$this->assertEqual($Controller->NameTest->alias, 'Name');
532
* testPersistent method
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');
549
Configure::write('Cache.disable', true);
553
* testPaginate method
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();
565
$results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
566
$this->assertEqual($results, array(1, 2, 3));
568
$results = Set::extract($Controller->paginate('ControllerComment'), '{n}.ControllerComment.id');
569
$this->assertEqual($results, array(1, 2, 3, 4, 5, 6));
571
$Controller->modelClass = null;
573
$Controller->uses[0] = 'Plugin.ControllerPost';
574
$results = Set::extract($Controller->paginate(), '{n}.ControllerPost.id');
575
$this->assertEqual($results, array(1, 2, 3));
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));
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));
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));
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));
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));
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));
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');
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);
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);
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);
639
* testPaginateExtraParams method
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();
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']));
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']));
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'));
669
$Controller->passedArgs = array('limit' => 12);
670
$Controller->paginate = array('limit' => 30);
671
$result = $Controller->paginate('ControllerPost');
672
$paging = $Controller->params['paging']['ControllerPost'];
674
$this->assertEqual($Controller->ControllerPost->lastQuery['limit'], 12);
675
$this->assertEqual($paging['options']['limit'], 12);
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')
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);
689
$Controller->paginate = array(
690
'ControllerPaginateModel' => array('foo', 'contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id')
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);
699
* testPaginatePassedArgs method
704
function testPaginatePassedArgs() {
705
$Controller =& new Controller();
706
$Controller->uses = array('ControllerPost');
707
$Controller->passedArgs[] = array('1', '2', '3');
708
$Controller->params['url'] = array();
709
$Controller->constructClasses();
711
$Controller->paginate = array(
718
$conditions = array();
719
$Controller->paginate('ControllerPost',$conditions);
727
'conditions' => array()
729
$this->assertEqual($Controller->params['paging']['ControllerPost']['options'],$expected);
733
* Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
737
function testPaginateSpecialType() {
738
$Controller =& new Controller();
739
$Controller->uses = array('ControllerPost', 'ControllerComment');
740
$Controller->passedArgs[] = '1';
741
$Controller->params['url'] = array();
742
$Controller->constructClasses();
744
$Controller->paginate = array('ControllerPost' => array('popular', 'fields' => array('id', 'title')));
745
$result = $Controller->paginate('ControllerPost');
747
$this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(2, 3));
748
$this->assertEqual($Controller->ControllerPost->lastQuery['conditions'], array('ControllerPost.id > ' => '1'));
749
$this->assertFalse(isset($Controller->params['paging']['ControllerPost']['defaults'][0]));
750
$this->assertFalse(isset($Controller->params['paging']['ControllerPost']['options'][0]));
754
* testDefaultPaginateParams method
759
function testDefaultPaginateParams() {
760
$Controller =& new Controller();
761
$Controller->modelClass = 'ControllerPost';
762
$Controller->params['url'] = array();
763
$Controller->paginate = array('order' => 'ControllerPost.id DESC');
764
$Controller->constructClasses();
765
$results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
766
$this->assertEqual($Controller->params['paging']['ControllerPost']['defaults']['order'], 'ControllerPost.id DESC');
767
$this->assertEqual($Controller->params['paging']['ControllerPost']['options']['order'], 'ControllerPost.id DESC');
768
$this->assertEqual($results, array(3, 2, 1));
772
* test paginate() and virtualField interactions
776
function testPaginateOrderVirtualField() {
777
$Controller =& new Controller();
778
$Controller->uses = array('ControllerPost', 'ControllerComment');
779
$Controller->params['url'] = array();
780
$Controller->constructClasses();
781
$Controller->ControllerPost->virtualFields = array(
782
'offset_test' => 'ControllerPost.id + 1'
785
$Controller->paginate = array(
786
'fields' => array('id', 'title', 'offset_test'),
787
'order' => array('offset_test' => 'DESC')
789
$result = $Controller->paginate('ControllerPost');
790
$this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(4, 3, 2));
792
$Controller->passedArgs = array('sort' => 'offset_test', 'direction' => 'asc');
793
$result = $Controller->paginate('ControllerPost');
794
$this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(2, 3, 4));
803
function testFlash() {
804
$Controller =& new Controller();
805
$Controller->flash('this should work', '/flash');
806
$result = $Controller->output;
808
$expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
809
<html xmlns="http://www.w3.org/1999/xhtml">
811
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
812
<title>this should work</title>
814
P { text-align:center; font:bold 1.1em sans-serif }
815
A { color:#444; text-decoration:none }
816
A:HOVER { text-decoration: underline; color:#44E }
820
<p><a href="/flash">this should work</a></p>
823
$result = str_replace(array("\t", "\r\n", "\n"), "", $result);
824
$expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
825
$this->assertEqual($result, $expected);
827
App::build(array('views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)));
828
$Controller =& new Controller();
829
$Controller->flash('this should work', '/flash', 1, 'ajax2');
830
$result = $Controller->output;
831
$this->assertPattern('/Ajax!/', $result);
836
* testControllerSet method
841
function testControllerSet() {
842
$Controller =& new Controller();
843
$Controller->set('variable_with_underscores', null);
844
$this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
846
$Controller->viewVars = array();
847
$viewVars = array('ModelName' => array('id' => 1, 'name' => 'value'));
848
$Controller->set($viewVars);
849
$this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
851
$Controller->viewVars = array();
852
$Controller->set('variable_with_underscores', 'value');
853
$this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
855
$Controller->viewVars = array();
856
$viewVars = array('ModelName' => 'name');
857
$Controller->set($viewVars);
858
$this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
860
$Controller->set('title', 'someTitle');
861
$this->assertIdentical($Controller->viewVars['title'], 'someTitle');
862
$this->assertTrue(empty($Controller->pageTitle));
864
$Controller->viewVars = array();
865
$expected = array('ModelName' => 'name', 'ModelName2' => 'name2');
866
$Controller->set(array('ModelName', 'ModelName2'), array('name', 'name2'));
867
$this->assertIdentical($Controller->viewVars, $expected);
869
$Controller->viewVars = array();
870
$Controller->set(array(3 => 'three', 4 => 'four'));
871
$Controller->set(array(1 => 'one', 2 => 'two'));
872
$expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
873
$this->assertEqual($Controller->viewVars, $expected);
883
function testRender() {
885
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
888
$Controller =& new Controller();
889
$Controller->viewPath = 'posts';
891
$result = $Controller->render('index');
892
$this->assertPattern('/posts index/', $result);
894
$result = $Controller->render('/elements/test_element');
895
$this->assertPattern('/this is the test element/', $result);
897
$Controller = new TestController();
898
$Controller->constructClasses();
899
$Controller->ControllerComment->validationErrors = array('title' => 'tooShort');
900
$expected = $Controller->ControllerComment->validationErrors;
902
ClassRegistry::flush();
903
$Controller->viewPath = 'posts';
904
$result = $Controller->render('index');
905
$View = ClassRegistry::getObject('view');
906
$this->assertTrue(isset($View->validationErrors['ControllerComment']));
907
$this->assertEqual($expected, $View->validationErrors['ControllerComment']);
909
$Controller->ControllerComment->validationErrors = array();
910
ClassRegistry::flush();
915
* test that a component beforeRender can change the controller view class.
919
function testComponentBeforeRenderChangingViewClass() {
920
$core = App::core('views');
923
TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS,
927
$Controller =& new Controller();
928
$Controller->uses = array();
929
$Controller->components = array('Test');
930
$Controller->constructClasses();
931
$Controller->Test->viewclass = 'Theme';
932
$Controller->viewPath = 'posts';
933
$Controller->theme = 'test_theme';
934
$result = $Controller->render('index');
935
$this->assertPattern('/default test_theme layout/', $result);
940
* testToBeInheritedGuardmethods method
945
function testToBeInheritedGuardmethods() {
946
$Controller =& new Controller();
947
$this->assertTrue($Controller->_beforeScaffold(''));
948
$this->assertTrue($Controller->_afterScaffoldSave(''));
949
$this->assertTrue($Controller->_afterScaffoldSaveError(''));
950
$this->assertFalse($Controller->_scaffoldError(''));
954
* testRedirect method
959
function testRedirect() {
962
101 => "Switching Protocols",
966
203 => "Non-Authoritative Information",
968
205 => "Reset Content",
969
206 => "Partial Content",
970
300 => "Multiple Choices",
971
301 => "Moved Permanently",
974
304 => "Not Modified",
976
307 => "Temporary Redirect",
977
400 => "Bad Request",
978
401 => "Unauthorized",
979
402 => "Payment Required",
982
405 => "Method Not Allowed",
983
406 => "Not Acceptable",
984
407 => "Proxy Authentication Required",
985
408 => "Request Time-out",
988
411 => "Length Required",
989
412 => "Precondition Failed",
990
413 => "Request Entity Too Large",
991
414 => "Request-URI Too Large",
992
415 => "Unsupported Media Type",
993
416 => "Requested range not satisfiable",
994
417 => "Expectation Failed",
995
500 => "Internal Server Error",
996
501 => "Not Implemented",
997
502 => "Bad Gateway",
998
503 => "Service Unavailable",
999
504 => "Gateway Time-out"
1002
Mock::generatePartial('Controller', 'MockController', array('header'));
1003
Mock::generate('TestComponent', 'MockTestComponent');
1004
Mock::generate('TestComponent', 'MockTestBComponent');
1006
App::import('Helper', 'Cache');
1008
foreach ($codes as $code => $msg) {
1009
$MockController =& new MockController();
1010
$MockController->Component =& new Component();
1011
$MockController->Component->init($MockController);
1012
$MockController->expectAt(0, 'header', array("HTTP/1.1 {$code} {$msg}"));
1013
$MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
1014
$MockController->expectCallCount('header', 2);
1015
$MockController->redirect('http://cakephp.org', (int)$code, false);
1016
$this->assertFalse($MockController->autoRender);
1018
foreach ($codes as $code => $msg) {
1019
$MockController =& new MockController();
1020
$MockController->Component =& new Component();
1021
$MockController->Component->init($MockController);
1022
$MockController->expectAt(0, 'header', array("HTTP/1.1 {$code} {$msg}"));
1023
$MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
1024
$MockController->expectCallCount('header', 2);
1025
$MockController->redirect('http://cakephp.org', $msg, false);
1026
$this->assertFalse($MockController->autoRender);
1029
$MockController =& new MockController();
1030
$MockController->Component =& new Component();
1031
$MockController->Component->init($MockController);
1032
$MockController->expectAt(0, 'header', array('Location: http://www.example.org/users/login'));
1033
$MockController->expectCallCount('header', 1);
1034
$MockController->redirect('http://www.example.org/users/login', null, false);
1036
$MockController =& new MockController();
1037
$MockController->Component =& new Component();
1038
$MockController->Component->init($MockController);
1039
$MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
1040
$MockController->expectAt(1, 'header', array('Location: http://www.example.org/users/login'));
1041
$MockController->expectCallCount('header', 2);
1042
$MockController->redirect('http://www.example.org/users/login', 301, false);
1044
$MockController =& new MockController();
1045
$MockController->components = array('MockTest');
1046
$MockController->Component =& new Component();
1047
$MockController->Component->init($MockController);
1048
$MockController->MockTest->setReturnValue('beforeRedirect', null);
1049
$MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
1050
$MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
1051
$MockController->expectCallCount('header', 2);
1052
$MockController->redirect('http://cakephp.org', 301, false);
1054
$MockController =& new MockController();
1055
$MockController->components = array('MockTest');
1056
$MockController->Component =& new Component();
1057
$MockController->Component->init($MockController);
1058
$MockController->MockTest->setReturnValue('beforeRedirect', 'http://book.cakephp.org');
1059
$MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
1060
$MockController->expectAt(1, 'header', array('Location: http://book.cakephp.org'));
1061
$MockController->expectCallCount('header', 2);
1062
$MockController->redirect('http://cakephp.org', 301, false);
1064
$MockController =& new MockController();
1065
$MockController->components = array('MockTest');
1066
$MockController->Component =& new Component();
1067
$MockController->Component->init($MockController);
1068
$MockController->MockTest->setReturnValue('beforeRedirect', false);
1069
$MockController->expectNever('header');
1070
$MockController->redirect('http://cakephp.org', 301, false);
1072
$MockController =& new MockController();
1073
$MockController->components = array('MockTest', 'MockTestB');
1074
$MockController->Component =& new Component();
1075
$MockController->Component->init($MockController);
1076
$MockController->MockTest->setReturnValue('beforeRedirect', 'http://book.cakephp.org');
1077
$MockController->MockTestB->setReturnValue('beforeRedirect', 'http://bakery.cakephp.org');
1078
$MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
1079
$MockController->expectAt(1, 'header', array('Location: http://bakery.cakephp.org'));
1080
$MockController->expectCallCount('header', 2);
1081
$MockController->redirect('http://cakephp.org', 301, false);
1085
* testMergeVars method
1090
function testMergeVars() {
1091
if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) {
1095
$TestController =& new TestController();
1096
$TestController->constructClasses();
1098
$testVars = get_class_vars('TestController');
1099
$appVars = get_class_vars('AppController');
1101
$components = is_array($appVars['components'])
1102
? array_merge($appVars['components'], $testVars['components'])
1103
: $testVars['components'];
1104
if (!in_array('Session', $components)) {
1105
$components[] = 'Session';
1107
$helpers = is_array($appVars['helpers'])
1108
? array_merge($appVars['helpers'], $testVars['helpers'])
1109
: $testVars['helpers'];
1110
$uses = is_array($appVars['uses'])
1111
? array_merge($appVars['uses'], $testVars['uses'])
1112
: $testVars['uses'];
1114
$this->assertEqual(count(array_diff($TestController->helpers, $helpers)), 0);
1115
$this->assertEqual(count(array_diff($TestController->uses, $uses)), 0);
1116
$this->assertEqual(count(array_diff_assoc(Set::normalize($TestController->components), Set::normalize($components))), 0);
1118
$TestController =& new AnotherTestController();
1119
$TestController->constructClasses();
1121
$appVars = get_class_vars('AppController');
1122
$testVars = get_class_vars('AnotherTestController');
1125
$this->assertTrue(in_array('ControllerPost', $appVars['uses']));
1126
$this->assertNull($testVars['uses']);
1128
$this->assertFalse(isset($TestController->ControllerPost));
1131
$TestController =& new ControllerCommentsController();
1132
$TestController->constructClasses();
1134
$appVars = get_class_vars('AppController');
1135
$testVars = get_class_vars('ControllerCommentsController');
1138
$this->assertTrue(in_array('ControllerPost', $appVars['uses']));
1139
$this->assertEqual(array('ControllerPost'), $testVars['uses']);
1141
$this->assertTrue(isset($TestController->ControllerPost));
1142
$this->assertTrue(isset($TestController->ControllerComment));
1146
* test that options from child classes replace those in the parent classes.
1151
function testChildComponentOptionsSupercedeParents() {
1152
if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) {
1155
$TestController =& new TestController();
1156
$expected = array('foo');
1157
$TestController->components = array('Cookie' => $expected);
1158
$TestController->constructClasses();
1159
$this->assertEqual($TestController->components['Cookie'], $expected);
1163
* Ensure that __mergeVars is not being greedy and merging with
1164
* AppController when you make an instance of Controller
1168
function testMergeVarsNotGreedy() {
1169
$Controller =& new Controller();
1170
$Controller->components = array();
1171
$Controller->uses = array();
1172
$Controller->constructClasses();
1174
$this->assertFalse(isset($Controller->Session));
1178
* testReferer method
1183
function testReferer() {
1184
$Controller =& new Controller();
1185
$_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
1186
$result = $Controller->referer(null, false);
1187
$expected = 'http://cakephp.org';
1188
$this->assertIdentical($result, $expected);
1190
$_SERVER['HTTP_REFERER'] = '';
1191
$result = $Controller->referer('http://cakephp.org', false);
1192
$expected = 'http://cakephp.org';
1193
$this->assertIdentical($result, $expected);
1195
$_SERVER['HTTP_REFERER'] = '';
1197
'controller' => 'pages',
1198
'action' => 'display',
1201
$result = $Controller->referer($referer, false);
1202
$expected = 'http://' . env('HTTP_HOST') . '/pages/display/home';
1203
$this->assertIdentical($result, $expected);
1205
$_SERVER['HTTP_REFERER'] = '';
1206
$result = $Controller->referer(null, false);
1208
$this->assertIdentical($result, $expected);
1210
$_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'/some/path';
1211
$result = $Controller->referer(null, false);
1212
$expected = '/some/path';
1213
$this->assertIdentical($result, $expected);
1215
$Controller->webroot .= '/';
1216
$_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'/some/path';
1217
$result = $Controller->referer(null, false);
1218
$expected = '/some/path';
1219
$this->assertIdentical($result, $expected);
1221
$_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'some/path';
1222
$result = $Controller->referer(null, false);
1223
$expected = '/some/path';
1224
$this->assertIdentical($result, $expected);
1226
$Controller->webroot = '/recipe/';
1228
$_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'recipes/add';
1229
$result = $Controller->referer();
1230
$expected = '/recipes/add';
1231
$this->assertIdentical($result, $expected);
1235
* testSetAction method
1240
function testSetAction() {
1241
$TestController =& new TestController();
1242
$TestController->setAction('index', 1, 2);
1243
$expected = array('testId' => 1, 'test2Id' => 2);
1244
$this->assertidentical($TestController->data, $expected);
1248
* testUnimplementedIsAuthorized method
1253
function testUnimplementedIsAuthorized() {
1254
$TestController =& new TestController();
1255
$TestController->isAuthorized();
1256
$this->assertError();
1260
* testValidateErrors method
1265
function testValidateErrors() {
1266
$TestController =& new TestController();
1267
$TestController->constructClasses();
1268
$this->assertFalse($TestController->validateErrors());
1269
$this->assertEqual($TestController->validate(), 0);
1271
$TestController->ControllerComment->invalidate('some_field', 'error_message');
1272
$TestController->ControllerComment->invalidate('some_field2', 'error_message2');
1273
$comment =& new ControllerComment();
1274
$comment->set('someVar', 'data');
1275
$result = $TestController->validateErrors($comment);
1276
$expected = array('some_field' => 'error_message', 'some_field2' => 'error_message2');
1277
$this->assertIdentical($result, $expected);
1278
$this->assertEqual($TestController->validate($comment), 2);
1282
* test that validateErrors works with any old model.
1286
function testValidateErrorsOnArbitraryModels() {
1287
$TestController =& new TestController();
1289
$Post = new ControllerPost();
1290
$Post->validate = array('title' => 'notEmpty');
1291
$Post->set('title', '');
1292
$result = $TestController->validateErrors($Post);
1294
$expected = array('title' => 'This field cannot be left blank');
1295
$this->assertEqual($result, $expected);
1299
* testPostConditions method
1304
function testPostConditions() {
1305
$Controller =& new Controller();
1309
'Model1' => array('field1' => '23'),
1310
'Model2' => array('field2' => 'string'),
1311
'Model3' => array('field3' => '23'),
1314
'Model1.field1' => '23',
1315
'Model2.field2' => 'string',
1316
'Model3.field3' => '23',
1318
$result = $Controller->postConditions($data);
1319
$this->assertIdentical($result, $expected);
1323
$Controller->data = array(
1324
'Model1' => array('field1' => '23'),
1325
'Model2' => array('field2' => 'string'),
1326
'Model3' => array('field3' => '23'),
1329
'Model1.field1' => '23',
1330
'Model2.field2' => 'string',
1331
'Model3.field3' => '23',
1333
$result = $Controller->postConditions($data);
1334
$this->assertIdentical($result, $expected);
1338
$Controller->data = array();
1339
$result = $Controller->postConditions($data);
1340
$this->assertNull($result);
1344
$Controller->data = array(
1345
'Model1' => array('field1' => '23'),
1346
'Model2' => array('field2' => 'string'),
1347
'Model3' => array('field3' => '23'),
1350
'Model1.field1' => '>',
1351
'Model2.field2' => 'LIKE',
1352
'Model3.field3' => '<=',
1355
'Model1.field1 >' => '23',
1356
'Model2.field2 LIKE' => "%string%",
1357
'Model3.field3 <=' => '23',
1359
$result = $Controller->postConditions($data, $ops);
1360
$this->assertIdentical($result, $expected);
1364
* testRequestHandlerPrefers method
1369
function testRequestHandlerPrefers(){
1370
Configure::write('debug', 2);
1371
$Controller =& new Controller();
1372
$Controller->components = array("RequestHandler");
1373
$Controller->modelClass='ControllerPost';
1374
$Controller->params['url']['ext'] = 'rss';
1375
$Controller->constructClasses();
1376
$Controller->Component->initialize($Controller);
1377
$Controller->beforeFilter();
1378
$Controller->Component->startup($Controller);
1380
$this->assertEqual($Controller->RequestHandler->prefers(), 'rss');
1385
* testControllerHttpCodes method
1390
function testControllerHttpCodes() {
1391
$Controller =& new Controller();
1392
$result = $Controller->httpCodes();
1393
$this->assertEqual(count($result), 39);
1395
$result = $Controller->httpCodes(100);
1396
$expected = array(100 => 'Continue');
1397
$this->assertEqual($result, $expected);
1400
1337 => 'Undefined Unicorn',
1401
1729 => 'Hardy-Ramanujan Located'
1404
$result = $Controller->httpCodes($codes);
1405
$this->assertTrue($result);
1406
$this->assertEqual(count($Controller->httpCodes()), 41);
1408
$result = $Controller->httpCodes(1337);
1409
$expected = array(1337 => 'Undefined Unicorn');
1410
$this->assertEqual($result, $expected);
1412
$codes = array(404 => 'Sorry Bro');
1413
$result = $Controller->httpCodes($codes);
1414
$this->assertTrue($result);
1415
$this->assertEqual(count($Controller->httpCodes()), 41);
1417
$result = $Controller->httpCodes(404);
1418
$expected = array(404 => 'Sorry Bro');
1419
$this->assertEqual($result, $expected);
1423
* Tests that the startup process calls the correct functions
1428
function testStartupProcess() {
1429
Mock::generatePartial('AnotherTestController','MockedController', array('beforeFilter', 'afterFilter'));
1430
Mock::generate('TestComponent', 'MockTestComponent', array('startup', 'initialize'));
1431
$MockedController =& new MockedController();
1432
$MockedController->components = array('MockTest');
1433
$MockedController->Component =& new Component();
1434
$MockedController->Component->init($MockedController);
1435
$MockedController->expectCallCount('beforeFilter', 1);
1436
$MockedController->MockTest->expectCallCount('initialize', 1);
1437
$MockedController->MockTest->expectCallCount('startup', 1);
1438
$MockedController->startupProcess();
1441
* Tests that the shutdown process calls the correct functions
1446
function testShutdownProcess() {
1447
Mock::generate('TestComponent', 'MockTestComponent', array('shutdown'));
1448
$MockedController =& new MockedController();
1449
$MockedController->components = array('MockTest');
1450
$MockedController->Component =& new Component();
1451
$MockedController->Component->init($MockedController);
1452
$MockedController->expectCallCount('afterFilter', 1);
1453
$MockedController->MockTest->expectCallCount('shutdown', 1);
1454
$MockedController->shutdownProcess();
b'\\ No newline at end of file'