ViewTest.php 48 KB

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