ViewTest.php 63 KB

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