ViewTest.php 43 KB

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