ViewTest.php 46 KB

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