ViewTest.php 41 KB

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