ViewTest.php 44 KB

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