ViewTest.php 43 KB

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