ViewTest.php 41 KB

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