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. * 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 bool $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 = $this->PostsController->createView();
  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 = $this->ThemePostsController->createView();
  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. $request = $this->getMock('Cake\Network\Request');
  262. $response = $this->getMock('Cake\Network\Response');
  263. $viewOptions = [ 'plugin' => null,
  264. 'name' => 'Pages',
  265. 'viewPath' => 'Pages'
  266. ];
  267. $request->action = 'display';
  268. $request->params['pass'] = array('home');
  269. $ThemeView = new TestThemeView(null, null, null, $viewOptions);
  270. $ThemeView->theme = 'TestTheme';
  271. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'home.ctp';
  272. $result = $ThemeView->getViewFileName('home');
  273. $this->assertPathEquals($expected, $result);
  274. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Posts' . DS . 'index.ctp';
  275. $result = $ThemeView->getViewFileName('/Posts/index');
  276. $this->assertPathEquals($expected, $result);
  277. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layout' . DS . 'default.ctp';
  278. $result = $ThemeView->getLayoutFileName();
  279. $this->assertPathEquals($expected, $result);
  280. $ThemeView->layoutPath = 'rss';
  281. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'rss' . DS . 'default.ctp';
  282. $result = $ThemeView->getLayoutFileName();
  283. $this->assertPathEquals($expected, $result);
  284. $ThemeView->layoutPath = 'Email' . DS . 'html';
  285. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'Email' . DS . 'html' . DS . 'default.ctp';
  286. $result = $ThemeView->getLayoutFileName();
  287. $this->assertPathEquals($expected, $result);
  288. }
  289. /**
  290. * Test getLayoutFileName method on plugin
  291. *
  292. * @return void
  293. */
  294. public function testPluginGetTemplate() {
  295. $viewOptions = ['plugin' => 'TestPlugin',
  296. 'name' => 'TestPlugin',
  297. 'viewPath' => 'Tests',
  298. 'view' => 'index'
  299. ];
  300. $View = new TestView(null, null, null, $viewOptions);
  301. $expected = Plugin::path('TestPlugin') . 'Template' . DS . 'Tests' . DS . 'index.ctp';
  302. $result = $View->getViewFileName('index');
  303. $this->assertEquals($expected, $result);
  304. $expected = Plugin::path('TestPlugin') . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  305. $result = $View->getLayoutFileName();
  306. $this->assertEquals($expected, $result);
  307. }
  308. /**
  309. * Test getViewFileName method on plugin
  310. *
  311. * @return void
  312. */
  313. public function testPluginThemedGetTemplate() {
  314. $viewOptions = ['plugin' => 'TestPlugin',
  315. 'name' => 'TestPlugin',
  316. 'viewPath' => 'Tests',
  317. 'view' => 'index',
  318. 'theme' => 'TestTheme'
  319. ];
  320. $ThemeView = new TestThemeView(null, null, null, $viewOptions);
  321. $themePath = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Themed' . DS . 'TestTheme' . DS;
  322. $expected = $themePath . 'Plugin' . DS . 'TestPlugin' . DS . 'Tests' . DS . 'index.ctp';
  323. $result = $ThemeView->getViewFileName('index');
  324. $this->assertPathEquals($expected, $result);
  325. $expected = $themePath . 'Plugin' . DS . 'TestPlugin' . DS . 'Layout' . DS . 'plugin_default.ctp';
  326. $result = $ThemeView->getLayoutFileName('plugin_default');
  327. $this->assertPathEquals($expected, $result);
  328. $expected = $themePath . 'Layout' . DS . 'default.ctp';
  329. $result = $ThemeView->getLayoutFileName('default');
  330. $this->assertPathEquals($expected, $result);
  331. }
  332. /**
  333. * Test that plugin/$plugin_name is only appended to the paths it should be.
  334. *
  335. * @return void
  336. */
  337. public function testPluginPathGeneration() {
  338. $viewOptions = ['plugin' => 'TestPlugin',
  339. 'name' => 'TestPlugin',
  340. 'viewPath' => 'Tests',
  341. 'view' => 'index'
  342. ];
  343. $View = new TestView(null, null, null, $viewOptions);
  344. $paths = $View->paths();
  345. $expected = array_merge(App::path('Template'), App::core('Template'));
  346. $this->assertEquals($expected, $paths);
  347. $paths = $View->paths('TestPlugin');
  348. $pluginPath = Plugin::path('TestPlugin');
  349. $expected = array(
  350. TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Plugin' . DS . 'TestPlugin' . DS,
  351. $pluginPath . 'Template' . DS,
  352. TEST_APP . 'TestApp' . DS . 'Template' . DS,
  353. CAKE . 'Template' . DS,
  354. );
  355. $this->assertPathEquals($expected, $paths);
  356. }
  357. /**
  358. * Test that CamelCase'd plugins still find their view files.
  359. *
  360. * @return void
  361. */
  362. public function testCamelCasePluginGetTemplate() {
  363. $viewOptions = ['plugin' => 'TestPlugin',
  364. 'name' => 'TestPlugin',
  365. 'viewPath' => 'Tests',
  366. 'view' => 'index'
  367. ];
  368. $View = new TestView(null, null, null, $viewOptions);
  369. $pluginPath = Plugin::path('TestPlugin');
  370. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'Template' . DS . 'Tests' . DS . 'index.ctp';
  371. $result = $View->getViewFileName('index');
  372. $this->assertPathEquals($expected, $result);
  373. $expected = $pluginPath . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  374. $result = $View->getLayoutFileName();
  375. $this->assertPathEquals($expected, $result);
  376. }
  377. /**
  378. * Test getViewFileName method
  379. *
  380. * @return void
  381. */
  382. public function testGetViewFileNames() {
  383. $viewOptions = ['plugin' => null,
  384. 'name' => 'Pages',
  385. 'viewPath' => 'Pages'
  386. ];
  387. $request = $this->getMock('Cake\Network\Request');
  388. $response = $this->getMock('Cake\Network\Response');
  389. $View = new TestView(null, null, null, $viewOptions);
  390. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'home.ctp';
  391. $result = $View->getViewFileName('home');
  392. $this->assertPathEquals($expected, $result);
  393. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  394. $result = $View->getViewFileName('/Posts/index');
  395. $this->assertPathEquals($expected, $result);
  396. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  397. $result = $View->getViewFileName('../Posts/index');
  398. $this->assertPathEquals($expected, $result);
  399. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'page.home.ctp';
  400. $result = $View->getViewFileName('page.home');
  401. $this->assertPathEquals($expected, $result, 'Should not ruin files with dots.');
  402. Plugin::load('TestPlugin');
  403. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'home.ctp';
  404. $result = $View->getViewFileName('TestPlugin.home');
  405. $this->assertPathEquals($expected, $result, 'Plugin is missing the view, cascade to app.');
  406. $View->viewPath = 'Tests';
  407. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'Template' . DS . 'Tests' . DS . 'index.ctp';
  408. $result = $View->getViewFileName('TestPlugin.index');
  409. $this->assertPathEquals($expected, $result);
  410. }
  411. /**
  412. * Test getting layout filenames
  413. *
  414. * @return void
  415. */
  416. public function testGetLayoutFileName() {
  417. $viewOptions = ['plugin' => null,
  418. 'name' => 'Pages',
  419. 'viewPath' => 'Pages',
  420. 'action' => 'display'
  421. ];
  422. $View = new TestView(null, null, null, $viewOptions);
  423. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  424. $result = $View->getLayoutFileName();
  425. $this->assertPathEquals($expected, $result);
  426. $View->layoutPath = 'rss';
  427. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'rss' . DS . 'default.ctp';
  428. $result = $View->getLayoutFileName();
  429. $this->assertPathEquals($expected, $result);
  430. $View->layoutPath = 'Email' . DS . 'html';
  431. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'Email' . DS . 'html' . DS . 'default.ctp';
  432. $result = $View->getLayoutFileName();
  433. $this->assertPathEquals($expected, $result);
  434. }
  435. /**
  436. * Test getting layout filenames for plugins.
  437. *
  438. * @return void
  439. */
  440. public function testGetLayoutFileNamePlugin() {
  441. $viewOptions = ['plugin' => null,
  442. 'name' => 'Pages',
  443. 'viewPath' => 'Pages',
  444. 'action' => 'display'
  445. ];
  446. $View = new TestView(null, null, null, $viewOptions);
  447. Plugin::load('TestPlugin');
  448. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  449. $result = $View->getLayoutFileName('TestPlugin.default');
  450. $this->assertPathEquals($expected, $result);
  451. $View->plugin = 'TestPlugin';
  452. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  453. $result = $View->getLayoutFileName('default');
  454. $this->assertPathEquals($expected, $result);
  455. }
  456. /**
  457. * Test for missing views
  458. *
  459. * @expectedException \Cake\View\Error\MissingViewException
  460. * @return void
  461. */
  462. public function testMissingView() {
  463. $viewOptions = ['plugin' => null,
  464. 'name' => 'Pages',
  465. 'viewPath' => 'Pages'
  466. ];
  467. $request = $this->getMock('Cake\Network\Request');
  468. $response = $this->getMock('Cake\Network\Response');
  469. $View = new TestView($request, $response, null, $viewOptions);
  470. ob_start();
  471. $View->getViewFileName('does_not_exist');
  472. ob_get_clean();
  473. }
  474. /**
  475. * Test for missing layouts
  476. *
  477. * @expectedException \Cake\View\Error\MissingLayoutException
  478. * @return void
  479. */
  480. public function testMissingLayout() {
  481. $viewOptions = ['plugin' => null,
  482. 'name' => 'Pages',
  483. 'viewPath' => 'Pages',
  484. 'layout' => 'whatever'
  485. ];
  486. $View = new TestView(null, null, null, $viewOptions);
  487. ob_start();
  488. $View->getLayoutFileName();
  489. ob_get_clean();
  490. }
  491. /**
  492. * Test viewVars method
  493. *
  494. * @return void
  495. */
  496. public function testViewVars() {
  497. $this->assertEquals(array('testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data'), $this->View->viewVars);
  498. }
  499. /**
  500. * Test generation of UUIDs method
  501. *
  502. * @return void
  503. */
  504. public function testUUIDGeneration() {
  505. Router::connect('/:controller', ['action' => 'index']);
  506. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  507. $this->assertEquals('form5988016017', $result);
  508. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  509. $this->assertEquals('formc3dc6be854', $result);
  510. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  511. $this->assertEquals('form28f92cc87f', $result);
  512. }
  513. /**
  514. * Test elementExists method
  515. *
  516. * @return void
  517. */
  518. public function testElementExists() {
  519. $result = $this->View->elementExists('test_element');
  520. $this->assertTrue($result);
  521. $result = $this->View->elementExists('TestPlugin.plugin_element');
  522. $this->assertTrue($result);
  523. $result = $this->View->elementExists('non_existent_element');
  524. $this->assertFalse($result);
  525. $result = $this->View->elementExists('TestPlugin.element');
  526. $this->assertFalse($result);
  527. $this->View->plugin = 'TestPlugin';
  528. $result = $this->View->elementExists('test_plugin_element');
  529. $this->assertTrue($result);
  530. }
  531. /**
  532. * Test element method
  533. *
  534. * @return void
  535. */
  536. public function testElement() {
  537. $result = $this->View->element('test_element');
  538. $this->assertEquals('this is the test element', $result);
  539. $result = $this->View->element('TestPlugin.plugin_element');
  540. $this->assertEquals('this is the plugin element using params[plugin]', $result);
  541. $this->View->plugin = 'TestPlugin';
  542. $result = $this->View->element('test_plugin_element');
  543. $this->assertEquals('this is the test set using View::$plugin plugin element', $result);
  544. }
  545. /**
  546. * Test elementInexistent method
  547. *
  548. * @expectedException PHPUnit_Framework_Error_Notice
  549. * @return void
  550. */
  551. public function testElementInexistent() {
  552. $this->View->element('non_existent_element');
  553. }
  554. /**
  555. * Test elementInexistent3 method
  556. *
  557. * @expectedException PHPUnit_Framework_Error_Notice
  558. * @return void
  559. */
  560. public function testElementInexistent3() {
  561. $this->View->element('test_plugin.plugin_element');
  562. }
  563. /**
  564. * Test that elements can have callbacks
  565. *
  566. * @return void
  567. */
  568. public function testElementCallbacks() {
  569. $count = 0;
  570. $callback = function ($event, $file) use (&$count) {
  571. $count++;
  572. };
  573. $events = $this->View->getEventManager();
  574. $events->attach($callback, 'View.beforeRender');
  575. $events->attach($callback, 'View.afterRender');
  576. $this->View->element('test_element', array(), array('callbacks' => true));
  577. $this->assertEquals(2, $count);
  578. }
  579. /**
  580. * Test that additional element viewVars don't get overwritten with helpers.
  581. *
  582. * @return void
  583. */
  584. public function testElementParamsDontOverwriteHelpers() {
  585. $Controller = new ViewPostsController();
  586. $Controller->helpers = array('Form');
  587. $View = $Controller->createView();
  588. $result = $View->element('type_check', array('form' => 'string'), array('callbacks' => true));
  589. $this->assertEquals('string', $result);
  590. $View->set('form', 'string');
  591. $result = $View->element('type_check', array(), array('callbacks' => true));
  592. $this->assertEquals('string', $result);
  593. }
  594. /**
  595. * Test elementCacheHelperNoCache method
  596. *
  597. * @return void
  598. */
  599. public function testElementCacheHelperNoCache() {
  600. $Controller = new ViewPostsController();
  601. $View = $Controller->createView();
  602. $View->loadHelpers();
  603. $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
  604. $this->assertEquals('this is the test element', $result);
  605. }
  606. /**
  607. * Test elementCache method
  608. *
  609. * @return void
  610. */
  611. public function testElementCache() {
  612. Cache::drop('test_view');
  613. Cache::config('test_view', [
  614. 'engine' => 'File',
  615. 'duration' => '+1 day',
  616. 'path' => CACHE . 'views/',
  617. 'prefix' => ''
  618. ]);
  619. Cache::clear(true, 'test_view');
  620. $View = $this->PostsController->createView();
  621. $View->elementCache = 'test_view';
  622. $result = $View->element('test_element', array(), array('cache' => true));
  623. $expected = 'this is the test element';
  624. $this->assertEquals($expected, $result);
  625. $result = Cache::read('element__test_element_cache_callbacks', 'test_view');
  626. $this->assertEquals($expected, $result);
  627. $result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
  628. $this->assertEquals($expected, $result);
  629. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  630. $this->assertEquals($expected, $result);
  631. $View->element('test_element', array(
  632. 'param' => 'one',
  633. 'foo' => 'two'
  634. ), array(
  635. 'cache' => array('key' => 'custom_key')
  636. ));
  637. $result = Cache::read('element_custom_key', 'test_view');
  638. $this->assertEquals($expected, $result);
  639. $View->elementCache = 'default';
  640. $View->element('test_element', array(
  641. 'param' => 'one',
  642. 'foo' => 'two'
  643. ), array(
  644. 'cache' => array('config' => 'test_view'),
  645. ));
  646. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  647. $this->assertEquals($expected, $result);
  648. Cache::clear(true, 'test_view');
  649. Cache::drop('test_view');
  650. }
  651. /**
  652. * Test __get allowing access to helpers.
  653. *
  654. * @return void
  655. */
  656. public function testMagicGetAndAddHelper() {
  657. $View = new View();
  658. $View->addHelper('Html');
  659. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html);
  660. }
  661. /**
  662. * Test loadHelpers method
  663. *
  664. * @return void
  665. */
  666. public function testLoadHelpers() {
  667. $View = new View();
  668. $View->helpers = array('Html', 'Form');
  669. $View->loadHelpers();
  670. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  671. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  672. }
  673. /**
  674. * Test lazy loading helpers
  675. *
  676. * @return void
  677. */
  678. public function testLazyLoadHelpers() {
  679. $View = new View();
  680. $View->helpers = array();
  681. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  682. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  683. }
  684. /**
  685. * Test the correct triggering of helper callbacks
  686. *
  687. * @return void
  688. */
  689. public function testHelperCallbackTriggering() {
  690. $View = $this->PostsController->createView();
  691. $manager = $this->getMock('Cake\Event\EventManager');
  692. $View->setEventManager($manager);
  693. $manager->expects($this->at(0))->method('dispatch')
  694. ->with(
  695. $this->logicalAnd(
  696. $this->isInstanceOf('Cake\Event\Event'),
  697. $this->attributeEqualTo('_name', 'View.beforeRender'),
  698. $this->attributeEqualTo('_subject', $View)
  699. )
  700. );
  701. $manager->expects($this->at(1))->method('dispatch')
  702. ->with(
  703. $this->logicalAnd(
  704. $this->isInstanceOf('Cake\Event\Event'),
  705. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  706. $this->attributeEqualTo('_subject', $View)
  707. )
  708. );
  709. $manager->expects($this->at(2))->method('dispatch')
  710. ->with(
  711. $this->logicalAnd(
  712. $this->isInstanceOf('Cake\Event\Event'),
  713. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  714. $this->attributeEqualTo('_subject', $View)
  715. )
  716. );
  717. $manager->expects($this->at(3))->method('dispatch')
  718. ->with(
  719. $this->logicalAnd(
  720. $this->isInstanceOf('Cake\Event\Event'),
  721. $this->attributeEqualTo('_name', 'View.afterRender'),
  722. $this->attributeEqualTo('_subject', $View)
  723. )
  724. );
  725. $manager->expects($this->at(4))->method('dispatch')
  726. ->with(
  727. $this->logicalAnd(
  728. $this->isInstanceOf('Cake\Event\Event'),
  729. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  730. $this->attributeEqualTo('_subject', $View)
  731. )
  732. );
  733. $manager->expects($this->at(5))->method('dispatch')
  734. ->with(
  735. $this->logicalAnd(
  736. $this->isInstanceOf('Cake\Event\Event'),
  737. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  738. $this->attributeEqualTo('_subject', $View)
  739. )
  740. );
  741. $manager->expects($this->at(6))->method('dispatch')
  742. ->with(
  743. $this->logicalAnd(
  744. $this->isInstanceOf('Cake\Event\Event'),
  745. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  746. $this->attributeEqualTo('_subject', $View)
  747. )
  748. );
  749. $manager->expects($this->at(7))->method('dispatch')
  750. ->with(
  751. $this->logicalAnd(
  752. $this->isInstanceOf('Cake\Event\Event'),
  753. $this->attributeEqualTo('_name', 'View.afterLayout'),
  754. $this->attributeEqualTo('_subject', $View)
  755. )
  756. );
  757. $View->render('index');
  758. }
  759. /**
  760. * Test beforeLayout method
  761. *
  762. * @return void
  763. */
  764. public function testBeforeLayout() {
  765. $this->PostsController->helpers = array(
  766. 'Session',
  767. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  768. 'Html'
  769. );
  770. $View = $this->PostsController->createView();
  771. $View->render('index');
  772. $this->assertEquals('Valuation', $View->helpers()->TestBeforeAfter->property);
  773. }
  774. /**
  775. * Test afterLayout method
  776. *
  777. * @return void
  778. */
  779. public function testAfterLayout() {
  780. $this->PostsController->helpers = array(
  781. 'Session',
  782. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  783. 'Html'
  784. );
  785. $this->PostsController->set('variable', 'values');
  786. $View = $this->PostsController->createView();
  787. $content = 'This is my view output';
  788. $result = $View->renderLayout($content, 'default');
  789. $this->assertRegExp('/modified in the afterlife/', $result);
  790. $this->assertRegExp('/This is my view output/', $result);
  791. }
  792. /**
  793. * Test renderLoadHelper method
  794. *
  795. * @return void
  796. */
  797. public function testRenderLoadHelper() {
  798. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  799. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  800. $result = $View->render('index', false);
  801. $this->assertEquals('posts index', $result);
  802. $attached = $View->helpers()->loaded();
  803. $this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
  804. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  805. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  806. $result = $View->render('index', false);
  807. $this->assertEquals('posts index', $result);
  808. $attached = $View->helpers()->loaded();
  809. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  810. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  811. }
  812. /**
  813. * Test render method
  814. *
  815. * @return void
  816. */
  817. public function testRender() {
  818. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  819. $result = $View->render('index');
  820. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  821. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  822. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  823. $this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
  824. $this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
  825. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  826. $result = $View->render(false, 'ajax2');
  827. $this->assertRegExp('/Ajax\!/', $result);
  828. $this->assertNull($View->render(false, 'ajax2'));
  829. $this->PostsController->helpers = array('Session', 'Cache', 'Html');
  830. $this->PostsController->constructClasses();
  831. $this->PostsController->cacheAction = array('index' => 3600);
  832. $this->PostsController->request->params['action'] = 'index';
  833. Configure::write('Cache.check', true);
  834. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  835. $result = $View->render('index');
  836. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  837. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  838. }
  839. /**
  840. * Test that View::$view works
  841. *
  842. * @return void
  843. */
  844. public function testRenderUsingViewProperty() {
  845. $this->PostsController->view = 'cache_form';
  846. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  847. $this->assertEquals('cache_form', $View->view);
  848. $result = $View->render();
  849. $this->assertRegExp('/Add User/', $result);
  850. }
  851. /**
  852. * Test render()ing a file in a subdir from a custom viewPath
  853. * in a plugin.
  854. *
  855. * @return void
  856. */
  857. public function testGetViewFileNameSubdirWithPluginAndViewPath() {
  858. $this->PostsController->plugin = 'TestPlugin';
  859. $this->PostsController->name = 'Posts';
  860. $this->PostsController->viewPath = 'Element';
  861. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  862. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  863. $result = $View->getViewFileName('sub_dir/sub_element');
  864. $expected = $pluginPath . 'Template' . DS . 'Element' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  865. $this->assertPathEquals($expected, $result);
  866. }
  867. /**
  868. * Test that view vars can replace the local helper variables
  869. * and not overwrite the $this->Helper references
  870. *
  871. * @return void
  872. */
  873. public function testViewVarOverwritingLocalHelperVar() {
  874. $Controller = new ViewPostsController();
  875. $Controller->helpers = array('Session', 'Html');
  876. $Controller->set('html', 'I am some test html');
  877. $View = $Controller->createView();
  878. $result = $View->render('helper_overwrite', false);
  879. $this->assertRegExp('/I am some test html/', $result);
  880. $this->assertRegExp('/Test link/', $result);
  881. }
  882. /**
  883. * Test getViewFileName method
  884. *
  885. * @return void
  886. */
  887. public function testViewFileName() {
  888. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  889. $result = $View->getViewFileName('index');
  890. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  891. $result = $View->getViewFileName('TestPlugin.index');
  892. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  893. $result = $View->getViewFileName('/Pages/home');
  894. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  895. $result = $View->getViewFileName('../Element/test_element');
  896. $this->assertRegExp('/Element(\/|\\\)test_element.ctp/', $result);
  897. $result = $View->getViewFileName('../Themed/TestTheme/Posts/index');
  898. $this->assertRegExp('/Themed(\/|\\\)TestTheme(\/|\\\)Posts(\/|\\\)index.ctp/', $result);
  899. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  900. $result = $View->getViewFileName('../Posts/index');
  901. $this->assertPathEquals($expected, $result);
  902. }
  903. /**
  904. * Test renderCache method
  905. *
  906. * @return void
  907. */
  908. public function testRenderCache() {
  909. $this->skipIf(!is_writable(CACHE . 'views/'), 'CACHE/views dir is not writable, cannot test renderCache.');
  910. $view = 'test_view';
  911. $View = $this->PostsController->createView();
  912. $path = CACHE . 'views/view_cache_' . $view;
  913. $cacheText = '<!--cachetime:' . time() . '-->some cacheText';
  914. $f = fopen($path, 'w+');
  915. fwrite($f, $cacheText);
  916. fclose($f);
  917. $result = $View->renderCache($path, '+1 second');
  918. $this->assertFalse($result);
  919. if (file_exists($path)) {
  920. unlink($path);
  921. }
  922. $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
  923. $f = fopen($path, 'w+');
  924. fwrite($f, $cacheText);
  925. fclose($f);
  926. $result = $View->renderCache($path, '+1 second');
  927. $this->assertRegExp('/^some cacheText/', $result);
  928. if (file_exists($path)) {
  929. unlink($path);
  930. }
  931. }
  932. /**
  933. * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
  934. *
  935. * @return void
  936. */
  937. public function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
  938. Configure::write('Cache.check', false);
  939. $View = $this->PostsController->createView();
  940. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  941. $View->helpers = array('Html', 'Form', 'Cache');
  942. $View->layout = 'cache_layout';
  943. $result = $View->render('index');
  944. $this->assertNotRegExp('/cake:nocache/', $result);
  945. }
  946. /**
  947. * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
  948. *
  949. * @return void
  950. */
  951. public function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
  952. Configure::write('Cache.check', true);
  953. $View = $this->PostsController->createView();
  954. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  955. $View->helpers = array('Html', 'Form');
  956. $View->layout = 'cache_layout';
  957. $result = $View->render('index');
  958. $this->assertNotRegExp('/cake:nocache/', $result);
  959. }
  960. /**
  961. * Test creating a block with capturing output.
  962. *
  963. * @return void
  964. */
  965. public function testBlockCapture() {
  966. $this->View->start('test');
  967. echo 'Block content';
  968. $this->View->end();
  969. $result = $this->View->fetch('test');
  970. $this->assertEquals('Block content', $result);
  971. }
  972. /**
  973. * Test block with startIfEmpty
  974. *
  975. * @return void
  976. */
  977. public function testBlockCaptureStartIfEmpty() {
  978. $this->View->startIfEmpty('test');
  979. echo "Block content 1";
  980. $this->View->end();
  981. $this->View->startIfEmpty('test');
  982. echo "Block content 2";
  983. $this->View->end();
  984. $result = $this->View->fetch('test');
  985. $this->assertEquals('Block content 1', $result);
  986. }
  987. /**
  988. * Test block with startIfEmpty
  989. *
  990. * @return void
  991. */
  992. public function testBlockCaptureStartStartIfEmpty() {
  993. $this->View->start('test');
  994. echo "Block content 1";
  995. $this->View->end();
  996. $this->View->startIfEmpty('test');
  997. echo "Block content 2";
  998. $this->View->end();
  999. $result = $this->View->fetch('test');
  1000. $this->assertEquals('Block content 1', $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');
  1012. echo ' content';
  1013. $this->View->end();
  1014. $result = $this->View->fetch('test');
  1015. $this->assertEquals('Block content', $result);
  1016. }
  1017. /**
  1018. * Test setting a block's content.
  1019. *
  1020. * @return void
  1021. */
  1022. public function testBlockSet() {
  1023. $this->View->assign('test', 'Block content');
  1024. $result = $this->View->fetch('test');
  1025. $this->assertEquals('Block content', $result);
  1026. }
  1027. /**
  1028. * Test resetting a block's content.
  1029. *
  1030. * @return void
  1031. */
  1032. public function testBlockReset() {
  1033. $this->View->assign('test', '');
  1034. $result = $this->View->fetch('test', 'This should not be returned');
  1035. $this->assertSame('', $result);
  1036. }
  1037. /**
  1038. * Test setting a block's content to null
  1039. *
  1040. * @return void
  1041. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
  1042. */
  1043. public function testBlockSetNull() {
  1044. $this->View->assign('testWithNull', null);
  1045. $result = $this->View->fetch('testWithNull');
  1046. $this->assertSame('', $result);
  1047. }
  1048. /**
  1049. * Test setting a block's content to an object with __toString magic method
  1050. *
  1051. * @return void
  1052. */
  1053. public function testBlockSetObjectWithToString() {
  1054. $objectWithToString = new TestObjectWithToString();
  1055. $this->View->assign('testWithObjectWithToString', $objectWithToString);
  1056. $result = $this->View->fetch('testWithObjectWithToString');
  1057. $this->assertSame("I'm ObjectWithToString", $result);
  1058. }
  1059. /**
  1060. * Test setting a block's content to an object without __toString magic method
  1061. *
  1062. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1063. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1064. *
  1065. * @expectedException PHPUnit_Framework_Error
  1066. * @return void
  1067. */
  1068. public function testBlockSetObjectWithoutToString() {
  1069. $objectWithToString = new TestObjectWithoutToString();
  1070. $this->View->assign('testWithObjectWithoutToString', $objectWithToString);
  1071. }
  1072. /**
  1073. * Test setting a block's content to a decimal
  1074. *
  1075. * @return void
  1076. */
  1077. public function testBlockSetDecimal() {
  1078. $this->View->assign('testWithDecimal', 1.23456789);
  1079. $result = $this->View->fetch('testWithDecimal');
  1080. $this->assertEquals('1.23456789', $result);
  1081. }
  1082. /**
  1083. * Data provider for block related tests.
  1084. *
  1085. * @return array
  1086. */
  1087. public static function blockValueProvider() {
  1088. return array(
  1089. 'string' => array('A string value'),
  1090. 'null' => array(null),
  1091. 'decimal' => array(1.23456),
  1092. 'object with __toString' => array(new TestObjectWithToString()),
  1093. );
  1094. }
  1095. /**
  1096. * Test appending to a block with append.
  1097. *
  1098. * @dataProvider blockValueProvider
  1099. * @return void
  1100. */
  1101. public function testBlockAppend($value) {
  1102. $this->View->assign('testBlock', 'Block');
  1103. $this->View->append('testBlock', $value);
  1104. $result = $this->View->fetch('testBlock');
  1105. $this->assertSame('Block' . $value, $result);
  1106. }
  1107. /**
  1108. * Test appending an object without __toString magic method to a block with append.
  1109. *
  1110. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1111. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1112. *
  1113. * @expectedException PHPUnit_Framework_Error
  1114. * @return void
  1115. */
  1116. public function testBlockAppendObjectWithoutToString() {
  1117. $object = new TestObjectWithoutToString();
  1118. $this->View->assign('testBlock', 'Block ');
  1119. $this->View->append('testBlock', $object);
  1120. }
  1121. /**
  1122. * Test prepending to a block with prepend.
  1123. *
  1124. * @dataProvider blockValueProvider
  1125. * @return void
  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. }