ViewTest.php 64 KB

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