ViewTest.php 64 KB

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