ViewTest.php 64 KB

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