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