PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/TestCase/View/ViewTest.php

https://github.com/binondord/cakephp
PHP | 1891 lines | 1225 code | 199 blank | 467 comment | 1 complexity | 36afcd4f22c8f2c4a75b4c07c7ce9aa3 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Cache\Cache;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  21. use Cake\Event\Event;
  22. use Cake\Event\EventListenerInterface;
  23. use Cake\Network\Request;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\TestCase;
  26. use Cake\View\Helper;
  27. use Cake\View\View;
  28. use TestApp\View\AppView;
  29. /**
  30. * ViewPostsController class
  31. *
  32. */
  33. class ViewPostsController extends Controller
  34. {
  35. /**
  36. * name property
  37. *
  38. * @var string
  39. */
  40. public $name = 'Posts';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed
  45. */
  46. public $uses = null;
  47. /**
  48. * index method
  49. *
  50. * @return void
  51. */
  52. public function index()
  53. {
  54. $this->set([
  55. 'testData' => 'Some test data',
  56. 'test2' => 'more data',
  57. 'test3' => 'even more data',
  58. ]);
  59. }
  60. /**
  61. * nocache_tags_with_element method
  62. *
  63. * @return void
  64. */
  65. public function nocache_multiple_element()
  66. {
  67. $this->set('foo', 'this is foo var');
  68. $this->set('bar', 'this is bar var');
  69. }
  70. }
  71. /**
  72. * ThemePostsController class
  73. *
  74. */
  75. class ThemePostsController extends Controller
  76. {
  77. public $theme = null;
  78. /**
  79. * index method
  80. *
  81. * @return void
  82. */
  83. public function index()
  84. {
  85. $this->set('testData', 'Some test data');
  86. $test2 = 'more data';
  87. $test3 = 'even more data';
  88. $this->set(compact('test2', 'test3'));
  89. }
  90. }
  91. /**
  92. * TestView class
  93. *
  94. */
  95. class TestView extends AppView
  96. {
  97. public function initialize()
  98. {
  99. $this->loadHelper('Html', ['mykey' => 'myval']);
  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. {
  109. return $this->_getViewFileName($name);
  110. }
  111. /**
  112. * getLayoutFileName method
  113. *
  114. * @param string $name The name of the layout to find.
  115. * @return string Filename for layout file (.ctp).
  116. */
  117. public function getLayoutFileName($name = null)
  118. {
  119. return $this->_getLayoutFileName($name);
  120. }
  121. /**
  122. * paths method
  123. *
  124. * @param string $plugin Optional plugin name to scan for view files.
  125. * @param bool $cached Set to true to force a refresh of view paths.
  126. * @return array paths
  127. */
  128. public function paths($plugin = null, $cached = true)
  129. {
  130. return $this->_paths($plugin, $cached);
  131. }
  132. /**
  133. * Setter for extension.
  134. *
  135. * @param string $ext The extension
  136. * @return void
  137. */
  138. public function ext($ext)
  139. {
  140. $this->_ext = $ext;
  141. }
  142. }
  143. /**
  144. * TestBeforeAfterHelper class
  145. *
  146. */
  147. class TestBeforeAfterHelper extends Helper
  148. {
  149. /**
  150. * property property
  151. *
  152. * @var string
  153. */
  154. public $property = '';
  155. /**
  156. * beforeLayout method
  157. *
  158. * @param string $viewFile View file name.
  159. * @return void
  160. */
  161. public function beforeLayout($viewFile)
  162. {
  163. $this->property = 'Valuation';
  164. }
  165. /**
  166. * afterLayout method
  167. *
  168. * @param string $layoutFile Layout file name.
  169. * @return void
  170. */
  171. public function afterLayout($layoutFile)
  172. {
  173. $this->_View->append('content', 'modified in the afterlife');
  174. }
  175. }
  176. /**
  177. * Class TestObjectWithToString
  178. *
  179. * An object with the magic method __toString() for testing with view blocks.
  180. */
  181. class TestObjectWithToString
  182. {
  183. /**
  184. * Return string value.
  185. *
  186. * @return string
  187. */
  188. public function __toString()
  189. {
  190. return "I'm ObjectWithToString";
  191. }
  192. }
  193. /**
  194. * Class TestObjectWithoutToString
  195. *
  196. * An object without the magic method __toString() for testing with view blocks.
  197. */
  198. class TestObjectWithoutToString
  199. {
  200. }
  201. /**
  202. * Class TestViewEventListenerInterface
  203. *
  204. * An event listener to test cakePHP events
  205. */
  206. class TestViewEventListenerInterface implements EventListenerInterface
  207. {
  208. /**
  209. * type of view before rendering has occurred
  210. *
  211. * @var string
  212. */
  213. public $beforeRenderViewType;
  214. /**
  215. * type of view after rendering has occurred
  216. *
  217. * @var string
  218. */
  219. public $afterRenderViewType;
  220. /**
  221. * implementedEvents method
  222. *
  223. * @return array
  224. */
  225. public function implementedEvents()
  226. {
  227. return [
  228. 'View.beforeRender' => 'beforeRender',
  229. 'View.afterRender' => 'afterRender'
  230. ];
  231. }
  232. /**
  233. * beforeRender method
  234. *
  235. * @param \Cake\Event\Event $event the event being sent
  236. * @return void
  237. */
  238. public function beforeRender(Event $event)
  239. {
  240. $this->beforeRenderViewType = $event->subject()->getCurrentType();
  241. }
  242. /**
  243. * afterRender method
  244. *
  245. * @param \Cake\Event\Event $event the event being sent
  246. * @return void
  247. */
  248. public function afterRender(Event $event)
  249. {
  250. $this->afterRenderViewType = $event->subject()->getCurrentType();
  251. }
  252. }
  253. /**
  254. * ViewTest class
  255. *
  256. */
  257. class ViewTest extends TestCase
  258. {
  259. /**
  260. * Fixtures used in this test.
  261. *
  262. * @var array
  263. */
  264. public $fixtures = ['core.users', 'core.posts'];
  265. /**
  266. * setUp method
  267. *
  268. * @return void
  269. */
  270. public function setUp()
  271. {
  272. parent::setUp();
  273. $request = new Request();
  274. $this->Controller = new Controller($request);
  275. $this->PostsController = new ViewPostsController($request);
  276. $this->PostsController->viewPath = 'Posts';
  277. $this->PostsController->index();
  278. $this->View = $this->PostsController->createView();
  279. $themeRequest = new Request('posts/index');
  280. $this->ThemeController = new Controller($themeRequest);
  281. $this->ThemePostsController = new ThemePostsController($themeRequest);
  282. $this->ThemePostsController->viewPath = 'Posts';
  283. $this->ThemePostsController->index();
  284. $this->ThemeView = $this->ThemePostsController->createView();
  285. Plugin::load(['TestPlugin', 'PluginJs', 'TestTheme', 'Company/TestPluginThree']);
  286. Configure::write('debug', true);
  287. }
  288. /**
  289. * tearDown method
  290. *
  291. * @return void
  292. */
  293. public function tearDown()
  294. {
  295. parent::tearDown();
  296. Plugin::unload();
  297. unset($this->View);
  298. unset($this->PostsController);
  299. unset($this->Controller);
  300. unset($this->ThemeView);
  301. unset($this->ThemePostsController);
  302. unset($this->ThemeController);
  303. }
  304. /**
  305. * Test getViewFileName method
  306. *
  307. * @return void
  308. */
  309. public function testGetTemplate()
  310. {
  311. $request = $this->getMock('Cake\Network\Request');
  312. $response = $this->getMock('Cake\Network\Response');
  313. $viewOptions = [
  314. 'plugin' => null,
  315. 'name' => 'Pages',
  316. 'viewPath' => 'Pages'
  317. ];
  318. $request->action = 'display';
  319. $request->params['pass'] = ['home'];
  320. $ThemeView = new TestView(null, null, null, $viewOptions);
  321. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS . 'Template' . DS . 'Pages' . DS . 'index.ctp';
  322. $result = $ThemeView->getViewFileName('Company/TestPluginThree./Pages/index');
  323. $this->assertPathEquals($expected, $result);
  324. $ThemeView = new TestView(null, null, null, $viewOptions);
  325. $ThemeView->theme = 'TestTheme';
  326. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'home.ctp';
  327. $result = $ThemeView->getViewFileName('home');
  328. $this->assertPathEquals($expected, $result);
  329. $expected = Plugin::path('TestTheme') . 'src' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  330. $result = $ThemeView->getViewFileName('/Posts/index');
  331. $this->assertPathEquals($expected, $result);
  332. $expected = Plugin::path('TestTheme') . 'src' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  333. $result = $ThemeView->getLayoutFileName();
  334. $this->assertPathEquals($expected, $result);
  335. $ThemeView->layoutPath = 'rss';
  336. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'rss' . DS . 'default.ctp';
  337. $result = $ThemeView->getLayoutFileName();
  338. $this->assertPathEquals($expected, $result);
  339. $ThemeView->layoutPath = 'Email' . DS . 'html';
  340. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'Email' . DS . 'html' . DS . 'default.ctp';
  341. $result = $ThemeView->getLayoutFileName();
  342. $this->assertPathEquals($expected, $result);
  343. $ThemeView = new TestView(null, null, null, $viewOptions);
  344. $ThemeView->theme = 'Company/TestPluginThree';
  345. $expected = Plugin::path('Company/TestPluginThree') . 'src' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  346. $result = $ThemeView->getLayoutFileName();
  347. $this->assertPathEquals($expected, $result);
  348. }
  349. /**
  350. * Test getLayoutFileName method on plugin
  351. *
  352. * @return void
  353. */
  354. public function testPluginGetTemplate()
  355. {
  356. $viewOptions = ['plugin' => 'TestPlugin',
  357. 'name' => 'TestPlugin',
  358. 'viewPath' => 'Tests',
  359. 'view' => 'index'
  360. ];
  361. $View = new TestView(null, null, null, $viewOptions);
  362. $expected = Plugin::path('TestPlugin') . 'src' . DS . 'Template' . DS . 'Tests' . DS . 'index.ctp';
  363. $result = $View->getViewFileName('index');
  364. $this->assertEquals($expected, $result);
  365. $expected = Plugin::path('TestPlugin') . 'src' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  366. $result = $View->getLayoutFileName();
  367. $this->assertEquals($expected, $result);
  368. }
  369. /**
  370. * Test getViewFileName method on plugin
  371. *
  372. * @return void
  373. */
  374. public function testPluginThemedGetTemplate()
  375. {
  376. $viewOptions = ['plugin' => 'TestPlugin',
  377. 'name' => 'TestPlugin',
  378. 'viewPath' => 'Tests',
  379. 'view' => 'index',
  380. 'theme' => 'TestTheme'
  381. ];
  382. $ThemeView = new TestView(null, null, null, $viewOptions);
  383. $themePath = Plugin::path('TestTheme') . 'src' . DS . 'Template' . DS;
  384. $expected = $themePath . 'Plugin' . DS . 'TestPlugin' . DS . 'Tests' . DS . 'index.ctp';
  385. $result = $ThemeView->getViewFileName('index');
  386. $this->assertPathEquals($expected, $result);
  387. $expected = $themePath . 'Plugin' . DS . 'TestPlugin' . DS . 'Layout' . DS . 'plugin_default.ctp';
  388. $result = $ThemeView->getLayoutFileName('plugin_default');
  389. $this->assertPathEquals($expected, $result);
  390. $expected = $themePath . 'Layout' . DS . 'default.ctp';
  391. $result = $ThemeView->getLayoutFileName('default');
  392. $this->assertPathEquals($expected, $result);
  393. }
  394. /**
  395. * Test that plugin/$plugin_name is only appended to the paths it should be.
  396. *
  397. * @return void
  398. */
  399. public function testPathPluginGeneration()
  400. {
  401. $viewOptions = ['plugin' => 'TestPlugin',
  402. 'name' => 'TestPlugin',
  403. 'viewPath' => 'Tests',
  404. 'view' => 'index'
  405. ];
  406. $View = new TestView(null, null, null, $viewOptions);
  407. $paths = $View->paths();
  408. $expected = array_merge(App::path('Template'), App::core('Template'));
  409. $this->assertEquals($expected, $paths);
  410. $paths = $View->paths('TestPlugin');
  411. $pluginPath = Plugin::path('TestPlugin');
  412. $expected = [
  413. TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Plugin' . DS . 'TestPlugin' . DS,
  414. $pluginPath . 'src' . DS . 'Template' . DS,
  415. TEST_APP . 'TestApp' . DS . 'Template' . DS,
  416. CAKE . 'Template' . DS,
  417. ];
  418. $this->assertPathEquals($expected, $paths);
  419. }
  420. /**
  421. * Test that themed plugin paths are generated correctly.
  422. *
  423. * @return void
  424. */
  425. public function testPathThemedPluginGeneration()
  426. {
  427. $viewOptions = ['plugin' => 'TestPlugin',
  428. 'name' => 'TestPlugin',
  429. 'viewPath' => 'Tests',
  430. 'view' => 'index',
  431. 'theme' => 'TestTheme'
  432. ];
  433. $View = new TestView(null, null, null, $viewOptions);
  434. $paths = $View->paths('TestPlugin');
  435. $pluginPath = Plugin::path('TestPlugin');
  436. $themePath = Plugin::path('TestTheme');
  437. $expected = [
  438. $themePath . 'src' . DS . 'Template' . DS . 'Plugin' . DS . 'TestPlugin' . DS,
  439. $themePath . 'src' . DS . 'Template' . DS,
  440. TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Plugin' . DS . 'TestPlugin' . DS,
  441. $pluginPath . 'src' . DS . 'Template' . DS,
  442. TEST_APP . 'TestApp' . DS . 'Template' . DS,
  443. CAKE . 'Template' . DS,
  444. ];
  445. $this->assertPathEquals($expected, $paths);
  446. }
  447. /**
  448. * Test that CamelCase'd plugins still find their view files.
  449. *
  450. * @return void
  451. */
  452. public function testCamelCasePluginGetTemplate()
  453. {
  454. $viewOptions = ['plugin' => 'TestPlugin',
  455. 'name' => 'TestPlugin',
  456. 'viewPath' => 'Tests',
  457. 'view' => 'index'
  458. ];
  459. $View = new TestView(null, null, null, $viewOptions);
  460. $pluginPath = Plugin::path('TestPlugin');
  461. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS .
  462. 'Template' . DS . 'Tests' . DS . 'index.ctp';
  463. $result = $View->getViewFileName('index');
  464. $this->assertPathEquals($expected, $result);
  465. $expected = $pluginPath . 'src' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  466. $result = $View->getLayoutFileName();
  467. $this->assertPathEquals($expected, $result);
  468. }
  469. /**
  470. * Test getViewFileName method
  471. *
  472. * @return void
  473. */
  474. public function testGetViewFileNames()
  475. {
  476. $viewOptions = [
  477. 'plugin' => null,
  478. 'name' => 'Pages',
  479. 'viewPath' => 'Pages'
  480. ];
  481. $request = $this->getMock('Cake\Network\Request');
  482. $response = $this->getMock('Cake\Network\Response');
  483. $View = new TestView(null, null, null, $viewOptions);
  484. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'home.ctp';
  485. $result = $View->getViewFileName('home');
  486. $this->assertPathEquals($expected, $result);
  487. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  488. $result = $View->getViewFileName('/Posts/index');
  489. $this->assertPathEquals($expected, $result);
  490. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  491. $result = $View->getViewFileName('../Posts/index');
  492. $this->assertPathEquals($expected, $result);
  493. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'page.home.ctp';
  494. $result = $View->getViewFileName('page.home');
  495. $this->assertPathEquals($expected, $result, 'Should not ruin files with dots.');
  496. Plugin::load('TestPlugin');
  497. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'home.ctp';
  498. $result = $View->getViewFileName('TestPlugin.home');
  499. $this->assertPathEquals($expected, $result, 'Plugin is missing the view, cascade to app.');
  500. $View->viewPath = 'Tests';
  501. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS .
  502. 'Template' . DS . 'Tests' . DS . 'index.ctp';
  503. $result = $View->getViewFileName('TestPlugin.index');
  504. $this->assertPathEquals($expected, $result);
  505. }
  506. /**
  507. * Test that getViewFileName() protects against malicious directory traversal.
  508. *
  509. * @expectedException \InvalidArgumentException
  510. * @return void
  511. */
  512. public function testGetViewFileNameDirectoryTraversal()
  513. {
  514. $viewOptions = [
  515. 'plugin' => null,
  516. 'name' => 'Pages',
  517. 'viewPath' => 'Pages',
  518. ];
  519. $request = $this->getMock('Cake\Network\Request');
  520. $response = $this->getMock('Cake\Network\Response');
  521. $view = new TestView(null, null, null, $viewOptions);
  522. $view->ext('.php');
  523. $view->getViewFileName('../../../../bootstrap');
  524. }
  525. /**
  526. * Test getting layout filenames
  527. *
  528. * @return void
  529. */
  530. public function testGetLayoutFileName()
  531. {
  532. $viewOptions = ['plugin' => null,
  533. 'name' => 'Pages',
  534. 'viewPath' => 'Pages',
  535. 'action' => 'display'
  536. ];
  537. $View = new TestView(null, null, null, $viewOptions);
  538. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  539. $result = $View->getLayoutFileName();
  540. $this->assertPathEquals($expected, $result);
  541. $View->layoutPath = 'rss';
  542. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'rss' . DS . 'default.ctp';
  543. $result = $View->getLayoutFileName();
  544. $this->assertPathEquals($expected, $result);
  545. $View->layoutPath = 'Email' . DS . 'html';
  546. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'Email' . DS . 'html' . DS . 'default.ctp';
  547. $result = $View->getLayoutFileName();
  548. $this->assertPathEquals($expected, $result);
  549. }
  550. /**
  551. * Test getting layout filenames for plugins.
  552. *
  553. * @return void
  554. */
  555. public function testGetLayoutFileNamePlugin()
  556. {
  557. $viewOptions = ['plugin' => null,
  558. 'name' => 'Pages',
  559. 'viewPath' => 'Pages',
  560. 'action' => 'display'
  561. ];
  562. $View = new TestView(null, null, null, $viewOptions);
  563. Plugin::load('TestPlugin');
  564. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS .
  565. 'Template' . DS . 'Layout' . DS . 'default.ctp';
  566. $result = $View->getLayoutFileName('TestPlugin.default');
  567. $this->assertPathEquals($expected, $result);
  568. $View->plugin = 'TestPlugin';
  569. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS .
  570. 'Template' . DS . 'Layout' . DS . 'default.ctp';
  571. $result = $View->getLayoutFileName('default');
  572. $this->assertPathEquals($expected, $result);
  573. }
  574. /**
  575. * Test getting layout filenames for prefix
  576. *
  577. * @return void
  578. */
  579. public function testGetLayoutFileNamePrefix()
  580. {
  581. $View = new TestView();
  582. // Prefix specific layout
  583. $View->request->params['prefix'] = 'foo_prefix';
  584. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS .
  585. 'FooPrefix' . DS . 'Layout' . DS . 'default.ctp';
  586. $result = $View->getLayoutFileName();
  587. $this->assertPathEquals($expected, $result);
  588. $View->request->params['prefix'] = 'FooPrefix';
  589. $result = $View->getLayoutFileName();
  590. $this->assertPathEquals($expected, $result);
  591. // Nested prefix layout
  592. $View->request->params['prefix'] = 'foo_prefix/bar_prefix';
  593. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS .
  594. 'FooPrefix' . DS . 'BarPrefix' . DS . 'Layout' . DS . 'default.ctp';
  595. $result = $View->getLayoutFileName();
  596. $this->assertPathEquals($expected, $result);
  597. $View->request->params['prefix'] = 'foo_prefix/bar_prefix';
  598. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS .
  599. 'FooPrefix' . DS . 'Layout' . DS . 'nested_prefix_cascade.ctp';
  600. $result = $View->getLayoutFileName('nested_prefix_cascade');
  601. $this->assertPathEquals($expected, $result);
  602. // Fallback to app's layout
  603. $View->request->params['prefix'] = 'Admin';
  604. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS .
  605. 'Layout' . DS . 'default.ctp';
  606. $result = $View->getLayoutFileName();
  607. $this->assertPathEquals($expected, $result);
  608. }
  609. /**
  610. * Test that getLayoutFileName() protects against malicious directory traversal.
  611. *
  612. * @expectedException \InvalidArgumentException
  613. * @return void
  614. */
  615. public function testGetLayoutFileNameDirectoryTraversal()
  616. {
  617. $viewOptions = [
  618. 'plugin' => null,
  619. 'name' => 'Pages',
  620. 'viewPath' => 'Pages',
  621. ];
  622. $request = $this->getMock('Cake\Network\Request');
  623. $response = $this->getMock('Cake\Network\Response');
  624. $view = new TestView(null, null, null, $viewOptions);
  625. $view->ext('.php');
  626. $view->getLayoutFileName('../../../../bootstrap');
  627. }
  628. /**
  629. * Test for missing views
  630. *
  631. * @expectedException \Cake\View\Exception\MissingTemplateException
  632. * @return void
  633. */
  634. public function testMissingTemplate()
  635. {
  636. $viewOptions = ['plugin' => null,
  637. 'name' => 'Pages',
  638. 'viewPath' => 'Pages'
  639. ];
  640. $request = $this->getMock('Cake\Network\Request');
  641. $response = $this->getMock('Cake\Network\Response');
  642. $View = new TestView($request, $response, null, $viewOptions);
  643. $View->getViewFileName('does_not_exist');
  644. }
  645. /**
  646. * Test for missing layouts
  647. *
  648. * @expectedException \Cake\View\Exception\MissingLayoutException
  649. * @return void
  650. */
  651. public function testMissingLayout()
  652. {
  653. $viewOptions = ['plugin' => null,
  654. 'name' => 'Pages',
  655. 'viewPath' => 'Pages',
  656. 'layout' => 'whatever'
  657. ];
  658. $View = new TestView(null, null, null, $viewOptions);
  659. $View->getLayoutFileName();
  660. }
  661. /**
  662. * Test viewVars method
  663. *
  664. * @return void
  665. */
  666. public function testViewVars()
  667. {
  668. $this->assertEquals(['testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data'], $this->View->viewVars);
  669. }
  670. /**
  671. * Test generation of UUIDs method
  672. *
  673. * @return void
  674. */
  675. public function testUUIDGeneration()
  676. {
  677. Router::connect('/:controller', ['action' => 'index']);
  678. $result = $this->View->uuid('form', ['controller' => 'posts', 'action' => 'index']);
  679. $this->assertEquals('form5988016017', $result);
  680. $result = $this->View->uuid('form', ['controller' => 'posts', 'action' => 'index']);
  681. $this->assertEquals('formc3dc6be854', $result);
  682. $result = $this->View->uuid('form', ['controller' => 'posts', 'action' => 'index']);
  683. $this->assertEquals('form28f92cc87f', $result);
  684. }
  685. /**
  686. * Test elementExists method
  687. *
  688. * @return void
  689. */
  690. public function testElementExists()
  691. {
  692. $result = $this->View->elementExists('test_element');
  693. $this->assertTrue($result);
  694. $result = $this->View->elementExists('TestPlugin.plugin_element');
  695. $this->assertTrue($result);
  696. $result = $this->View->elementExists('non_existent_element');
  697. $this->assertFalse($result);
  698. $result = $this->View->elementExists('TestPlugin.element');
  699. $this->assertFalse($result);
  700. $this->View->plugin = 'TestPlugin';
  701. $result = $this->View->elementExists('test_plugin_element');
  702. $this->assertTrue($result);
  703. }
  704. /**
  705. * Test element method
  706. *
  707. * @return void
  708. */
  709. public function testElement()
  710. {
  711. $result = $this->View->element('test_element');
  712. $this->assertEquals('this is the test element', $result);
  713. $result = $this->View->element('TestPlugin.plugin_element');
  714. $this->assertEquals('this is the plugin element using params[plugin]', $result);
  715. $this->View->plugin = 'TestPlugin';
  716. $result = $this->View->element('test_plugin_element');
  717. $this->assertEquals('this is the test set using View::$plugin plugin element', $result);
  718. }
  719. /**
  720. * Test element method with a prefix
  721. *
  722. * @return void
  723. */
  724. public function testPrefixElement()
  725. {
  726. $this->View->request->params['prefix'] = 'Admin';
  727. $result = $this->View->element('prefix_element');
  728. $this->assertEquals('this is a prefixed test element', $result);
  729. $result = $this->View->element('TestPlugin.plugin_element');
  730. $this->assertEquals('this is the plugin prefixed element using params[plugin]', $result);
  731. $this->View->plugin = 'TestPlugin';
  732. $result = $this->View->element('test_plugin_element');
  733. $this->assertEquals('this is the test set using View::$plugin plugin prefixed element', $result);
  734. $this->View->request->params['prefix'] = 'FooPrefix/BarPrefix';
  735. $result = $this->View->element('prefix_element');
  736. $this->assertEquals('this is a nested prefixed test element', $result);
  737. $this->View->request->params['prefix'] = 'FooPrefix/BarPrefix';
  738. $result = $this->View->element('prefix_element_in_parent');
  739. $this->assertEquals('this is a nested prefixed test element in first level element', $result);
  740. }
  741. /**
  742. * Test elementInexistent method
  743. *
  744. * @expectedException \Cake\View\Exception\MissingElementException
  745. * @return void
  746. */
  747. public function testElementInexistent()
  748. {
  749. $this->View->element('non_existent_element');
  750. }
  751. /**
  752. * Test elementInexistent3 method
  753. *
  754. * @expectedException \Cake\View\Exception\MissingElementException
  755. * @return void
  756. */
  757. public function testElementInexistent3()
  758. {
  759. $this->View->element('test_plugin.plugin_element');
  760. }
  761. /**
  762. * Test that elements can have callbacks
  763. *
  764. * @return void
  765. */
  766. public function testElementCallbacks()
  767. {
  768. $count = 0;
  769. $callback = function ($event, $file) use (&$count) {
  770. $count++;
  771. };
  772. $events = $this->View->eventManager();
  773. $events->attach($callback, 'View.beforeRender');
  774. $events->attach($callback, 'View.afterRender');
  775. $this->View->element('test_element', [], ['callbacks' => true]);
  776. $this->assertEquals(2, $count);
  777. }
  778. /**
  779. * Test that additional element viewVars don't get overwritten with helpers.
  780. *
  781. * @return void
  782. */
  783. public function testElementParamsDontOverwriteHelpers()
  784. {
  785. $Controller = new ViewPostsController();
  786. $Controller->helpers = ['Form'];
  787. $View = $Controller->createView();
  788. $result = $View->element('type_check', ['form' => 'string'], ['callbacks' => true]);
  789. $this->assertEquals('string', $result);
  790. $View->set('form', 'string');
  791. $result = $View->element('type_check', [], ['callbacks' => true]);
  792. $this->assertEquals('string', $result);
  793. }
  794. /**
  795. * Test elementCacheHelperNoCache method
  796. *
  797. * @return void
  798. */
  799. public function testElementCacheHelperNoCache()
  800. {
  801. $Controller = new ViewPostsController();
  802. $View = $Controller->createView();
  803. $result = $View->element('test_element', ['ram' => 'val', 'test' => ['foo', 'bar']]);
  804. $this->assertEquals('this is the test element', $result);
  805. }
  806. /**
  807. * Test elementCache method
  808. *
  809. * @return void
  810. */
  811. public function testElementCache()
  812. {
  813. Cache::drop('test_view');
  814. Cache::config('test_view', [
  815. 'engine' => 'File',
  816. 'duration' => '+1 day',
  817. 'path' => CACHE . 'views/',
  818. 'prefix' => ''
  819. ]);
  820. Cache::clear(false, 'test_view');
  821. $View = $this->PostsController->createView();
  822. $View->elementCache = 'test_view';
  823. $result = $View->element('test_element', [], ['cache' => true]);
  824. $expected = 'this is the test element';
  825. $this->assertEquals($expected, $result);
  826. $result = Cache::read('element__test_element_cache_callbacks', 'test_view');
  827. $this->assertEquals($expected, $result);
  828. $result = $View->element('test_element', ['param' => 'one', 'foo' => 'two'], ['cache' => true]);
  829. $this->assertEquals($expected, $result);
  830. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  831. $this->assertEquals($expected, $result);
  832. $View->element('test_element', [
  833. 'param' => 'one',
  834. 'foo' => 'two'
  835. ], [
  836. 'cache' => ['key' => 'custom_key']
  837. ]);
  838. $result = Cache::read('element_custom_key', 'test_view');
  839. $this->assertEquals($expected, $result);
  840. $View->elementCache = 'default';
  841. $View->element('test_element', [
  842. 'param' => 'one',
  843. 'foo' => 'two'
  844. ], [
  845. 'cache' => ['config' => 'test_view'],
  846. ]);
  847. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  848. $this->assertEquals($expected, $result);
  849. Cache::clear(true, 'test_view');
  850. Cache::drop('test_view');
  851. }
  852. /**
  853. * Test element events
  854. *
  855. * @return void
  856. */
  857. public function testViewEvent()
  858. {
  859. $View = $this->PostsController->createView();
  860. $View->autoLayout = false;
  861. $listener = new TestViewEventListenerInterface();
  862. $View->eventManager()->attach($listener);
  863. $View->render('index');
  864. $this->assertEquals(View::TYPE_VIEW, $listener->beforeRenderViewType);
  865. $this->assertEquals(View::TYPE_VIEW, $listener->afterRenderViewType);
  866. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  867. $View->element('test_element', [], ['callbacks' => true]);
  868. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  869. $this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType);
  870. $this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType);
  871. }
  872. /**
  873. * Test loading helper using loadHelper().
  874. *
  875. * @return void
  876. */
  877. public function testLoadHelper()
  878. {
  879. $View = new View();
  880. $View->loadHelper('Html', ['foo' => 'bar']);
  881. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html);
  882. $config = $View->Html->config();
  883. $this->assertEquals('bar', $config['foo']);
  884. }
  885. /**
  886. * Test loading helper when duplicate.
  887. *
  888. * @return void
  889. */
  890. public function testLoadHelperDuplicate()
  891. {
  892. $View = new View();
  893. $this->assertNotEmpty($View->loadHelper('Html', ['foo' => 'bar']));
  894. try {
  895. $View->loadHelper('Html', ['test' => 'value']);
  896. $this->fail('No exception');
  897. } catch (\RuntimeException $e) {
  898. $this->assertContains('The "Html" alias has already been loaded', $e->getMessage());
  899. }
  900. }
  901. /**
  902. * Test loadHelpers method
  903. *
  904. * @return void
  905. */
  906. public function testLoadHelpers()
  907. {
  908. $View = new View();
  909. $View->helpers = ['Html' => ['foo' => 'bar'], 'Form' => ['foo' => 'baz']];
  910. $View->loadHelpers();
  911. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  912. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  913. $config = $View->Html->config();
  914. $this->assertEquals('bar', $config['foo']);
  915. $config = $View->Form->config();
  916. $this->assertEquals('baz', $config['foo']);
  917. }
  918. /**
  919. * Test lazy loading helpers
  920. *
  921. * @return void
  922. */
  923. public function testLazyLoadHelpers()
  924. {
  925. $View = new View();
  926. $View->helpers = [];
  927. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  928. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  929. }
  930. /**
  931. * Test manipulating class properties in initialize()
  932. *
  933. * @return void
  934. */
  935. public function testInitialize()
  936. {
  937. $View = new TestView();
  938. $config = $View->Html->config();
  939. $this->assertEquals('myval', $config['mykey']);
  940. }
  941. /**
  942. * Test the correct triggering of helper callbacks
  943. *
  944. * @return void
  945. */
  946. public function testHelperCallbackTriggering()
  947. {
  948. $View = $this->PostsController->createView();
  949. $manager = $this->getMock('Cake\Event\EventManager');
  950. $View->eventManager($manager);
  951. $manager->expects($this->at(0))->method('dispatch')
  952. ->with(
  953. $this->logicalAnd(
  954. $this->isInstanceOf('Cake\Event\Event'),
  955. $this->attributeEqualTo('_name', 'View.beforeRender'),
  956. $this->attributeEqualTo('_subject', $View)
  957. )
  958. );
  959. $manager->expects($this->at(1))->method('dispatch')
  960. ->with(
  961. $this->logicalAnd(
  962. $this->isInstanceOf('Cake\Event\Event'),
  963. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  964. $this->attributeEqualTo('_subject', $View)
  965. )
  966. );
  967. $manager->expects($this->at(2))->method('dispatch')
  968. ->with(
  969. $this->logicalAnd(
  970. $this->isInstanceOf('Cake\Event\Event'),
  971. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  972. $this->attributeEqualTo('_subject', $View)
  973. )
  974. );
  975. $manager->expects($this->at(3))->method('dispatch')
  976. ->with(
  977. $this->logicalAnd(
  978. $this->isInstanceOf('Cake\Event\Event'),
  979. $this->attributeEqualTo('_name', 'View.afterRender'),
  980. $this->attributeEqualTo('_subject', $View)
  981. )
  982. );
  983. $manager->expects($this->at(4))->method('dispatch')
  984. ->with(
  985. $this->logicalAnd(
  986. $this->isInstanceOf('Cake\Event\Event'),
  987. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  988. $this->attributeEqualTo('_subject', $View)
  989. )
  990. );
  991. $manager->expects($this->at(5))->method('dispatch')
  992. ->with(
  993. $this->logicalAnd(
  994. $this->isInstanceOf('Cake\Event\Event'),
  995. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  996. $this->attributeEqualTo('_subject', $View)
  997. )
  998. );
  999. $manager->expects($this->at(6))->method('dispatch')
  1000. ->with(
  1001. $this->logicalAnd(
  1002. $this->isInstanceOf('Cake\Event\Event'),
  1003. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  1004. $this->attributeEqualTo('_subject', $View)
  1005. )
  1006. );
  1007. $manager->expects($this->at(7))->method('dispatch')
  1008. ->with(
  1009. $this->logicalAnd(
  1010. $this->isInstanceOf('Cake\Event\Event'),
  1011. $this->attributeEqualTo('_name', 'View.afterLayout'),
  1012. $this->attributeEqualTo('_subject', $View)
  1013. )
  1014. );
  1015. $View->render('index');
  1016. }
  1017. /**
  1018. * Test beforeLayout method
  1019. *
  1020. * @return void
  1021. */
  1022. public function testBeforeLayout()
  1023. {
  1024. $this->PostsController->helpers = [
  1025. 'TestBeforeAfter' => ['className' => __NAMESPACE__ . '\TestBeforeAfterHelper'],
  1026. 'Html'
  1027. ];
  1028. $View = $this->PostsController->createView();
  1029. $View->render('index');
  1030. $this->assertEquals('Valuation', $View->helpers()->TestBeforeAfter->property);
  1031. }
  1032. /**
  1033. * Test afterLayout method
  1034. *
  1035. * @return void
  1036. */
  1037. public function testAfterLayout()
  1038. {
  1039. $this->PostsController->helpers = [
  1040. 'TestBeforeAfter' => ['className' => __NAMESPACE__ . '\TestBeforeAfterHelper'],
  1041. 'Html'
  1042. ];
  1043. $this->PostsController->set('variable', 'values');
  1044. $View = $this->PostsController->createView();
  1045. $content = 'This is my view output';
  1046. $result = $View->renderLayout($content, 'default');
  1047. $this->assertRegExp('/modified in the afterlife/', $result);
  1048. $this->assertRegExp('/This is my view output/', $result);
  1049. }
  1050. /**
  1051. * Test renderLoadHelper method
  1052. *
  1053. * @return void
  1054. */
  1055. public function testRenderLoadHelper()
  1056. {
  1057. $this->PostsController->helpers = ['Form', 'Number'];
  1058. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1059. $result = $View->render('index', false);
  1060. $this->assertEquals('posts index', $result);
  1061. $attached = $View->helpers()->loaded();
  1062. // HtmlHelper is loaded in TestView::initialize()
  1063. $this->assertEquals(['Html', 'Form', 'Number'], $attached);
  1064. $this->PostsController->helpers = ['Html', 'Form', 'Number', 'TestPlugin.PluggedHelper'];
  1065. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1066. $result = $View->render('index', false);
  1067. $this->assertEquals('posts index', $result);
  1068. $attached = $View->helpers()->loaded();
  1069. $expected = ['Html', 'Form', 'Number', 'PluggedHelper'];
  1070. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  1071. }
  1072. /**
  1073. * Test render method
  1074. *
  1075. * @return void
  1076. */
  1077. public function testRender()
  1078. {
  1079. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1080. $result = $View->render('index');
  1081. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\"\/>\s*<title>/", $result);
  1082. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1083. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1084. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1085. $result = $View->render(false, 'ajax2');
  1086. $this->assertRegExp('/Ajax\!/', $result);
  1087. $this->assertNull($View->render(false, 'ajax2'));
  1088. $this->PostsController->helpers = ['Html'];
  1089. $this->PostsController->request->params['action'] = 'index';
  1090. Configure::write('Cache.check', true);
  1091. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1092. $result = $View->render('index');
  1093. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\"\/>\s*<title>/", $result);
  1094. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1095. }
  1096. /**
  1097. * Test that View::$view works
  1098. *
  1099. * @return void
  1100. */
  1101. public function testRenderUsingViewProperty()
  1102. {
  1103. $this->PostsController->view = 'cache_form';
  1104. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1105. $this->assertEquals('cache_form', $View->view);
  1106. $result = $View->render();
  1107. $this->assertRegExp('/Add User/', $result);
  1108. }
  1109. /**
  1110. * Test render()ing a file in a subdir from a custom viewPath
  1111. * in a plugin.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testGetViewFileNameSubdirWithPluginAndViewPath()
  1116. {
  1117. $this->PostsController->plugin = 'TestPlugin';
  1118. $this->PostsController->name = 'Posts';
  1119. $this->PostsController->viewPath = 'Element';
  1120. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1121. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  1122. $result = $View->getViewFileName('sub_dir/sub_element');
  1123. $expected = $pluginPath . 'src' . DS . 'Template' . DS . 'Element' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  1124. $this->assertPathEquals($expected, $result);
  1125. }
  1126. /**
  1127. * Test that view vars can replace the local helper variables
  1128. * and not overwrite the $this->Helper references
  1129. *
  1130. * @return void
  1131. */
  1132. public function testViewVarOverwritingLocalHelperVar()
  1133. {
  1134. $Controller = new ViewPostsController();
  1135. $Controller->helpers = ['Html'];
  1136. $Controller->set('html', 'I am some test html');
  1137. $View = $Controller->createView();
  1138. $result = $View->render('helper_overwrite', false);
  1139. $this->assertRegExp('/I am some test html/', $result);
  1140. $this->assertRegExp('/Test link/', $result);
  1141. }
  1142. /**
  1143. * Test getViewFileName method
  1144. *
  1145. * @return void
  1146. */
  1147. public function testViewFileName()
  1148. {
  1149. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1150. $result = $View->getViewFileName('index');
  1151. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1152. $result = $View->getViewFileName('TestPlugin.index');
  1153. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1154. $result = $View->getViewFileName('/Pages/home');
  1155. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  1156. $result = $View->getViewFileName('../Element/test_element');
  1157. $this->assertRegExp('/Element(\/|\\\)test_element.ctp/', $result);
  1158. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  1159. $result = $View->getViewFileName('../Posts/index');
  1160. $this->assertPathEquals($expected, $result);
  1161. }
  1162. /**
  1163. * Test creating a block with capturing output.
  1164. *
  1165. * @return void
  1166. */
  1167. public function testBlockCaptureOverwrite()
  1168. {
  1169. $this->View->start('test');
  1170. echo 'Block content';
  1171. $this->View->end();
  1172. $this->View->start('test');
  1173. echo 'New content';
  1174. $this->View->end();
  1175. $result = $this->View->fetch('test');
  1176. $this->assertEquals('New content', $result);
  1177. }
  1178. /**
  1179. * Test that blocks can be fetched inside a block with the same name
  1180. *
  1181. * @return void
  1182. */
  1183. public function testBlockExtend()
  1184. {
  1185. $this->View->start('test');
  1186. echo 'Block content';
  1187. $this->View->end();
  1188. $this->View->start('test');
  1189. echo $this->View->fetch('test');
  1190. echo 'New content';
  1191. $this->View->end();
  1192. $result = $this->View->fetch('test');
  1193. $this->assertEquals('Block contentNew content', $result);
  1194. }
  1195. /**
  1196. * Test creating a block with capturing output.
  1197. *
  1198. * @return void
  1199. */
  1200. public function testBlockCapture()
  1201. {
  1202. $this->View->start('test');
  1203. echo 'Block content';
  1204. $this->View->end();
  1205. $result = $this->View->fetch('test');
  1206. $this->assertEquals('Block content', $result);
  1207. }
  1208. /**
  1209. * Test appending to a block with capturing output.
  1210. *
  1211. * @return void
  1212. */
  1213. public function testBlockAppendCapture()
  1214. {
  1215. $this->View->start('test');
  1216. echo 'Content ';
  1217. $this->View->end();
  1218. $this->View->append('test');
  1219. echo 'appended';
  1220. $this->View->end();
  1221. $result = $this->View->fetch('test');
  1222. $this->assertEquals('Content appended', $result);
  1223. }
  1224. /**
  1225. * Test setting a block's content.
  1226. *
  1227. * @return void
  1228. */
  1229. public function testBlockSet()
  1230. {
  1231. $this->View->assign('test', 'Block content');
  1232. $result = $this->View->fetch('test');
  1233. $this->assertEquals('Block content', $result);
  1234. }
  1235. /**
  1236. * Test resetting a block's content.
  1237. *
  1238. * @return void
  1239. */
  1240. public function testBlockReset()
  1241. {
  1242. $this->View->assign('test', '');
  1243. $result = $this->View->fetch('test', 'This should not be returned');
  1244. $this->assertSame('', $result);
  1245. }
  1246. /**
  1247. * Test checking a block's existance.
  1248. *
  1249. * @return void
  1250. */
  1251. public function testBlockExist()
  1252. {
  1253. $this->assertFalse($this->View->exists('test'));
  1254. $this->View->assign('test', 'Block content');
  1255. $this->assertTrue($this->View->exists('test'));
  1256. }
  1257. /**
  1258. * Test setting a block's content to null
  1259. *
  1260. * @return void
  1261. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
  1262. */
  1263. public function testBlockSetNull()
  1264. {
  1265. $this->View->assign('testWithNull', null);
  1266. $result = $this->View->fetch('testWithNull');
  1267. $this->assertSame('', $result);
  1268. }
  1269. /**
  1270. * Test setting a block's content to an object with __toString magic method
  1271. *
  1272. * @return void
  1273. */
  1274. public function testBlockSetObjectWithToString()
  1275. {
  1276. $objectWithToString = new TestObjectWithToString();
  1277. $this->View->assign('testWithObjectWithToString', $objectWithToString);
  1278. $result = $this->View->fetch('testWithObjectWithToString');
  1279. $this->assertSame("I'm ObjectWithToString", $result);
  1280. }
  1281. /**
  1282. * Test setting a block's content to an object without __toString magic method
  1283. *
  1284. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1285. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1286. *
  1287. * @expectedException \PHPUnit_Framework_Error
  1288. * @return void
  1289. */
  1290. public function testBlockSetObjectWithoutToString()
  1291. {
  1292. $objectWithToString = new TestObjectWithoutToString();
  1293. $this->View->assign('testWithObjectWithoutToString', $objectWithToString);
  1294. }
  1295. /**
  1296. * Test setting a block's content to a decimal
  1297. *
  1298. * @return void
  1299. */
  1300. public function testBlockSetDecimal()
  1301. {
  1302. $this->View->assign('testWithDecimal', 1.23456789);
  1303. $result = $this->View->fetch('testWithDecimal');
  1304. $this->assertEquals('1.23456789', $result);
  1305. }
  1306. /**
  1307. * Data provider for block related tests.
  1308. *
  1309. * @return array
  1310. */
  1311. public static function blockValueProvider()
  1312. {
  1313. return [
  1314. 'string' => ['A string value'],
  1315. 'decimal' => [1.23456],
  1316. 'object with __toString' => [new TestObjectWithToString()],
  1317. ];
  1318. }
  1319. /**
  1320. * Test appending to a block with append.
  1321. *
  1322. * @param mixed $value Value
  1323. * @return void
  1324. * @dataProvider blockValueProvider
  1325. */
  1326. public function testBlockAppend($value)
  1327. {
  1328. $this->View->assign('testBlock', 'Block');
  1329. $this->View->append('testBlock', $value);
  1330. $result = $this->View->fetch('testBlock');
  1331. $this->assertSame('Block' . $value, $result);
  1332. }
  1333. /**
  1334. * Test appending an object without __toString magic method to a block with append.
  1335. *
  1336. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1337. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1338. *
  1339. * @expectedException \PHPUnit_Framework_Error
  1340. * @return void
  1341. */
  1342. public function testBlockAppendObjectWithoutToString()
  1343. {
  1344. $object = new TestObjectWithoutToString();
  1345. $this->View->assign('testBlock', 'Block ');
  1346. $this->View->append('testBlock', $object);
  1347. }
  1348. /**
  1349. * Test prepending to a block with prepend.
  1350. *
  1351. * @param mixed $value Value
  1352. * @return void
  1353. * @dataProvider blockValueProvider
  1354. */
  1355. public function testBlockPrepend($value)
  1356. {
  1357. $this->View->assign('test', 'Block');
  1358. $this->View->prepend('test', $value);
  1359. $result = $this->View->fetch('test');
  1360. $this->assertEquals($value . 'Block', $result);
  1361. }
  1362. /**
  1363. * Test prepending an object without __toString magic method to a block with prepend.
  1364. *
  1365. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1366. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1367. *
  1368. * @expectedException \PHPUnit_Framework_Error
  1369. * @return void
  1370. */
  1371. public function testBlockPrependObjectWithoutToString()
  1372. {
  1373. $object = new TestObjectWithoutToString();
  1374. $this->View->assign('test', 'Block ');
  1375. $this->View->prepend('test', $object);
  1376. }
  1377. /**
  1378. * You should be able to append to undefined blocks.
  1379. *
  1380. * @return void
  1381. */
  1382. public function testBlockAppendUndefined()
  1383. {
  1384. $this->View->append('test', 'Unknown');
  1385. $result = $this->View->fetch('test');
  1386. $this->assertEquals('Unknown', $result);
  1387. }
  1388. /**
  1389. * You should be able to prepend to undefined blocks.
  1390. *
  1391. * @return void
  1392. */
  1393. public function testBlockPrependUndefined()
  1394. {
  1395. $this->View->prepend('test', 'Unknown');
  1396. $result = $this->View->fetch('test');
  1397. $this->assertEquals('Unknown', $result);
  1398. }
  1399. /**
  1400. * Test getting block names
  1401. *
  1402. * @return void
  1403. */
  1404. public function testBlocks()
  1405. {
  1406. $this->View->append('test', 'one');
  1407. $this->View->assign('test1', 'one');
  1408. $this->assertEquals(['test', 'test1'], $this->View->blocks());
  1409. }
  1410. /**
  1411. * Test that blocks can be nested.
  1412. *
  1413. * @return void
  1414. */
  1415. public function testNestedBlocks()
  1416. {
  1417. $this->View->start('first');
  1418. echo 'In first ';
  1419. $this->View->start('second');

Large files files are truncated, but you can click here to view the full file