ViewTest.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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. $View->loadHelpers();
  713. $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
  714. $this->assertEquals('this is the test element', $result);
  715. }
  716. /**
  717. * Test elementCache method
  718. *
  719. * @return void
  720. */
  721. public function testElementCache() {
  722. Cache::drop('test_view');
  723. Cache::config('test_view', [
  724. 'engine' => 'File',
  725. 'duration' => '+1 day',
  726. 'path' => CACHE . 'views/',
  727. 'prefix' => ''
  728. ]);
  729. Cache::clear(true, 'test_view');
  730. $View = $this->PostsController->createView();
  731. $View->elementCache = 'test_view';
  732. $result = $View->element('test_element', array(), array('cache' => true));
  733. $expected = 'this is the test element';
  734. $this->assertEquals($expected, $result);
  735. $result = Cache::read('element__test_element_cache_callbacks', 'test_view');
  736. $this->assertEquals($expected, $result);
  737. $result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
  738. $this->assertEquals($expected, $result);
  739. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  740. $this->assertEquals($expected, $result);
  741. $View->element('test_element', array(
  742. 'param' => 'one',
  743. 'foo' => 'two'
  744. ), array(
  745. 'cache' => array('key' => 'custom_key')
  746. ));
  747. $result = Cache::read('element_custom_key', 'test_view');
  748. $this->assertEquals($expected, $result);
  749. $View->elementCache = 'default';
  750. $View->element('test_element', array(
  751. 'param' => 'one',
  752. 'foo' => 'two'
  753. ), array(
  754. 'cache' => array('config' => 'test_view'),
  755. ));
  756. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  757. $this->assertEquals($expected, $result);
  758. Cache::clear(true, 'test_view');
  759. Cache::drop('test_view');
  760. }
  761. /**
  762. * Test element events
  763. *
  764. * @return void
  765. */
  766. public function testViewEvent() {
  767. $View = $this->PostsController->createView();
  768. $View->autoLayout = false;
  769. $listener = new TestViewEventListener();
  770. $View->eventManager()->attach($listener);
  771. $View->render('index');
  772. $this->assertEquals(View::TYPE_VIEW, $listener->beforeRenderViewType);
  773. $this->assertEquals(View::TYPE_VIEW, $listener->afterRenderViewType);
  774. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  775. $View->element('test_element', array(), array('callbacks' => true));
  776. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  777. $this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType);
  778. $this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType);
  779. }
  780. /**
  781. * Test __get allowing access to helpers.
  782. *
  783. * @return void
  784. */
  785. public function testMagicGetAndAddHelper() {
  786. $View = new View();
  787. $View->addHelper('Html');
  788. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html);
  789. }
  790. /**
  791. * Test loadHelpers method
  792. *
  793. * @return void
  794. */
  795. public function testLoadHelpers() {
  796. $View = new View();
  797. $View->helpers = array('Html', 'Form');
  798. $View->loadHelpers();
  799. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  800. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  801. }
  802. /**
  803. * Test lazy loading helpers
  804. *
  805. * @return void
  806. */
  807. public function testLazyLoadHelpers() {
  808. $View = new View();
  809. $View->helpers = array();
  810. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  811. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  812. }
  813. /**
  814. * Test the correct triggering of helper callbacks
  815. *
  816. * @return void
  817. */
  818. public function testHelperCallbackTriggering() {
  819. $View = $this->PostsController->createView();
  820. $manager = $this->getMock('Cake\Event\EventManager');
  821. $View->eventManager($manager);
  822. $manager->expects($this->at(0))->method('dispatch')
  823. ->with(
  824. $this->logicalAnd(
  825. $this->isInstanceOf('Cake\Event\Event'),
  826. $this->attributeEqualTo('_name', 'View.beforeRender'),
  827. $this->attributeEqualTo('_subject', $View)
  828. )
  829. );
  830. $manager->expects($this->at(1))->method('dispatch')
  831. ->with(
  832. $this->logicalAnd(
  833. $this->isInstanceOf('Cake\Event\Event'),
  834. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  835. $this->attributeEqualTo('_subject', $View)
  836. )
  837. );
  838. $manager->expects($this->at(2))->method('dispatch')
  839. ->with(
  840. $this->logicalAnd(
  841. $this->isInstanceOf('Cake\Event\Event'),
  842. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  843. $this->attributeEqualTo('_subject', $View)
  844. )
  845. );
  846. $manager->expects($this->at(3))->method('dispatch')
  847. ->with(
  848. $this->logicalAnd(
  849. $this->isInstanceOf('Cake\Event\Event'),
  850. $this->attributeEqualTo('_name', 'View.afterRender'),
  851. $this->attributeEqualTo('_subject', $View)
  852. )
  853. );
  854. $manager->expects($this->at(4))->method('dispatch')
  855. ->with(
  856. $this->logicalAnd(
  857. $this->isInstanceOf('Cake\Event\Event'),
  858. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  859. $this->attributeEqualTo('_subject', $View)
  860. )
  861. );
  862. $manager->expects($this->at(5))->method('dispatch')
  863. ->with(
  864. $this->logicalAnd(
  865. $this->isInstanceOf('Cake\Event\Event'),
  866. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  867. $this->attributeEqualTo('_subject', $View)
  868. )
  869. );
  870. $manager->expects($this->at(6))->method('dispatch')
  871. ->with(
  872. $this->logicalAnd(
  873. $this->isInstanceOf('Cake\Event\Event'),
  874. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  875. $this->attributeEqualTo('_subject', $View)
  876. )
  877. );
  878. $manager->expects($this->at(7))->method('dispatch')
  879. ->with(
  880. $this->logicalAnd(
  881. $this->isInstanceOf('Cake\Event\Event'),
  882. $this->attributeEqualTo('_name', 'View.afterLayout'),
  883. $this->attributeEqualTo('_subject', $View)
  884. )
  885. );
  886. $View->render('index');
  887. }
  888. /**
  889. * Test beforeLayout method
  890. *
  891. * @return void
  892. */
  893. public function testBeforeLayout() {
  894. $this->PostsController->helpers = array(
  895. 'Session',
  896. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  897. 'Html'
  898. );
  899. $View = $this->PostsController->createView();
  900. $View->render('index');
  901. $this->assertEquals('Valuation', $View->helpers()->TestBeforeAfter->property);
  902. }
  903. /**
  904. * Test afterLayout method
  905. *
  906. * @return void
  907. */
  908. public function testAfterLayout() {
  909. $this->PostsController->helpers = array(
  910. 'Session',
  911. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  912. 'Html'
  913. );
  914. $this->PostsController->set('variable', 'values');
  915. $View = $this->PostsController->createView();
  916. $content = 'This is my view output';
  917. $result = $View->renderLayout($content, 'default');
  918. $this->assertRegExp('/modified in the afterlife/', $result);
  919. $this->assertRegExp('/This is my view output/', $result);
  920. }
  921. /**
  922. * Test renderLoadHelper method
  923. *
  924. * @return void
  925. */
  926. public function testRenderLoadHelper() {
  927. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  928. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  929. $result = $View->render('index', false);
  930. $this->assertEquals('posts index', $result);
  931. $attached = $View->helpers()->loaded();
  932. $this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
  933. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  934. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  935. $result = $View->render('index', false);
  936. $this->assertEquals('posts index', $result);
  937. $attached = $View->helpers()->loaded();
  938. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  939. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  940. }
  941. /**
  942. * Test render method
  943. *
  944. * @return void
  945. */
  946. public function testRender() {
  947. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  948. $result = $View->render('index');
  949. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  950. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  951. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  952. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  953. $result = $View->render(false, 'ajax2');
  954. $this->assertRegExp('/Ajax\!/', $result);
  955. $this->assertNull($View->render(false, 'ajax2'));
  956. $this->PostsController->helpers = array('Session', 'Html');
  957. $this->PostsController->request->params['action'] = 'index';
  958. Configure::write('Cache.check', true);
  959. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  960. $result = $View->render('index');
  961. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  962. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  963. }
  964. /**
  965. * Test that View::$view works
  966. *
  967. * @return void
  968. */
  969. public function testRenderUsingViewProperty() {
  970. $this->PostsController->view = 'cache_form';
  971. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  972. $this->assertEquals('cache_form', $View->view);
  973. $result = $View->render();
  974. $this->assertRegExp('/Add User/', $result);
  975. }
  976. /**
  977. * Test render()ing a file in a subdir from a custom viewPath
  978. * in a plugin.
  979. *
  980. * @return void
  981. */
  982. public function testGetViewFileNameSubdirWithPluginAndViewPath() {
  983. $this->PostsController->plugin = 'TestPlugin';
  984. $this->PostsController->name = 'Posts';
  985. $this->PostsController->viewPath = 'Element';
  986. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  987. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  988. $result = $View->getViewFileName('sub_dir/sub_element');
  989. $expected = $pluginPath . 'src' . DS . 'Template' . DS . 'Element' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  990. $this->assertPathEquals($expected, $result);
  991. }
  992. /**
  993. * Test that view vars can replace the local helper variables
  994. * and not overwrite the $this->Helper references
  995. *
  996. * @return void
  997. */
  998. public function testViewVarOverwritingLocalHelperVar() {
  999. $Controller = new ViewPostsController();
  1000. $Controller->helpers = array('Session', 'Html');
  1001. $Controller->set('html', 'I am some test html');
  1002. $View = $Controller->createView();
  1003. $result = $View->render('helper_overwrite', false);
  1004. $this->assertRegExp('/I am some test html/', $result);
  1005. $this->assertRegExp('/Test link/', $result);
  1006. }
  1007. /**
  1008. * Test getViewFileName method
  1009. *
  1010. * @return void
  1011. */
  1012. public function testViewFileName() {
  1013. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1014. $result = $View->getViewFileName('index');
  1015. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1016. $result = $View->getViewFileName('TestPlugin.index');
  1017. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1018. $result = $View->getViewFileName('/Pages/home');
  1019. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  1020. $result = $View->getViewFileName('../Element/test_element');
  1021. $this->assertRegExp('/Element(\/|\\\)test_element.ctp/', $result);
  1022. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  1023. $result = $View->getViewFileName('../Posts/index');
  1024. $this->assertPathEquals($expected, $result);
  1025. }
  1026. /**
  1027. * Test creating a block with capturing output.
  1028. *
  1029. * @return void
  1030. */
  1031. public function testBlockCaptureOverwrite() {
  1032. $this->View->start('test');
  1033. echo 'Block content';
  1034. $this->View->end();
  1035. $this->View->start('test');
  1036. echo 'New content';
  1037. $this->View->end();
  1038. $result = $this->View->fetch('test');
  1039. $this->assertEquals('New content', $result);
  1040. }
  1041. /**
  1042. * Test that blocks can be fetched inside a block with the same name
  1043. *
  1044. * @return void
  1045. */
  1046. public function testBlockExtend() {
  1047. $this->View->start('test');
  1048. echo 'Block content';
  1049. $this->View->end();
  1050. $this->View->start('test');
  1051. echo $this->View->fetch('test');
  1052. echo 'New content';
  1053. $this->View->end();
  1054. $result = $this->View->fetch('test');
  1055. $this->assertEquals('Block contentNew content', $result);
  1056. }
  1057. /**
  1058. * Test creating a block with capturing output.
  1059. *
  1060. * @return void
  1061. */
  1062. public function testBlockCapture() {
  1063. $this->View->start('test');
  1064. echo 'Block content';
  1065. $this->View->end();
  1066. $result = $this->View->fetch('test');
  1067. $this->assertEquals('Block content', $result);
  1068. }
  1069. /**
  1070. * Test appending to a block with capturing output.
  1071. *
  1072. * @return void
  1073. */
  1074. public function testBlockAppendCapture() {
  1075. $this->View->start('test');
  1076. echo 'Content ';
  1077. $this->View->end();
  1078. $this->View->append('test');
  1079. echo 'appended';
  1080. $this->View->end();
  1081. $result = $this->View->fetch('test');
  1082. $this->assertEquals('Content appended', $result);
  1083. }
  1084. /**
  1085. * Test setting a block's content.
  1086. *
  1087. * @return void
  1088. */
  1089. public function testBlockSet() {
  1090. $this->View->assign('test', 'Block content');
  1091. $result = $this->View->fetch('test');
  1092. $this->assertEquals('Block content', $result);
  1093. }
  1094. /**
  1095. * Test resetting a block's content.
  1096. *
  1097. * @return void
  1098. */
  1099. public function testBlockReset() {
  1100. $this->View->assign('test', '');
  1101. $result = $this->View->fetch('test', 'This should not be returned');
  1102. $this->assertSame('', $result);
  1103. }
  1104. /**
  1105. * Test setting a block's content to null
  1106. *
  1107. * @return void
  1108. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
  1109. */
  1110. public function testBlockSetNull() {
  1111. $this->View->assign('testWithNull', null);
  1112. $result = $this->View->fetch('testWithNull');
  1113. $this->assertSame('', $result);
  1114. }
  1115. /**
  1116. * Test setting a block's content to an object with __toString magic method
  1117. *
  1118. * @return void
  1119. */
  1120. public function testBlockSetObjectWithToString() {
  1121. $objectWithToString = new TestObjectWithToString();
  1122. $this->View->assign('testWithObjectWithToString', $objectWithToString);
  1123. $result = $this->View->fetch('testWithObjectWithToString');
  1124. $this->assertSame("I'm ObjectWithToString", $result);
  1125. }
  1126. /**
  1127. * Test setting a block's content to an object without __toString magic method
  1128. *
  1129. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1130. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1131. *
  1132. * @expectedException PHPUnit_Framework_Error
  1133. * @return void
  1134. */
  1135. public function testBlockSetObjectWithoutToString() {
  1136. $objectWithToString = new TestObjectWithoutToString();
  1137. $this->View->assign('testWithObjectWithoutToString', $objectWithToString);
  1138. }
  1139. /**
  1140. * Test setting a block's content to a decimal
  1141. *
  1142. * @return void
  1143. */
  1144. public function testBlockSetDecimal() {
  1145. $this->View->assign('testWithDecimal', 1.23456789);
  1146. $result = $this->View->fetch('testWithDecimal');
  1147. $this->assertEquals('1.23456789', $result);
  1148. }
  1149. /**
  1150. * Data provider for block related tests.
  1151. *
  1152. * @return array
  1153. */
  1154. public static function blockValueProvider() {
  1155. return array(
  1156. 'string' => array('A string value'),
  1157. 'decimal' => array(1.23456),
  1158. 'object with __toString' => array(new TestObjectWithToString()),
  1159. );
  1160. }
  1161. /**
  1162. * Test appending to a block with append.
  1163. *
  1164. * @param mixed $value Value
  1165. * @return void
  1166. * @dataProvider blockValueProvider
  1167. */
  1168. public function testBlockAppend($value) {
  1169. $this->View->assign('testBlock', 'Block');
  1170. $this->View->append('testBlock', $value);
  1171. $result = $this->View->fetch('testBlock');
  1172. $this->assertSame('Block' . $value, $result);
  1173. }
  1174. /**
  1175. * Test appending an object without __toString magic method to a block with append.
  1176. *
  1177. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1178. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1179. *
  1180. * @expectedException PHPUnit_Framework_Error
  1181. * @return void
  1182. */
  1183. public function testBlockAppendObjectWithoutToString() {
  1184. $object = new TestObjectWithoutToString();
  1185. $this->View->assign('testBlock', 'Block ');
  1186. $this->View->append('testBlock', $object);
  1187. }
  1188. /**
  1189. * Test prepending to a block with prepend.
  1190. *
  1191. * @param mixed $value Value
  1192. * @return void
  1193. * @dataProvider blockValueProvider
  1194. */
  1195. public function testBlockPrepend($value) {
  1196. $this->View->assign('test', 'Block');
  1197. $this->View->prepend('test', $value);
  1198. $result = $this->View->fetch('test');
  1199. $this->assertEquals($value . 'Block', $result);
  1200. }
  1201. /**
  1202. * Test prepending an object without __toString magic method to a block with prepend.
  1203. *
  1204. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1205. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1206. *
  1207. * @expectedException PHPUnit_Framework_Error
  1208. * @return void
  1209. */
  1210. public function testBlockPrependObjectWithoutToString() {
  1211. $object = new TestObjectWithoutToString();
  1212. $this->View->assign('test', 'Block ');
  1213. $this->View->prepend('test', $object);
  1214. }
  1215. /**
  1216. * You should be able to append to undefined blocks.
  1217. *
  1218. * @return void
  1219. */
  1220. public function testBlockAppendUndefined() {
  1221. $this->View->append('test', 'Unknown');
  1222. $result = $this->View->fetch('test');
  1223. $this->assertEquals('Unknown', $result);
  1224. }
  1225. /**
  1226. * You should be able to prepend to undefined blocks.
  1227. *
  1228. * @return void
  1229. */
  1230. public function testBlockPrependUndefined() {
  1231. $this->View->prepend('test', 'Unknown');
  1232. $result = $this->View->fetch('test');
  1233. $this->assertEquals('Unknown', $result);
  1234. }
  1235. /**
  1236. * Test getting block names
  1237. *
  1238. * @return void
  1239. */
  1240. public function testBlocks() {
  1241. $this->View->append('test', 'one');
  1242. $this->View->assign('test1', 'one');
  1243. $this->assertEquals(array('test', 'test1'), $this->View->blocks());
  1244. }
  1245. /**
  1246. * Test that blocks can be nested.
  1247. *
  1248. * @return void
  1249. */
  1250. public function testNestedBlocks() {
  1251. $this->View->start('first');
  1252. echo 'In first ';
  1253. $this->View->start('second');
  1254. echo 'In second';
  1255. $this->View->end();
  1256. echo 'In first';
  1257. $this->View->end();
  1258. $this->assertEquals('In first In first', $this->View->fetch('first'));
  1259. $this->assertEquals('In second', $this->View->fetch('second'));
  1260. }
  1261. /**
  1262. * Test that starting the same block twice throws an exception
  1263. *
  1264. * @expectedException \Cake\Core\Exception\Exception
  1265. * @return void
  1266. */
  1267. public function testStartBlocksTwice() {
  1268. $this->View->start('first');
  1269. $this->View->start('first');
  1270. }
  1271. /**
  1272. * Test that an exception gets thrown when you leave a block open at the end
  1273. * of a view.
  1274. *
  1275. * @expectedException \LogicException
  1276. * @return void
  1277. */
  1278. public function testExceptionOnOpenBlock() {
  1279. $this->View->render('open_block');
  1280. }
  1281. /**
  1282. * Test nested extended views.
  1283. *
  1284. * @return void
  1285. */
  1286. public function testExtendNested() {
  1287. $this->View->layout = false;
  1288. $content = $this->View->render('nested_extends');
  1289. $expected = <<<TEXT
  1290. This is the second parent.
  1291. This is the first parent.
  1292. This is the first template.
  1293. Sidebar Content.
  1294. TEXT;
  1295. $this->assertEquals($expected, $content);
  1296. }
  1297. /**
  1298. * Make sure that extending the current view with itself causes an exception
  1299. *
  1300. * @expectedException LogicException
  1301. * @return void
  1302. */
  1303. public function testExtendSelf() {
  1304. $this->View->layout = false;
  1305. $this->View->render('extend_self');
  1306. }
  1307. /**
  1308. * Make sure that extending in a loop causes an exception
  1309. *
  1310. * @expectedException LogicException
  1311. * @return void
  1312. */
  1313. public function testExtendLoop() {
  1314. $this->View->layout = false;
  1315. $this->View->render('extend_loop');
  1316. }
  1317. /**
  1318. * Test extend() in an element and a view.
  1319. *
  1320. * @return void
  1321. */
  1322. public function testExtendElement() {
  1323. $this->View->layout = false;
  1324. $content = $this->View->render('extend_element');
  1325. $expected = <<<TEXT
  1326. Parent View.
  1327. View content.
  1328. Parent Element.
  1329. Element content.
  1330. TEXT;
  1331. $this->assertEquals($expected, $content);
  1332. }
  1333. /**
  1334. * Extending an element which doesn't exist should throw a missing view exception
  1335. *
  1336. * @expectedException LogicException
  1337. * @return void
  1338. */
  1339. public function testExtendMissingElement() {
  1340. $this->View->layout = false;
  1341. $this->View->render('extend_missing_element');
  1342. }
  1343. /**
  1344. * Test extend() preceeded by an element()
  1345. *
  1346. * @return void
  1347. */
  1348. public function testExtendWithElementBeforeExtend() {
  1349. $this->View->layout = false;
  1350. $result = $this->View->render('extend_with_element');
  1351. $expected = <<<TEXT
  1352. Parent View.
  1353. this is the test elementThe view
  1354. TEXT;
  1355. $this->assertEquals($expected, $result);
  1356. }
  1357. /**
  1358. * Test memory leaks that existed in _paths at one point.
  1359. *
  1360. * @return void
  1361. */
  1362. public function testMemoryLeakInPaths() {
  1363. $this->ThemeController->plugin = null;
  1364. $this->ThemeController->name = 'Posts';
  1365. $this->ThemeController->viewPath = 'Posts';
  1366. $this->ThemeController->layout = 'whatever';
  1367. $this->ThemeController->theme = 'TestTheme';
  1368. $View = $this->ThemeController->createView();
  1369. $View->element('test_element');
  1370. $start = memory_get_usage();
  1371. for ($i = 0; $i < 10; $i++) {
  1372. $View->element('test_element');
  1373. }
  1374. $end = memory_get_usage();
  1375. $this->assertLessThanOrEqual($start + 5000, $end);
  1376. }
  1377. /**
  1378. * Tests that a view block uses default value when not assigned and uses assigned value when it is
  1379. *
  1380. * @return void
  1381. */
  1382. public function testBlockDefaultValue() {
  1383. $default = 'Default';
  1384. $result = $this->View->fetch('title', $default);
  1385. $this->assertEquals($default, $result);
  1386. $expected = 'My Title';
  1387. $this->View->assign('title', $expected);
  1388. $result = $this->View->fetch('title', $default);
  1389. $this->assertEquals($expected, $result);
  1390. }
  1391. /**
  1392. * Tests that a view variable uses default value when not assigned and uses assigned value when it is
  1393. *
  1394. * @return void
  1395. */
  1396. public function testViewVarDefaultValue() {
  1397. $default = 'Default';
  1398. $result = $this->View->get('title', $default);
  1399. $this->assertEquals($default, $result);
  1400. $expected = 'Back to the Future';
  1401. $this->View->set('title', $expected);
  1402. $result = $this->View->get('title', $default);
  1403. $this->assertEquals($expected, $result);
  1404. }
  1405. /**
  1406. * Test the helpers() method.
  1407. *
  1408. * @return void
  1409. */
  1410. public function testHelpers() {
  1411. $this->assertInstanceOf('Cake\View\HelperRegistry', $this->View->helpers());
  1412. $result = $this->View->helpers();
  1413. $this->assertSame($result, $this->View->helpers());
  1414. }
  1415. }