ViewTest.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  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'] = 'foo_prefix';
  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. $View->request->params['prefix'] = 'FooPrefix';
  554. $result = $View->getLayoutFileName();
  555. $this->assertPathEquals($expected, $result);
  556. // Nested prefix layout
  557. $View->request->params['prefix'] = 'foo_prefix/bar_prefix';
  558. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS .
  559. 'FooPrefix' . DS . 'BarPrefix' . DS . 'Layout' . DS . 'default.ctp';
  560. $result = $View->getLayoutFileName();
  561. $this->assertPathEquals($expected, $result);
  562. // Fallback to app's layout
  563. $View->request->params['prefix'] = 'Admin';
  564. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS .
  565. 'Layout' . DS . 'default.ctp';
  566. $result = $View->getLayoutFileName();
  567. $this->assertPathEquals($expected, $result);
  568. }
  569. /**
  570. * Test that getLayoutFileName() protects against malicious directory traversal.
  571. *
  572. * @expectedException \InvalidArgumentException
  573. * @return void
  574. */
  575. public function testGetLayoutFileNameDirectoryTraversal() {
  576. $viewOptions = [
  577. 'plugin' => null,
  578. 'name' => 'Pages',
  579. 'viewPath' => 'Pages',
  580. ];
  581. $request = $this->getMock('Cake\Network\Request');
  582. $response = $this->getMock('Cake\Network\Response');
  583. $view = new TestView(null, null, null, $viewOptions);
  584. $view->ext('.php');
  585. $view->getLayoutFileName('../../../../bootstrap');
  586. }
  587. /**
  588. * Test for missing views
  589. *
  590. * @expectedException \Cake\View\Exception\MissingTemplateException
  591. * @return void
  592. */
  593. public function testMissingTemplate() {
  594. $viewOptions = ['plugin' => null,
  595. 'name' => 'Pages',
  596. 'viewPath' => 'Pages'
  597. ];
  598. $request = $this->getMock('Cake\Network\Request');
  599. $response = $this->getMock('Cake\Network\Response');
  600. $View = new TestView($request, $response, null, $viewOptions);
  601. $View->getViewFileName('does_not_exist');
  602. }
  603. /**
  604. * Test for missing layouts
  605. *
  606. * @expectedException \Cake\View\Exception\MissingLayoutException
  607. * @return void
  608. */
  609. public function testMissingLayout() {
  610. $viewOptions = ['plugin' => null,
  611. 'name' => 'Pages',
  612. 'viewPath' => 'Pages',
  613. 'layout' => 'whatever'
  614. ];
  615. $View = new TestView(null, null, null, $viewOptions);
  616. $View->getLayoutFileName();
  617. }
  618. /**
  619. * Test viewVars method
  620. *
  621. * @return void
  622. */
  623. public function testViewVars() {
  624. $this->assertEquals(array('testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data'), $this->View->viewVars);
  625. }
  626. /**
  627. * Test generation of UUIDs method
  628. *
  629. * @return void
  630. */
  631. public function testUUIDGeneration() {
  632. Router::connect('/:controller', ['action' => 'index']);
  633. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  634. $this->assertEquals('form5988016017', $result);
  635. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  636. $this->assertEquals('formc3dc6be854', $result);
  637. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  638. $this->assertEquals('form28f92cc87f', $result);
  639. }
  640. /**
  641. * Test elementExists method
  642. *
  643. * @return void
  644. */
  645. public function testElementExists() {
  646. $result = $this->View->elementExists('test_element');
  647. $this->assertTrue($result);
  648. $result = $this->View->elementExists('TestPlugin.plugin_element');
  649. $this->assertTrue($result);
  650. $result = $this->View->elementExists('non_existent_element');
  651. $this->assertFalse($result);
  652. $result = $this->View->elementExists('TestPlugin.element');
  653. $this->assertFalse($result);
  654. $this->View->plugin = 'TestPlugin';
  655. $result = $this->View->elementExists('test_plugin_element');
  656. $this->assertTrue($result);
  657. }
  658. /**
  659. * Test element method
  660. *
  661. * @return void
  662. */
  663. public function testElement() {
  664. $result = $this->View->element('test_element');
  665. $this->assertEquals('this is the test element', $result);
  666. $result = $this->View->element('TestPlugin.plugin_element');
  667. $this->assertEquals('this is the plugin element using params[plugin]', $result);
  668. $this->View->plugin = 'TestPlugin';
  669. $result = $this->View->element('test_plugin_element');
  670. $this->assertEquals('this is the test set using View::$plugin plugin element', $result);
  671. }
  672. /**
  673. * Test elementInexistent method
  674. *
  675. * @expectedException \Cake\View\Exception\MissingElementException
  676. * @return void
  677. */
  678. public function testElementInexistent() {
  679. $this->View->element('non_existent_element');
  680. }
  681. /**
  682. * Test elementInexistent3 method
  683. *
  684. * @expectedException \Cake\View\Exception\MissingElementException
  685. * @return void
  686. */
  687. public function testElementInexistent3() {
  688. $this->View->element('test_plugin.plugin_element');
  689. }
  690. /**
  691. * Test that elements can have callbacks
  692. *
  693. * @return void
  694. */
  695. public function testElementCallbacks() {
  696. $count = 0;
  697. $callback = function ($event, $file) use (&$count) {
  698. $count++;
  699. };
  700. $events = $this->View->eventManager();
  701. $events->attach($callback, 'View.beforeRender');
  702. $events->attach($callback, 'View.afterRender');
  703. $this->View->element('test_element', array(), array('callbacks' => true));
  704. $this->assertEquals(2, $count);
  705. }
  706. /**
  707. * Test that additional element viewVars don't get overwritten with helpers.
  708. *
  709. * @return void
  710. */
  711. public function testElementParamsDontOverwriteHelpers() {
  712. $Controller = new ViewPostsController();
  713. $Controller->helpers = array('Form');
  714. $View = $Controller->createView();
  715. $result = $View->element('type_check', array('form' => 'string'), array('callbacks' => true));
  716. $this->assertEquals('string', $result);
  717. $View->set('form', 'string');
  718. $result = $View->element('type_check', array(), array('callbacks' => true));
  719. $this->assertEquals('string', $result);
  720. }
  721. /**
  722. * Test elementCacheHelperNoCache method
  723. *
  724. * @return void
  725. */
  726. public function testElementCacheHelperNoCache() {
  727. $Controller = new ViewPostsController();
  728. $View = $Controller->createView();
  729. $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
  730. $this->assertEquals('this is the test element', $result);
  731. }
  732. /**
  733. * Test elementCache method
  734. *
  735. * @return void
  736. */
  737. public function testElementCache() {
  738. Cache::drop('test_view');
  739. Cache::config('test_view', [
  740. 'engine' => 'File',
  741. 'duration' => '+1 day',
  742. 'path' => CACHE . 'views/',
  743. 'prefix' => ''
  744. ]);
  745. Cache::clear(true, 'test_view');
  746. $View = $this->PostsController->createView();
  747. $View->elementCache = 'test_view';
  748. $result = $View->element('test_element', array(), array('cache' => true));
  749. $expected = 'this is the test element';
  750. $this->assertEquals($expected, $result);
  751. $result = Cache::read('element__test_element_cache_callbacks', 'test_view');
  752. $this->assertEquals($expected, $result);
  753. $result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
  754. $this->assertEquals($expected, $result);
  755. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  756. $this->assertEquals($expected, $result);
  757. $View->element('test_element', array(
  758. 'param' => 'one',
  759. 'foo' => 'two'
  760. ), array(
  761. 'cache' => array('key' => 'custom_key')
  762. ));
  763. $result = Cache::read('element_custom_key', 'test_view');
  764. $this->assertEquals($expected, $result);
  765. $View->elementCache = 'default';
  766. $View->element('test_element', array(
  767. 'param' => 'one',
  768. 'foo' => 'two'
  769. ), array(
  770. 'cache' => array('config' => 'test_view'),
  771. ));
  772. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  773. $this->assertEquals($expected, $result);
  774. Cache::clear(true, 'test_view');
  775. Cache::drop('test_view');
  776. }
  777. /**
  778. * Test element events
  779. *
  780. * @return void
  781. */
  782. public function testViewEvent() {
  783. $View = $this->PostsController->createView();
  784. $View->autoLayout = false;
  785. $listener = new TestViewEventListenerInterface();
  786. $View->eventManager()->attach($listener);
  787. $View->render('index');
  788. $this->assertEquals(View::TYPE_VIEW, $listener->beforeRenderViewType);
  789. $this->assertEquals(View::TYPE_VIEW, $listener->afterRenderViewType);
  790. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  791. $View->element('test_element', array(), array('callbacks' => true));
  792. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  793. $this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType);
  794. $this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType);
  795. }
  796. /**
  797. * Test loading helper using loadHelper().
  798. *
  799. * @return void
  800. */
  801. public function testLoadHelper() {
  802. $View = new View();
  803. $View->loadHelper('Html', ['foo' => 'bar']);
  804. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html);
  805. $config = $View->Html->config();
  806. $this->assertEquals('bar', $config['foo']);
  807. }
  808. /**
  809. * Test loading helper when duplicate.
  810. *
  811. * @return void
  812. */
  813. public function testLoadHelperDuplicate() {
  814. $View = new View();
  815. $this->assertNotEmpty($View->loadHelper('Html', ['foo' => 'bar']));
  816. try {
  817. $View->loadHelper('Html', ['test' => 'value']);
  818. $this->fail('No exception');
  819. } catch (\RuntimeException $e) {
  820. $this->assertContains('The "Html" alias has already been loaded', $e->getMessage());
  821. }
  822. }
  823. /**
  824. * Test loadHelpers method
  825. *
  826. * @return void
  827. */
  828. public function testLoadHelpers() {
  829. $View = new View();
  830. $View->helpers = ['Html' => ['foo' => 'bar'], 'Form' => ['foo' => 'baz']];
  831. $View->loadHelpers();
  832. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  833. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  834. $config = $View->Html->config();
  835. $this->assertEquals('bar', $config['foo']);
  836. $config = $View->Form->config();
  837. $this->assertEquals('baz', $config['foo']);
  838. }
  839. /**
  840. * Test lazy loading helpers
  841. *
  842. * @return void
  843. */
  844. public function testLazyLoadHelpers() {
  845. $View = new View();
  846. $View->helpers = array();
  847. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  848. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  849. }
  850. /**
  851. * Test manipulating class properties in initialize()
  852. *
  853. * @return void
  854. */
  855. public function testInitialize() {
  856. $View = new TestView();
  857. $config = $View->Html->config();
  858. $this->assertEquals('myval', $config['mykey']);
  859. }
  860. /**
  861. * Test the correct triggering of helper callbacks
  862. *
  863. * @return void
  864. */
  865. public function testHelperCallbackTriggering() {
  866. $View = $this->PostsController->createView();
  867. $manager = $this->getMock('Cake\Event\EventManager');
  868. $View->eventManager($manager);
  869. $manager->expects($this->at(0))->method('dispatch')
  870. ->with(
  871. $this->logicalAnd(
  872. $this->isInstanceOf('Cake\Event\Event'),
  873. $this->attributeEqualTo('_name', 'View.beforeRender'),
  874. $this->attributeEqualTo('_subject', $View)
  875. )
  876. );
  877. $manager->expects($this->at(1))->method('dispatch')
  878. ->with(
  879. $this->logicalAnd(
  880. $this->isInstanceOf('Cake\Event\Event'),
  881. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  882. $this->attributeEqualTo('_subject', $View)
  883. )
  884. );
  885. $manager->expects($this->at(2))->method('dispatch')
  886. ->with(
  887. $this->logicalAnd(
  888. $this->isInstanceOf('Cake\Event\Event'),
  889. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  890. $this->attributeEqualTo('_subject', $View)
  891. )
  892. );
  893. $manager->expects($this->at(3))->method('dispatch')
  894. ->with(
  895. $this->logicalAnd(
  896. $this->isInstanceOf('Cake\Event\Event'),
  897. $this->attributeEqualTo('_name', 'View.afterRender'),
  898. $this->attributeEqualTo('_subject', $View)
  899. )
  900. );
  901. $manager->expects($this->at(4))->method('dispatch')
  902. ->with(
  903. $this->logicalAnd(
  904. $this->isInstanceOf('Cake\Event\Event'),
  905. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  906. $this->attributeEqualTo('_subject', $View)
  907. )
  908. );
  909. $manager->expects($this->at(5))->method('dispatch')
  910. ->with(
  911. $this->logicalAnd(
  912. $this->isInstanceOf('Cake\Event\Event'),
  913. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  914. $this->attributeEqualTo('_subject', $View)
  915. )
  916. );
  917. $manager->expects($this->at(6))->method('dispatch')
  918. ->with(
  919. $this->logicalAnd(
  920. $this->isInstanceOf('Cake\Event\Event'),
  921. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  922. $this->attributeEqualTo('_subject', $View)
  923. )
  924. );
  925. $manager->expects($this->at(7))->method('dispatch')
  926. ->with(
  927. $this->logicalAnd(
  928. $this->isInstanceOf('Cake\Event\Event'),
  929. $this->attributeEqualTo('_name', 'View.afterLayout'),
  930. $this->attributeEqualTo('_subject', $View)
  931. )
  932. );
  933. $View->render('index');
  934. }
  935. /**
  936. * Test beforeLayout method
  937. *
  938. * @return void
  939. */
  940. public function testBeforeLayout() {
  941. $this->PostsController->helpers = array(
  942. 'Session',
  943. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  944. 'Html'
  945. );
  946. $View = $this->PostsController->createView();
  947. $View->render('index');
  948. $this->assertEquals('Valuation', $View->helpers()->TestBeforeAfter->property);
  949. }
  950. /**
  951. * Test afterLayout method
  952. *
  953. * @return void
  954. */
  955. public function testAfterLayout() {
  956. $this->PostsController->helpers = array(
  957. 'Session',
  958. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  959. 'Html'
  960. );
  961. $this->PostsController->set('variable', 'values');
  962. $View = $this->PostsController->createView();
  963. $content = 'This is my view output';
  964. $result = $View->renderLayout($content, 'default');
  965. $this->assertRegExp('/modified in the afterlife/', $result);
  966. $this->assertRegExp('/This is my view output/', $result);
  967. }
  968. /**
  969. * Test renderLoadHelper method
  970. *
  971. * @return void
  972. */
  973. public function testRenderLoadHelper() {
  974. $this->PostsController->helpers = array('Session', 'Form', 'Number');
  975. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  976. $result = $View->render('index', false);
  977. $this->assertEquals('posts index', $result);
  978. $attached = $View->helpers()->loaded();
  979. // HtmlHelper is loaded in TestView::initialize()
  980. $this->assertEquals(array('Html', 'Session', 'Form', 'Number'), $attached);
  981. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  982. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  983. $result = $View->render('index', false);
  984. $this->assertEquals('posts index', $result);
  985. $attached = $View->helpers()->loaded();
  986. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  987. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  988. }
  989. /**
  990. * Test render method
  991. *
  992. * @return void
  993. */
  994. public function testRender() {
  995. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  996. $result = $View->render('index');
  997. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  998. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  999. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1000. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1001. $result = $View->render(false, 'ajax2');
  1002. $this->assertRegExp('/Ajax\!/', $result);
  1003. $this->assertNull($View->render(false, 'ajax2'));
  1004. $this->PostsController->helpers = array('Session', 'Html');
  1005. $this->PostsController->request->params['action'] = 'index';
  1006. Configure::write('Cache.check', true);
  1007. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1008. $result = $View->render('index');
  1009. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  1010. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  1011. }
  1012. /**
  1013. * Test that View::$view works
  1014. *
  1015. * @return void
  1016. */
  1017. public function testRenderUsingViewProperty() {
  1018. $this->PostsController->view = 'cache_form';
  1019. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1020. $this->assertEquals('cache_form', $View->view);
  1021. $result = $View->render();
  1022. $this->assertRegExp('/Add User/', $result);
  1023. }
  1024. /**
  1025. * Test render()ing a file in a subdir from a custom viewPath
  1026. * in a plugin.
  1027. *
  1028. * @return void
  1029. */
  1030. public function testGetViewFileNameSubdirWithPluginAndViewPath() {
  1031. $this->PostsController->plugin = 'TestPlugin';
  1032. $this->PostsController->name = 'Posts';
  1033. $this->PostsController->viewPath = 'Element';
  1034. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1035. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  1036. $result = $View->getViewFileName('sub_dir/sub_element');
  1037. $expected = $pluginPath . 'src' . DS . 'Template' . DS . 'Element' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  1038. $this->assertPathEquals($expected, $result);
  1039. }
  1040. /**
  1041. * Test that view vars can replace the local helper variables
  1042. * and not overwrite the $this->Helper references
  1043. *
  1044. * @return void
  1045. */
  1046. public function testViewVarOverwritingLocalHelperVar() {
  1047. $Controller = new ViewPostsController();
  1048. $Controller->helpers = array('Session', 'Html');
  1049. $Controller->set('html', 'I am some test html');
  1050. $View = $Controller->createView();
  1051. $result = $View->render('helper_overwrite', false);
  1052. $this->assertRegExp('/I am some test html/', $result);
  1053. $this->assertRegExp('/Test link/', $result);
  1054. }
  1055. /**
  1056. * Test getViewFileName method
  1057. *
  1058. * @return void
  1059. */
  1060. public function testViewFileName() {
  1061. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1062. $result = $View->getViewFileName('index');
  1063. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1064. $result = $View->getViewFileName('TestPlugin.index');
  1065. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1066. $result = $View->getViewFileName('/Pages/home');
  1067. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  1068. $result = $View->getViewFileName('../Element/test_element');
  1069. $this->assertRegExp('/Element(\/|\\\)test_element.ctp/', $result);
  1070. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  1071. $result = $View->getViewFileName('../Posts/index');
  1072. $this->assertPathEquals($expected, $result);
  1073. }
  1074. /**
  1075. * Test creating a block with capturing output.
  1076. *
  1077. * @return void
  1078. */
  1079. public function testBlockCaptureOverwrite() {
  1080. $this->View->start('test');
  1081. echo 'Block content';
  1082. $this->View->end();
  1083. $this->View->start('test');
  1084. echo 'New content';
  1085. $this->View->end();
  1086. $result = $this->View->fetch('test');
  1087. $this->assertEquals('New content', $result);
  1088. }
  1089. /**
  1090. * Test that blocks can be fetched inside a block with the same name
  1091. *
  1092. * @return void
  1093. */
  1094. public function testBlockExtend() {
  1095. $this->View->start('test');
  1096. echo 'Block content';
  1097. $this->View->end();
  1098. $this->View->start('test');
  1099. echo $this->View->fetch('test');
  1100. echo 'New content';
  1101. $this->View->end();
  1102. $result = $this->View->fetch('test');
  1103. $this->assertEquals('Block contentNew content', $result);
  1104. }
  1105. /**
  1106. * Test creating a block with capturing output.
  1107. *
  1108. * @return void
  1109. */
  1110. public function testBlockCapture() {
  1111. $this->View->start('test');
  1112. echo 'Block content';
  1113. $this->View->end();
  1114. $result = $this->View->fetch('test');
  1115. $this->assertEquals('Block content', $result);
  1116. }
  1117. /**
  1118. * Test appending to a block with capturing output.
  1119. *
  1120. * @return void
  1121. */
  1122. public function testBlockAppendCapture() {
  1123. $this->View->start('test');
  1124. echo 'Content ';
  1125. $this->View->end();
  1126. $this->View->append('test');
  1127. echo 'appended';
  1128. $this->View->end();
  1129. $result = $this->View->fetch('test');
  1130. $this->assertEquals('Content appended', $result);
  1131. }
  1132. /**
  1133. * Test setting a block's content.
  1134. *
  1135. * @return void
  1136. */
  1137. public function testBlockSet() {
  1138. $this->View->assign('test', 'Block content');
  1139. $result = $this->View->fetch('test');
  1140. $this->assertEquals('Block content', $result);
  1141. }
  1142. /**
  1143. * Test resetting a block's content.
  1144. *
  1145. * @return void
  1146. */
  1147. public function testBlockReset() {
  1148. $this->View->assign('test', '');
  1149. $result = $this->View->fetch('test', 'This should not be returned');
  1150. $this->assertSame('', $result);
  1151. }
  1152. /**
  1153. * Test setting a block's content to null
  1154. *
  1155. * @return void
  1156. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
  1157. */
  1158. public function testBlockSetNull() {
  1159. $this->View->assign('testWithNull', null);
  1160. $result = $this->View->fetch('testWithNull');
  1161. $this->assertSame('', $result);
  1162. }
  1163. /**
  1164. * Test setting a block's content to an object with __toString magic method
  1165. *
  1166. * @return void
  1167. */
  1168. public function testBlockSetObjectWithToString() {
  1169. $objectWithToString = new TestObjectWithToString();
  1170. $this->View->assign('testWithObjectWithToString', $objectWithToString);
  1171. $result = $this->View->fetch('testWithObjectWithToString');
  1172. $this->assertSame("I'm ObjectWithToString", $result);
  1173. }
  1174. /**
  1175. * Test setting a block's content to an object without __toString magic method
  1176. *
  1177. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1178. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1179. *
  1180. * @expectedException \PHPUnit_Framework_Error
  1181. * @return void
  1182. */
  1183. public function testBlockSetObjectWithoutToString() {
  1184. $objectWithToString = new TestObjectWithoutToString();
  1185. $this->View->assign('testWithObjectWithoutToString', $objectWithToString);
  1186. }
  1187. /**
  1188. * Test setting a block's content to a decimal
  1189. *
  1190. * @return void
  1191. */
  1192. public function testBlockSetDecimal() {
  1193. $this->View->assign('testWithDecimal', 1.23456789);
  1194. $result = $this->View->fetch('testWithDecimal');
  1195. $this->assertEquals('1.23456789', $result);
  1196. }
  1197. /**
  1198. * Data provider for block related tests.
  1199. *
  1200. * @return array
  1201. */
  1202. public static function blockValueProvider() {
  1203. return array(
  1204. 'string' => array('A string value'),
  1205. 'decimal' => array(1.23456),
  1206. 'object with __toString' => array(new TestObjectWithToString()),
  1207. );
  1208. }
  1209. /**
  1210. * Test appending to a block with append.
  1211. *
  1212. * @param mixed $value Value
  1213. * @return void
  1214. * @dataProvider blockValueProvider
  1215. */
  1216. public function testBlockAppend($value) {
  1217. $this->View->assign('testBlock', 'Block');
  1218. $this->View->append('testBlock', $value);
  1219. $result = $this->View->fetch('testBlock');
  1220. $this->assertSame('Block' . $value, $result);
  1221. }
  1222. /**
  1223. * Test appending an object without __toString magic method to a block with append.
  1224. *
  1225. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1226. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1227. *
  1228. * @expectedException \PHPUnit_Framework_Error
  1229. * @return void
  1230. */
  1231. public function testBlockAppendObjectWithoutToString() {
  1232. $object = new TestObjectWithoutToString();
  1233. $this->View->assign('testBlock', 'Block ');
  1234. $this->View->append('testBlock', $object);
  1235. }
  1236. /**
  1237. * Test prepending to a block with prepend.
  1238. *
  1239. * @param mixed $value Value
  1240. * @return void
  1241. * @dataProvider blockValueProvider
  1242. */
  1243. public function testBlockPrepend($value) {
  1244. $this->View->assign('test', 'Block');
  1245. $this->View->prepend('test', $value);
  1246. $result = $this->View->fetch('test');
  1247. $this->assertEquals($value . 'Block', $result);
  1248. }
  1249. /**
  1250. * Test prepending an object without __toString magic method to a block with prepend.
  1251. *
  1252. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1253. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1254. *
  1255. * @expectedException \PHPUnit_Framework_Error
  1256. * @return void
  1257. */
  1258. public function testBlockPrependObjectWithoutToString() {
  1259. $object = new TestObjectWithoutToString();
  1260. $this->View->assign('test', 'Block ');
  1261. $this->View->prepend('test', $object);
  1262. }
  1263. /**
  1264. * You should be able to append to undefined blocks.
  1265. *
  1266. * @return void
  1267. */
  1268. public function testBlockAppendUndefined() {
  1269. $this->View->append('test', 'Unknown');
  1270. $result = $this->View->fetch('test');
  1271. $this->assertEquals('Unknown', $result);
  1272. }
  1273. /**
  1274. * You should be able to prepend to undefined blocks.
  1275. *
  1276. * @return void
  1277. */
  1278. public function testBlockPrependUndefined() {
  1279. $this->View->prepend('test', 'Unknown');
  1280. $result = $this->View->fetch('test');
  1281. $this->assertEquals('Unknown', $result);
  1282. }
  1283. /**
  1284. * Test getting block names
  1285. *
  1286. * @return void
  1287. */
  1288. public function testBlocks() {
  1289. $this->View->append('test', 'one');
  1290. $this->View->assign('test1', 'one');
  1291. $this->assertEquals(array('test', 'test1'), $this->View->blocks());
  1292. }
  1293. /**
  1294. * Test that blocks can be nested.
  1295. *
  1296. * @return void
  1297. */
  1298. public function testNestedBlocks() {
  1299. $this->View->start('first');
  1300. echo 'In first ';
  1301. $this->View->start('second');
  1302. echo 'In second';
  1303. $this->View->end();
  1304. echo 'In first';
  1305. $this->View->end();
  1306. $this->assertEquals('In first In first', $this->View->fetch('first'));
  1307. $this->assertEquals('In second', $this->View->fetch('second'));
  1308. }
  1309. /**
  1310. * Test that starting the same block twice throws an exception
  1311. *
  1312. * @return void
  1313. */
  1314. public function testStartBlocksTwice() {
  1315. try {
  1316. $this->View->start('first');
  1317. $this->View->start('first');
  1318. $this->fail('No exception');
  1319. } catch (\Cake\Core\Exception\Exception $e) {
  1320. ob_end_clean();
  1321. $this->assertTrue(true);
  1322. }
  1323. }
  1324. /**
  1325. * Test that an exception gets thrown when you leave a block open at the end
  1326. * of a view.
  1327. *
  1328. * @return void
  1329. */
  1330. public function testExceptionOnOpenBlock() {
  1331. try {
  1332. $this->View->render('open_block');
  1333. $this->fail('No exception');
  1334. } catch (\LogicException $e) {
  1335. ob_end_clean();
  1336. $this->assertContains('The "no_close" block was left open', $e->getMessage());
  1337. }
  1338. }
  1339. /**
  1340. * Test nested extended views.
  1341. *
  1342. * @return void
  1343. */
  1344. public function testExtendNested() {
  1345. $this->View->layout = false;
  1346. $content = $this->View->render('nested_extends');
  1347. $expected = <<<TEXT
  1348. This is the second parent.
  1349. This is the first parent.
  1350. This is the first template.
  1351. Sidebar Content.
  1352. TEXT;
  1353. $this->assertEquals($expected, $content);
  1354. }
  1355. /**
  1356. * Make sure that extending the current view with itself causes an exception
  1357. *
  1358. * @return void
  1359. */
  1360. public function testExtendSelf() {
  1361. try {
  1362. $this->View->layout = false;
  1363. $this->View->render('extend_self');
  1364. $this->fail('No exception');
  1365. } catch (\LogicException $e) {
  1366. ob_end_clean();
  1367. $this->assertContains('cannot have views extend themselves', $e->getMessage());
  1368. }
  1369. }
  1370. /**
  1371. * Make sure that extending in a loop causes an exception
  1372. *
  1373. * @return void
  1374. */
  1375. public function testExtendLoop() {
  1376. try {
  1377. $this->View->layout = false;
  1378. $this->View->render('extend_loop');
  1379. } catch (\LogicException $e) {
  1380. ob_end_clean();
  1381. $this->assertContains('cannot have views extend in a loop', $e->getMessage());
  1382. }
  1383. }
  1384. /**
  1385. * Test extend() in an element and a view.
  1386. *
  1387. * @return void
  1388. */
  1389. public function testExtendElement() {
  1390. $this->View->layout = false;
  1391. $content = $this->View->render('extend_element');
  1392. $expected = <<<TEXT
  1393. Parent View.
  1394. View content.
  1395. Parent Element.
  1396. Element content.
  1397. TEXT;
  1398. $this->assertEquals($expected, $content);
  1399. }
  1400. /**
  1401. * Extending an element which doesn't exist should throw a missing view exception
  1402. *
  1403. * @return void
  1404. */
  1405. public function testExtendMissingElement() {
  1406. try {
  1407. $this->View->layout = false;
  1408. $this->View->render('extend_missing_element');
  1409. $this->fail('No exception');
  1410. } catch (\LogicException $e) {
  1411. ob_end_clean();
  1412. ob_end_clean();
  1413. $this->assertContains('element', $e->getMessage());
  1414. }
  1415. }
  1416. /**
  1417. * Test extend() preceeded by an element()
  1418. *
  1419. * @return void
  1420. */
  1421. public function testExtendWithElementBeforeExtend() {
  1422. $this->View->layout = false;
  1423. $result = $this->View->render('extend_with_element');
  1424. $expected = <<<TEXT
  1425. Parent View.
  1426. this is the test elementThe view
  1427. TEXT;
  1428. $this->assertEquals($expected, $result);
  1429. }
  1430. /**
  1431. * Test memory leaks that existed in _paths at one point.
  1432. *
  1433. * @return void
  1434. */
  1435. public function testMemoryLeakInPaths() {
  1436. $this->ThemeController->plugin = null;
  1437. $this->ThemeController->name = 'Posts';
  1438. $this->ThemeController->viewPath = 'Posts';
  1439. $this->ThemeController->layout = 'whatever';
  1440. $this->ThemeController->theme = 'TestTheme';
  1441. $View = $this->ThemeController->createView();
  1442. $View->element('test_element');
  1443. $start = memory_get_usage();
  1444. for ($i = 0; $i < 10; $i++) {
  1445. $View->element('test_element');
  1446. }
  1447. $end = memory_get_usage();
  1448. $this->assertLessThanOrEqual($start + 5000, $end);
  1449. }
  1450. /**
  1451. * Tests that a view block uses default value when not assigned and uses assigned value when it is
  1452. *
  1453. * @return void
  1454. */
  1455. public function testBlockDefaultValue() {
  1456. $default = 'Default';
  1457. $result = $this->View->fetch('title', $default);
  1458. $this->assertEquals($default, $result);
  1459. $expected = 'My Title';
  1460. $this->View->assign('title', $expected);
  1461. $result = $this->View->fetch('title', $default);
  1462. $this->assertEquals($expected, $result);
  1463. }
  1464. /**
  1465. * Tests that a view variable uses default value when not assigned and uses assigned value when it is
  1466. *
  1467. * @return void
  1468. */
  1469. public function testViewVarDefaultValue() {
  1470. $default = 'Default';
  1471. $result = $this->View->get('title', $default);
  1472. $this->assertEquals($default, $result);
  1473. $expected = 'Back to the Future';
  1474. $this->View->set('title', $expected);
  1475. $result = $this->View->get('title', $default);
  1476. $this->assertEquals($expected, $result);
  1477. }
  1478. /**
  1479. * Test the helpers() method.
  1480. *
  1481. * @return void
  1482. */
  1483. public function testHelpers() {
  1484. $this->assertInstanceOf('Cake\View\HelperRegistry', $this->View->helpers());
  1485. $result = $this->View->helpers();
  1486. $this->assertSame($result, $this->View->helpers());
  1487. }
  1488. }