ViewTest.php 44 KB

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