ViewTest.php 43 KB

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