ViewTest.php 58 KB

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