ViewTest.php 45 KB

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