ViewTest.php 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Cache\Cache;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  21. use Cake\Event\Event;
  22. use Cake\Event\EventListenerInterface;
  23. use Cake\Http\ServerRequest;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\TestCase;
  26. use Cake\View\Helper;
  27. use Cake\View\View;
  28. use TestApp\View\AppView;
  29. /**
  30. * ViewPostsController class
  31. */
  32. class ViewPostsController extends Controller
  33. {
  34. /**
  35. * name property
  36. *
  37. * @var string
  38. */
  39. public $name = 'Posts';
  40. /**
  41. * index method
  42. *
  43. * @return void
  44. */
  45. public function index()
  46. {
  47. $this->set([
  48. 'testData' => 'Some test data',
  49. 'test2' => 'more data',
  50. 'test3' => 'even more data',
  51. ]);
  52. }
  53. /**
  54. * nocache_tags_with_element method
  55. *
  56. * @return void
  57. */
  58. public function nocache_multiple_element()
  59. {
  60. $this->set('foo', 'this is foo var');
  61. $this->set('bar', 'this is bar var');
  62. }
  63. }
  64. /**
  65. * ThemePostsController class
  66. */
  67. class ThemePostsController extends Controller
  68. {
  69. /**
  70. * index method
  71. *
  72. * @return void
  73. */
  74. public function index()
  75. {
  76. $this->set('testData', 'Some test data');
  77. $test2 = 'more data';
  78. $test3 = 'even more data';
  79. $this->set(compact('test2', 'test3'));
  80. }
  81. }
  82. /**
  83. * TestView class
  84. */
  85. class TestView extends AppView
  86. {
  87. public function initialize()
  88. {
  89. $this->loadHelper('Html', ['mykey' => 'myval']);
  90. }
  91. /**
  92. * getViewFileName method
  93. *
  94. * @param string $name Controller action to find template filename for
  95. * @return string Template filename
  96. */
  97. public function getViewFileName($name = null)
  98. {
  99. return $this->_getViewFileName($name);
  100. }
  101. /**
  102. * getLayoutFileName method
  103. *
  104. * @param string $name The name of the layout to find.
  105. * @return string Filename for layout file (.ctp).
  106. */
  107. public function getLayoutFileName($name = null)
  108. {
  109. return $this->_getLayoutFileName($name);
  110. }
  111. /**
  112. * paths method
  113. *
  114. * @param string $plugin Optional plugin name to scan for view files.
  115. * @param bool $cached Set to true to force a refresh of view paths.
  116. * @return array paths
  117. */
  118. public function paths($plugin = null, $cached = true)
  119. {
  120. return $this->_paths($plugin, $cached);
  121. }
  122. /**
  123. * Setter for extension.
  124. *
  125. * @param string $ext The extension
  126. * @return void
  127. */
  128. public function ext($ext)
  129. {
  130. $this->_ext = $ext;
  131. }
  132. }
  133. /**
  134. * TestBeforeAfterHelper class
  135. */
  136. class TestBeforeAfterHelper extends Helper
  137. {
  138. /**
  139. * property property
  140. *
  141. * @var string
  142. */
  143. public $property = '';
  144. /**
  145. * beforeLayout method
  146. *
  147. * @param string $viewFile View file name.
  148. * @return void
  149. */
  150. public function beforeLayout($viewFile)
  151. {
  152. $this->property = 'Valuation';
  153. }
  154. /**
  155. * afterLayout method
  156. *
  157. * @param string $layoutFile Layout file name.
  158. * @return void
  159. */
  160. public function afterLayout($layoutFile)
  161. {
  162. $this->_View->append('content', 'modified in the afterlife');
  163. }
  164. }
  165. /**
  166. * TestObjectWithToString
  167. *
  168. * An object with the magic method __toString() for testing with view blocks.
  169. */
  170. class TestObjectWithToString
  171. {
  172. /**
  173. * Return string value.
  174. *
  175. * @return string
  176. */
  177. public function __toString()
  178. {
  179. return "I'm ObjectWithToString";
  180. }
  181. }
  182. /**
  183. * TestObjectWithoutToString
  184. *
  185. * An object without the magic method __toString() for testing with view blocks.
  186. */
  187. class TestObjectWithoutToString
  188. {
  189. }
  190. /**
  191. * TestViewEventListenerInterface
  192. *
  193. * An event listener to test cakePHP events
  194. */
  195. class TestViewEventListenerInterface implements EventListenerInterface
  196. {
  197. /**
  198. * type of view before rendering has occurred
  199. *
  200. * @var string
  201. */
  202. public $beforeRenderViewType;
  203. /**
  204. * type of view after rendering has occurred
  205. *
  206. * @var string
  207. */
  208. public $afterRenderViewType;
  209. /**
  210. * implementedEvents method
  211. *
  212. * @return array
  213. */
  214. public function implementedEvents()
  215. {
  216. return [
  217. 'View.beforeRender' => 'beforeRender',
  218. 'View.afterRender' => 'afterRender',
  219. ];
  220. }
  221. /**
  222. * beforeRender method
  223. *
  224. * @param \Cake\Event\Event $event the event being sent
  225. * @return void
  226. */
  227. public function beforeRender(Event $event)
  228. {
  229. $this->beforeRenderViewType = $event->getSubject()->getCurrentType();
  230. }
  231. /**
  232. * afterRender method
  233. *
  234. * @param \Cake\Event\Event $event the event being sent
  235. * @return void
  236. */
  237. public function afterRender(Event $event)
  238. {
  239. $this->afterRenderViewType = $event->getSubject()->getCurrentType();
  240. }
  241. }
  242. /**
  243. * ViewTest class
  244. */
  245. class ViewTest extends TestCase
  246. {
  247. /**
  248. * Fixtures used in this test.
  249. *
  250. * @var array
  251. */
  252. public $fixtures = ['core.Posts', 'core.Users'];
  253. /**
  254. * @var \Cake\View\View
  255. */
  256. public $View;
  257. /**
  258. * @var ViewPostsController
  259. */
  260. public $PostsController;
  261. /**
  262. * setUp method
  263. *
  264. * @return void
  265. */
  266. public function setUp()
  267. {
  268. parent::setUp();
  269. $request = new ServerRequest();
  270. $this->Controller = new Controller($request);
  271. $this->PostsController = new ViewPostsController($request);
  272. $this->PostsController->index();
  273. $this->View = $this->PostsController->createView();
  274. $this->View->setTemplatePath('Posts');
  275. $themeRequest = new ServerRequest('posts/index');
  276. $this->ThemeController = new Controller($themeRequest);
  277. $this->ThemePostsController = new ThemePostsController($themeRequest);
  278. $this->ThemePostsController->index();
  279. $this->ThemeView = $this->ThemePostsController->createView();
  280. $this->ThemeView->setTemplatePath('Posts');
  281. $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. $this->clearPlugins();
  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 . '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->setLayoutPath('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->setLayoutPath('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->setTheme('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. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'home.ctp';
  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 . 'src' . DS .
  541. 'Template' . DS . 'Tests' . DS . 'index.ctp';
  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 . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'json' . DS . 'index.ctp';
  579. $result = $view->getViewFileName('index');
  580. $this->assertPathEquals($expected, $result);
  581. $view->setSubDir('json');
  582. $result = $view->getViewFileName('index');
  583. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'json' . DS . 'index.ctp';
  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 . 'TestApp' . DS . 'Template' . DS . 'Jobs' . DS . 'json' . DS . 'index.ctp';
  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 . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  619. $result = $View->getLayoutFileName();
  620. $this->assertPathEquals($expected, $result);
  621. $View->setLayoutPath('rss');
  622. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'rss' . DS . 'default.ctp';
  623. $result = $View->getLayoutFileName();
  624. $this->assertPathEquals($expected, $result);
  625. $View->setLayoutPath('Email' . DS . 'html');
  626. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'Email' . DS . 'html' . DS . 'default.ctp';
  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 . 'src' . DS .
  644. 'Template' . DS . 'Layout' . DS . 'default.ctp';
  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 . 'src' . DS .
  649. 'Template' . DS . 'Layout' . DS . 'default.ctp';
  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 . 'TestApp' . DS . 'Template' . DS .
  664. 'FooPrefix' . DS . 'Layout' . DS . 'default.ctp';
  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 . 'TestApp' . DS . 'Template' . DS .
  673. 'FooPrefix' . DS . 'BarPrefix' . DS . 'Layout' . DS . 'default.ctp';
  674. $result = $View->getLayoutFileName();
  675. $this->assertPathEquals($expected, $result);
  676. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS .
  677. 'FooPrefix' . DS . 'Layout' . DS . 'nested_prefix_cascade.ctp';
  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 . 'TestApp' . DS . 'Template' . DS .
  683. 'Layout' . DS . 'default.ctp';
  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' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data'], $this->View->viewVars);
  747. }
  748. /**
  749. * Test generation of UUIDs method
  750. *
  751. * @return void
  752. */
  753. public function testUUIDGeneration()
  754. {
  755. $this->deprecated(function () {
  756. Router::connect('/:controller', ['action' => 'index']);
  757. $result = $this->View->uuid('form', ['controller' => 'posts', 'action' => 'index']);
  758. $this->assertEquals('form5988016017', $result);
  759. $result = $this->View->uuid('form', ['controller' => 'posts', 'action' => 'index']);
  760. $this->assertEquals('formc3dc6be854', $result);
  761. $result = $this->View->uuid('form', ['controller' => 'posts', 'action' => 'index']);
  762. $this->assertEquals('form28f92cc87f', $result);
  763. });
  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->setRequest($this->View->getRequest()->withParam('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->setRequest($this->View->getRequest()->withParam('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->setRequest($this->View->getRequest()->withParam('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 (Event $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. $Controller->helpers = ['Form'];
  870. $View = $Controller->createView();
  871. $result = $View->element('type_check', ['form' => 'string'], ['callbacks' => true]);
  872. $this->assertEquals('string', $result);
  873. $View->set('form', 'string');
  874. $result = $View->element('type_check', [], ['callbacks' => true]);
  875. $this->assertEquals('string', $result);
  876. }
  877. /**
  878. * Test elementCacheHelperNoCache method
  879. *
  880. * @return void
  881. */
  882. public function testElementCacheHelperNoCache()
  883. {
  884. $Controller = new ViewPostsController();
  885. $View = $Controller->createView();
  886. $result = $View->element('test_element', ['ram' => 'val', 'test' => ['foo', 'bar']]);
  887. $this->assertEquals('this is the test element', $result);
  888. }
  889. /**
  890. * Test elementCache method
  891. *
  892. * @return void
  893. */
  894. public function testElementCache()
  895. {
  896. Cache::drop('test_view');
  897. Cache::setConfig('test_view', [
  898. 'engine' => 'File',
  899. 'duration' => '+1 day',
  900. 'path' => CACHE . 'views/',
  901. 'prefix' => '',
  902. ]);
  903. Cache::clear(false, 'test_view');
  904. $View = $this->PostsController->createView();
  905. $View->setElementCache('test_view');
  906. $result = $View->element('test_element', [], ['cache' => true]);
  907. $expected = 'this is the test element';
  908. $this->assertEquals($expected, $result);
  909. $result = Cache::read('element__test_element', 'test_view');
  910. $this->assertEquals($expected, $result);
  911. $result = $View->element('test_element', ['param' => 'one', 'foo' => 'two'], ['cache' => true]);
  912. $this->assertEquals($expected, $result);
  913. $result = Cache::read('element__test_element_param_foo', 'test_view');
  914. $this->assertEquals($expected, $result);
  915. $View->element('test_element', [
  916. 'param' => 'one',
  917. 'foo' => 'two',
  918. ], [
  919. 'cache' => ['key' => 'custom_key'],
  920. ]);
  921. $result = Cache::read('element_custom_key', 'test_view');
  922. $this->assertEquals($expected, $result);
  923. $View->setElementCache('default');
  924. $View->element('test_element', [
  925. 'param' => 'one',
  926. 'foo' => 'two',
  927. ], [
  928. 'cache' => ['config' => 'test_view'],
  929. ]);
  930. $result = Cache::read('element__test_element_param_foo', 'test_view');
  931. $this->assertEquals($expected, $result);
  932. Cache::clear(true, 'test_view');
  933. Cache::drop('test_view');
  934. }
  935. /**
  936. * Test element events
  937. *
  938. * @return void
  939. */
  940. public function testViewEvent()
  941. {
  942. $View = $this->PostsController->createView();
  943. $View->setTemplatePath($this->PostsController->getName());
  944. $View->enableAutoLayout(false);
  945. $listener = new TestViewEventListenerInterface();
  946. $View->getEventManager()->on($listener);
  947. $View->render('index');
  948. $this->assertEquals(View::TYPE_VIEW, $listener->beforeRenderViewType);
  949. $this->assertEquals(View::TYPE_VIEW, $listener->afterRenderViewType);
  950. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  951. $View->element('test_element', [], ['callbacks' => true]);
  952. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  953. $this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType);
  954. $this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType);
  955. }
  956. /**
  957. * Test loading helper using loadHelper().
  958. *
  959. * @return void
  960. */
  961. public function testLoadHelper()
  962. {
  963. $View = new View();
  964. $View->loadHelper('Html', ['foo' => 'bar']);
  965. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html);
  966. $config = $View->Html->getConfig();
  967. $this->assertEquals('bar', $config['foo']);
  968. }
  969. /**
  970. * Test loading helper when duplicate.
  971. *
  972. * @return void
  973. */
  974. public function testLoadHelperDuplicate()
  975. {
  976. $View = new View();
  977. $this->assertNotEmpty($View->loadHelper('Html', ['foo' => 'bar']));
  978. try {
  979. $View->loadHelper('Html', ['test' => 'value']);
  980. $this->fail('No exception');
  981. } catch (\RuntimeException $e) {
  982. $this->assertContains('The "Html" alias has already been loaded', $e->getMessage());
  983. }
  984. }
  985. /**
  986. * Test loadHelpers method
  987. *
  988. * @return void
  989. */
  990. public function testLoadHelpers()
  991. {
  992. $View = new View(null, null, null, [
  993. 'helpers' => ['Html' => ['foo' => 'bar'], 'Form' => ['foo' => 'baz']],
  994. ]);
  995. $result = $View->loadHelpers();
  996. $this->assertSame($View, $result);
  997. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  998. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  999. $config = $View->Html->getConfig();
  1000. $this->assertEquals('bar', $config['foo']);
  1001. $config = $View->Form->getConfig();
  1002. $this->assertEquals('baz', $config['foo']);
  1003. }
  1004. /**
  1005. * Test lazy loading helpers
  1006. *
  1007. * @return void
  1008. */
  1009. public function testLazyLoadHelpers()
  1010. {
  1011. $View = new View();
  1012. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  1013. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  1014. }
  1015. /**
  1016. * Test manipulating class properties in initialize()
  1017. *
  1018. * @return void
  1019. */
  1020. public function testInitialize()
  1021. {
  1022. $View = new TestView();
  1023. $config = $View->Html->getConfig();
  1024. $this->assertEquals('myval', $config['mykey']);
  1025. }
  1026. /**
  1027. * Test the correct triggering of helper callbacks
  1028. *
  1029. * @return void
  1030. */
  1031. public function testHelperCallbackTriggering()
  1032. {
  1033. $View = $this->PostsController->createView();
  1034. $View->setTemplatePath($this->PostsController->getName());
  1035. $manager = $this->getMockBuilder('Cake\Event\EventManager')->getMock();
  1036. $View->setEventManager($manager);
  1037. $manager->expects($this->at(0))->method('dispatch')
  1038. ->with(
  1039. $this->logicalAnd(
  1040. $this->isInstanceOf('Cake\Event\Event'),
  1041. $this->attributeEqualTo('_name', 'View.beforeRender'),
  1042. $this->attributeEqualTo('_subject', $View)
  1043. )
  1044. );
  1045. $manager->expects($this->at(1))->method('dispatch')
  1046. ->with(
  1047. $this->logicalAnd(
  1048. $this->isInstanceOf('Cake\Event\Event'),
  1049. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  1050. $this->attributeEqualTo('_subject', $View)
  1051. )
  1052. );
  1053. $manager->expects($this->at(2))->method('dispatch')
  1054. ->with(
  1055. $this->logicalAnd(
  1056. $this->isInstanceOf('Cake\Event\Event'),
  1057. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  1058. $this->attributeEqualTo('_subject', $View)
  1059. )
  1060. );
  1061. $manager->expects($this->at(3))->method('dispatch')
  1062. ->with(
  1063. $this->logicalAnd(
  1064. $this->isInstanceOf('Cake\Event\Event'),
  1065. $this->attributeEqualTo('_name', 'View.afterRender'),
  1066. $this->attributeEqualTo('_subject', $View)
  1067. )
  1068. );
  1069. $manager->expects($this->at(4))->method('dispatch')
  1070. ->with(
  1071. $this->logicalAnd(
  1072. $this->isInstanceOf('Cake\Event\Event'),
  1073. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  1074. $this->attributeEqualTo('_subject', $View)
  1075. )
  1076. );
  1077. $manager->expects($this->at(5))->method('dispatch')
  1078. ->with(
  1079. $this->logicalAnd(
  1080. $this->isInstanceOf('Cake\Event\Event'),
  1081. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  1082. $this->attributeEqualTo('_subject', $View)
  1083. )
  1084. );
  1085. $manager->expects($this->at(6))->method('dispatch')
  1086. ->with(
  1087. $this->logicalAnd(
  1088. $this->isInstanceOf('Cake\Event\Event'),
  1089. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  1090. $this->attributeEqualTo('_subject', $View)
  1091. )
  1092. );
  1093. $manager->expects($this->at(7))->method('dispatch')
  1094. ->with(
  1095. $this->logicalAnd(
  1096. $this->isInstanceOf('Cake\Event\Event'),
  1097. $this->attributeEqualTo('_name', 'View.afterLayout'),
  1098. $this->attributeEqualTo('_subject', $View)
  1099. )
  1100. );
  1101. $View->render('index');
  1102. }
  1103. /**
  1104. * Test beforeLayout method
  1105. *
  1106. * @return void
  1107. */
  1108. public function testBeforeLayout()
  1109. {
  1110. $this->PostsController->helpers = [
  1111. 'TestBeforeAfter' => ['className' => __NAMESPACE__ . '\TestBeforeAfterHelper'],
  1112. 'Html',
  1113. ];
  1114. $View = $this->PostsController->createView();
  1115. $View->setTemplatePath($this->PostsController->getName());
  1116. $View->render('index');
  1117. $this->assertEquals('Valuation', $View->helpers()->TestBeforeAfter->property);
  1118. }
  1119. /**
  1120. * Test afterLayout method
  1121. *
  1122. * @return void
  1123. */
  1124. public function testAfterLayout()
  1125. {
  1126. $this->PostsController->helpers = [
  1127. 'TestBeforeAfter' => ['className' => __NAMESPACE__ . '\TestBeforeAfterHelper'],
  1128. 'Html',
  1129. ];
  1130. $this->PostsController->set('variable', 'values');
  1131. $View = $this->PostsController->createView();
  1132. $View->setTemplatePath($this->PostsController->getName());
  1133. $content = 'This is my view output';
  1134. $result = $View->renderLayout($content, 'default');
  1135. $this->assertRegExp('/modified in the afterlife/', $result);
  1136. $this->assertRegExp('/This is my view output/', $result);
  1137. }
  1138. /**
  1139. * Test renderLoadHelper method
  1140. *
  1141. * @return void
  1142. */
  1143. public function testRenderLoadHelper()
  1144. {
  1145. $this->PostsController->helpers = ['Form', 'Number'];
  1146. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1147. $View->setTemplatePath($this->PostsController->getName());
  1148. $result = $View->render('index', false);
  1149. $this->assertEquals('posts index', $result);
  1150. $attached = $View->helpers()->loaded();
  1151. // HtmlHelper is loaded in TestView::initialize()
  1152. $this->assertEquals(['Html', 'Form', 'Number'], $attached);
  1153. $this->PostsController->helpers = ['Html', 'Form', 'Number', 'TestPlugin.PluggedHelper'];
  1154. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1155. $View->setTemplatePath($this->PostsController->getName());
  1156. $result = $View->render('index', false);
  1157. $this->assertEquals('posts index', $result);
  1158. $attached = $View->helpers()->loaded();
  1159. $expected = ['Html', 'Form', 'Number', 'PluggedHelper'];
  1160. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  1161. }
  1162. /**
  1163. * Test render method
  1164. *
  1165. * @return void
  1166. */
  1167. public function testRender()
  1168. {
  1169. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1170. $View->setTemplatePath($this->PostsController->getName());
  1171. $result = $View->render('index');
  1172. $this->assertRegExp("/<meta charset=\"utf-8\"\/>\s*<title>/", $result);
  1173. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1174. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1175. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1176. $result = $View->render(false, 'ajax2');
  1177. $this->assertRegExp('/Ajax\!/', $result);
  1178. $this->assertNull($View->render(false, 'ajax2'));
  1179. $this->PostsController->helpers = ['Html'];
  1180. $this->PostsController->setRequest($this->PostsController->getRequest()->withParam('action', 'index'));
  1181. Configure::write('Cache.check', true);
  1182. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1183. $View->setTemplatePath($this->PostsController->getName());
  1184. $result = $View->render('index');
  1185. $this->assertRegExp("/<meta charset=\"utf-8\"\/>\s*<title>/", $result);
  1186. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1187. }
  1188. /**
  1189. * Test that View::$view works
  1190. *
  1191. * @return void
  1192. */
  1193. public function testRenderUsingViewProperty()
  1194. {
  1195. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1196. $View->setTemplatePath($this->PostsController->getName());
  1197. $View->setTemplate('cache_form');
  1198. $this->assertEquals('cache_form', $View->getTemplate());
  1199. $result = $View->render();
  1200. $this->assertRegExp('/Add User/', $result);
  1201. }
  1202. /**
  1203. * Test that layout set from view file takes precedence over layout set
  1204. * as argument to render().
  1205. *
  1206. * @return void
  1207. */
  1208. public function testRenderUsingLayoutArgument()
  1209. {
  1210. $error = new \PDOException();
  1211. $error->queryString = 'this is sql string';
  1212. $message = 'it works';
  1213. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1214. $View->set(compact('error', 'message'));
  1215. $View->setTemplatePath('Error');
  1216. $result = $View->render('pdo_error', 'error');
  1217. $this->assertRegExp('/this is sql string/', $result);
  1218. $this->assertRegExp('/it works/', $result);
  1219. }
  1220. /**
  1221. * Test render()ing a file in a subdir from a custom viewPath
  1222. * in a plugin.
  1223. *
  1224. * @return void
  1225. */
  1226. public function testGetViewFileNameSubdirWithPluginAndViewPath()
  1227. {
  1228. $this->PostsController->setPlugin('TestPlugin');
  1229. $this->PostsController->setName('Posts');
  1230. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1231. $View->setTemplatePath('Element');
  1232. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  1233. $result = $View->getViewFileName('sub_dir/sub_element');
  1234. $expected = $pluginPath . 'src' . DS . 'Template' . DS . 'Element' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  1235. $this->assertPathEquals($expected, $result);
  1236. }
  1237. /**
  1238. * Test that view vars can replace the local helper variables
  1239. * and not overwrite the $this->Helper references
  1240. *
  1241. * @return void
  1242. */
  1243. public function testViewVarOverwritingLocalHelperVar()
  1244. {
  1245. $Controller = new ViewPostsController();
  1246. $Controller->helpers = ['Html'];
  1247. $Controller->set('html', 'I am some test html');
  1248. $View = $Controller->createView();
  1249. $View->setTemplatePath($Controller->getName());
  1250. $result = $View->render('helper_overwrite', false);
  1251. $this->assertRegExp('/I am some test html/', $result);
  1252. $this->assertRegExp('/Test link/', $result);
  1253. }
  1254. /**
  1255. * Test getViewFileName method
  1256. *
  1257. * @return void
  1258. */
  1259. public function testViewFileName()
  1260. {
  1261. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1262. $View->setTemplatePath('Posts');
  1263. $result = $View->getViewFileName('index');
  1264. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1265. $result = $View->getViewFileName('TestPlugin.index');
  1266. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1267. $result = $View->getViewFileName('/Pages/home');
  1268. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  1269. $result = $View->getViewFileName('../Element/test_element');
  1270. $this->assertRegExp('/Element(\/|\\\)test_element.ctp/', $result);
  1271. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  1272. $result = $View->getViewFileName('../Posts/index');
  1273. $this->assertPathEquals($expected, $result);
  1274. }
  1275. /**
  1276. * Test creating a block with capturing output.
  1277. *
  1278. * @return void
  1279. */
  1280. public function testBlockCaptureOverwrite()
  1281. {
  1282. $result = $this->View->start('test');
  1283. $this->assertSame($this->View, $result);
  1284. echo 'Block content';
  1285. $result = $this->View->end();
  1286. $this->assertSame($this->View, $result);
  1287. $this->View->start('test');
  1288. echo 'New content';
  1289. $this->View->end();
  1290. $result = $this->View->fetch('test');
  1291. $this->assertEquals('New content', $result);
  1292. }
  1293. /**
  1294. * Test that blocks can be fetched inside a block with the same name
  1295. *
  1296. * @return void
  1297. */
  1298. public function testBlockExtend()
  1299. {
  1300. $this->View->start('test');
  1301. echo 'Block content';
  1302. $this->View->end();
  1303. $this->View->start('test');
  1304. echo $this->View->fetch('test');
  1305. echo 'New content';
  1306. $this->View->end();
  1307. $result = $this->View->fetch('test');
  1308. $this->assertEquals('Block contentNew content', $result);
  1309. }
  1310. /**
  1311. * Test creating a block with capturing output.
  1312. *
  1313. * @return void
  1314. */
  1315. public function testBlockCapture()
  1316. {
  1317. $this->View->start('test');
  1318. echo 'Block content';
  1319. $this->View->end();
  1320. $result = $this->View->fetch('test');
  1321. $this->assertEquals('Block content', $result);
  1322. }
  1323. /**
  1324. * Test appending to a block with capturing output.
  1325. *
  1326. * @return void
  1327. */
  1328. public function testBlockAppendCapture()
  1329. {
  1330. $this->View->start('test');
  1331. echo 'Content ';
  1332. $this->View->end();
  1333. $result = $this->View->append('test');
  1334. $this->assertSame($this->View, $result);
  1335. echo 'appended';
  1336. $this->View->end();
  1337. $result = $this->View->fetch('test');
  1338. $this->assertEquals('Content appended', $result);
  1339. }
  1340. /**
  1341. * Test setting a block's content.
  1342. *
  1343. * @return void
  1344. */
  1345. public function testBlockSet()
  1346. {
  1347. $result = $this->View->assign('test', 'Block content');
  1348. $this->assertSame($this->View, $result);
  1349. $result = $this->View->fetch('test');
  1350. $this->assertEquals('Block content', $result);
  1351. }
  1352. /**
  1353. * Test resetting a block's content.
  1354. *
  1355. * @return void
  1356. */
  1357. public function testBlockReset()
  1358. {
  1359. $this->View->assign('test', '');
  1360. $result = $this->View->fetch('test', 'This should not be returned');
  1361. $this->assertSame('', $result);
  1362. }
  1363. /**
  1364. * Test resetting a block's content with reset.
  1365. *
  1366. * @return void
  1367. */
  1368. public function testBlockResetFunc()
  1369. {
  1370. $this->View->assign('test', 'Block content');
  1371. $result = $this->View->fetch('test', 'This should not be returned');
  1372. $this->assertSame('Block content', $result);
  1373. $result = $this->View->reset('test');
  1374. $this->assertSame($this->View, $result);
  1375. $result = $this->View->fetch('test', 'This should not be returned');
  1376. $this->assertSame('', $result);
  1377. }
  1378. /**
  1379. * Test checking a block's existence.
  1380. *
  1381. * @return void
  1382. */
  1383. public function testBlockExist()
  1384. {
  1385. $this->assertFalse($this->View->exists('test'));
  1386. $this->View->assign('test', 'Block content');
  1387. $this->assertTrue($this->View->exists('test'));
  1388. }
  1389. /**
  1390. * Test setting a block's content to null
  1391. *
  1392. * @return void
  1393. */
  1394. public function testBlockSetNull()
  1395. {
  1396. $this->View->assign('testWithNull', null);
  1397. $result = $this->View->fetch('testWithNull');
  1398. $this->assertSame('', $result);
  1399. }
  1400. /**
  1401. * Test setting a block's content to an object with __toString magic method
  1402. *
  1403. * @return void
  1404. */
  1405. public function testBlockSetObjectWithToString()
  1406. {
  1407. $objectWithToString = new TestObjectWithToString();
  1408. $this->View->assign('testWithObjectWithToString', $objectWithToString);
  1409. $result = $this->View->fetch('testWithObjectWithToString');
  1410. $this->assertSame("I'm ObjectWithToString", $result);
  1411. }
  1412. /**
  1413. * Test setting a block's content to an object without __toString magic method
  1414. *
  1415. * @return void
  1416. */
  1417. public function testBlockSetObjectWithoutToString()
  1418. {
  1419. $this->checkException(
  1420. 'Object of class ' . TestObjectWithoutToString::class . ' could not be converted to string'
  1421. );
  1422. $objectWithToString = new TestObjectWithoutToString();
  1423. $this->View->assign('testWithObjectWithoutToString', $objectWithToString);
  1424. }
  1425. /**
  1426. * Test setting a block's content to a decimal
  1427. *
  1428. * @return void
  1429. */
  1430. public function testBlockSetDecimal()
  1431. {
  1432. $this->View->assign('testWithDecimal', 1.23456789);
  1433. $result = $this->View->fetch('testWithDecimal');
  1434. $this->assertEquals('1.23456789', $result);
  1435. }
  1436. /**
  1437. * Data provider for block related tests.
  1438. *
  1439. * @return array
  1440. */
  1441. public static function blockValueProvider()
  1442. {
  1443. return [
  1444. 'string' => ['A string value'],
  1445. 'decimal' => [1.23456],
  1446. 'object with __toString' => [new TestObjectWithToString()],
  1447. ];
  1448. }
  1449. /**
  1450. * Test appending to a block with append.
  1451. *
  1452. * @param mixed $value Value
  1453. * @return void
  1454. * @dataProvider blockValueProvider
  1455. */
  1456. public function testBlockAppend($value)
  1457. {
  1458. $this->View->assign('testBlock', 'Block');
  1459. $this->View->append('testBlock', $value);
  1460. $result = $this->View->fetch('testBlock');
  1461. $this->assertSame('Block' . $value, $result);
  1462. }
  1463. /**
  1464. * Test appending an object without __toString magic method to a block with append.
  1465. *
  1466. * @return void
  1467. */
  1468. public function testBlockAppendObjectWithoutToString()
  1469. {
  1470. $this->checkException(
  1471. 'Object of class ' . TestObjectWithoutToString::class . ' could not be converted to string'
  1472. );
  1473. $object = new TestObjectWithoutToString();
  1474. $this->View->assign('testBlock', 'Block ');
  1475. $this->View->append('testBlock', $object);
  1476. }
  1477. /**
  1478. * Test prepending to a block with prepend.
  1479. *
  1480. * @param mixed $value Value
  1481. * @return void
  1482. * @dataProvider blockValueProvider
  1483. */
  1484. public function testBlockPrepend($value)
  1485. {
  1486. $this->View->assign('test', 'Block');
  1487. $result = $this->View->prepend('test', $value);
  1488. $this->assertSame($this->View, $result);
  1489. $result = $this->View->fetch('test');
  1490. $this->assertEquals($value . 'Block', $result);
  1491. }
  1492. /**
  1493. * Test prepending an object without __toString magic method to a block with prepend.
  1494. *
  1495. * @return void
  1496. */
  1497. public function testBlockPrependObjectWithoutToString()
  1498. {
  1499. $this->checkException(
  1500. 'Object of class ' . TestObjectWithoutToString::class . ' could not be converted to string'
  1501. );
  1502. $object = new TestObjectWithoutToString();
  1503. $this->View->assign('test', 'Block ');
  1504. $this->View->prepend('test', $object);
  1505. }
  1506. /**
  1507. * You should be able to append to undefined blocks.
  1508. *
  1509. * @return void
  1510. */
  1511. public function testBlockAppendUndefined()
  1512. {
  1513. $result = $this->View->append('test', 'Unknown');
  1514. $this->assertSame($this->View, $result);
  1515. $result = $this->View->fetch('test');
  1516. $this->assertEquals('Unknown', $result);
  1517. }
  1518. /**
  1519. * You should be able to prepend to undefined blocks.
  1520. *
  1521. * @return void
  1522. */
  1523. public function testBlockPrependUndefined()
  1524. {
  1525. $this->View->prepend('test', 'Unknown');
  1526. $result = $this->View->fetch('test');
  1527. $this->assertEquals('Unknown', $result);
  1528. }
  1529. /**
  1530. * Test getting block names
  1531. *
  1532. * @return void
  1533. */
  1534. public function testBlocks()
  1535. {
  1536. $this->View->append('test', 'one');
  1537. $this->View->assign('test1', 'one');
  1538. $this->assertEquals(['test', 'test1'], $this->View->blocks());
  1539. }
  1540. /**
  1541. * Test that blocks can be nested.
  1542. *
  1543. * @return void
  1544. */
  1545. public function testNestedBlocks()
  1546. {
  1547. $this->View->start('first');
  1548. echo 'In first ';
  1549. $this->View->start('second');
  1550. echo 'In second';
  1551. $this->View->end();
  1552. echo 'In first';
  1553. $this->View->end();
  1554. $this->assertEquals('In first In first', $this->View->fetch('first'));
  1555. $this->assertEquals('In second', $this->View->fetch('second'));
  1556. }
  1557. /**
  1558. * Test that starting the same block twice throws an exception
  1559. *
  1560. * @return void
  1561. */
  1562. public function testStartBlocksTwice()
  1563. {
  1564. try {
  1565. $this->View->start('first');
  1566. $this->View->start('first');
  1567. $this->fail('No exception');
  1568. } catch (\Cake\Core\Exception\Exception $e) {
  1569. ob_end_clean();
  1570. $this->assertTrue(true);
  1571. }
  1572. }
  1573. /**
  1574. * Test that an exception gets thrown when you leave a block open at the end
  1575. * of a view.
  1576. *
  1577. * @return void
  1578. */
  1579. public function testExceptionOnOpenBlock()
  1580. {
  1581. try {
  1582. $this->View->render('open_block');
  1583. $this->fail('No exception');
  1584. } catch (\LogicException $e) {
  1585. ob_end_clean();
  1586. $this->assertContains('The "no_close" block was left open', $e->getMessage());
  1587. }
  1588. }
  1589. /**
  1590. * Test nested extended views.
  1591. *
  1592. * @return void
  1593. */
  1594. public function testExtendNested()
  1595. {
  1596. $this->View->setLayout(false);
  1597. $content = $this->View->render('nested_extends');
  1598. $expected = <<<TEXT
  1599. This is the second parent.
  1600. This is the first parent.
  1601. This is the first template.
  1602. Sidebar Content.
  1603. TEXT;
  1604. $this->assertEquals($expected, $content);
  1605. }
  1606. /**
  1607. * Make sure that extending the current view with itself causes an exception
  1608. *
  1609. * @return void
  1610. */
  1611. public function testExtendSelf()
  1612. {
  1613. try {
  1614. $this->View->setLayout(false);
  1615. $this->View->render('extend_self');
  1616. $this->fail('No exception');
  1617. } catch (\LogicException $e) {
  1618. ob_end_clean();
  1619. $this->assertContains('cannot have views extend themselves', $e->getMessage());
  1620. }
  1621. }
  1622. /**
  1623. * Make sure that extending in a loop causes an exception
  1624. *
  1625. * @return void
  1626. */
  1627. public function testExtendLoop()
  1628. {
  1629. try {
  1630. $this->View->setLayout(false);
  1631. $this->View->render('extend_loop');
  1632. $this->fail('No exception');
  1633. } catch (\LogicException $e) {
  1634. ob_end_clean();
  1635. $this->assertContains('cannot have views extend in a loop', $e->getMessage());
  1636. }
  1637. }
  1638. /**
  1639. * Test extend() in an element and a view.
  1640. *
  1641. * @return void
  1642. */
  1643. public function testExtendElement()
  1644. {
  1645. $this->View->setLayout(false);
  1646. $content = $this->View->render('extend_element');
  1647. $expected = <<<TEXT
  1648. Parent View.
  1649. View content.
  1650. Parent Element.
  1651. Element content.
  1652. TEXT;
  1653. $this->assertEquals($expected, $content);
  1654. }
  1655. /**
  1656. * Test extend() in an element and a view.
  1657. *
  1658. * @return void
  1659. */
  1660. public function testExtendPrefixElement()
  1661. {
  1662. $this->View->setRequest($this->View->getRequest()->withParam('prefix', 'Admin'));
  1663. $this->View->setLayout(false);
  1664. $content = $this->View->render('extend_element');
  1665. $expected = <<<TEXT
  1666. Parent View.
  1667. View content.
  1668. Parent Element.
  1669. Prefix Element content.
  1670. TEXT;
  1671. $this->assertEquals($expected, $content);
  1672. }
  1673. /**
  1674. * Extending an element which doesn't exist should throw a missing view exception
  1675. *
  1676. * @return void
  1677. */
  1678. public function testExtendMissingElement()
  1679. {
  1680. try {
  1681. $this->View->setLayout(false);
  1682. $this->View->render('extend_missing_element');
  1683. $this->fail('No exception');
  1684. } catch (\LogicException $e) {
  1685. ob_end_clean();
  1686. ob_end_clean();
  1687. $this->assertContains('element', $e->getMessage());
  1688. }
  1689. }
  1690. /**
  1691. * Test extend() preceded by an element()
  1692. *
  1693. * @return void
  1694. */
  1695. public function testExtendWithElementBeforeExtend()
  1696. {
  1697. $this->View->setLayout(false);
  1698. $result = $this->View->render('extend_with_element');
  1699. $expected = <<<TEXT
  1700. Parent View.
  1701. this is the test elementThe view
  1702. TEXT;
  1703. $this->assertEquals($expected, $result);
  1704. }
  1705. /**
  1706. * Test extend() preceded by an element()
  1707. *
  1708. * @return void
  1709. */
  1710. public function testExtendWithPrefixElementBeforeExtend()
  1711. {
  1712. $this->View->setRequest($this->View->getRequest()->withParam('prefix', 'Admin'));
  1713. $this->View->setLayout(false);
  1714. $result = $this->View->render('extend_with_element');
  1715. $expected = <<<TEXT
  1716. Parent View.
  1717. this is the test prefix elementThe view
  1718. TEXT;
  1719. $this->assertEquals($expected, $result);
  1720. }
  1721. /**
  1722. * Test memory leaks that existed in _paths at one point.
  1723. *
  1724. * @return void
  1725. */
  1726. public function testMemoryLeakInPaths()
  1727. {
  1728. $this->skipIf(env('CODECOVERAGE') == 1, 'Running coverage this causes this tests to fail sometimes.');
  1729. $this->ThemeController->setPlugin(null);
  1730. $this->ThemeController->setName('Posts');
  1731. $View = $this->ThemeController->createView();
  1732. $View->setTemplatePath('Posts');
  1733. $View->setLayout('whatever');
  1734. $View->setTheme('TestTheme');
  1735. $View->element('test_element');
  1736. $start = memory_get_usage();
  1737. for ($i = 0; $i < 10; $i++) {
  1738. $View->element('test_element');
  1739. }
  1740. $end = memory_get_usage();
  1741. $this->assertLessThanOrEqual($start + 15000, $end);
  1742. }
  1743. /**
  1744. * Tests that a view block uses default value when not assigned and uses assigned value when it is
  1745. *
  1746. * @return void
  1747. */
  1748. public function testBlockDefaultValue()
  1749. {
  1750. $default = 'Default';
  1751. $result = $this->View->fetch('title', $default);
  1752. $this->assertEquals($default, $result);
  1753. $expected = 'My Title';
  1754. $this->View->assign('title', $expected);
  1755. $result = $this->View->fetch('title', $default);
  1756. $this->assertEquals($expected, $result);
  1757. }
  1758. /**
  1759. * Tests that a view variable uses default value when not assigned and uses assigned value when it is
  1760. *
  1761. * @return void
  1762. */
  1763. public function testViewVarDefaultValue()
  1764. {
  1765. $default = 'Default';
  1766. $result = $this->View->get('title', $default);
  1767. $this->assertEquals($default, $result);
  1768. $expected = 'Back to the Future';
  1769. $this->View->set('title', $expected);
  1770. $result = $this->View->get('title', $default);
  1771. $this->assertEquals($expected, $result);
  1772. }
  1773. /**
  1774. * Test the helpers() method.
  1775. *
  1776. * @return void
  1777. */
  1778. public function testHelpers()
  1779. {
  1780. $this->assertInstanceOf('Cake\View\HelperRegistry', $this->View->helpers());
  1781. $result = $this->View->helpers();
  1782. $this->assertSame($result, $this->View->helpers());
  1783. }
  1784. /**
  1785. * Test getTemplatePath() and setTemplatePath().
  1786. *
  1787. * @return void
  1788. */
  1789. public function testGetSetTemplatePath()
  1790. {
  1791. $result = $this->View->setTemplatePath('foo');
  1792. $this->assertSame($this->View, $result);
  1793. $templatePath = $this->View->getTemplatePath();
  1794. $this->assertSame($templatePath, 'foo');
  1795. }
  1796. /**
  1797. * Test getLayoutPath() and setLayoutPath().
  1798. *
  1799. * @return void
  1800. */
  1801. public function testGetSetLayoutPath()
  1802. {
  1803. $result = $this->View->setLayoutPath('foo');
  1804. $this->assertSame($this->View, $result);
  1805. $layoutPath = $this->View->getLayoutPath();
  1806. $this->assertSame($layoutPath, 'foo');
  1807. }
  1808. /**
  1809. * Test isAutoLayoutEnabled() and enableAutoLayout().
  1810. *
  1811. * @return void
  1812. */
  1813. public function testAutoLayout()
  1814. {
  1815. $result = $this->View->enableAutoLayout(false);
  1816. $this->assertSame($this->View, $result);
  1817. $autoLayout = $this->View->isAutoLayoutEnabled();
  1818. $this->assertSame($autoLayout, false);
  1819. $this->View->enableAutoLayout();
  1820. $autoLayout = $this->View->isAutoLayoutEnabled();
  1821. $this->assertSame($autoLayout, true);
  1822. }
  1823. /**
  1824. * testDisableAutoLayout
  1825. *
  1826. * @return void
  1827. */
  1828. public function testDisableAutoLayout()
  1829. {
  1830. $this->assertTrue($this->View->isAutoLayoutEnabled());
  1831. $result = $this->View->disableAutoLayout();
  1832. $this->assertSame($this->View, $result);
  1833. $autoLayout = $this->View->isAutoLayoutEnabled();
  1834. $this->assertFalse($this->View->isAutoLayoutEnabled());
  1835. }
  1836. /**
  1837. * Test getTheme() and setTheme().
  1838. *
  1839. * @return void
  1840. */
  1841. public function testGetSetTheme()
  1842. {
  1843. $result = $this->View->setTheme('foo');
  1844. $this->assertSame($this->View, $result);
  1845. $theme = $this->View->getTheme();
  1846. $this->assertSame($theme, 'foo');
  1847. }
  1848. /**
  1849. * Test getTemplate() and setTemplate().
  1850. *
  1851. * @return void
  1852. */
  1853. public function testGetSetTemplate()
  1854. {
  1855. $result = $this->View->setTemplate('foo');
  1856. $this->assertSame($this->View, $result);
  1857. $template = $this->View->getTemplate();
  1858. $this->assertSame($template, 'foo');
  1859. }
  1860. /**
  1861. * Test setLayout() and getLayout().
  1862. *
  1863. * @return void
  1864. */
  1865. public function testGetSetLayout()
  1866. {
  1867. $result = $this->View->setLayout('foo');
  1868. $this->assertSame($this->View, $result);
  1869. $layout = $this->View->getLayout();
  1870. $this->assertSame($layout, 'foo');
  1871. }
  1872. /**
  1873. * Test getName() and getPlugin().
  1874. *
  1875. * @return void
  1876. */
  1877. public function testGetNamePlugin()
  1878. {
  1879. $this->assertSame('Posts', $this->View->getName());
  1880. $this->assertNull($this->View->getPlugin());
  1881. $this->assertSame($this->View, $this->View->setPlugin('TestPlugin'));
  1882. $this->assertSame('TestPlugin', $this->View->getPlugin());
  1883. }
  1884. /**
  1885. * Test testHasRendered property
  1886. *
  1887. * @return void
  1888. */
  1889. public function testHasRendered()
  1890. {
  1891. $this->assertFalse($this->View->hasRendered);
  1892. $this->View->render('index');
  1893. $this->assertTrue($this->View->hasRendered);
  1894. }
  1895. /**
  1896. * Test magic getter and setter for removed properties.
  1897. *
  1898. * @group deprecated
  1899. * @return void
  1900. */
  1901. public function testMagicGetterSetter()
  1902. {
  1903. $this->deprecated(function () {
  1904. $View = $this->View;
  1905. $View->view = 'myview';
  1906. $this->assertEquals('myview', $View->template());
  1907. $this->assertEquals('myview', $View->view);
  1908. $View->viewPath = 'mypath';
  1909. $this->assertEquals('mypath', $View->templatePath());
  1910. $this->assertEquals('mypath', $View->templatePath);
  1911. });
  1912. }
  1913. protected function checkException($message)
  1914. {
  1915. if (version_compare(PHP_VERSION, '7.4', '>=')) {
  1916. $this->expectException(\Error::class);
  1917. } else {
  1918. $this->expectException(\PHPUnit\Framework\Error\Error::class);
  1919. }
  1920. $this->expectExceptionMessage($message);
  1921. }
  1922. }