ViewTest.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  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\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. // Fallback to app's layout
  598. $View->request->params['prefix'] = 'Admin';
  599. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS .
  600. 'Layout' . DS . 'default.ctp';
  601. $result = $View->getLayoutFileName();
  602. $this->assertPathEquals($expected, $result);
  603. }
  604. /**
  605. * Test that getLayoutFileName() protects against malicious directory traversal.
  606. *
  607. * @expectedException \InvalidArgumentException
  608. * @return void
  609. */
  610. public function testGetLayoutFileNameDirectoryTraversal()
  611. {
  612. $viewOptions = [
  613. 'plugin' => null,
  614. 'name' => 'Pages',
  615. 'viewPath' => 'Pages',
  616. ];
  617. $request = $this->getMock('Cake\Network\Request');
  618. $response = $this->getMock('Cake\Network\Response');
  619. $view = new TestView(null, null, null, $viewOptions);
  620. $view->ext('.php');
  621. $view->getLayoutFileName('../../../../bootstrap');
  622. }
  623. /**
  624. * Test for missing views
  625. *
  626. * @expectedException \Cake\View\Exception\MissingTemplateException
  627. * @return void
  628. */
  629. public function testMissingTemplate()
  630. {
  631. $viewOptions = ['plugin' => null,
  632. 'name' => 'Pages',
  633. 'viewPath' => 'Pages'
  634. ];
  635. $request = $this->getMock('Cake\Network\Request');
  636. $response = $this->getMock('Cake\Network\Response');
  637. $View = new TestView($request, $response, null, $viewOptions);
  638. $View->getViewFileName('does_not_exist');
  639. }
  640. /**
  641. * Test for missing layouts
  642. *
  643. * @expectedException \Cake\View\Exception\MissingLayoutException
  644. * @return void
  645. */
  646. public function testMissingLayout()
  647. {
  648. $viewOptions = ['plugin' => null,
  649. 'name' => 'Pages',
  650. 'viewPath' => 'Pages',
  651. 'layout' => 'whatever'
  652. ];
  653. $View = new TestView(null, null, null, $viewOptions);
  654. $View->getLayoutFileName();
  655. }
  656. /**
  657. * Test viewVars method
  658. *
  659. * @return void
  660. */
  661. public function testViewVars()
  662. {
  663. $this->assertEquals(['testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data'], $this->View->viewVars);
  664. }
  665. /**
  666. * Test generation of UUIDs method
  667. *
  668. * @return void
  669. */
  670. public function testUUIDGeneration()
  671. {
  672. Router::connect('/:controller', ['action' => 'index']);
  673. $result = $this->View->uuid('form', ['controller' => 'posts', 'action' => 'index']);
  674. $this->assertEquals('form5988016017', $result);
  675. $result = $this->View->uuid('form', ['controller' => 'posts', 'action' => 'index']);
  676. $this->assertEquals('formc3dc6be854', $result);
  677. $result = $this->View->uuid('form', ['controller' => 'posts', 'action' => 'index']);
  678. $this->assertEquals('form28f92cc87f', $result);
  679. }
  680. /**
  681. * Test elementExists method
  682. *
  683. * @return void
  684. */
  685. public function testElementExists()
  686. {
  687. $result = $this->View->elementExists('test_element');
  688. $this->assertTrue($result);
  689. $result = $this->View->elementExists('TestPlugin.plugin_element');
  690. $this->assertTrue($result);
  691. $result = $this->View->elementExists('non_existent_element');
  692. $this->assertFalse($result);
  693. $result = $this->View->elementExists('TestPlugin.element');
  694. $this->assertFalse($result);
  695. $this->View->plugin = 'TestPlugin';
  696. $result = $this->View->elementExists('test_plugin_element');
  697. $this->assertTrue($result);
  698. }
  699. /**
  700. * Test element method
  701. *
  702. * @return void
  703. */
  704. public function testElement()
  705. {
  706. $result = $this->View->element('test_element');
  707. $this->assertEquals('this is the test element', $result);
  708. $result = $this->View->element('TestPlugin.plugin_element');
  709. $this->assertEquals('this is the plugin element using params[plugin]', $result);
  710. $this->View->plugin = 'TestPlugin';
  711. $result = $this->View->element('test_plugin_element');
  712. $this->assertEquals('this is the test set using View::$plugin plugin element', $result);
  713. }
  714. /**
  715. * Test elementInexistent method
  716. *
  717. * @expectedException \Cake\View\Exception\MissingElementException
  718. * @return void
  719. */
  720. public function testElementInexistent()
  721. {
  722. $this->View->element('non_existent_element');
  723. }
  724. /**
  725. * Test elementInexistent3 method
  726. *
  727. * @expectedException \Cake\View\Exception\MissingElementException
  728. * @return void
  729. */
  730. public function testElementInexistent3()
  731. {
  732. $this->View->element('test_plugin.plugin_element');
  733. }
  734. /**
  735. * Test that elements can have callbacks
  736. *
  737. * @return void
  738. */
  739. public function testElementCallbacks()
  740. {
  741. $count = 0;
  742. $callback = function ($event, $file) use (&$count) {
  743. $count++;
  744. };
  745. $events = $this->View->eventManager();
  746. $events->attach($callback, 'View.beforeRender');
  747. $events->attach($callback, 'View.afterRender');
  748. $this->View->element('test_element', [], ['callbacks' => true]);
  749. $this->assertEquals(2, $count);
  750. }
  751. /**
  752. * Test that additional element viewVars don't get overwritten with helpers.
  753. *
  754. * @return void
  755. */
  756. public function testElementParamsDontOverwriteHelpers()
  757. {
  758. $Controller = new ViewPostsController();
  759. $Controller->helpers = ['Form'];
  760. $View = $Controller->createView();
  761. $result = $View->element('type_check', ['form' => 'string'], ['callbacks' => true]);
  762. $this->assertEquals('string', $result);
  763. $View->set('form', 'string');
  764. $result = $View->element('type_check', [], ['callbacks' => true]);
  765. $this->assertEquals('string', $result);
  766. }
  767. /**
  768. * Test elementCacheHelperNoCache method
  769. *
  770. * @return void
  771. */
  772. public function testElementCacheHelperNoCache()
  773. {
  774. $Controller = new ViewPostsController();
  775. $View = $Controller->createView();
  776. $result = $View->element('test_element', ['ram' => 'val', 'test' => ['foo', 'bar']]);
  777. $this->assertEquals('this is the test element', $result);
  778. }
  779. /**
  780. * Test elementCache method
  781. *
  782. * @return void
  783. */
  784. public function testElementCache()
  785. {
  786. Cache::drop('test_view');
  787. Cache::config('test_view', [
  788. 'engine' => 'File',
  789. 'duration' => '+1 day',
  790. 'path' => CACHE . 'views/',
  791. 'prefix' => ''
  792. ]);
  793. Cache::clear(false, 'test_view');
  794. $View = $this->PostsController->createView();
  795. $View->elementCache = 'test_view';
  796. $result = $View->element('test_element', [], ['cache' => true]);
  797. $expected = 'this is the test element';
  798. $this->assertEquals($expected, $result);
  799. $result = Cache::read('element__test_element_cache_callbacks', 'test_view');
  800. $this->assertEquals($expected, $result);
  801. $result = $View->element('test_element', ['param' => 'one', 'foo' => 'two'], ['cache' => true]);
  802. $this->assertEquals($expected, $result);
  803. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  804. $this->assertEquals($expected, $result);
  805. $View->element('test_element', [
  806. 'param' => 'one',
  807. 'foo' => 'two'
  808. ], [
  809. 'cache' => ['key' => 'custom_key']
  810. ]);
  811. $result = Cache::read('element_custom_key', 'test_view');
  812. $this->assertEquals($expected, $result);
  813. $View->elementCache = 'default';
  814. $View->element('test_element', [
  815. 'param' => 'one',
  816. 'foo' => 'two'
  817. ], [
  818. 'cache' => ['config' => 'test_view'],
  819. ]);
  820. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  821. $this->assertEquals($expected, $result);
  822. Cache::clear(true, 'test_view');
  823. Cache::drop('test_view');
  824. }
  825. /**
  826. * Test element events
  827. *
  828. * @return void
  829. */
  830. public function testViewEvent()
  831. {
  832. $View = $this->PostsController->createView();
  833. $View->autoLayout = false;
  834. $listener = new TestViewEventListenerInterface();
  835. $View->eventManager()->attach($listener);
  836. $View->render('index');
  837. $this->assertEquals(View::TYPE_VIEW, $listener->beforeRenderViewType);
  838. $this->assertEquals(View::TYPE_VIEW, $listener->afterRenderViewType);
  839. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  840. $View->element('test_element', [], ['callbacks' => true]);
  841. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  842. $this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType);
  843. $this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType);
  844. }
  845. /**
  846. * Test loading helper using loadHelper().
  847. *
  848. * @return void
  849. */
  850. public function testLoadHelper()
  851. {
  852. $View = new View();
  853. $View->loadHelper('Html', ['foo' => 'bar']);
  854. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html);
  855. $config = $View->Html->config();
  856. $this->assertEquals('bar', $config['foo']);
  857. }
  858. /**
  859. * Test loading helper when duplicate.
  860. *
  861. * @return void
  862. */
  863. public function testLoadHelperDuplicate()
  864. {
  865. $View = new View();
  866. $this->assertNotEmpty($View->loadHelper('Html', ['foo' => 'bar']));
  867. try {
  868. $View->loadHelper('Html', ['test' => 'value']);
  869. $this->fail('No exception');
  870. } catch (\RuntimeException $e) {
  871. $this->assertContains('The "Html" alias has already been loaded', $e->getMessage());
  872. }
  873. }
  874. /**
  875. * Test loadHelpers method
  876. *
  877. * @return void
  878. */
  879. public function testLoadHelpers()
  880. {
  881. $View = new View();
  882. $View->helpers = ['Html' => ['foo' => 'bar'], 'Form' => ['foo' => 'baz']];
  883. $View->loadHelpers();
  884. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  885. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  886. $config = $View->Html->config();
  887. $this->assertEquals('bar', $config['foo']);
  888. $config = $View->Form->config();
  889. $this->assertEquals('baz', $config['foo']);
  890. }
  891. /**
  892. * Test lazy loading helpers
  893. *
  894. * @return void
  895. */
  896. public function testLazyLoadHelpers()
  897. {
  898. $View = new View();
  899. $View->helpers = [];
  900. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  901. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  902. }
  903. /**
  904. * Test manipulating class properties in initialize()
  905. *
  906. * @return void
  907. */
  908. public function testInitialize()
  909. {
  910. $View = new TestView();
  911. $config = $View->Html->config();
  912. $this->assertEquals('myval', $config['mykey']);
  913. }
  914. /**
  915. * Test the correct triggering of helper callbacks
  916. *
  917. * @return void
  918. */
  919. public function testHelperCallbackTriggering()
  920. {
  921. $View = $this->PostsController->createView();
  922. $manager = $this->getMock('Cake\Event\EventManager');
  923. $View->eventManager($manager);
  924. $manager->expects($this->at(0))->method('dispatch')
  925. ->with(
  926. $this->logicalAnd(
  927. $this->isInstanceOf('Cake\Event\Event'),
  928. $this->attributeEqualTo('_name', 'View.beforeRender'),
  929. $this->attributeEqualTo('_subject', $View)
  930. )
  931. );
  932. $manager->expects($this->at(1))->method('dispatch')
  933. ->with(
  934. $this->logicalAnd(
  935. $this->isInstanceOf('Cake\Event\Event'),
  936. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  937. $this->attributeEqualTo('_subject', $View)
  938. )
  939. );
  940. $manager->expects($this->at(2))->method('dispatch')
  941. ->with(
  942. $this->logicalAnd(
  943. $this->isInstanceOf('Cake\Event\Event'),
  944. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  945. $this->attributeEqualTo('_subject', $View)
  946. )
  947. );
  948. $manager->expects($this->at(3))->method('dispatch')
  949. ->with(
  950. $this->logicalAnd(
  951. $this->isInstanceOf('Cake\Event\Event'),
  952. $this->attributeEqualTo('_name', 'View.afterRender'),
  953. $this->attributeEqualTo('_subject', $View)
  954. )
  955. );
  956. $manager->expects($this->at(4))->method('dispatch')
  957. ->with(
  958. $this->logicalAnd(
  959. $this->isInstanceOf('Cake\Event\Event'),
  960. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  961. $this->attributeEqualTo('_subject', $View)
  962. )
  963. );
  964. $manager->expects($this->at(5))->method('dispatch')
  965. ->with(
  966. $this->logicalAnd(
  967. $this->isInstanceOf('Cake\Event\Event'),
  968. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  969. $this->attributeEqualTo('_subject', $View)
  970. )
  971. );
  972. $manager->expects($this->at(6))->method('dispatch')
  973. ->with(
  974. $this->logicalAnd(
  975. $this->isInstanceOf('Cake\Event\Event'),
  976. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  977. $this->attributeEqualTo('_subject', $View)
  978. )
  979. );
  980. $manager->expects($this->at(7))->method('dispatch')
  981. ->with(
  982. $this->logicalAnd(
  983. $this->isInstanceOf('Cake\Event\Event'),
  984. $this->attributeEqualTo('_name', 'View.afterLayout'),
  985. $this->attributeEqualTo('_subject', $View)
  986. )
  987. );
  988. $View->render('index');
  989. }
  990. /**
  991. * Test beforeLayout method
  992. *
  993. * @return void
  994. */
  995. public function testBeforeLayout()
  996. {
  997. $this->PostsController->helpers = [
  998. 'Session',
  999. 'TestBeforeAfter' => ['className' => __NAMESPACE__ . '\TestBeforeAfterHelper'],
  1000. 'Html'
  1001. ];
  1002. $View = $this->PostsController->createView();
  1003. $View->render('index');
  1004. $this->assertEquals('Valuation', $View->helpers()->TestBeforeAfter->property);
  1005. }
  1006. /**
  1007. * Test afterLayout method
  1008. *
  1009. * @return void
  1010. */
  1011. public function testAfterLayout()
  1012. {
  1013. $this->PostsController->helpers = [
  1014. 'Session',
  1015. 'TestBeforeAfter' => ['className' => __NAMESPACE__ . '\TestBeforeAfterHelper'],
  1016. 'Html'
  1017. ];
  1018. $this->PostsController->set('variable', 'values');
  1019. $View = $this->PostsController->createView();
  1020. $content = 'This is my view output';
  1021. $result = $View->renderLayout($content, 'default');
  1022. $this->assertRegExp('/modified in the afterlife/', $result);
  1023. $this->assertRegExp('/This is my view output/', $result);
  1024. }
  1025. /**
  1026. * Test renderLoadHelper method
  1027. *
  1028. * @return void
  1029. */
  1030. public function testRenderLoadHelper()
  1031. {
  1032. $this->PostsController->helpers = ['Session', 'Form', 'Number'];
  1033. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1034. $result = $View->render('index', false);
  1035. $this->assertEquals('posts index', $result);
  1036. $attached = $View->helpers()->loaded();
  1037. // HtmlHelper is loaded in TestView::initialize()
  1038. $this->assertEquals(['Html', 'Session', 'Form', 'Number'], $attached);
  1039. $this->PostsController->helpers = ['Html', 'Form', 'Number', 'TestPlugin.PluggedHelper'];
  1040. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1041. $result = $View->render('index', false);
  1042. $this->assertEquals('posts index', $result);
  1043. $attached = $View->helpers()->loaded();
  1044. $expected = ['Html', 'Form', 'Number', 'PluggedHelper'];
  1045. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  1046. }
  1047. /**
  1048. * Test render method
  1049. *
  1050. * @return void
  1051. */
  1052. public function testRender()
  1053. {
  1054. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1055. $result = $View->render('index');
  1056. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\"\/>\s*<title>/", $result);
  1057. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1058. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1059. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1060. $result = $View->render(false, 'ajax2');
  1061. $this->assertRegExp('/Ajax\!/', $result);
  1062. $this->assertNull($View->render(false, 'ajax2'));
  1063. $this->PostsController->helpers = ['Session', 'Html'];
  1064. $this->PostsController->request->params['action'] = 'index';
  1065. Configure::write('Cache.check', true);
  1066. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1067. $result = $View->render('index');
  1068. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\"\/>\s*<title>/", $result);
  1069. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1070. }
  1071. /**
  1072. * Test that View::$view works
  1073. *
  1074. * @return void
  1075. */
  1076. public function testRenderUsingViewProperty()
  1077. {
  1078. $this->PostsController->view = 'cache_form';
  1079. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1080. $this->assertEquals('cache_form', $View->view);
  1081. $result = $View->render();
  1082. $this->assertRegExp('/Add User/', $result);
  1083. }
  1084. /**
  1085. * Test render()ing a file in a subdir from a custom viewPath
  1086. * in a plugin.
  1087. *
  1088. * @return void
  1089. */
  1090. public function testGetViewFileNameSubdirWithPluginAndViewPath()
  1091. {
  1092. $this->PostsController->plugin = 'TestPlugin';
  1093. $this->PostsController->name = 'Posts';
  1094. $this->PostsController->viewPath = 'Element';
  1095. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1096. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  1097. $result = $View->getViewFileName('sub_dir/sub_element');
  1098. $expected = $pluginPath . 'src' . DS . 'Template' . DS . 'Element' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  1099. $this->assertPathEquals($expected, $result);
  1100. }
  1101. /**
  1102. * Test that view vars can replace the local helper variables
  1103. * and not overwrite the $this->Helper references
  1104. *
  1105. * @return void
  1106. */
  1107. public function testViewVarOverwritingLocalHelperVar()
  1108. {
  1109. $Controller = new ViewPostsController();
  1110. $Controller->helpers = ['Session', 'Html'];
  1111. $Controller->set('html', 'I am some test html');
  1112. $View = $Controller->createView();
  1113. $result = $View->render('helper_overwrite', false);
  1114. $this->assertRegExp('/I am some test html/', $result);
  1115. $this->assertRegExp('/Test link/', $result);
  1116. }
  1117. /**
  1118. * Test getViewFileName method
  1119. *
  1120. * @return void
  1121. */
  1122. public function testViewFileName()
  1123. {
  1124. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1125. $result = $View->getViewFileName('index');
  1126. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1127. $result = $View->getViewFileName('TestPlugin.index');
  1128. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1129. $result = $View->getViewFileName('/Pages/home');
  1130. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  1131. $result = $View->getViewFileName('../Element/test_element');
  1132. $this->assertRegExp('/Element(\/|\\\)test_element.ctp/', $result);
  1133. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  1134. $result = $View->getViewFileName('../Posts/index');
  1135. $this->assertPathEquals($expected, $result);
  1136. }
  1137. /**
  1138. * Test creating a block with capturing output.
  1139. *
  1140. * @return void
  1141. */
  1142. public function testBlockCaptureOverwrite()
  1143. {
  1144. $this->View->start('test');
  1145. echo 'Block content';
  1146. $this->View->end();
  1147. $this->View->start('test');
  1148. echo 'New content';
  1149. $this->View->end();
  1150. $result = $this->View->fetch('test');
  1151. $this->assertEquals('New content', $result);
  1152. }
  1153. /**
  1154. * Test that blocks can be fetched inside a block with the same name
  1155. *
  1156. * @return void
  1157. */
  1158. public function testBlockExtend()
  1159. {
  1160. $this->View->start('test');
  1161. echo 'Block content';
  1162. $this->View->end();
  1163. $this->View->start('test');
  1164. echo $this->View->fetch('test');
  1165. echo 'New content';
  1166. $this->View->end();
  1167. $result = $this->View->fetch('test');
  1168. $this->assertEquals('Block contentNew content', $result);
  1169. }
  1170. /**
  1171. * Test creating a block with capturing output.
  1172. *
  1173. * @return void
  1174. */
  1175. public function testBlockCapture()
  1176. {
  1177. $this->View->start('test');
  1178. echo 'Block content';
  1179. $this->View->end();
  1180. $result = $this->View->fetch('test');
  1181. $this->assertEquals('Block content', $result);
  1182. }
  1183. /**
  1184. * Test appending to a block with capturing output.
  1185. *
  1186. * @return void
  1187. */
  1188. public function testBlockAppendCapture()
  1189. {
  1190. $this->View->start('test');
  1191. echo 'Content ';
  1192. $this->View->end();
  1193. $this->View->append('test');
  1194. echo 'appended';
  1195. $this->View->end();
  1196. $result = $this->View->fetch('test');
  1197. $this->assertEquals('Content appended', $result);
  1198. }
  1199. /**
  1200. * Test setting a block's content.
  1201. *
  1202. * @return void
  1203. */
  1204. public function testBlockSet()
  1205. {
  1206. $this->View->assign('test', 'Block content');
  1207. $result = $this->View->fetch('test');
  1208. $this->assertEquals('Block content', $result);
  1209. }
  1210. /**
  1211. * Test resetting a block's content.
  1212. *
  1213. * @return void
  1214. */
  1215. public function testBlockReset()
  1216. {
  1217. $this->View->assign('test', '');
  1218. $result = $this->View->fetch('test', 'This should not be returned');
  1219. $this->assertSame('', $result);
  1220. }
  1221. /**
  1222. * Test checking a block's existance.
  1223. *
  1224. * @return void
  1225. */
  1226. public function testBlockExist()
  1227. {
  1228. $this->assertFalse($this->View->exists('test'));
  1229. $this->View->assign('test', 'Block content');
  1230. $this->assertTrue($this->View->exists('test'));
  1231. }
  1232. /**
  1233. * Test setting a block's content to null
  1234. *
  1235. * @return void
  1236. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
  1237. */
  1238. public function testBlockSetNull()
  1239. {
  1240. $this->View->assign('testWithNull', null);
  1241. $result = $this->View->fetch('testWithNull');
  1242. $this->assertSame('', $result);
  1243. }
  1244. /**
  1245. * Test setting a block's content to an object with __toString magic method
  1246. *
  1247. * @return void
  1248. */
  1249. public function testBlockSetObjectWithToString()
  1250. {
  1251. $objectWithToString = new TestObjectWithToString();
  1252. $this->View->assign('testWithObjectWithToString', $objectWithToString);
  1253. $result = $this->View->fetch('testWithObjectWithToString');
  1254. $this->assertSame("I'm ObjectWithToString", $result);
  1255. }
  1256. /**
  1257. * Test setting a block's content to an object without __toString magic method
  1258. *
  1259. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1260. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1261. *
  1262. * @expectedException \PHPUnit_Framework_Error
  1263. * @return void
  1264. */
  1265. public function testBlockSetObjectWithoutToString()
  1266. {
  1267. $objectWithToString = new TestObjectWithoutToString();
  1268. $this->View->assign('testWithObjectWithoutToString', $objectWithToString);
  1269. }
  1270. /**
  1271. * Test setting a block's content to a decimal
  1272. *
  1273. * @return void
  1274. */
  1275. public function testBlockSetDecimal()
  1276. {
  1277. $this->View->assign('testWithDecimal', 1.23456789);
  1278. $result = $this->View->fetch('testWithDecimal');
  1279. $this->assertEquals('1.23456789', $result);
  1280. }
  1281. /**
  1282. * Data provider for block related tests.
  1283. *
  1284. * @return array
  1285. */
  1286. public static function blockValueProvider()
  1287. {
  1288. return [
  1289. 'string' => ['A string value'],
  1290. 'decimal' => [1.23456],
  1291. 'object with __toString' => [new TestObjectWithToString()],
  1292. ];
  1293. }
  1294. /**
  1295. * Test appending to a block with append.
  1296. *
  1297. * @param mixed $value Value
  1298. * @return void
  1299. * @dataProvider blockValueProvider
  1300. */
  1301. public function testBlockAppend($value)
  1302. {
  1303. $this->View->assign('testBlock', 'Block');
  1304. $this->View->append('testBlock', $value);
  1305. $result = $this->View->fetch('testBlock');
  1306. $this->assertSame('Block' . $value, $result);
  1307. }
  1308. /**
  1309. * Test appending an object without __toString magic method to a block with append.
  1310. *
  1311. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1312. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1313. *
  1314. * @expectedException \PHPUnit_Framework_Error
  1315. * @return void
  1316. */
  1317. public function testBlockAppendObjectWithoutToString()
  1318. {
  1319. $object = new TestObjectWithoutToString();
  1320. $this->View->assign('testBlock', 'Block ');
  1321. $this->View->append('testBlock', $object);
  1322. }
  1323. /**
  1324. * Test prepending to a block with prepend.
  1325. *
  1326. * @param mixed $value Value
  1327. * @return void
  1328. * @dataProvider blockValueProvider
  1329. */
  1330. public function testBlockPrepend($value)
  1331. {
  1332. $this->View->assign('test', 'Block');
  1333. $this->View->prepend('test', $value);
  1334. $result = $this->View->fetch('test');
  1335. $this->assertEquals($value . 'Block', $result);
  1336. }
  1337. /**
  1338. * Test prepending an object without __toString magic method to a block with prepend.
  1339. *
  1340. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1341. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1342. *
  1343. * @expectedException \PHPUnit_Framework_Error
  1344. * @return void
  1345. */
  1346. public function testBlockPrependObjectWithoutToString()
  1347. {
  1348. $object = new TestObjectWithoutToString();
  1349. $this->View->assign('test', 'Block ');
  1350. $this->View->prepend('test', $object);
  1351. }
  1352. /**
  1353. * You should be able to append to undefined blocks.
  1354. *
  1355. * @return void
  1356. */
  1357. public function testBlockAppendUndefined()
  1358. {
  1359. $this->View->append('test', 'Unknown');
  1360. $result = $this->View->fetch('test');
  1361. $this->assertEquals('Unknown', $result);
  1362. }
  1363. /**
  1364. * You should be able to prepend to undefined blocks.
  1365. *
  1366. * @return void
  1367. */
  1368. public function testBlockPrependUndefined()
  1369. {
  1370. $this->View->prepend('test', 'Unknown');
  1371. $result = $this->View->fetch('test');
  1372. $this->assertEquals('Unknown', $result);
  1373. }
  1374. /**
  1375. * Test getting block names
  1376. *
  1377. * @return void
  1378. */
  1379. public function testBlocks()
  1380. {
  1381. $this->View->append('test', 'one');
  1382. $this->View->assign('test1', 'one');
  1383. $this->assertEquals(['test', 'test1'], $this->View->blocks());
  1384. }
  1385. /**
  1386. * Test that blocks can be nested.
  1387. *
  1388. * @return void
  1389. */
  1390. public function testNestedBlocks()
  1391. {
  1392. $this->View->start('first');
  1393. echo 'In first ';
  1394. $this->View->start('second');
  1395. echo 'In second';
  1396. $this->View->end();
  1397. echo 'In first';
  1398. $this->View->end();
  1399. $this->assertEquals('In first In first', $this->View->fetch('first'));
  1400. $this->assertEquals('In second', $this->View->fetch('second'));
  1401. }
  1402. /**
  1403. * Test that starting the same block twice throws an exception
  1404. *
  1405. * @return void
  1406. */
  1407. public function testStartBlocksTwice()
  1408. {
  1409. try {
  1410. $this->View->start('first');
  1411. $this->View->start('first');
  1412. $this->fail('No exception');
  1413. } catch (\Cake\Core\Exception\Exception $e) {
  1414. ob_end_clean();
  1415. $this->assertTrue(true);
  1416. }
  1417. }
  1418. /**
  1419. * Test that an exception gets thrown when you leave a block open at the end
  1420. * of a view.
  1421. *
  1422. * @return void
  1423. */
  1424. public function testExceptionOnOpenBlock()
  1425. {
  1426. try {
  1427. $this->View->render('open_block');
  1428. $this->fail('No exception');
  1429. } catch (\LogicException $e) {
  1430. ob_end_clean();
  1431. $this->assertContains('The "no_close" block was left open', $e->getMessage());
  1432. }
  1433. }
  1434. /**
  1435. * Test nested extended views.
  1436. *
  1437. * @return void
  1438. */
  1439. public function testExtendNested()
  1440. {
  1441. $this->View->layout = false;
  1442. $content = $this->View->render('nested_extends');
  1443. $expected = <<<TEXT
  1444. This is the second parent.
  1445. This is the first parent.
  1446. This is the first template.
  1447. Sidebar Content.
  1448. TEXT;
  1449. $this->assertEquals($expected, $content);
  1450. }
  1451. /**
  1452. * Make sure that extending the current view with itself causes an exception
  1453. *
  1454. * @return void
  1455. */
  1456. public function testExtendSelf()
  1457. {
  1458. try {
  1459. $this->View->layout = false;
  1460. $this->View->render('extend_self');
  1461. $this->fail('No exception');
  1462. } catch (\LogicException $e) {
  1463. ob_end_clean();
  1464. $this->assertContains('cannot have views extend themselves', $e->getMessage());
  1465. }
  1466. }
  1467. /**
  1468. * Make sure that extending in a loop causes an exception
  1469. *
  1470. * @return void
  1471. */
  1472. public function testExtendLoop()
  1473. {
  1474. try {
  1475. $this->View->layout = false;
  1476. $this->View->render('extend_loop');
  1477. } catch (\LogicException $e) {
  1478. ob_end_clean();
  1479. $this->assertContains('cannot have views extend in a loop', $e->getMessage());
  1480. }
  1481. }
  1482. /**
  1483. * Test extend() in an element and a view.
  1484. *
  1485. * @return void
  1486. */
  1487. public function testExtendElement()
  1488. {
  1489. $this->View->layout = false;
  1490. $content = $this->View->render('extend_element');
  1491. $expected = <<<TEXT
  1492. Parent View.
  1493. View content.
  1494. Parent Element.
  1495. Element content.
  1496. TEXT;
  1497. $this->assertEquals($expected, $content);
  1498. }
  1499. /**
  1500. * Extending an element which doesn't exist should throw a missing view exception
  1501. *
  1502. * @return void
  1503. */
  1504. public function testExtendMissingElement()
  1505. {
  1506. try {
  1507. $this->View->layout = false;
  1508. $this->View->render('extend_missing_element');
  1509. $this->fail('No exception');
  1510. } catch (\LogicException $e) {
  1511. ob_end_clean();
  1512. ob_end_clean();
  1513. $this->assertContains('element', $e->getMessage());
  1514. }
  1515. }
  1516. /**
  1517. * Test extend() preceeded by an element()
  1518. *
  1519. * @return void
  1520. */
  1521. public function testExtendWithElementBeforeExtend()
  1522. {
  1523. $this->View->layout = false;
  1524. $result = $this->View->render('extend_with_element');
  1525. $expected = <<<TEXT
  1526. Parent View.
  1527. this is the test elementThe view
  1528. TEXT;
  1529. $this->assertEquals($expected, $result);
  1530. }
  1531. /**
  1532. * Test memory leaks that existed in _paths at one point.
  1533. *
  1534. * @return void
  1535. */
  1536. public function testMemoryLeakInPaths()
  1537. {
  1538. $this->ThemeController->plugin = null;
  1539. $this->ThemeController->name = 'Posts';
  1540. $this->ThemeController->viewPath = 'Posts';
  1541. $this->ThemeController->layout = 'whatever';
  1542. $this->ThemeController->theme = 'TestTheme';
  1543. $View = $this->ThemeController->createView();
  1544. $View->element('test_element');
  1545. $start = memory_get_usage();
  1546. for ($i = 0; $i < 10; $i++) {
  1547. $View->element('test_element');
  1548. }
  1549. $end = memory_get_usage();
  1550. $this->assertLessThanOrEqual($start + 5000, $end);
  1551. }
  1552. /**
  1553. * Tests that a view block uses default value when not assigned and uses assigned value when it is
  1554. *
  1555. * @return void
  1556. */
  1557. public function testBlockDefaultValue()
  1558. {
  1559. $default = 'Default';
  1560. $result = $this->View->fetch('title', $default);
  1561. $this->assertEquals($default, $result);
  1562. $expected = 'My Title';
  1563. $this->View->assign('title', $expected);
  1564. $result = $this->View->fetch('title', $default);
  1565. $this->assertEquals($expected, $result);
  1566. }
  1567. /**
  1568. * Tests that a view variable uses default value when not assigned and uses assigned value when it is
  1569. *
  1570. * @return void
  1571. */
  1572. public function testViewVarDefaultValue()
  1573. {
  1574. $default = 'Default';
  1575. $result = $this->View->get('title', $default);
  1576. $this->assertEquals($default, $result);
  1577. $expected = 'Back to the Future';
  1578. $this->View->set('title', $expected);
  1579. $result = $this->View->get('title', $default);
  1580. $this->assertEquals($expected, $result);
  1581. }
  1582. /**
  1583. * Test the helpers() method.
  1584. *
  1585. * @return void
  1586. */
  1587. public function testHelpers()
  1588. {
  1589. $this->assertInstanceOf('Cake\View\HelperRegistry', $this->View->helpers());
  1590. $result = $this->View->helpers();
  1591. $this->assertSame($result, $this->View->helpers());
  1592. }
  1593. }