ViewTest.php 60 KB

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