ViewTest.php 55 KB

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