ViewTest.php 41 KB

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