ViewTest.php 62 KB

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