pictcode / lib / Cake / Test / Case / View / ViewTest.php @ a0ff9cef
履歴 | 表示 | アノテート | ダウンロード (49.259 KB)
| 1 | 635eef61 | spyder1211 | <?php
 | 
|---|---|---|---|
| 2 | /**
 | ||
| 3 |  * ViewTest file
 | ||
| 4 |  *
 | ||
| 5 |  * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
 | ||
| 6 |  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 | ||
| 7 |  *
 | ||
| 8 |  * Licensed under The MIT License
 | ||
| 9 |  * For full copyright and license information, please see the LICENSE.txt
 | ||
| 10 |  * Redistributions of files must retain the above copyright notice
 | ||
| 11 |  *
 | ||
| 12 |  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 | ||
| 13 |  * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
 | ||
| 14 |  * @package       Cake.Test.Case.View
 | ||
| 15 |  * @since         CakePHP(tm) v 1.2.0.4206
 | ||
| 16 |  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 | ||
| 17 |  */
 | ||
| 18 | |||
| 19 | App::uses('View', 'View'); | ||
| 20 | App::uses('Helper', 'View'); | ||
| 21 | App::uses('Controller', 'Controller'); | ||
| 22 | App::uses('CacheHelper', 'View/Helper'); | ||
| 23 | App::uses('HtmlHelper', 'View/Helper'); | ||
| 24 | App::uses('ErrorHandler', 'Error'); | ||
| 25 | App::uses('CakeEventManager', 'Event'); | ||
| 26 | App::uses('CakeEventListener', 'Event'); | ||
| 27 | |||
| 28 | /**
 | ||
| 29 |  * ViewPostsController class
 | ||
| 30 |  *
 | ||
| 31 |  * @package       Cake.Test.Case.View
 | ||
| 32 |  */
 | ||
| 33 | class ViewPostsController extends Controller { | ||
| 34 | |||
| 35 | /**
 | ||
| 36 |  * name property
 | ||
| 37 |  *
 | ||
| 38 |  * @var string
 | ||
| 39 |  */
 | ||
| 40 | public $name = 'Posts'; | ||
| 41 | |||
| 42 | /**
 | ||
| 43 |  * uses property
 | ||
| 44 |  *
 | ||
| 45 |  * @var mixed
 | ||
| 46 |  */
 | ||
| 47 | public $uses = null; | ||
| 48 | |||
| 49 | /**
 | ||
| 50 |  * index method
 | ||
| 51 |  *
 | ||
| 52 |  * @return void
 | ||
| 53 |  */
 | ||
| 54 | public function index() { | ||
| 55 | $this->set(array( | ||
| 56 | 'testData' => 'Some test data', | ||
| 57 | 'test2' => 'more data', | ||
| 58 | 'test3' => 'even more data', | ||
| 59 | )); | ||
| 60 | } | ||
| 61 | |||
| 62 | /**
 | ||
| 63 |  * nocache_tags_with_element method
 | ||
| 64 |  *
 | ||
| 65 |  * @return void
 | ||
| 66 |  */
 | ||
| 67 | public function nocache_multiple_element() { | ||
| 68 | $this->set('foo', 'this is foo var'); | ||
| 69 | $this->set('bar', 'this is bar var'); | ||
| 70 | } | ||
| 71 | |||
| 72 | } | ||
| 73 | |||
| 74 | /**
 | ||
| 75 |  * ThemePostsController class
 | ||
| 76 |  *
 | ||
| 77 |  * @package       Cake.Test.Case.View
 | ||
| 78 |  */
 | ||
| 79 | class ThemePostsController extends Controller { | ||
| 80 | |||
| 81 | public $theme = null; | ||
| 82 | |||
| 83 | /**
 | ||
| 84 |  * index method
 | ||
| 85 |  *
 | ||
| 86 |  * @return void
 | ||
| 87 |  */
 | ||
| 88 | public function index() { | ||
| 89 | $this->set('testData', 'Some test data'); | ||
| 90 | $test2 = 'more data'; | ||
| 91 | $test3 = 'even more data'; | ||
| 92 | $this->set(compact('test2', 'test3')); | ||
| 93 | } | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | /**
 | ||
| 98 |  * TestThemeView class
 | ||
| 99 |  *
 | ||
| 100 |  * @package       Cake.Test.Case.View
 | ||
| 101 |  */
 | ||
| 102 | class TestThemeView extends View { | ||
| 103 | |||
| 104 | /**
 | ||
| 105 |  * renderElement method
 | ||
| 106 |  *
 | ||
| 107 |  * @param string $name
 | ||
| 108 |  * @param array $params
 | ||
| 109 |  * @return string The given name
 | ||
| 110 |  */
 | ||
| 111 | public function renderElement($name, $params = array()) { | ||
| 112 | return $name; | ||
| 113 | } | ||
| 114 | |||
| 115 | /**
 | ||
| 116 |  * getViewFileName method
 | ||
| 117 |  *
 | ||
| 118 |  * @param string $name Controller action to find template filename for
 | ||
| 119 |  * @return string Template filename
 | ||
| 120 |  */
 | ||
| 121 | public function getViewFileName($name = null) { | ||
| 122 | return $this->_getViewFileName($name); | ||
| 123 | } | ||
| 124 | |||
| 125 | /**
 | ||
| 126 |  * getLayoutFileName method
 | ||
| 127 |  *
 | ||
| 128 |  * @param string $name The name of the layout to find.
 | ||
| 129 |  * @return string Filename for layout file (.ctp).
 | ||
| 130 |  */
 | ||
| 131 | public function getLayoutFileName($name = null) { | ||
| 132 | return $this->_getLayoutFileName($name); | ||
| 133 | } | ||
| 134 | |||
| 135 | } | ||
| 136 | |||
| 137 | /**
 | ||
| 138 |  * TestView class
 | ||
| 139 |  *
 | ||
| 140 |  * @package       Cake.Test.Case.View
 | ||
| 141 |  */
 | ||
| 142 | class TestView extends View { | ||
| 143 | |||
| 144 | /**
 | ||
| 145 |  * getViewFileName method
 | ||
| 146 |  *
 | ||
| 147 |  * @param string $name Controller action to find template filename for
 | ||
| 148 |  * @return string Template filename
 | ||
| 149 |  */
 | ||
| 150 | public function getViewFileName($name = null) { | ||
| 151 | return $this->_getViewFileName($name); | ||
| 152 | } | ||
| 153 | |||
| 154 | /**
 | ||
| 155 |  * getLayoutFileName method
 | ||
| 156 |  *
 | ||
| 157 |  * @param string $name The name of the layout to find.
 | ||
| 158 |  * @return string Filename for layout file (.ctp).
 | ||
| 159 |  */
 | ||
| 160 | public function getLayoutFileName($name = null) { | ||
| 161 | return $this->_getLayoutFileName($name); | ||
| 162 | } | ||
| 163 | |||
| 164 | /**
 | ||
| 165 |  * paths method
 | ||
| 166 |  *
 | ||
| 167 |  * @param string $plugin Optional plugin name to scan for view files.
 | ||
| 168 |  * @param bool $cached Set to true to force a refresh of view paths.
 | ||
| 169 |  * @return array paths
 | ||
| 170 |  */
 | ||
| 171 | public function paths($plugin = null, $cached = true) { | ||
| 172 | return $this->_paths($plugin, $cached); | ||
| 173 | } | ||
| 174 | |||
| 175 | /**
 | ||
| 176 |  * Test only function to return instance scripts.
 | ||
| 177 |  *
 | ||
| 178 |  * @return array Scripts
 | ||
| 179 |  */
 | ||
| 180 | public function scripts() { | ||
| 181 | return $this->_scripts; | ||
| 182 | } | ||
| 183 | |||
| 184 | } | ||
| 185 | |||
| 186 | /**
 | ||
| 187 |  * TestBeforeAfterHelper class
 | ||
| 188 |  *
 | ||
| 189 |  * @package       Cake.Test.Case.View
 | ||
| 190 |  */
 | ||
| 191 | class TestBeforeAfterHelper extends Helper { | ||
| 192 | |||
| 193 | /**
 | ||
| 194 |  * property property
 | ||
| 195 |  *
 | ||
| 196 |  * @var string
 | ||
| 197 |  */
 | ||
| 198 | public $property = ''; | ||
| 199 | |||
| 200 | /**
 | ||
| 201 |  * beforeLayout method
 | ||
| 202 |  *
 | ||
| 203 |  * @param string $viewFile
 | ||
| 204 |  * @return void
 | ||
| 205 |  */
 | ||
| 206 | public function beforeLayout($viewFile) { | ||
| 207 | $this->property = 'Valuation'; | ||
| 208 | } | ||
| 209 | |||
| 210 | /**
 | ||
| 211 |  * afterLayout method
 | ||
| 212 |  *
 | ||
| 213 |  * @param string $layoutFile
 | ||
| 214 |  * @return void
 | ||
| 215 |  */
 | ||
| 216 | public function afterLayout($layoutFile) { | ||
| 217 | $this->_View->output .= 'modified in the afterlife'; | ||
| 218 | } | ||
| 219 | |||
| 220 | } | ||
| 221 | |||
| 222 | /**
 | ||
| 223 |  * Class TestObjectWithToString
 | ||
| 224 |  *
 | ||
| 225 |  * An object with the magic method __toString() for testing with view blocks.
 | ||
| 226 |  */
 | ||
| 227 | class TestObjectWithToString { | ||
| 228 | |||
| 229 | public function __toString() { | ||
| 230 | return "I'm ObjectWithToString"; | ||
| 231 | } | ||
| 232 | |||
| 233 | } | ||
| 234 | |||
| 235 | /**
 | ||
| 236 |  * Class TestObjectWithoutToString
 | ||
| 237 |  *
 | ||
| 238 |  * An object without the magic method __toString() for testing with view blocks.
 | ||
| 239 |  */
 | ||
| 240 | class TestObjectWithoutToString { | ||
| 241 | } | ||
| 242 | |||
| 243 | /**
 | ||
| 244 |  * Class TestViewEventListener
 | ||
| 245 |  *
 | ||
| 246 |  * An event listener to test cakePHP events
 | ||
| 247 |  */
 | ||
| 248 | class TestViewEventListener implements CakeEventListener { | ||
| 249 | |||
| 250 | /**
 | ||
| 251 |  * type of view before rendering has occurred
 | ||
| 252 |  *
 | ||
| 253 |  * @var string
 | ||
| 254 |  */
 | ||
| 255 | public $beforeRenderViewType; | ||
| 256 | |||
| 257 | /**
 | ||
| 258 |  * type of view after rendering has occurred
 | ||
| 259 |  *
 | ||
| 260 |  * @var string
 | ||
| 261 |  */
 | ||
| 262 | public $afterRenderViewType; | ||
| 263 | |||
| 264 | /**
 | ||
| 265 |  * implementedEvents method
 | ||
| 266 |  *
 | ||
| 267 |  * @return array
 | ||
| 268 |  */
 | ||
| 269 | public function implementedEvents() { | ||
| 270 | return array( | ||
| 271 | 'View.beforeRender' => 'beforeRender', | ||
| 272 | 'View.afterRender' => 'afterRender' | ||
| 273 | ); | ||
| 274 | } | ||
| 275 | |||
| 276 | /**
 | ||
| 277 |  * beforeRender method
 | ||
| 278 |  *
 | ||
| 279 |  * @param CakeEvent $event the event being sent
 | ||
| 280 |  * @return void
 | ||
| 281 |  */
 | ||
| 282 | public function beforeRender($event) { | ||
| 283 | $this->beforeRenderViewType = $event->subject()->getCurrentType(); | ||
| 284 | } | ||
| 285 | |||
| 286 | /**
 | ||
| 287 |  * afterRender method
 | ||
| 288 |  *
 | ||
| 289 |  * @param CakeEvent $event the event being sent
 | ||
| 290 |  * @return void
 | ||
| 291 |  */
 | ||
| 292 | public function afterRender($event) { | ||
| 293 | $this->afterRenderViewType = $event->subject()->getCurrentType(); | ||
| 294 | } | ||
| 295 | |||
| 296 | } | ||
| 297 | |||
| 298 | /**
 | ||
| 299 |  * ViewTest class
 | ||
| 300 |  *
 | ||
| 301 |  * @package       Cake.Test.Case.View
 | ||
| 302 |  */
 | ||
| 303 | class ViewTest extends CakeTestCase { | ||
| 304 | |||
| 305 | /**
 | ||
| 306 |  * Fixtures used in this test.
 | ||
| 307 |  *
 | ||
| 308 |  * @var array
 | ||
| 309 |  */
 | ||
| 310 | public $fixtures = array('core.user', 'core.post'); | ||
| 311 | |||
| 312 | /**
 | ||
| 313 |  * setUp method
 | ||
| 314 |  *
 | ||
| 315 |  * @return void
 | ||
| 316 |  */
 | ||
| 317 | public function setUp() { | ||
| 318 |                 parent::setUp();
 | ||
| 319 | |||
| 320 | $request = $this->getMock('CakeRequest'); | ||
| 321 | $this->Controller = new Controller($request); | ||
| 322 | $this->PostsController = new ViewPostsController($request); | ||
| 323 | $this->PostsController->viewPath = 'Posts'; | ||
| 324 | $this->PostsController->index(); | ||
| 325 | $this->View = new View($this->PostsController); | ||
| 326 | |||
| 327 | $themeRequest = new CakeRequest('posts/index'); | ||
| 328 | $this->ThemeController = new Controller($themeRequest); | ||
| 329 | $this->ThemePostsController = new ThemePostsController($themeRequest); | ||
| 330 | $this->ThemePostsController->viewPath = 'posts'; | ||
| 331 | $this->ThemePostsController->index(); | ||
| 332 | $this->ThemeView = new View($this->ThemePostsController); | ||
| 333 | |||
| 334 | App::build(array( | ||
| 335 | 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS), | ||
| 336 | 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS) | ||
| 337 | ), App::RESET); | ||
| 338 | App::objects('plugins', null, false); | ||
| 339 | |||
| 340 | CakePlugin::load(array('TestPlugin', 'TestPlugin', 'PluginJs')); | ||
| 341 | Configure::write('debug', 2); | ||
| 342 | } | ||
| 343 | |||
| 344 | /**
 | ||
| 345 |  * tearDown method
 | ||
| 346 |  *
 | ||
| 347 |  * @return void
 | ||
| 348 |  */
 | ||
| 349 | public function tearDown() { | ||
| 350 |                 parent::tearDown();
 | ||
| 351 |                 CakePlugin::unload();
 | ||
| 352 | unset($this->View); | ||
| 353 | unset($this->PostsController); | ||
| 354 | unset($this->Controller); | ||
| 355 | unset($this->ThemeView); | ||
| 356 | unset($this->ThemePostsController); | ||
| 357 | unset($this->ThemeController); | ||
| 358 | } | ||
| 359 | |||
| 360 | /**
 | ||
| 361 |  * Test getViewFileName method
 | ||
| 362 |  *
 | ||
| 363 |  * @return void
 | ||
| 364 |  */
 | ||
| 365 | public function testGetTemplate() { | ||
| 366 | $this->Controller->plugin = null; | ||
| 367 | $this->Controller->name = 'Pages'; | ||
| 368 | $this->Controller->viewPath = 'Pages'; | ||
| 369 | $this->Controller->action = 'display'; | ||
| 370 | $this->Controller->params['pass'] = array('home'); | ||
| 371 | |||
| 372 | $ThemeView = new TestThemeView($this->Controller); | ||
| 373 | $ThemeView->theme = 'test_theme'; | ||
| 374 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'home.ctp'; | ||
| 375 | $result = $ThemeView->getViewFileName('home'); | ||
| 376 | $this->assertEquals($expected, $result); | ||
| 377 | |||
| 378 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Posts' . DS . 'index.ctp'; | ||
| 379 | $result = $ThemeView->getViewFileName('/Posts/index'); | ||
| 380 | $this->assertEquals($expected, $result); | ||
| 381 | |||
| 382 | $ThemeView->theme = 'TestTheme'; | ||
| 383 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layouts' . DS . 'default.ctp'; | ||
| 384 | $result = $ThemeView->getLayoutFileName(); | ||
| 385 | $this->assertEquals($expected, $result); | ||
| 386 | |||
| 387 | $ThemeView->layoutPath = 'rss'; | ||
| 388 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'rss' . DS . 'default.ctp'; | ||
| 389 | $result = $ThemeView->getLayoutFileName(); | ||
| 390 | $this->assertEquals($expected, $result); | ||
| 391 | |||
| 392 | $ThemeView->layoutPath = 'Emails' . DS . 'html'; | ||
| 393 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'Emails' . DS . 'html' . DS . 'default.ctp'; | ||
| 394 | $result = $ThemeView->getLayoutFileName(); | ||
| 395 | $this->assertEquals($expected, $result); | ||
| 396 | } | ||
| 397 | |||
| 398 | /**
 | ||
| 399 |  * Test that plugin files with absolute file paths are scoped
 | ||
| 400 |  * to the plugin and do now allow any file path.
 | ||
| 401 |  *
 | ||
| 402 |  * @expectedException MissingViewException
 | ||
| 403 |  * @return void
 | ||
| 404 |  */
 | ||
| 405 | public function testPluginGetTemplateAbsoluteFail() { | ||
| 406 | $this->Controller->viewPath = 'Pages'; | ||
| 407 | $this->Controller->action = 'display'; | ||
| 408 | $this->Controller->params['pass'] = array('home'); | ||
| 409 | |||
| 410 | $view = new TestThemeView($this->Controller); | ||
| 411 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'View' . DS . 'Pages' . DS . 'index.ctp'; | ||
| 412 | $result = $view->getViewFileName('Company/TestPluginThree./Pages/index'); | ||
| 413 | $this->assertPathEquals($expected, $result); | ||
| 414 | |||
| 415 | $view->getViewFileName('Company/TestPluginThree./etc/passwd'); | ||
| 416 | } | ||
| 417 | |||
| 418 | /**
 | ||
| 419 |  * Test getLayoutFileName method on plugin
 | ||
| 420 |  *
 | ||
| 421 |  * @return void
 | ||
| 422 |  */
 | ||
| 423 | public function testPluginGetTemplate() { | ||
| 424 | $this->Controller->plugin = 'TestPlugin'; | ||
| 425 | $this->Controller->name = 'TestPlugin'; | ||
| 426 | $this->Controller->viewPath = 'Tests'; | ||
| 427 | $this->Controller->action = 'index'; | ||
| 428 | |||
| 429 | $View = new TestView($this->Controller); | ||
| 430 | |||
| 431 | $expected = CakePlugin::path('TestPlugin') . 'View' . DS . 'Tests' . DS . 'index.ctp'; | ||
| 432 | $result = $View->getViewFileName('index'); | ||
| 433 | $this->assertEquals($expected, $result); | ||
| 434 | |||
| 435 | $expected = CakePlugin::path('TestPlugin') . 'View' . DS . 'Layouts' . DS . 'default.ctp'; | ||
| 436 | $result = $View->getLayoutFileName(); | ||
| 437 | $this->assertEquals($expected, $result); | ||
| 438 | } | ||
| 439 | |||
| 440 | /**
 | ||
| 441 |  * Test getViewFileName method on plugin
 | ||
| 442 |  *
 | ||
| 443 |  * @return void
 | ||
| 444 |  */
 | ||
| 445 | public function testPluginThemedGetTemplate() { | ||
| 446 | $this->Controller->plugin = 'TestPlugin'; | ||
| 447 | $this->Controller->name = 'TestPlugin'; | ||
| 448 | $this->Controller->viewPath = 'Tests'; | ||
| 449 | $this->Controller->action = 'index'; | ||
| 450 | $this->Controller->theme = 'TestTheme'; | ||
| 451 | |||
| 452 | $ThemeView = new TestThemeView($this->Controller); | ||
| 453 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Tests' . DS . 'index.ctp'; | ||
| 454 | $result = $ThemeView->getViewFileName('index'); | ||
| 455 | $this->assertEquals($expected, $result); | ||
| 456 | |||
| 457 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Layouts' . DS . 'plugin_default.ctp'; | ||
| 458 | $result = $ThemeView->getLayoutFileName('plugin_default'); | ||
| 459 | $this->assertEquals($expected, $result); | ||
| 460 | |||
| 461 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layouts' . DS . 'default.ctp'; | ||
| 462 | $result = $ThemeView->getLayoutFileName('default'); | ||
| 463 | $this->assertEquals($expected, $result); | ||
| 464 | } | ||
| 465 | |||
| 466 | /**
 | ||
| 467 |  * Test that plugin/$plugin_name is only appended to the paths it should be.
 | ||
| 468 |  *
 | ||
| 469 |  * @return void
 | ||
| 470 |  */
 | ||
| 471 | public function testPluginPathGeneration() { | ||
| 472 | $this->Controller->plugin = 'TestPlugin'; | ||
| 473 | $this->Controller->name = 'TestPlugin'; | ||
| 474 | $this->Controller->viewPath = 'Tests'; | ||
| 475 | $this->Controller->action = 'index'; | ||
| 476 | |||
| 477 | $View = new TestView($this->Controller); | ||
| 478 | $paths = $View->paths(); | ||
| 479 | $expected = array_merge(App::path('View'), App::core('View'), App::core('Console/Templates/skel/View')); | ||
| 480 | $this->assertEquals($expected, $paths); | ||
| 481 | |||
| 482 | $paths = $View->paths('TestPlugin'); | ||
| 483 | $pluginPath = CakePlugin::path('TestPlugin'); | ||
| 484 | $expected = array( | ||
| 485 | CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Plugin' . DS . 'TestPlugin' . DS, | ||
| 486 | $pluginPath . 'View' . DS, | ||
| 487 | CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS, | ||
| 488 | CAKE . 'View' . DS, | ||
| 489 | CAKE . 'Console' . DS . 'Templates' . DS . 'skel' . DS . 'View' . DS | ||
| 490 | ); | ||
| 491 | $this->assertEquals($expected, $paths); | ||
| 492 | } | ||
| 493 | |||
| 494 | /**
 | ||
| 495 |  * Test that CamelCase'd plugins still find their view files.
 | ||
| 496 |  *
 | ||
| 497 |  * @return void
 | ||
| 498 |  */
 | ||
| 499 | public function testCamelCasePluginGetTemplate() { | ||
| 500 | $this->Controller->plugin = 'TestPlugin'; | ||
| 501 | $this->Controller->name = 'TestPlugin'; | ||
| 502 | $this->Controller->viewPath = 'Tests'; | ||
| 503 | $this->Controller->action = 'index'; | ||
| 504 | |||
| 505 | $View = new TestView($this->Controller); | ||
| 506 | App::build(array( | ||
| 507 | 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS), | ||
| 508 | 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS) | ||
| 509 | )); | ||
| 510 | |||
| 511 | $pluginPath = CakePlugin::path('TestPlugin'); | ||
| 512 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'View' . DS . 'Tests' . DS . 'index.ctp'; | ||
| 513 | $result = $View->getViewFileName('index'); | ||
| 514 | $this->assertEquals($expected, $result); | ||
| 515 | |||
| 516 | $expected = $pluginPath . 'View' . DS . 'Layouts' . DS . 'default.ctp'; | ||
| 517 | $result = $View->getLayoutFileName(); | ||
| 518 | $this->assertEquals($expected, $result); | ||
| 519 | } | ||
| 520 | |||
| 521 | /**
 | ||
| 522 |  * Test getViewFileName method
 | ||
| 523 |  *
 | ||
| 524 |  * @return void
 | ||
| 525 |  */
 | ||
| 526 | public function testGetViewFileNames() { | ||
| 527 | $this->Controller->plugin = null; | ||
| 528 | $this->Controller->name = 'Pages'; | ||
| 529 | $this->Controller->viewPath = 'Pages'; | ||
| 530 | $this->Controller->action = 'display'; | ||
| 531 | $this->Controller->params['pass'] = array('home'); | ||
| 532 | |||
| 533 | $View = new TestView($this->Controller); | ||
| 534 | |||
| 535 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'home.ctp'; | ||
| 536 | $result = $View->getViewFileName('home'); | ||
| 537 | $this->assertEquals($expected, $result); | ||
| 538 | |||
| 539 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'index.ctp'; | ||
| 540 | $result = $View->getViewFileName('/Posts/index'); | ||
| 541 | $this->assertEquals($expected, $result); | ||
| 542 | |||
| 543 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'index.ctp'; | ||
| 544 | $result = $View->getViewFileName('../Posts/index'); | ||
| 545 | $this->assertEquals($expected, $result); | ||
| 546 | |||
| 547 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'page.home.ctp'; | ||
| 548 | $result = $View->getViewFileName('page.home'); | ||
| 549 | $this->assertEquals($expected, $result, 'Should not ruin files with dots.'); | ||
| 550 | |||
| 551 | CakePlugin::load('TestPlugin'); | ||
| 552 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'home.ctp'; | ||
| 553 | $result = $View->getViewFileName('TestPlugin.home'); | ||
| 554 | $this->assertEquals($expected, $result, 'Plugin is missing the view, cascade to app.'); | ||
| 555 | |||
| 556 | $View->viewPath = 'Tests'; | ||
| 557 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'View' . DS . 'Tests' . DS . 'index.ctp'; | ||
| 558 | $result = $View->getViewFileName('TestPlugin.index'); | ||
| 559 | $this->assertEquals($expected, $result); | ||
| 560 | } | ||
| 561 | |||
| 562 | /**
 | ||
| 563 |  * Test getting layout filenames
 | ||
| 564 |  *
 | ||
| 565 |  * @return void
 | ||
| 566 |  */
 | ||
| 567 | public function testGetLayoutFileName() { | ||
| 568 | $this->Controller->plugin = null; | ||
| 569 | $this->Controller->name = 'Pages'; | ||
| 570 | $this->Controller->viewPath = 'Pages'; | ||
| 571 | $this->Controller->action = 'display'; | ||
| 572 | |||
| 573 | $View = new TestView($this->Controller); | ||
| 574 | |||
| 575 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'default.ctp'; | ||
| 576 | $result = $View->getLayoutFileName(); | ||
| 577 | $this->assertEquals($expected, $result); | ||
| 578 | |||
| 579 | $View->layoutPath = 'rss'; | ||
| 580 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'rss' . DS . 'default.ctp'; | ||
| 581 | $result = $View->getLayoutFileName(); | ||
| 582 | $this->assertEquals($expected, $result); | ||
| 583 | |||
| 584 | $View->layoutPath = 'Emails' . DS . 'html'; | ||
| 585 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'Emails' . DS . 'html' . DS . 'default.ctp'; | ||
| 586 | $result = $View->getLayoutFileName(); | ||
| 587 | $this->assertEquals($expected, $result); | ||
| 588 | } | ||
| 589 | |||
| 590 | /**
 | ||
| 591 |  * Test getting layout filenames for plugins.
 | ||
| 592 |  *
 | ||
| 593 |  * @return void
 | ||
| 594 |  */
 | ||
| 595 | public function testGetLayoutFileNamePlugin() { | ||
| 596 | $this->Controller->plugin = null; | ||
| 597 | $this->Controller->name = 'Pages'; | ||
| 598 | $this->Controller->viewPath = 'Pages'; | ||
| 599 | $this->Controller->action = 'display'; | ||
| 600 | |||
| 601 | $View = new TestView($this->Controller); | ||
| 602 | CakePlugin::load('TestPlugin'); | ||
| 603 | |||
| 604 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'View' . DS . 'Layouts' . DS . 'default.ctp'; | ||
| 605 | $result = $View->getLayoutFileName('TestPlugin.default'); | ||
| 606 | $this->assertEquals($expected, $result); | ||
| 607 | |||
| 608 | $View->plugin = 'TestPlugin'; | ||
| 609 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'View' . DS . 'Layouts' . DS . 'default.ctp'; | ||
| 610 | $result = $View->getLayoutFileName('default'); | ||
| 611 | $this->assertEquals($expected, $result); | ||
| 612 | } | ||
| 613 | |||
| 614 | /**
 | ||
| 615 |  * Test for missing views
 | ||
| 616 |  *
 | ||
| 617 |  * @expectedException MissingViewException
 | ||
| 618 |  * @return void
 | ||
| 619 |  */
 | ||
| 620 | public function testMissingView() { | ||
| 621 | $this->Controller->plugin = null; | ||
| 622 | $this->Controller->name = 'Pages'; | ||
| 623 | $this->Controller->viewPath = 'Pages'; | ||
| 624 | $this->Controller->action = 'display'; | ||
| 625 | $this->Controller->params['pass'] = array('home'); | ||
| 626 | |||
| 627 | $View = new TestView($this->Controller); | ||
| 628 | ob_start(); | ||
| 629 | $View->getViewFileName('does_not_exist'); | ||
| 630 | |||
| 631 | $this->ThemeController->plugin = null; | ||
| 632 | $this->ThemeController->name = 'Pages'; | ||
| 633 | $this->ThemeController->viewPath = 'Pages'; | ||
| 634 | $this->ThemeController->action = 'display'; | ||
| 635 | $this->ThemeController->theme = 'my_theme'; | ||
| 636 | |||
| 637 | $this->ThemeController->params['pass'] = array('home'); | ||
| 638 | |||
| 639 | $View = new TestThemeView($this->ThemeController); | ||
| 640 | $View->getViewFileName('does_not_exist'); | ||
| 641 | } | ||
| 642 | |||
| 643 | /**
 | ||
| 644 |  * Test for missing layouts
 | ||
| 645 |  *
 | ||
| 646 |  * @expectedException MissingLayoutException
 | ||
| 647 |  * @return void
 | ||
| 648 |  */
 | ||
| 649 | public function testMissingLayout() { | ||
| 650 | $this->Controller->plugin = null; | ||
| 651 | $this->Controller->name = 'Posts'; | ||
| 652 | $this->Controller->viewPath = 'Posts'; | ||
| 653 | $this->Controller->layout = 'whatever'; | ||
| 654 | |||
| 655 | $View = new TestView($this->Controller); | ||
| 656 | ob_start(); | ||
| 657 |                 $View->getLayoutFileName();
 | ||
| 658 | ob_get_clean(); | ||
| 659 | |||
| 660 | $this->ThemeController->plugin = null; | ||
| 661 | $this->ThemeController->name = 'Posts'; | ||
| 662 | $this->ThemeController->viewPath = 'posts'; | ||
| 663 | $this->ThemeController->layout = 'whatever'; | ||
| 664 | $this->ThemeController->theme = 'my_theme'; | ||
| 665 | |||
| 666 | $View = new TestThemeView($this->ThemeController); | ||
| 667 |                 $View->getLayoutFileName();
 | ||
| 668 | } | ||
| 669 | |||
| 670 | /**
 | ||
| 671 |  * Test viewVars method
 | ||
| 672 |  *
 | ||
| 673 |  * @return void
 | ||
| 674 |  */
 | ||
| 675 | public function testViewVars() { | ||
| 676 | $this->assertEquals(array('testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data'), $this->View->viewVars); | ||
| 677 | } | ||
| 678 | |||
| 679 | /**
 | ||
| 680 |  * Test generation of UUIDs method
 | ||
| 681 |  *
 | ||
| 682 |  * @return void
 | ||
| 683 |  */
 | ||
| 684 | public function testUUIDGeneration() { | ||
| 685 | $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index')); | ||
| 686 | $this->assertEquals('form5988016017', $result); | ||
| 687 | $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index')); | ||
| 688 | $this->assertEquals('formc3dc6be854', $result); | ||
| 689 | $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index')); | ||
| 690 | $this->assertEquals('form28f92cc87f', $result); | ||
| 691 | } | ||
| 692 | |||
| 693 | /**
 | ||
| 694 |  * Test addInlineScripts method
 | ||
| 695 |  *
 | ||
| 696 |  * @return void
 | ||
| 697 |  */
 | ||
| 698 | public function testAddInlineScripts() { | ||
| 699 | $View = new TestView($this->Controller); | ||
| 700 | $View->addScript('prototype.js'); | ||
| 701 | $View->addScript('prototype.js'); | ||
| 702 | $this->assertEquals(array('prototype.js'), $View->scripts()); | ||
| 703 | |||
| 704 | $View->addScript('mainEvent', 'Event.observe(window, "load", function() { doSomething(); }, true);'); | ||
| 705 | $this->assertEquals(array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);'), $View->scripts()); | ||
| 706 | } | ||
| 707 | |||
| 708 | /**
 | ||
| 709 |  * Test elementExists method
 | ||
| 710 |  *
 | ||
| 711 |  * @return void
 | ||
| 712 |  */
 | ||
| 713 | public function testElementExists() { | ||
| 714 | $result = $this->View->elementExists('test_element'); | ||
| 715 | $this->assertTrue($result); | ||
| 716 | |||
| 717 | $result = $this->View->elementExists('TestPlugin.plugin_element'); | ||
| 718 | $this->assertTrue($result); | ||
| 719 | |||
| 720 | $result = $this->View->elementExists('non_existent_element'); | ||
| 721 | $this->assertFalse($result); | ||
| 722 | |||
| 723 | $result = $this->View->elementExists('TestPlugin.element'); | ||
| 724 | $this->assertFalse($result); | ||
| 725 | |||
| 726 | $this->View->plugin = 'TestPlugin'; | ||
| 727 | $result = $this->View->elementExists('test_plugin_element'); | ||
| 728 | $this->assertTrue($result); | ||
| 729 | } | ||
| 730 | |||
| 731 | /**
 | ||
| 732 |  * Test element method
 | ||
| 733 |  *
 | ||
| 734 |  * @return void
 | ||
| 735 |  */
 | ||
| 736 | public function testElement() { | ||
| 737 | $result = $this->View->element('test_element'); | ||
| 738 | $this->assertEquals('this is the test element', $result); | ||
| 739 | |||
| 740 | $result = $this->View->element('plugin_element', array(), array('plugin' => 'TestPlugin')); | ||
| 741 | $this->assertEquals('this is the plugin element using params[plugin]', $result); | ||
| 742 | |||
| 743 | $result = $this->View->element('plugin_element', array(), array('plugin' => 'test_plugin')); | ||
| 744 | $this->assertEquals('this is the plugin element using params[plugin]', $result); | ||
| 745 | |||
| 746 | $result = $this->View->element('TestPlugin.plugin_element'); | ||
| 747 | $this->assertEquals('this is the plugin element using params[plugin]', $result); | ||
| 748 | |||
| 749 | $this->View->plugin = 'TestPlugin'; | ||
| 750 | $result = $this->View->element('test_plugin_element'); | ||
| 751 | $this->assertEquals('this is the test set using View::$plugin plugin element', $result); | ||
| 752 | } | ||
| 753 | |||
| 754 | /**
 | ||
| 755 |  * Test elementInexistent method
 | ||
| 756 |  *
 | ||
| 757 |  * @expectedException PHPUnit_Framework_Error_Notice
 | ||
| 758 |  * @return void
 | ||
| 759 |  */
 | ||
| 760 | public function testElementInexistent() { | ||
| 761 | $this->View->element('non_existent_element'); | ||
| 762 | } | ||
| 763 | |||
| 764 | /**
 | ||
| 765 |  * Test elementInexistent2 method
 | ||
| 766 |  *
 | ||
| 767 |  * @expectedException PHPUnit_Framework_Error_Notice
 | ||
| 768 |  * @return void
 | ||
| 769 |  */
 | ||
| 770 | public function testElementInexistent2() { | ||
| 771 | $this->View->element('TestPlugin.plugin_element', array(), array('plugin' => 'test_plugin')); | ||
| 772 | } | ||
| 773 | |||
| 774 | /**
 | ||
| 775 |  * Test elementInexistent3 method
 | ||
| 776 |  *
 | ||
| 777 |  * @expectedException PHPUnit_Framework_Error_Notice
 | ||
| 778 |  * @return void
 | ||
| 779 |  */
 | ||
| 780 | public function testElementInexistent3() { | ||
| 781 | $this->View->element('test_plugin.plugin_element'); | ||
| 782 | } | ||
| 783 | |||
| 784 | /**
 | ||
| 785 |  * Test that elements can have callbacks
 | ||
| 786 |  *
 | ||
| 787 |  * @return void
 | ||
| 788 |  */
 | ||
| 789 | public function testElementCallbacks() { | ||
| 790 | $Helper = $this->getMock('Helper', array(), array($this->View), 'ElementCallbackMockHtmlHelper'); | ||
| 791 | $this->View->helpers = array('ElementCallbackMockHtml'); | ||
| 792 | $this->View->loadHelpers(); | ||
| 793 | |||
| 794 | $this->View->Helpers->set('ElementCallbackMockHtml', $Helper); | ||
| 795 | $this->View->ElementCallbackMockHtml = $Helper; | ||
| 796 | |||
| 797 | $this->View->ElementCallbackMockHtml->expects($this->at(0))->method('beforeRender'); | ||
| 798 | $this->View->ElementCallbackMockHtml->expects($this->at(1))->method('afterRender'); | ||
| 799 | |||
| 800 | $this->View->element('test_element', array(), array('callbacks' => true)); | ||
| 801 | } | ||
| 802 | |||
| 803 | /**
 | ||
| 804 |  * Test that additional element viewVars don't get overwritten with helpers.
 | ||
| 805 |  *
 | ||
| 806 |  * @return void
 | ||
| 807 |  */
 | ||
| 808 | public function testElementParamsDontOverwriteHelpers() { | ||
| 809 | $Controller = new ViewPostsController(); | ||
| 810 | $Controller->helpers = array('Form'); | ||
| 811 | |||
| 812 | $View = new View($Controller); | ||
| 813 | $result = $View->element('type_check', array('form' => 'string'), array('callbacks' => true)); | ||
| 814 | $this->assertEquals('string', $result); | ||
| 815 | |||
| 816 | $View->set('form', 'string'); | ||
| 817 | $result = $View->element('type_check', array(), array('callbacks' => true)); | ||
| 818 | $this->assertEquals('string', $result); | ||
| 819 | } | ||
| 820 | |||
| 821 | /**
 | ||
| 822 |  * Test elementCacheHelperNoCache method
 | ||
| 823 |  *
 | ||
| 824 |  * @return void
 | ||
| 825 |  */
 | ||
| 826 | public function testElementCacheHelperNoCache() { | ||
| 827 | $Controller = new ViewPostsController(); | ||
| 828 | $View = new TestView($Controller); | ||
| 829 |                 $View->loadHelpers();
 | ||
| 830 | $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar'))); | ||
| 831 | $this->assertEquals('this is the test element', $result); | ||
| 832 | } | ||
| 833 | |||
| 834 | /**
 | ||
| 835 |  * Test elementCache method
 | ||
| 836 |  *
 | ||
| 837 |  * @return void
 | ||
| 838 |  */
 | ||
| 839 | public function testElementCache() { | ||
| 840 | Cache::drop('test_view'); | ||
| 841 | Cache::config('test_view', array( | ||
| 842 | 'engine' => 'File', | ||
| 843 | 'duration' => '+1 day', | ||
| 844 | 'path' => CACHE . 'views' . DS, | ||
| 845 | 'prefix' => '' | ||
| 846 | )); | ||
| 847 | Cache::clear(true, 'test_view'); | ||
| 848 | |||
| 849 | $View = new TestView($this->PostsController); | ||
| 850 | $View->elementCache = 'test_view'; | ||
| 851 | |||
| 852 | $result = $View->element('test_element', array(), array('cache' => true)); | ||
| 853 | $expected = 'this is the test element'; | ||
| 854 | $this->assertEquals($expected, $result); | ||
| 855 | |||
| 856 | $result = Cache::read('element__test_element_cache_callbacks', 'test_view'); | ||
| 857 | $this->assertEquals($expected, $result); | ||
| 858 | |||
| 859 | $result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true)); | ||
| 860 | $this->assertEquals($expected, $result); | ||
| 861 | |||
| 862 | $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view'); | ||
| 863 | $this->assertEquals($expected, $result); | ||
| 864 | |||
| 865 | $View->element('test_element', array( | ||
| 866 | 'param' => 'one', | ||
| 867 | 'foo' => 'two' | ||
| 868 |                 ), array(
 | ||
| 869 | 'cache' => array('key' => 'custom_key') | ||
| 870 | )); | ||
| 871 | $result = Cache::read('element_custom_key', 'test_view'); | ||
| 872 | $this->assertEquals($expected, $result); | ||
| 873 | |||
| 874 | $View->elementCache = 'default'; | ||
| 875 | $View->element('test_element', array( | ||
| 876 | 'param' => 'one', | ||
| 877 | 'foo' => 'two' | ||
| 878 |                 ), array(
 | ||
| 879 | 'cache' => array('config' => 'test_view'), | ||
| 880 | )); | ||
| 881 | $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view'); | ||
| 882 | $this->assertEquals($expected, $result); | ||
| 883 | |||
| 884 | Cache::clear(true, 'test_view'); | ||
| 885 | Cache::drop('test_view'); | ||
| 886 | } | ||
| 887 | |||
| 888 | /**
 | ||
| 889 |  * Test element events
 | ||
| 890 |  *
 | ||
| 891 |  * @return void
 | ||
| 892 |  */
 | ||
| 893 | public function testViewEvent() { | ||
| 894 | $View = new View($this->PostsController); | ||
| 895 | $View->autoLayout = false; | ||
| 896 | $listener = new TestViewEventListener(); | ||
| 897 | |||
| 898 | $View->getEventManager()->attach($listener); | ||
| 899 | |||
| 900 | $View->render('index'); | ||
| 901 | $this->assertEquals(View::TYPE_VIEW, $listener->beforeRenderViewType); | ||
| 902 | $this->assertEquals(View::TYPE_VIEW, $listener->afterRenderViewType); | ||
| 903 | |||
| 904 | $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW); | ||
| 905 | $View->element('test_element', array(), array('callbacks' => true)); | ||
| 906 | $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW); | ||
| 907 | |||
| 908 | $this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType); | ||
| 909 | $this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType); | ||
| 910 | } | ||
| 911 | |||
| 912 | /**
 | ||
| 913 |  * Test __get allowing access to helpers.
 | ||
| 914 |  *
 | ||
| 915 |  * @return void
 | ||
| 916 |  */
 | ||
| 917 | public function testMagicGet() { | ||
| 918 | $View = new View($this->PostsController); | ||
| 919 | $View->loadHelper('Html'); | ||
| 920 | $this->assertInstanceOf('HtmlHelper', $View->Html); | ||
| 921 | } | ||
| 922 | |||
| 923 | /**
 | ||
| 924 |  * Test that ctp is used as a fallback file extension for elements
 | ||
| 925 |  *
 | ||
| 926 |  * @return void
 | ||
| 927 |  */
 | ||
| 928 | public function testElementCtpFallback() { | ||
| 929 | $View = new TestView($this->PostsController); | ||
| 930 | $View->ext = '.missing'; | ||
| 931 | $element = 'test_element'; | ||
| 932 | $expected = 'this is the test element'; | ||
| 933 | $result = $View->element($element); | ||
| 934 | |||
| 935 | $this->assertEquals($expected, $result); | ||
| 936 | } | ||
| 937 | |||
| 938 | /**
 | ||
| 939 |  * Test loadHelpers method
 | ||
| 940 |  *
 | ||
| 941 |  * @return void
 | ||
| 942 |  */
 | ||
| 943 | public function testLoadHelpers() { | ||
| 944 | $View = new View($this->PostsController); | ||
| 945 | |||
| 946 | $View->helpers = array('Html', 'Form'); | ||
| 947 |                 $View->loadHelpers();
 | ||
| 948 | |||
| 949 | $this->assertInstanceOf('HtmlHelper', $View->Html, 'Object type is wrong.'); | ||
| 950 | $this->assertInstanceOf('FormHelper', $View->Form, 'Object type is wrong.'); | ||
| 951 | } | ||
| 952 | |||
| 953 | /**
 | ||
| 954 |  * Test lazy loading helpers
 | ||
| 955 |  *
 | ||
| 956 |  * @return void
 | ||
| 957 |  */
 | ||
| 958 | public function testLazyLoadHelpers() { | ||
| 959 | $View = new View($this->PostsController); | ||
| 960 | |||
| 961 | $View->helpers = array(); | ||
| 962 | $this->assertInstanceOf('HtmlHelper', $View->Html, 'Object type is wrong.'); | ||
| 963 | $this->assertInstanceOf('FormHelper', $View->Form, 'Object type is wrong.'); | ||
| 964 | } | ||
| 965 | |||
| 966 | /**
 | ||
| 967 |  * Test the correct triggering of helper callbacks
 | ||
| 968 |  *
 | ||
| 969 |  * @return void
 | ||
| 970 |  */
 | ||
| 971 | public function testHelperCallbackTriggering() { | ||
| 972 | $View = new View($this->PostsController); | ||
| 973 | $View->helpers = array(); | ||
| 974 | $View->Helpers = $this->getMock('HelperCollection', array('trigger'), array($View)); | ||
| 975 | |||
| 976 | $View->Helpers->expects($this->at(0))->method('trigger') | ||
| 977 | ->with( | ||
| 978 |                                 $this->logicalAnd(
 | ||
| 979 | $this->isInstanceOf('CakeEvent'), | ||
| 980 | $this->attributeEqualTo('_name', 'View.beforeRender'), | ||
| 981 | $this->attributeEqualTo('_subject', $View) | ||
| 982 | ) | ||
| 983 | ); | ||
| 984 | $View->Helpers->expects($this->at(1))->method('trigger') | ||
| 985 | ->with( | ||
| 986 |                                 $this->logicalAnd(
 | ||
| 987 | $this->isInstanceOf('CakeEvent'), | ||
| 988 | $this->attributeEqualTo('_name', 'View.beforeRenderFile'), | ||
| 989 | $this->attributeEqualTo('_subject', $View) | ||
| 990 | ) | ||
| 991 | ); | ||
| 992 | |||
| 993 | $View->Helpers->expects($this->at(2))->method('trigger') | ||
| 994 | ->with( | ||
| 995 |                                 $this->logicalAnd(
 | ||
| 996 | $this->isInstanceOf('CakeEvent'), | ||
| 997 | $this->attributeEqualTo('_name', 'View.afterRenderFile'), | ||
| 998 | $this->attributeEqualTo('_subject', $View) | ||
| 999 | ) | ||
| 1000 | ); | ||
| 1001 | $View->Helpers->expects($this->at(3))->method('trigger') | ||
| 1002 | ->with( | ||
| 1003 |                                 $this->logicalAnd(
 | ||
| 1004 | $this->isInstanceOf('CakeEvent'), | ||
| 1005 | $this->attributeEqualTo('_name', 'View.afterRender'), | ||
| 1006 | $this->attributeEqualTo('_subject', $View) | ||
| 1007 | ) | ||
| 1008 | ); | ||
| 1009 | |||
| 1010 | $View->Helpers->expects($this->at(4))->method('trigger') | ||
| 1011 | ->with( | ||
| 1012 |                                 $this->logicalAnd(
 | ||
| 1013 | $this->isInstanceOf('CakeEvent'), | ||
| 1014 | $this->attributeEqualTo('_name', 'View.beforeLayout'), | ||
| 1015 | $this->attributeEqualTo('_subject', $View) | ||
| 1016 | ) | ||
| 1017 | ); | ||
| 1018 | |||
| 1019 | $View->Helpers->expects($this->at(5))->method('trigger') | ||
| 1020 | ->with( | ||
| 1021 |                                 $this->logicalAnd(
 | ||
| 1022 | $this->isInstanceOf('CakeEvent'), | ||
| 1023 | $this->attributeEqualTo('_name', 'View.beforeRenderFile'), | ||
| 1024 | $this->attributeEqualTo('_subject', $View) | ||
| 1025 | ) | ||
| 1026 | ); | ||
| 1027 | |||
| 1028 | $View->Helpers->expects($this->at(6))->method('trigger') | ||
| 1029 | ->with( | ||
| 1030 |                                 $this->logicalAnd(
 | ||
| 1031 | $this->isInstanceOf('CakeEvent'), | ||
| 1032 | $this->attributeEqualTo('_name', 'View.afterRenderFile'), | ||
| 1033 | $this->attributeEqualTo('_subject', $View) | ||
| 1034 | ) | ||
| 1035 | ); | ||
| 1036 | |||
| 1037 | $View->Helpers->expects($this->at(7))->method('trigger') | ||
| 1038 | ->with( | ||
| 1039 |                                 $this->logicalAnd(
 | ||
| 1040 | $this->isInstanceOf('CakeEvent'), | ||
| 1041 | $this->attributeEqualTo('_name', 'View.afterLayout'), | ||
| 1042 | $this->attributeEqualTo('_subject', $View) | ||
| 1043 | ) | ||
| 1044 | ); | ||
| 1045 | |||
| 1046 | $View->render('index'); | ||
| 1047 | } | ||
| 1048 | |||
| 1049 | /**
 | ||
| 1050 |  * Test beforeLayout method
 | ||
| 1051 |  *
 | ||
| 1052 |  * @return void
 | ||
| 1053 |  */
 | ||
| 1054 | public function testBeforeLayout() { | ||
| 1055 | $this->PostsController->helpers = array('Session', 'TestBeforeAfter', 'Html'); | ||
| 1056 | $View = new View($this->PostsController); | ||
| 1057 | $View->render('index'); | ||
| 1058 | $this->assertEquals('Valuation', $View->Helpers->TestBeforeAfter->property); | ||
| 1059 | } | ||
| 1060 | |||
| 1061 | /**
 | ||
| 1062 |  * Test afterLayout method
 | ||
| 1063 |  *
 | ||
| 1064 |  * @return void
 | ||
| 1065 |  */
 | ||
| 1066 | public function testAfterLayout() { | ||
| 1067 | $this->PostsController->helpers = array('Session', 'TestBeforeAfter', 'Html'); | ||
| 1068 | $this->PostsController->set('variable', 'values'); | ||
| 1069 | |||
| 1070 | $View = new View($this->PostsController); | ||
| 1071 | ClassRegistry::addObject('afterView', $View); | ||
| 1072 | |||
| 1073 | $content = 'This is my view output'; | ||
| 1074 | $result = $View->renderLayout($content, 'default'); | ||
| 1075 | $this->assertRegExp('/modified in the afterlife/', $result); | ||
| 1076 | $this->assertRegExp('/This is my view output/', $result); | ||
| 1077 | } | ||
| 1078 | |||
| 1079 | /**
 | ||
| 1080 |  * Test renderLoadHelper method
 | ||
| 1081 |  *
 | ||
| 1082 |  * @return void
 | ||
| 1083 |  */
 | ||
| 1084 | public function testRenderLoadHelper() { | ||
| 1085 | $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number'); | ||
| 1086 | $View = new TestView($this->PostsController); | ||
| 1087 | |||
| 1088 | $result = $View->render('index', false); | ||
| 1089 | $this->assertEquals('posts index', $result); | ||
| 1090 | |||
| 1091 | $attached = $View->Helpers->loaded(); | ||
| 1092 | $this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached); | ||
| 1093 | |||
| 1094 | $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper'); | ||
| 1095 | $View = new TestView($this->PostsController); | ||
| 1096 | |||
| 1097 | $result = $View->render('index', false); | ||
| 1098 | $this->assertEquals('posts index', $result); | ||
| 1099 | |||
| 1100 | $attached = $View->Helpers->loaded(); | ||
| 1101 | $expected = array('Html', 'Form', 'Number', 'PluggedHelper'); | ||
| 1102 | $this->assertEquals($expected, $attached, 'Attached helpers are wrong.'); | ||
| 1103 | } | ||
| 1104 | |||
| 1105 | /**
 | ||
| 1106 |  * Test render method
 | ||
| 1107 |  *
 | ||
| 1108 |  * @return void
 | ||
| 1109 |  */
 | ||
| 1110 | public function testRender() { | ||
| 1111 | $View = new TestView($this->PostsController); | ||
| 1112 | $result = $View->render('index'); | ||
| 1113 | |||
| 1114 | $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result); | ||
| 1115 | $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result); | ||
| 1116 | $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result); | ||
| 1117 | |||
| 1118 | $this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var'); | ||
| 1119 | $this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var'); | ||
| 1120 | |||
| 1121 | $this->PostsController->set('url', 'flash'); | ||
| 1122 | $this->PostsController->set('message', 'yo what up'); | ||
| 1123 | $this->PostsController->set('pause', 3); | ||
| 1124 | $this->PostsController->set('pageTitle', 'yo what up'); | ||
| 1125 | |||
| 1126 | $View = new TestView($this->PostsController); | ||
| 1127 | $result = $View->render(false, 'flash'); | ||
| 1128 | |||
| 1129 | $this->assertRegExp("/<title>yo what up<\/title>/", $result); | ||
| 1130 | $this->assertRegExp("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result); | ||
| 1131 | |||
| 1132 | $this->assertNull($View->render(false, 'flash')); | ||
| 1133 | |||
| 1134 | $this->PostsController->helpers = array('Session', 'Cache', 'Html'); | ||
| 1135 | $this->PostsController->constructClasses(); | ||
| 1136 | $this->PostsController->cacheAction = array('index' => 3600); | ||
| 1137 | $this->PostsController->request->params['action'] = 'index'; | ||
| 1138 | Configure::write('Cache.check', true); | ||
| 1139 | |||
| 1140 | $View = new TestView($this->PostsController); | ||
| 1141 | $result = $View->render('index'); | ||
| 1142 | |||
| 1143 | $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result); | ||
| 1144 | $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result); | ||
| 1145 | $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result); | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | /**
 | ||
| 1149 |  * Test that View::$view works
 | ||
| 1150 |  *
 | ||
| 1151 |  * @return void
 | ||
| 1152 |  */
 | ||
| 1153 | public function testRenderUsingViewProperty() { | ||
| 1154 | $this->PostsController->view = 'cache_form'; | ||
| 1155 | $View = new TestView($this->PostsController); | ||
| 1156 | |||
| 1157 | $this->assertEquals('cache_form', $View->view); | ||
| 1158 | $result = $View->render(); | ||
| 1159 | $this->assertRegExp('/Add User/', $result); | ||
| 1160 | } | ||
| 1161 | |||
| 1162 | /**
 | ||
| 1163 |  * Test render()ing a file in a subdir from a custom viewPath
 | ||
| 1164 |  * in a plugin.
 | ||
| 1165 |  *
 | ||
| 1166 |  * @return void
 | ||
| 1167 |  */
 | ||
| 1168 | public function testGetViewFileNameSubdirWithPluginAndViewPath() { | ||
| 1169 | $this->PostsController->plugin = 'TestPlugin'; | ||
| 1170 | $this->PostsController->viewPath = 'Elements'; | ||
| 1171 | $this->PostsController->name = 'Posts'; | ||
| 1172 | $View = new TestView($this->PostsController); | ||
| 1173 | |||
| 1174 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . | ||
| 1175 | DS . 'View' . DS . 'Elements' . DS . 'sub_dir' . DS . 'sub_element.ctp'; | ||
| 1176 | $this->assertEquals($expected, $View->getViewFileName('sub_dir/sub_element')); | ||
| 1177 | } | ||
| 1178 | |||
| 1179 | /**
 | ||
| 1180 |  * Test that view vars can replace the local helper variables
 | ||
| 1181 |  * and not overwrite the $this->Helper references
 | ||
| 1182 |  *
 | ||
| 1183 |  * @return void
 | ||
| 1184 |  */
 | ||
| 1185 | public function testViewVarOverwritingLocalHelperVar() { | ||
| 1186 | $Controller = new ViewPostsController(); | ||
| 1187 | $Controller->helpers = array('Session', 'Html'); | ||
| 1188 | $Controller->set('html', 'I am some test html'); | ||
| 1189 | $View = new View($Controller); | ||
| 1190 | $result = $View->render('helper_overwrite', false); | ||
| 1191 | |||
| 1192 | $this->assertRegExp('/I am some test html/', $result); | ||
| 1193 | $this->assertRegExp('/Test link/', $result); | ||
| 1194 | } | ||
| 1195 | |||
| 1196 | /**
 | ||
| 1197 |  * Test getViewFileName method
 | ||
| 1198 |  *
 | ||
| 1199 |  * @return void
 | ||
| 1200 |  */
 | ||
| 1201 | public function testViewFileName() { | ||
| 1202 | $View = new TestView($this->PostsController); | ||
| 1203 | |||
| 1204 | $result = $View->getViewFileName('index'); | ||
| 1205 | $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result); | ||
| 1206 | |||
| 1207 | $result = $View->getViewFileName('TestPlugin.index'); | ||
| 1208 | $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result); | ||
| 1209 | |||
| 1210 | $result = $View->getViewFileName('/Pages/home'); | ||
| 1211 | $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result); | ||
| 1212 | |||
| 1213 | $result = $View->getViewFileName('../Elements/test_element'); | ||
| 1214 | $this->assertRegExp('/Elements(\/|\\\)test_element.ctp/', $result); | ||
| 1215 | |||
| 1216 | $result = $View->getViewFileName('../Themed/TestTheme/Posts/index'); | ||
| 1217 | $this->assertRegExp('/Themed(\/|\\\)TestTheme(\/|\\\)Posts(\/|\\\)index.ctp/', $result); | ||
| 1218 | |||
| 1219 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'index.ctp'; | ||
| 1220 | $result = $View->getViewFileName('../Posts/index'); | ||
| 1221 | $this->assertEquals($expected, $result); | ||
| 1222 | } | ||
| 1223 | |||
| 1224 | /**
 | ||
| 1225 |  * Test renderCache method
 | ||
| 1226 |  *
 | ||
| 1227 |  * @return void
 | ||
| 1228 |  */
 | ||
| 1229 | public function testRenderCache() { | ||
| 1230 | $this->skipIf(!is_writable(CACHE . 'views' . DS), 'CACHE/views dir is not writable, cannot test renderCache.'); | ||
| 1231 | |||
| 1232 | $view = 'test_view'; | ||
| 1233 | $View = new View($this->PostsController); | ||
| 1234 | $path = CACHE . 'views' . DS . 'view_cache_' . $view; | ||
| 1235 | |||
| 1236 | $cacheText = '<!--cachetime:' . time() . '-->some cacheText'; | ||
| 1237 | $f = fopen($path, 'w+'); | ||
| 1238 | fwrite($f, $cacheText); | ||
| 1239 | fclose($f); | ||
| 1240 | |||
| 1241 | $result = $View->renderCache($path, '+1 second'); | ||
| 1242 | $this->assertFalse($result); | ||
| 1243 | if (file_exists($path)) { | ||
| 1244 | unlink($path); | ||
| 1245 | } | ||
| 1246 | |||
| 1247 | $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText'; | ||
| 1248 | $f = fopen($path, 'w+'); | ||
| 1249 | fwrite($f, $cacheText); | ||
| 1250 | fclose($f); | ||
| 1251 | $result = $View->renderCache($path, '+1 second'); | ||
| 1252 | |||
| 1253 | $this->assertRegExp('/^some cacheText/', $result); | ||
| 1254 | |||
| 1255 | if (file_exists($path)) { | ||
| 1256 | unlink($path); | ||
| 1257 | } | ||
| 1258 | } | ||
| 1259 | |||
| 1260 | /**
 | ||
| 1261 |  * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
 | ||
| 1262 |  *
 | ||
| 1263 |  * @return void
 | ||
| 1264 |  */
 | ||
| 1265 | public function testRenderStrippingNoCacheTagsOnlyCacheHelper() { | ||
| 1266 | Configure::write('Cache.check', false); | ||
| 1267 | $View = new View($this->PostsController); | ||
| 1268 | $View->set(array('superman' => 'clark', 'variable' => 'var')); | ||
| 1269 | $View->helpers = array('Html', 'Form', 'Cache'); | ||
| 1270 | $View->layout = 'cache_layout'; | ||
| 1271 | $result = $View->render('index'); | ||
| 1272 | $this->assertNotRegExp('/cake:nocache/', $result); | ||
| 1273 | } | ||
| 1274 | |||
| 1275 | /**
 | ||
| 1276 |  * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
 | ||
| 1277 |  *
 | ||
| 1278 |  * @return void
 | ||
| 1279 |  */
 | ||
| 1280 | public function testRenderStrippingNoCacheTagsOnlyCacheCheck() { | ||
| 1281 | Configure::write('Cache.check', true); | ||
| 1282 | $View = new View($this->PostsController); | ||
| 1283 | $View->set(array('superman' => 'clark', 'variable' => 'var')); | ||
| 1284 | $View->helpers = array('Html', 'Form'); | ||
| 1285 | $View->layout = 'cache_layout'; | ||
| 1286 | $result = $View->render('index'); | ||
| 1287 | $this->assertNotRegExp('/cake:nocache/', $result); | ||
| 1288 | } | ||
| 1289 | |||
| 1290 | /**
 | ||
| 1291 |  * testSet method
 | ||
| 1292 |  *
 | ||
| 1293 |  * @return void
 | ||
| 1294 |  */
 | ||
| 1295 | public function testSet() { | ||
| 1296 | $View = new TestView($this->PostsController); | ||
| 1297 | $View->viewVars = array(); | ||
| 1298 | $View->set('somekey', 'someValue'); | ||
| 1299 | $this->assertSame($View->viewVars, array('somekey' => 'someValue')); | ||
| 1300 | $this->assertSame($View->getVars(), array('somekey')); | ||
| 1301 | |||
| 1302 | $View->viewVars = array(); | ||
| 1303 | $keys = array('key1', 'key2'); | ||
| 1304 | $values = array('value1', 'value2'); | ||
| 1305 | $View->set($keys, $values); | ||
| 1306 | $this->assertSame($View->viewVars, array('key1' => 'value1', 'key2' => 'value2')); | ||
| 1307 | $this->assertSame($View->getVars(), array('key1', 'key2')); | ||
| 1308 | $this->assertSame($View->getVar('key1'), 'value1'); | ||
| 1309 | $this->assertNull($View->getVar('key3')); | ||
| 1310 | |||
| 1311 | $View->set(array('key3' => 'value3')); | ||
| 1312 | $this->assertSame($View->getVar('key3'), 'value3'); | ||
| 1313 | |||
| 1314 | $View->viewVars = array(); | ||
| 1315 | $View->set(array(3 => 'three', 4 => 'four')); | ||
| 1316 | $View->set(array(1 => 'one', 2 => 'two')); | ||
| 1317 | $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two'); | ||
| 1318 | $this->assertEquals($expected, $View->viewVars); | ||
| 1319 | } | ||
| 1320 | |||
| 1321 | /**
 | ||
| 1322 |  * testBadExt method
 | ||
| 1323 |  *
 | ||
| 1324 |  * @expectedException MissingViewException
 | ||
| 1325 |  * @return void
 | ||
| 1326 |  */
 | ||
| 1327 | public function testBadExt() { | ||
| 1328 | $this->PostsController->action = 'something'; | ||
| 1329 | $this->PostsController->ext = '.whatever'; | ||
| 1330 | |||
| 1331 | $View = new TestView($this->PostsController); | ||
| 1332 | $View->render('this_is_missing'); | ||
| 1333 | } | ||
| 1334 | |||
| 1335 | /**
 | ||
| 1336 |  * testAltExt method
 | ||
| 1337 |  *
 | ||
| 1338 |  * @return void
 | ||
| 1339 |  */
 | ||
| 1340 | public function testAltExt() { | ||
| 1341 | $this->PostsController->ext = '.alt'; | ||
| 1342 | $View = new TestView($this->PostsController); | ||
| 1343 | $result = $View->render('alt_ext', false); | ||
| 1344 | $this->assertEquals('alt ext', $result); | ||
| 1345 | } | ||
| 1346 | |||
| 1347 | /**
 | ||
| 1348 |  * testAltBadExt method
 | ||
| 1349 |  *
 | ||
| 1350 |  * @expectedException MissingViewException
 | ||
| 1351 |  * @return void
 | ||
| 1352 |  */
 | ||
| 1353 | public function testAltBadExt() { | ||
| 1354 | $View = new TestView($this->PostsController); | ||
| 1355 | $View->render('alt_ext'); | ||
| 1356 | } | ||
| 1357 | |||
| 1358 | /**
 | ||
| 1359 |  * Test creating a block with capturing output.
 | ||
| 1360 |  *
 | ||
| 1361 |  * @return void
 | ||
| 1362 |  */
 | ||
| 1363 | public function testBlockCapture() { | ||
| 1364 | $this->View->start('test'); | ||
| 1365 | echo 'Block content'; | ||
| 1366 | $this->View->end(); | ||
| 1367 | |||
| 1368 | $result = $this->View->fetch('test'); | ||
| 1369 | $this->assertEquals('Block content', $result); | ||
| 1370 | } | ||
| 1371 | |||
| 1372 | /**
 | ||
| 1373 |  * Test block with startIfEmpty
 | ||
| 1374 |  *
 | ||
| 1375 |  * @return void
 | ||
| 1376 |  */
 | ||
| 1377 | public function testBlockCaptureStartIfEmpty() { | ||
| 1378 | $this->View->startIfEmpty('test'); | ||
| 1379 | echo "Block content 1"; | ||
| 1380 | $this->View->end(); | ||
| 1381 | |||
| 1382 | $this->View->startIfEmpty('test'); | ||
| 1383 | echo "Block content 2"; | ||
| 1384 | $this->View->end(); | ||
| 1385 | |||
| 1386 | $result = $this->View->fetch('test'); | ||
| 1387 | $this->assertEquals('Block content 1', $result); | ||
| 1388 | } | ||
| 1389 | |||
| 1390 | /**
 | ||
| 1391 |  * Test block with startIfEmpty
 | ||
| 1392 |  *
 | ||
| 1393 |  * @return void
 | ||
| 1394 |  */
 | ||
| 1395 | public function testBlockCaptureStartStartIfEmpty() { | ||
| 1396 | $this->View->start('test'); | ||
| 1397 | echo "Block content 1"; | ||
| 1398 | $this->View->end(); | ||
| 1399 | |||
| 1400 | $this->View->startIfEmpty('test'); | ||
| 1401 | echo "Block content 2"; | ||
| 1402 | $this->View->end(); | ||
| 1403 | |||
| 1404 | $result = $this->View->fetch('test'); | ||
| 1405 | $this->assertEquals('Block content 1', $result); | ||
| 1406 | } | ||
| 1407 | |||
| 1408 | /**
 | ||
| 1409 |  * Test appending to a block with capturing output.
 | ||
| 1410 |  *
 | ||
| 1411 |  * @return void
 | ||
| 1412 |  */
 | ||
| 1413 | public function testBlockCaptureAppend() { | ||
| 1414 | $this->View->start('test'); | ||
| 1415 | echo 'Block'; | ||
| 1416 | $this->View->end(); | ||
| 1417 | |||
| 1418 | $this->View->append('test'); | ||
| 1419 | echo ' content'; | ||
| 1420 | $this->View->end(); | ||
| 1421 | |||
| 1422 | $result = $this->View->fetch('test'); | ||
| 1423 | $this->assertEquals('Block content', $result); | ||
| 1424 | } | ||
| 1425 | |||
| 1426 | /**
 | ||
| 1427 |  * Test setting a block's content.
 | ||
| 1428 |  *
 | ||
| 1429 |  * @return void
 | ||
| 1430 |  */
 | ||
| 1431 | public function testBlockSet() { | ||
| 1432 | $this->View->assign('test', 'Block content'); | ||
| 1433 | $result = $this->View->fetch('test'); | ||
| 1434 | $this->assertEquals('Block content', $result); | ||
| 1435 | } | ||
| 1436 | |||
| 1437 | /**
 | ||
| 1438 |  * Test resetting a block's content.
 | ||
| 1439 |  *
 | ||
| 1440 |  * @return void
 | ||
| 1441 |  */
 | ||
| 1442 | public function testBlockReset() { | ||
| 1443 | $this->View->assign('test', ''); | ||
| 1444 | $result = $this->View->fetch('test', 'This should not be returned'); | ||
| 1445 | $this->assertSame('', $result); | ||
| 1446 | } | ||
| 1447 | |||
| 1448 | /**
 | ||
| 1449 |  * Test checking a block's existance.
 | ||
| 1450 |  *
 | ||
| 1451 |  * @return void
 | ||
| 1452 |  */
 | ||
| 1453 | public function testBlockExist() { | ||
| 1454 | $this->assertFalse($this->View->exists('test')); | ||
| 1455 | $this->View->assign('test', 'Block content'); | ||
| 1456 | $this->assertTrue($this->View->exists('test')); | ||
| 1457 | } | ||
| 1458 | |||
| 1459 | /**
 | ||
| 1460 |  * Test setting a block's content to null
 | ||
| 1461 |  *
 | ||
| 1462 |  * @return void
 | ||
| 1463 |  * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
 | ||
| 1464 |  */
 | ||
| 1465 | public function testBlockSetNull() { | ||
| 1466 | $this->View->assign('testWithNull', null); | ||
| 1467 | $result = $this->View->fetch('testWithNull'); | ||
| 1468 | $this->assertSame('', $result); | ||
| 1469 | } | ||
| 1470 | |||
| 1471 | /**
 | ||
| 1472 |  * Test setting a block's content to an object with __toString magic method
 | ||
| 1473 |  *
 | ||
| 1474 |  * @return void
 | ||
| 1475 |  */
 | ||
| 1476 | public function testBlockSetObjectWithToString() { | ||
| 1477 | $objectWithToString = new TestObjectWithToString(); | ||
| 1478 | $this->View->assign('testWithObjectWithToString', $objectWithToString); | ||
| 1479 | $result = $this->View->fetch('testWithObjectWithToString'); | ||
| 1480 | $this->assertSame("I'm ObjectWithToString", $result); | ||
| 1481 | } | ||
| 1482 | |||
| 1483 | /**
 | ||
| 1484 |  * Test setting a block's content to an object without __toString magic method
 | ||
| 1485 |  *
 | ||
| 1486 |  * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
 | ||
| 1487 |  * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
 | ||
| 1488 |  *
 | ||
| 1489 |  * @expectedException PHPUnit_Framework_Error
 | ||
| 1490 |  * @return void
 | ||
| 1491 |  */
 | ||
| 1492 | public function testBlockSetObjectWithoutToString() { | ||
| 1493 | $objectWithToString = new TestObjectWithoutToString(); | ||
| 1494 | $this->View->assign('testWithObjectWithoutToString', $objectWithToString); | ||
| 1495 | } | ||
| 1496 | |||
| 1497 | /**
 | ||
| 1498 |  * Test setting a block's content to a decimal
 | ||
| 1499 |  *
 | ||
| 1500 |  * @return void
 | ||
| 1501 |  */
 | ||
| 1502 | public function testBlockSetDecimal() { | ||
| 1503 | $this->View->assign('testWithDecimal', 1.23456789); | ||
| 1504 | $result = $this->View->fetch('testWithDecimal'); | ||
| 1505 | $this->assertEquals('1.23456789', $result); | ||
| 1506 | } | ||
| 1507 | |||
| 1508 | /**
 | ||
| 1509 |  * Data provider for block related tests.
 | ||
| 1510 |  *
 | ||
| 1511 |  * @return array
 | ||
| 1512 |  */
 | ||
| 1513 | public static function blockValueProvider() { | ||
| 1514 | return array( | ||
| 1515 | 'string' => array('A string value'), | ||
| 1516 | 'null' => array(null), | ||
| 1517 | 'decimal' => array(1.23456), | ||
| 1518 | 'object with __toString' => array(new TestObjectWithToString()), | ||
| 1519 | ); | ||
| 1520 | } | ||
| 1521 | |||
| 1522 | /**
 | ||
| 1523 |  * Test appending to a block with append.
 | ||
| 1524 |  *
 | ||
| 1525 |  * @dataProvider blockValueProvider
 | ||
| 1526 |  * @return void
 | ||
| 1527 |  */
 | ||
| 1528 | public function testBlockAppend($value) { | ||
| 1529 | $this->View->assign('testBlock', 'Block'); | ||
| 1530 | $this->View->append('testBlock', $value); | ||
| 1531 | |||
| 1532 | $result = $this->View->fetch('testBlock'); | ||
| 1533 | $this->assertSame('Block' . $value, $result); | ||
| 1534 | } | ||
| 1535 | |||
| 1536 | /**
 | ||
| 1537 |  * Test appending an object without __toString magic method to a block with append.
 | ||
| 1538 |  *
 | ||
| 1539 |  * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
 | ||
| 1540 |  * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
 | ||
| 1541 |  *
 | ||
| 1542 |  * @expectedException PHPUnit_Framework_Error
 | ||
| 1543 |  * @return void
 | ||
| 1544 |  */
 | ||
| 1545 | public function testBlockAppendObjectWithoutToString() { | ||
| 1546 | $object = new TestObjectWithoutToString(); | ||
| 1547 | $this->View->assign('testBlock', 'Block '); | ||
| 1548 | $this->View->append('testBlock', $object); | ||
| 1549 | } | ||
| 1550 | |||
| 1551 | /**
 | ||
| 1552 |  * Test prepending to a block with prepend.
 | ||
| 1553 |  *
 | ||
| 1554 |  * @dataProvider blockValueProvider
 | ||
| 1555 |  * @return void
 | ||
| 1556 |  */
 | ||
| 1557 | public function testBlockPrepend($value) { | ||
| 1558 | $this->View->assign('test', 'Block'); | ||
| 1559 | $this->View->prepend('test', $value); | ||
| 1560 | |||
| 1561 | $result = $this->View->fetch('test'); | ||
| 1562 | $this->assertEquals($value . 'Block', $result); | ||
| 1563 | } | ||
| 1564 | |||
| 1565 | /**
 | ||
| 1566 |  * Test prepending an object without __toString magic method to a block with prepend.
 | ||
| 1567 |  *
 | ||
| 1568 |  * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
 | ||
| 1569 |  * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
 | ||
| 1570 |  *
 | ||
| 1571 |  * @expectedException PHPUnit_Framework_Error
 | ||
| 1572 |  * @return void
 | ||
| 1573 |  */
 | ||
| 1574 | public function testBlockPrependObjectWithoutToString() { | ||
| 1575 | $object = new TestObjectWithoutToString(); | ||
| 1576 | $this->View->assign('test', 'Block '); | ||
| 1577 | $this->View->prepend('test', $object); | ||
| 1578 | } | ||
| 1579 | |||
| 1580 | /**
 | ||
| 1581 |  * You should be able to append to undefined blocks.
 | ||
| 1582 |  *
 | ||
| 1583 |  * @return void
 | ||
| 1584 |  */
 | ||
| 1585 | public function testBlockAppendUndefined() { | ||
| 1586 | $this->View->append('test', 'Unknown'); | ||
| 1587 | $result = $this->View->fetch('test'); | ||
| 1588 | $this->assertEquals('Unknown', $result); | ||
| 1589 | } | ||
| 1590 | |||
| 1591 | /**
 | ||
| 1592 |  * You should be able to prepend to undefined blocks.
 | ||
| 1593 |  *
 | ||
| 1594 |  * @return void
 | ||
| 1595 |  */
 | ||
| 1596 | public function testBlockPrependUndefined() { | ||
| 1597 | $this->View->prepend('test', 'Unknown'); | ||
| 1598 | $result = $this->View->fetch('test'); | ||
| 1599 | $this->assertEquals('Unknown', $result); | ||
| 1600 | } | ||
| 1601 | |||
| 1602 | /**
 | ||
| 1603 |  * Test getting block names
 | ||
| 1604 |  *
 | ||
| 1605 |  * @return void
 | ||
| 1606 |  */
 | ||
| 1607 | public function testBlocks() { | ||
| 1608 | $this->View->append('test', 'one'); | ||
| 1609 | $this->View->assign('test1', 'one'); | ||
| 1610 | |||
| 1611 | $this->assertEquals(array('test', 'test1'), $this->View->blocks()); | ||
| 1612 | } | ||
| 1613 | |||
| 1614 | /**
 | ||
| 1615 |  * Test that blocks can be nested.
 | ||
| 1616 |  *
 | ||
| 1617 |  * @return void
 | ||
| 1618 |  */
 | ||
| 1619 | public function testNestedBlocks() { | ||
| 1620 | $this->View->start('first'); | ||
| 1621 | echo 'In first '; | ||
| 1622 | $this->View->start('second'); | ||
| 1623 | echo 'In second'; | ||
| 1624 | $this->View->end(); | ||
| 1625 | echo 'In first'; | ||
| 1626 | $this->View->end(); | ||
| 1627 | |||
| 1628 | $this->assertEquals('In first In first', $this->View->fetch('first')); | ||
| 1629 | $this->assertEquals('In second', $this->View->fetch('second')); | ||
| 1630 | } | ||
| 1631 | |||
| 1632 | /**
 | ||
| 1633 |  * Test that starting the same block twice throws an exception
 | ||
| 1634 |  *
 | ||
| 1635 |  * @expectedException CakeException
 | ||
| 1636 |  * @return void
 | ||
| 1637 |  */
 | ||
| 1638 | public function testStartBlocksTwice() { | ||
| 1639 | $this->View->start('first'); | ||
| 1640 | echo 'In first '; | ||
| 1641 | $this->View->start('second'); | ||
| 1642 | echo 'In second'; | ||
| 1643 | $this->View->start('first'); | ||
| 1644 | } | ||
| 1645 | |||
| 1646 | /**
 | ||
| 1647 |  * Test that an exception gets thrown when you leave a block open at the end
 | ||
| 1648 |  * of a view.
 | ||
| 1649 |  *
 | ||
| 1650 |  * @expectedException CakeException
 | ||
| 1651 |  * @return void
 | ||
| 1652 |  */
 | ||
| 1653 | public function testExceptionOnOpenBlock() { | ||
| 1654 | $this->View->render('open_block'); | ||
| 1655 | } | ||
| 1656 | |||
| 1657 | /**
 | ||
| 1658 |  * Test nested extended views.
 | ||
| 1659 |  *
 | ||
| 1660 |  * @return void
 | ||
| 1661 |  */
 | ||
| 1662 | public function testExtendNested() { | ||
| 1663 | $this->View->layout = false; | ||
| 1664 | $content = $this->View->render('nested_extends'); | ||
| 1665 | $expected = <<<TEXT | ||
| 1666 | This is the second parent.
 | ||
| 1667 | This is the first parent.
 | ||
| 1668 | This is the first template.
 | ||
| 1669 | Sidebar Content.
 | ||
| 1670 | TEXT;
 | ||
| 1671 | $this->assertEquals($expected, $content); | ||
| 1672 | } | ||
| 1673 | |||
| 1674 | /**
 | ||
| 1675 |  * Make sure that extending the current view with itself causes an exception
 | ||
| 1676 |  *
 | ||
| 1677 |  * @expectedException LogicException
 | ||
| 1678 |  * @return void
 | ||
| 1679 |  */
 | ||
| 1680 | public function testExtendSelf() { | ||
| 1681 | $this->View->layout = false; | ||
| 1682 | $this->View->render('extend_self'); | ||
| 1683 | } | ||
| 1684 | |||
| 1685 | /**
 | ||
| 1686 |  * Make sure that extending in a loop causes an exception
 | ||
| 1687 |  *
 | ||
| 1688 |  * @expectedException LogicException
 | ||
| 1689 |  * @return void
 | ||
| 1690 |  */
 | ||
| 1691 | public function testExtendLoop() { | ||
| 1692 | $this->View->layout = false; | ||
| 1693 | $this->View->render('extend_loop'); | ||
| 1694 | } | ||
| 1695 | |||
| 1696 | /**
 | ||
| 1697 |  * Test extend() in an element and a view.
 | ||
| 1698 |  *
 | ||
| 1699 |  * @return void
 | ||
| 1700 |  */
 | ||
| 1701 | public function testExtendElement() { | ||
| 1702 | $this->View->layout = false; | ||
| 1703 | $content = $this->View->render('extend_element'); | ||
| 1704 | $expected = <<<TEXT | ||
| 1705 | Parent View.
 | ||
| 1706 | View content.
 | ||
| 1707 | Parent Element.
 | ||
| 1708 | Element content.
 | ||
| 1709 | 
 | ||
| 1710 | TEXT;
 | ||
| 1711 | $this->assertEquals($expected, $content); | ||
| 1712 | } | ||
| 1713 | |||
| 1714 | /**
 | ||
| 1715 |  * Extending an element which doesn't exist should throw a missing view exception
 | ||
| 1716 |  *
 | ||
| 1717 |  * @expectedException LogicException
 | ||
| 1718 |  * @return void
 | ||
| 1719 |  */
 | ||
| 1720 | public function testExtendMissingElement() { | ||
| 1721 | $this->View->layout = false; | ||
| 1722 | $this->View->render('extend_missing_element'); | ||
| 1723 | } | ||
| 1724 | |||
| 1725 | /**
 | ||
| 1726 |  * Test extend() preceeded by an element()
 | ||
| 1727 |  *
 | ||
| 1728 |  * @return void
 | ||
| 1729 |  */
 | ||
| 1730 | public function testExtendWithElementBeforeExtend() { | ||
| 1731 | $this->View->layout = false; | ||
| 1732 | $result = $this->View->render('extend_with_element'); | ||
| 1733 | $expected = <<<TEXT | ||
| 1734 | Parent View.
 | ||
| 1735 | this is the test elementThe view
 | ||
| 1736 | 
 | ||
| 1737 | TEXT;
 | ||
| 1738 | $this->assertEquals($expected, $result); | ||
| 1739 | } | ||
| 1740 | |||
| 1741 | /**
 | ||
| 1742 |  * Test that setting arbitrary properties still works.
 | ||
| 1743 |  *
 | ||
| 1744 |  * @return void
 | ||
| 1745 |  */
 | ||
| 1746 | public function testPropertySettingMagicGet() { | ||
| 1747 | $this->assertFalse(isset($this->View->action)); | ||
| 1748 | $this->View->request->params['action'] = 'login'; | ||
| 1749 | $this->assertEquals('login', $this->View->action); | ||
| 1750 | $this->assertTrue(isset($this->View->action)); | ||
| 1751 | $this->assertTrue(!empty($this->View->action)); | ||
| 1752 | } | ||
| 1753 | |||
| 1754 | /**
 | ||
| 1755 |  * Test memory leaks that existed in _paths at one point.
 | ||
| 1756 |  *
 | ||
| 1757 |  * @return void
 | ||
| 1758 |  */
 | ||
| 1759 | public function testMemoryLeakInPaths() { | ||
| 1760 | $this->ThemeController->plugin = null; | ||
| 1761 | $this->ThemeController->name = 'Posts'; | ||
| 1762 | $this->ThemeController->viewPath = 'posts'; | ||
| 1763 | $this->ThemeController->layout = 'whatever'; | ||
| 1764 | $this->ThemeController->theme = 'TestTheme'; | ||
| 1765 | |||
| 1766 | $View = new View($this->ThemeController); | ||
| 1767 | $View->element('test_element'); | ||
| 1768 | |||
| 1769 |                 $start = memory_get_usage();
 | ||
| 1770 | for ($i = 0; $i < 10; $i++) { | ||
| 1771 | $View->element('test_element'); | ||
| 1772 | } | ||
| 1773 |                 $end = memory_get_usage();
 | ||
| 1774 | $this->assertLessThanOrEqual($start + 5000, $end); | ||
| 1775 | } | ||
| 1776 | |||
| 1777 | /**
 | ||
| 1778 |  * Tests that a view block uses default value when not assigned and uses assigned value when it is
 | ||
| 1779 |  *
 | ||
| 1780 |  * @return void
 | ||
| 1781 |  */
 | ||
| 1782 | public function testBlockDefaultValue() { | ||
| 1783 | $default = 'Default'; | ||
| 1784 | $result = $this->View->fetch('title', $default); | ||
| 1785 | $this->assertEquals($default, $result); | ||
| 1786 | |||
| 1787 | $expected = 'My Title'; | ||
| 1788 | $this->View->assign('title', $expected); | ||
| 1789 | $result = $this->View->fetch('title', $default); | ||
| 1790 | $this->assertEquals($expected, $result); | ||
| 1791 | } | ||
| 1792 | |||
| 1793 | /**
 | ||
| 1794 |  * Tests that a view variable uses default value when not assigned and uses assigned value when it is
 | ||
| 1795 |  *
 | ||
| 1796 |  * @return void
 | ||
| 1797 |  */
 | ||
| 1798 | public function testViewVarDefaultValue() { | ||
| 1799 | $default = 'Default'; | ||
| 1800 | $result = $this->View->get('title', $default); | ||
| 1801 | $this->assertEquals($default, $result); | ||
| 1802 | |||
| 1803 | $expected = 'Back to the Future'; | ||
| 1804 | $this->View->set('title', $expected); | ||
| 1805 | $result = $this->View->get('title', $default); | ||
| 1806 | $this->assertEquals($expected, $result); | ||
| 1807 | } | ||
| 1808 | } |