PageRenderTime 74ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Test/Case/View/ViewTest.php

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