ViewTest.php 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288
  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 elementCache method with namespaces and subfolder
  937. *
  938. * @return void
  939. */
  940. public function testElementCacheSubfolder()
  941. {
  942. Cache::drop('test_view');
  943. Cache::setConfig('test_view', [
  944. 'engine' => 'File',
  945. 'duration' => '+1 day',
  946. 'path' => CACHE . 'views/',
  947. 'prefix' => '',
  948. ]);
  949. Cache::clear(true, 'test_view');
  950. $View = $this->PostsController->createView();
  951. $View->setElementCache('test_view');
  952. $result = $View->element('subfolder/test_element', [], ['cache' => true]);
  953. $expected = 'this is the test element in subfolder';
  954. $this->assertEquals($expected, trim($result));
  955. $result = Cache::read('element__subfolder_test_element', 'test_view');
  956. $this->assertEquals($expected, trim($result));
  957. }
  958. /**
  959. * Test element events
  960. *
  961. * @return void
  962. */
  963. public function testViewEvent()
  964. {
  965. $View = $this->PostsController->createView();
  966. $View->setTemplatePath($this->PostsController->getName());
  967. $View->enableAutoLayout(false);
  968. $listener = new TestViewEventListenerInterface();
  969. $View->getEventManager()->on($listener);
  970. $View->render('index');
  971. $this->assertEquals(View::TYPE_VIEW, $listener->beforeRenderViewType);
  972. $this->assertEquals(View::TYPE_VIEW, $listener->afterRenderViewType);
  973. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  974. $View->element('test_element', [], ['callbacks' => true]);
  975. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  976. $this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType);
  977. $this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType);
  978. }
  979. /**
  980. * Test loading helper using loadHelper().
  981. *
  982. * @return void
  983. */
  984. public function testLoadHelper()
  985. {
  986. $View = new View();
  987. $View->loadHelper('Html', ['foo' => 'bar']);
  988. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html);
  989. $config = $View->Html->getConfig();
  990. $this->assertEquals('bar', $config['foo']);
  991. }
  992. /**
  993. * Test loading helper when duplicate.
  994. *
  995. * @return void
  996. */
  997. public function testLoadHelperDuplicate()
  998. {
  999. $View = new View();
  1000. $this->assertNotEmpty($View->loadHelper('Html', ['foo' => 'bar']));
  1001. try {
  1002. $View->loadHelper('Html', ['test' => 'value']);
  1003. $this->fail('No exception');
  1004. } catch (\RuntimeException $e) {
  1005. $this->assertContains('The "Html" alias has already been loaded', $e->getMessage());
  1006. }
  1007. }
  1008. /**
  1009. * Test loadHelpers method
  1010. *
  1011. * @return void
  1012. */
  1013. public function testLoadHelpers()
  1014. {
  1015. $View = new View(null, null, null, [
  1016. 'helpers' => ['Html' => ['foo' => 'bar'], 'Form' => ['foo' => 'baz']],
  1017. ]);
  1018. $result = $View->loadHelpers();
  1019. $this->assertSame($View, $result);
  1020. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  1021. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  1022. $config = $View->Html->getConfig();
  1023. $this->assertEquals('bar', $config['foo']);
  1024. $config = $View->Form->getConfig();
  1025. $this->assertEquals('baz', $config['foo']);
  1026. }
  1027. /**
  1028. * Test lazy loading helpers
  1029. *
  1030. * @return void
  1031. */
  1032. public function testLazyLoadHelpers()
  1033. {
  1034. $View = new View();
  1035. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  1036. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  1037. }
  1038. /**
  1039. * Test manipulating class properties in initialize()
  1040. *
  1041. * @return void
  1042. */
  1043. public function testInitialize()
  1044. {
  1045. $View = new TestView();
  1046. $config = $View->Html->getConfig();
  1047. $this->assertEquals('myval', $config['mykey']);
  1048. }
  1049. /**
  1050. * Test the correct triggering of helper callbacks
  1051. *
  1052. * @return void
  1053. */
  1054. public function testHelperCallbackTriggering()
  1055. {
  1056. $View = $this->PostsController->createView();
  1057. $View->setTemplatePath($this->PostsController->getName());
  1058. $manager = $this->getMockBuilder('Cake\Event\EventManager')->getMock();
  1059. $View->setEventManager($manager);
  1060. $manager->expects($this->at(0))->method('dispatch')
  1061. ->with(
  1062. $this->logicalAnd(
  1063. $this->isInstanceOf('Cake\Event\Event'),
  1064. $this->attributeEqualTo('_name', 'View.beforeRender'),
  1065. $this->attributeEqualTo('_subject', $View)
  1066. )
  1067. );
  1068. $manager->expects($this->at(1))->method('dispatch')
  1069. ->with(
  1070. $this->logicalAnd(
  1071. $this->isInstanceOf('Cake\Event\Event'),
  1072. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  1073. $this->attributeEqualTo('_subject', $View)
  1074. )
  1075. );
  1076. $manager->expects($this->at(2))->method('dispatch')
  1077. ->with(
  1078. $this->logicalAnd(
  1079. $this->isInstanceOf('Cake\Event\Event'),
  1080. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  1081. $this->attributeEqualTo('_subject', $View)
  1082. )
  1083. );
  1084. $manager->expects($this->at(3))->method('dispatch')
  1085. ->with(
  1086. $this->logicalAnd(
  1087. $this->isInstanceOf('Cake\Event\Event'),
  1088. $this->attributeEqualTo('_name', 'View.afterRender'),
  1089. $this->attributeEqualTo('_subject', $View)
  1090. )
  1091. );
  1092. $manager->expects($this->at(4))->method('dispatch')
  1093. ->with(
  1094. $this->logicalAnd(
  1095. $this->isInstanceOf('Cake\Event\Event'),
  1096. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  1097. $this->attributeEqualTo('_subject', $View)
  1098. )
  1099. );
  1100. $manager->expects($this->at(5))->method('dispatch')
  1101. ->with(
  1102. $this->logicalAnd(
  1103. $this->isInstanceOf('Cake\Event\Event'),
  1104. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  1105. $this->attributeEqualTo('_subject', $View)
  1106. )
  1107. );
  1108. $manager->expects($this->at(6))->method('dispatch')
  1109. ->with(
  1110. $this->logicalAnd(
  1111. $this->isInstanceOf('Cake\Event\Event'),
  1112. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  1113. $this->attributeEqualTo('_subject', $View)
  1114. )
  1115. );
  1116. $manager->expects($this->at(7))->method('dispatch')
  1117. ->with(
  1118. $this->logicalAnd(
  1119. $this->isInstanceOf('Cake\Event\Event'),
  1120. $this->attributeEqualTo('_name', 'View.afterLayout'),
  1121. $this->attributeEqualTo('_subject', $View)
  1122. )
  1123. );
  1124. $View->render('index');
  1125. }
  1126. /**
  1127. * Test beforeLayout method
  1128. *
  1129. * @return void
  1130. */
  1131. public function testBeforeLayout()
  1132. {
  1133. $this->PostsController->helpers = [
  1134. 'TestBeforeAfter' => ['className' => __NAMESPACE__ . '\TestBeforeAfterHelper'],
  1135. 'Html',
  1136. ];
  1137. $View = $this->PostsController->createView();
  1138. $View->setTemplatePath($this->PostsController->getName());
  1139. $View->render('index');
  1140. $this->assertEquals('Valuation', $View->helpers()->TestBeforeAfter->property);
  1141. }
  1142. /**
  1143. * Test afterLayout method
  1144. *
  1145. * @return void
  1146. */
  1147. public function testAfterLayout()
  1148. {
  1149. $this->PostsController->helpers = [
  1150. 'TestBeforeAfter' => ['className' => __NAMESPACE__ . '\TestBeforeAfterHelper'],
  1151. 'Html',
  1152. ];
  1153. $this->PostsController->set('variable', 'values');
  1154. $View = $this->PostsController->createView();
  1155. $View->setTemplatePath($this->PostsController->getName());
  1156. $content = 'This is my view output';
  1157. $result = $View->renderLayout($content, 'default');
  1158. $this->assertRegExp('/modified in the afterlife/', $result);
  1159. $this->assertRegExp('/This is my view output/', $result);
  1160. }
  1161. /**
  1162. * Test renderLoadHelper method
  1163. *
  1164. * @return void
  1165. */
  1166. public function testRenderLoadHelper()
  1167. {
  1168. $this->PostsController->helpers = ['Form', 'Number'];
  1169. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1170. $View->setTemplatePath($this->PostsController->getName());
  1171. $result = $View->render('index', false);
  1172. $this->assertEquals('posts index', $result);
  1173. $attached = $View->helpers()->loaded();
  1174. // HtmlHelper is loaded in TestView::initialize()
  1175. $this->assertEquals(['Html', 'Form', 'Number'], $attached);
  1176. $this->PostsController->helpers = ['Html', 'Form', 'Number', 'TestPlugin.PluggedHelper'];
  1177. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1178. $View->setTemplatePath($this->PostsController->getName());
  1179. $result = $View->render('index', false);
  1180. $this->assertEquals('posts index', $result);
  1181. $attached = $View->helpers()->loaded();
  1182. $expected = ['Html', 'Form', 'Number', 'PluggedHelper'];
  1183. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  1184. }
  1185. /**
  1186. * Test render method
  1187. *
  1188. * @return void
  1189. */
  1190. public function testRender()
  1191. {
  1192. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1193. $View->setTemplatePath($this->PostsController->getName());
  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. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1198. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1199. $result = $View->render(false, 'ajax2');
  1200. $this->assertRegExp('/Ajax\!/', $result);
  1201. $this->assertNull($View->render(false, 'ajax2'));
  1202. $this->PostsController->helpers = ['Html'];
  1203. $this->PostsController->setRequest($this->PostsController->getRequest()->withParam('action', 'index'));
  1204. Configure::write('Cache.check', true);
  1205. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1206. $View->setTemplatePath($this->PostsController->getName());
  1207. $result = $View->render('index');
  1208. $this->assertRegExp("/<meta charset=\"utf-8\"\/>\s*<title>/", $result);
  1209. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1210. }
  1211. /**
  1212. * Test that View::$view works
  1213. *
  1214. * @return void
  1215. */
  1216. public function testRenderUsingViewProperty()
  1217. {
  1218. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1219. $View->setTemplatePath($this->PostsController->getName());
  1220. $View->setTemplate('cache_form');
  1221. $this->assertEquals('cache_form', $View->getTemplate());
  1222. $result = $View->render();
  1223. $this->assertRegExp('/Add User/', $result);
  1224. }
  1225. /**
  1226. * Test that layout set from view file takes precedence over layout set
  1227. * as argument to render().
  1228. *
  1229. * @return void
  1230. */
  1231. public function testRenderUsingLayoutArgument()
  1232. {
  1233. $error = new \PDOException();
  1234. $error->queryString = 'this is sql string';
  1235. $message = 'it works';
  1236. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1237. $View->set(compact('error', 'message'));
  1238. $View->setTemplatePath('Error');
  1239. $result = $View->render('pdo_error', 'error');
  1240. $this->assertRegExp('/this is sql string/', $result);
  1241. $this->assertRegExp('/it works/', $result);
  1242. }
  1243. /**
  1244. * Test render()ing a file in a subdir from a custom viewPath
  1245. * in a plugin.
  1246. *
  1247. * @return void
  1248. */
  1249. public function testGetViewFileNameSubdirWithPluginAndViewPath()
  1250. {
  1251. $this->PostsController->setPlugin('TestPlugin');
  1252. $this->PostsController->setName('Posts');
  1253. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1254. $View->setTemplatePath('Element');
  1255. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  1256. $result = $View->getViewFileName('sub_dir/sub_element');
  1257. $expected = $pluginPath . 'src' . DS . 'Template' . DS . 'Element' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  1258. $this->assertPathEquals($expected, $result);
  1259. }
  1260. /**
  1261. * Test that view vars can replace the local helper variables
  1262. * and not overwrite the $this->Helper references
  1263. *
  1264. * @return void
  1265. */
  1266. public function testViewVarOverwritingLocalHelperVar()
  1267. {
  1268. $Controller = new ViewPostsController();
  1269. $Controller->helpers = ['Html'];
  1270. $Controller->set('html', 'I am some test html');
  1271. $View = $Controller->createView();
  1272. $View->setTemplatePath($Controller->getName());
  1273. $result = $View->render('helper_overwrite', false);
  1274. $this->assertRegExp('/I am some test html/', $result);
  1275. $this->assertRegExp('/Test link/', $result);
  1276. }
  1277. /**
  1278. * Test getViewFileName method
  1279. *
  1280. * @return void
  1281. */
  1282. public function testViewFileName()
  1283. {
  1284. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1285. $View->setTemplatePath('Posts');
  1286. $result = $View->getViewFileName('index');
  1287. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1288. $result = $View->getViewFileName('TestPlugin.index');
  1289. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1290. $result = $View->getViewFileName('/Pages/home');
  1291. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  1292. $result = $View->getViewFileName('../Element/test_element');
  1293. $this->assertRegExp('/Element(\/|\\\)test_element.ctp/', $result);
  1294. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  1295. $result = $View->getViewFileName('../Posts/index');
  1296. $this->assertPathEquals($expected, $result);
  1297. }
  1298. /**
  1299. * Test creating a block with capturing output.
  1300. *
  1301. * @return void
  1302. */
  1303. public function testBlockCaptureOverwrite()
  1304. {
  1305. $result = $this->View->start('test');
  1306. $this->assertSame($this->View, $result);
  1307. echo 'Block content';
  1308. $result = $this->View->end();
  1309. $this->assertSame($this->View, $result);
  1310. $this->View->start('test');
  1311. echo 'New content';
  1312. $this->View->end();
  1313. $result = $this->View->fetch('test');
  1314. $this->assertEquals('New content', $result);
  1315. }
  1316. /**
  1317. * Test that blocks can be fetched inside a block with the same name
  1318. *
  1319. * @return void
  1320. */
  1321. public function testBlockExtend()
  1322. {
  1323. $this->View->start('test');
  1324. echo 'Block content';
  1325. $this->View->end();
  1326. $this->View->start('test');
  1327. echo $this->View->fetch('test');
  1328. echo 'New content';
  1329. $this->View->end();
  1330. $result = $this->View->fetch('test');
  1331. $this->assertEquals('Block contentNew content', $result);
  1332. }
  1333. /**
  1334. * Test creating a block with capturing output.
  1335. *
  1336. * @return void
  1337. */
  1338. public function testBlockCapture()
  1339. {
  1340. $this->View->start('test');
  1341. echo 'Block content';
  1342. $this->View->end();
  1343. $result = $this->View->fetch('test');
  1344. $this->assertEquals('Block content', $result);
  1345. }
  1346. /**
  1347. * Test appending to a block with capturing output.
  1348. *
  1349. * @return void
  1350. */
  1351. public function testBlockAppendCapture()
  1352. {
  1353. $this->View->start('test');
  1354. echo 'Content ';
  1355. $this->View->end();
  1356. $result = $this->View->append('test');
  1357. $this->assertSame($this->View, $result);
  1358. echo 'appended';
  1359. $this->View->end();
  1360. $result = $this->View->fetch('test');
  1361. $this->assertEquals('Content appended', $result);
  1362. }
  1363. /**
  1364. * Test setting a block's content.
  1365. *
  1366. * @return void
  1367. */
  1368. public function testBlockSet()
  1369. {
  1370. $result = $this->View->assign('test', 'Block content');
  1371. $this->assertSame($this->View, $result);
  1372. $result = $this->View->fetch('test');
  1373. $this->assertEquals('Block content', $result);
  1374. }
  1375. /**
  1376. * Test resetting a block's content.
  1377. *
  1378. * @return void
  1379. */
  1380. public function testBlockReset()
  1381. {
  1382. $this->View->assign('test', '');
  1383. $result = $this->View->fetch('test', 'This should not be returned');
  1384. $this->assertSame('', $result);
  1385. }
  1386. /**
  1387. * Test resetting a block's content with reset.
  1388. *
  1389. * @return void
  1390. */
  1391. public function testBlockResetFunc()
  1392. {
  1393. $this->View->assign('test', 'Block content');
  1394. $result = $this->View->fetch('test', 'This should not be returned');
  1395. $this->assertSame('Block content', $result);
  1396. $result = $this->View->reset('test');
  1397. $this->assertSame($this->View, $result);
  1398. $result = $this->View->fetch('test', 'This should not be returned');
  1399. $this->assertSame('', $result);
  1400. }
  1401. /**
  1402. * Test checking a block's existence.
  1403. *
  1404. * @return void
  1405. */
  1406. public function testBlockExist()
  1407. {
  1408. $this->assertFalse($this->View->exists('test'));
  1409. $this->View->assign('test', 'Block content');
  1410. $this->assertTrue($this->View->exists('test'));
  1411. }
  1412. /**
  1413. * Test setting a block's content to null
  1414. *
  1415. * @return void
  1416. */
  1417. public function testBlockSetNull()
  1418. {
  1419. $this->View->assign('testWithNull', null);
  1420. $result = $this->View->fetch('testWithNull');
  1421. $this->assertSame('', $result);
  1422. }
  1423. /**
  1424. * Test setting a block's content to an object with __toString magic method
  1425. *
  1426. * @return void
  1427. */
  1428. public function testBlockSetObjectWithToString()
  1429. {
  1430. $objectWithToString = new TestObjectWithToString();
  1431. $this->View->assign('testWithObjectWithToString', $objectWithToString);
  1432. $result = $this->View->fetch('testWithObjectWithToString');
  1433. $this->assertSame("I'm ObjectWithToString", $result);
  1434. }
  1435. /**
  1436. * Test setting a block's content to an object without __toString magic method
  1437. *
  1438. * @return void
  1439. */
  1440. public function testBlockSetObjectWithoutToString()
  1441. {
  1442. $this->checkException(
  1443. 'Object of class ' . TestObjectWithoutToString::class . ' could not be converted to string'
  1444. );
  1445. $objectWithToString = new TestObjectWithoutToString();
  1446. $this->View->assign('testWithObjectWithoutToString', $objectWithToString);
  1447. }
  1448. /**
  1449. * Test setting a block's content to a decimal
  1450. *
  1451. * @return void
  1452. */
  1453. public function testBlockSetDecimal()
  1454. {
  1455. $this->View->assign('testWithDecimal', 1.23456789);
  1456. $result = $this->View->fetch('testWithDecimal');
  1457. $this->assertEquals('1.23456789', $result);
  1458. }
  1459. /**
  1460. * Data provider for block related tests.
  1461. *
  1462. * @return array
  1463. */
  1464. public static function blockValueProvider()
  1465. {
  1466. return [
  1467. 'string' => ['A string value'],
  1468. 'decimal' => [1.23456],
  1469. 'object with __toString' => [new TestObjectWithToString()],
  1470. ];
  1471. }
  1472. /**
  1473. * Test appending to a block with append.
  1474. *
  1475. * @param mixed $value Value
  1476. * @return void
  1477. * @dataProvider blockValueProvider
  1478. */
  1479. public function testBlockAppend($value)
  1480. {
  1481. $this->View->assign('testBlock', 'Block');
  1482. $this->View->append('testBlock', $value);
  1483. $result = $this->View->fetch('testBlock');
  1484. $this->assertSame('Block' . $value, $result);
  1485. }
  1486. /**
  1487. * Test appending an object without __toString magic method to a block with append.
  1488. *
  1489. * @return void
  1490. */
  1491. public function testBlockAppendObjectWithoutToString()
  1492. {
  1493. $this->checkException(
  1494. 'Object of class ' . TestObjectWithoutToString::class . ' could not be converted to string'
  1495. );
  1496. $object = new TestObjectWithoutToString();
  1497. $this->View->assign('testBlock', 'Block ');
  1498. $this->View->append('testBlock', $object);
  1499. }
  1500. /**
  1501. * Test prepending to a block with prepend.
  1502. *
  1503. * @param mixed $value Value
  1504. * @return void
  1505. * @dataProvider blockValueProvider
  1506. */
  1507. public function testBlockPrepend($value)
  1508. {
  1509. $this->View->assign('test', 'Block');
  1510. $result = $this->View->prepend('test', $value);
  1511. $this->assertSame($this->View, $result);
  1512. $result = $this->View->fetch('test');
  1513. $this->assertEquals($value . 'Block', $result);
  1514. }
  1515. /**
  1516. * Test prepending an object without __toString magic method to a block with prepend.
  1517. *
  1518. * @return void
  1519. */
  1520. public function testBlockPrependObjectWithoutToString()
  1521. {
  1522. $this->checkException(
  1523. 'Object of class ' . TestObjectWithoutToString::class . ' could not be converted to string'
  1524. );
  1525. $object = new TestObjectWithoutToString();
  1526. $this->View->assign('test', 'Block ');
  1527. $this->View->prepend('test', $object);
  1528. }
  1529. /**
  1530. * You should be able to append to undefined blocks.
  1531. *
  1532. * @return void
  1533. */
  1534. public function testBlockAppendUndefined()
  1535. {
  1536. $result = $this->View->append('test', 'Unknown');
  1537. $this->assertSame($this->View, $result);
  1538. $result = $this->View->fetch('test');
  1539. $this->assertEquals('Unknown', $result);
  1540. }
  1541. /**
  1542. * You should be able to prepend to undefined blocks.
  1543. *
  1544. * @return void
  1545. */
  1546. public function testBlockPrependUndefined()
  1547. {
  1548. $this->View->prepend('test', 'Unknown');
  1549. $result = $this->View->fetch('test');
  1550. $this->assertEquals('Unknown', $result);
  1551. }
  1552. /**
  1553. * Test getting block names
  1554. *
  1555. * @return void
  1556. */
  1557. public function testBlocks()
  1558. {
  1559. $this->View->append('test', 'one');
  1560. $this->View->assign('test1', 'one');
  1561. $this->assertEquals(['test', 'test1'], $this->View->blocks());
  1562. }
  1563. /**
  1564. * Test that blocks can be nested.
  1565. *
  1566. * @return void
  1567. */
  1568. public function testNestedBlocks()
  1569. {
  1570. $this->View->start('first');
  1571. echo 'In first ';
  1572. $this->View->start('second');
  1573. echo 'In second';
  1574. $this->View->end();
  1575. echo 'In first';
  1576. $this->View->end();
  1577. $this->assertEquals('In first In first', $this->View->fetch('first'));
  1578. $this->assertEquals('In second', $this->View->fetch('second'));
  1579. }
  1580. /**
  1581. * Test that starting the same block twice throws an exception
  1582. *
  1583. * @return void
  1584. */
  1585. public function testStartBlocksTwice()
  1586. {
  1587. try {
  1588. $this->View->start('first');
  1589. $this->View->start('first');
  1590. $this->fail('No exception');
  1591. } catch (\Cake\Core\Exception\Exception $e) {
  1592. ob_end_clean();
  1593. $this->assertTrue(true);
  1594. }
  1595. }
  1596. /**
  1597. * Test that an exception gets thrown when you leave a block open at the end
  1598. * of a view.
  1599. *
  1600. * @return void
  1601. */
  1602. public function testExceptionOnOpenBlock()
  1603. {
  1604. try {
  1605. $this->View->render('open_block');
  1606. $this->fail('No exception');
  1607. } catch (\LogicException $e) {
  1608. ob_end_clean();
  1609. $this->assertContains('The "no_close" block was left open', $e->getMessage());
  1610. }
  1611. }
  1612. /**
  1613. * Test nested extended views.
  1614. *
  1615. * @return void
  1616. */
  1617. public function testExtendNested()
  1618. {
  1619. $this->View->setLayout(false);
  1620. $content = $this->View->render('nested_extends');
  1621. $expected = <<<TEXT
  1622. This is the second parent.
  1623. This is the first parent.
  1624. This is the first template.
  1625. Sidebar Content.
  1626. TEXT;
  1627. $this->assertEquals($expected, $content);
  1628. }
  1629. /**
  1630. * Make sure that extending the current view with itself causes an exception
  1631. *
  1632. * @return void
  1633. */
  1634. public function testExtendSelf()
  1635. {
  1636. try {
  1637. $this->View->setLayout(false);
  1638. $this->View->render('extend_self');
  1639. $this->fail('No exception');
  1640. } catch (\LogicException $e) {
  1641. $this->assertContains('cannot have views extend themselves', $e->getMessage());
  1642. }
  1643. }
  1644. /**
  1645. * Make sure that extending in a loop causes an exception
  1646. *
  1647. * @return void
  1648. */
  1649. public function testExtendLoop()
  1650. {
  1651. try {
  1652. $this->View->setLayout(false);
  1653. $this->View->render('extend_loop');
  1654. $this->fail('No exception');
  1655. } catch (\LogicException $e) {
  1656. $this->assertContains('cannot have views extend in a loop', $e->getMessage());
  1657. }
  1658. }
  1659. /**
  1660. * Test extend() in an element and a view.
  1661. *
  1662. * @return void
  1663. */
  1664. public function testExtendElement()
  1665. {
  1666. $this->View->setLayout(false);
  1667. $content = $this->View->render('extend_element');
  1668. $expected = <<<TEXT
  1669. Parent View.
  1670. View content.
  1671. Parent Element.
  1672. Element content.
  1673. TEXT;
  1674. $this->assertEquals($expected, $content);
  1675. }
  1676. /**
  1677. * Test extend() in an element and a view.
  1678. *
  1679. * @return void
  1680. */
  1681. public function testExtendPrefixElement()
  1682. {
  1683. $this->View->setRequest($this->View->getRequest()->withParam('prefix', 'Admin'));
  1684. $this->View->setLayout(false);
  1685. $content = $this->View->render('extend_element');
  1686. $expected = <<<TEXT
  1687. Parent View.
  1688. View content.
  1689. Parent Element.
  1690. Prefix Element content.
  1691. TEXT;
  1692. $this->assertEquals($expected, $content);
  1693. }
  1694. /**
  1695. * Extending an element which doesn't exist should throw a missing view exception
  1696. *
  1697. * @return void
  1698. */
  1699. public function testExtendMissingElement()
  1700. {
  1701. try {
  1702. $this->View->setLayout(false);
  1703. $this->View->render('extend_missing_element');
  1704. $this->fail('No exception');
  1705. } catch (\LogicException $e) {
  1706. $this->assertContains('element', $e->getMessage());
  1707. }
  1708. }
  1709. /**
  1710. * Test extend() preceded by an element()
  1711. *
  1712. * @return void
  1713. */
  1714. public function testExtendWithElementBeforeExtend()
  1715. {
  1716. $this->View->setLayout(false);
  1717. $result = $this->View->render('extend_with_element');
  1718. $expected = <<<TEXT
  1719. Parent View.
  1720. this is the test elementThe view
  1721. TEXT;
  1722. $this->assertEquals($expected, $result);
  1723. }
  1724. /**
  1725. * Test extend() preceded by an element()
  1726. *
  1727. * @return void
  1728. */
  1729. public function testExtendWithPrefixElementBeforeExtend()
  1730. {
  1731. $this->View->setRequest($this->View->getRequest()->withParam('prefix', 'Admin'));
  1732. $this->View->setLayout(false);
  1733. $result = $this->View->render('extend_with_element');
  1734. $expected = <<<TEXT
  1735. Parent View.
  1736. this is the test prefix elementThe view
  1737. TEXT;
  1738. $this->assertEquals($expected, $result);
  1739. }
  1740. /**
  1741. * Tests that the buffers that are opened when evaluating a template
  1742. * are being closed in case an exception happens.
  1743. *
  1744. * @return void
  1745. */
  1746. public function testBuffersOpenedDuringTemplateEvaluationAreBeingClosed()
  1747. {
  1748. $bufferLevel = ob_get_level();
  1749. $e = null;
  1750. try {
  1751. $this->View->element('exception_with_open_buffers');
  1752. } catch (\Exception $e) {
  1753. }
  1754. $this->assertNotNull($e);
  1755. $this->assertEquals('Exception with open buffers', $e->getMessage());
  1756. $this->assertEquals($bufferLevel, ob_get_level());
  1757. }
  1758. /**
  1759. * Tests that the buffers that are opened during block caching are
  1760. * being closed in case an exception happens.
  1761. *
  1762. * @return void
  1763. */
  1764. public function testBuffersOpenedDuringBlockCachingAreBeingClosed()
  1765. {
  1766. Cache::drop('test_view');
  1767. Cache::setConfig('test_view', [
  1768. 'engine' => 'File',
  1769. 'duration' => '+1 day',
  1770. 'path' => CACHE . 'views/',
  1771. 'prefix' => '',
  1772. ]);
  1773. Cache::clear(false, 'test_view');
  1774. $bufferLevel = ob_get_level();
  1775. $e = null;
  1776. try {
  1777. $this->View->cache(function () {
  1778. ob_start();
  1779. throw new \Exception('Exception with open buffers');
  1780. }, [
  1781. 'key' => __FUNCTION__,
  1782. 'config' => 'test_view',
  1783. ]);
  1784. } catch (\Exception $e) {
  1785. }
  1786. Cache::clear(false, 'test_view');
  1787. Cache::drop('test_view');
  1788. $this->assertNotNull($e);
  1789. $this->assertEquals('Exception with open buffers', $e->getMessage());
  1790. $this->assertEquals($bufferLevel, ob_get_level());
  1791. }
  1792. /**
  1793. * Test memory leaks that existed in _paths at one point.
  1794. *
  1795. * @return void
  1796. */
  1797. public function testMemoryLeakInPaths()
  1798. {
  1799. $this->skipIf(env('CODECOVERAGE') == 1, 'Running coverage this causes this tests to fail sometimes.');
  1800. $this->ThemeController->setPlugin(null);
  1801. $this->ThemeController->setName('Posts');
  1802. $View = $this->ThemeController->createView();
  1803. $View->setTemplatePath('Posts');
  1804. $View->setLayout('whatever');
  1805. $View->setTheme('TestTheme');
  1806. $View->element('test_element');
  1807. $start = memory_get_usage();
  1808. for ($i = 0; $i < 10; $i++) {
  1809. $View->element('test_element');
  1810. }
  1811. $end = memory_get_usage();
  1812. $this->assertLessThanOrEqual($start + 15000, $end);
  1813. }
  1814. /**
  1815. * Tests that a view block uses default value when not assigned and uses assigned value when it is
  1816. *
  1817. * @return void
  1818. */
  1819. public function testBlockDefaultValue()
  1820. {
  1821. $default = 'Default';
  1822. $result = $this->View->fetch('title', $default);
  1823. $this->assertEquals($default, $result);
  1824. $expected = 'My Title';
  1825. $this->View->assign('title', $expected);
  1826. $result = $this->View->fetch('title', $default);
  1827. $this->assertEquals($expected, $result);
  1828. }
  1829. /**
  1830. * Tests that a view variable uses default value when not assigned and uses assigned value when it is
  1831. *
  1832. * @return void
  1833. */
  1834. public function testViewVarDefaultValue()
  1835. {
  1836. $default = 'Default';
  1837. $result = $this->View->get('title', $default);
  1838. $this->assertEquals($default, $result);
  1839. $expected = 'Back to the Future';
  1840. $this->View->set('title', $expected);
  1841. $result = $this->View->get('title', $default);
  1842. $this->assertEquals($expected, $result);
  1843. }
  1844. /**
  1845. * Test the helpers() method.
  1846. *
  1847. * @return void
  1848. */
  1849. public function testHelpers()
  1850. {
  1851. $this->assertInstanceOf('Cake\View\HelperRegistry', $this->View->helpers());
  1852. $result = $this->View->helpers();
  1853. $this->assertSame($result, $this->View->helpers());
  1854. }
  1855. /**
  1856. * Test getTemplatePath() and setTemplatePath().
  1857. *
  1858. * @return void
  1859. */
  1860. public function testGetSetTemplatePath()
  1861. {
  1862. $result = $this->View->setTemplatePath('foo');
  1863. $this->assertSame($this->View, $result);
  1864. $templatePath = $this->View->getTemplatePath();
  1865. $this->assertSame($templatePath, 'foo');
  1866. }
  1867. /**
  1868. * Test getLayoutPath() and setLayoutPath().
  1869. *
  1870. * @return void
  1871. */
  1872. public function testGetSetLayoutPath()
  1873. {
  1874. $result = $this->View->setLayoutPath('foo');
  1875. $this->assertSame($this->View, $result);
  1876. $layoutPath = $this->View->getLayoutPath();
  1877. $this->assertSame($layoutPath, 'foo');
  1878. }
  1879. /**
  1880. * Test isAutoLayoutEnabled() and enableAutoLayout().
  1881. *
  1882. * @return void
  1883. */
  1884. public function testAutoLayout()
  1885. {
  1886. $result = $this->View->enableAutoLayout(false);
  1887. $this->assertSame($this->View, $result);
  1888. $autoLayout = $this->View->isAutoLayoutEnabled();
  1889. $this->assertSame($autoLayout, false);
  1890. $this->View->enableAutoLayout();
  1891. $autoLayout = $this->View->isAutoLayoutEnabled();
  1892. $this->assertSame($autoLayout, true);
  1893. }
  1894. /**
  1895. * testDisableAutoLayout
  1896. *
  1897. * @return void
  1898. */
  1899. public function testDisableAutoLayout()
  1900. {
  1901. $this->assertTrue($this->View->isAutoLayoutEnabled());
  1902. $result = $this->View->disableAutoLayout();
  1903. $this->assertSame($this->View, $result);
  1904. $autoLayout = $this->View->isAutoLayoutEnabled();
  1905. $this->assertFalse($this->View->isAutoLayoutEnabled());
  1906. }
  1907. /**
  1908. * Test getTheme() and setTheme().
  1909. *
  1910. * @return void
  1911. */
  1912. public function testGetSetTheme()
  1913. {
  1914. $result = $this->View->setTheme('foo');
  1915. $this->assertSame($this->View, $result);
  1916. $theme = $this->View->getTheme();
  1917. $this->assertSame($theme, 'foo');
  1918. }
  1919. /**
  1920. * Test getTemplate() and setTemplate().
  1921. *
  1922. * @return void
  1923. */
  1924. public function testGetSetTemplate()
  1925. {
  1926. $result = $this->View->setTemplate('foo');
  1927. $this->assertSame($this->View, $result);
  1928. $template = $this->View->getTemplate();
  1929. $this->assertSame($template, 'foo');
  1930. }
  1931. /**
  1932. * Test setLayout() and getLayout().
  1933. *
  1934. * @return void
  1935. */
  1936. public function testGetSetLayout()
  1937. {
  1938. $result = $this->View->setLayout('foo');
  1939. $this->assertSame($this->View, $result);
  1940. $layout = $this->View->getLayout();
  1941. $this->assertSame($layout, 'foo');
  1942. }
  1943. /**
  1944. * Test getName() and getPlugin().
  1945. *
  1946. * @return void
  1947. */
  1948. public function testGetNamePlugin()
  1949. {
  1950. $this->assertSame('Posts', $this->View->getName());
  1951. $this->assertNull($this->View->getPlugin());
  1952. $this->assertSame($this->View, $this->View->setPlugin('TestPlugin'));
  1953. $this->assertSame('TestPlugin', $this->View->getPlugin());
  1954. }
  1955. /**
  1956. * Test testHasRendered property
  1957. *
  1958. * @return void
  1959. */
  1960. public function testHasRendered()
  1961. {
  1962. $this->assertFalse($this->View->hasRendered);
  1963. $this->View->render('index');
  1964. $this->assertTrue($this->View->hasRendered);
  1965. }
  1966. /**
  1967. * Test magic getter and setter for removed properties.
  1968. *
  1969. * @group deprecated
  1970. * @return void
  1971. */
  1972. public function testMagicGetterSetter()
  1973. {
  1974. $this->deprecated(function () {
  1975. $View = $this->View;
  1976. $View->view = 'myview';
  1977. $this->assertEquals('myview', $View->template());
  1978. $this->assertEquals('myview', $View->view);
  1979. $View->viewPath = 'mypath';
  1980. $this->assertEquals('mypath', $View->templatePath());
  1981. $this->assertEquals('mypath', $View->templatePath);
  1982. });
  1983. }
  1984. protected function checkException($message)
  1985. {
  1986. if (version_compare(PHP_VERSION, '7.4', '>=')) {
  1987. $this->expectException(\Error::class);
  1988. } else {
  1989. $this->expectException(\PHPUnit\Framework\Error\Error::class);
  1990. }
  1991. $this->expectExceptionMessage($message);
  1992. }
  1993. }