ViewTest.php 64 KB

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