ViewTest.php 56 KB

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