ViewTest.php 42 KB

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