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