ViewTest.php 43 KB

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