ViewTest.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  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 addInlineScripts method
  515. *
  516. * @return void
  517. */
  518. public function testAddInlineScripts() {
  519. $View = new TestView();
  520. $View->addScript('prototype.js');
  521. $View->addScript('prototype.js');
  522. $this->assertEquals(array('prototype.js'), $View->scripts());
  523. $View->addScript('mainEvent', 'Event.observe(window, "load", function() { doSomething(); }, true);');
  524. $this->assertEquals(array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);'), $View->scripts());
  525. }
  526. /**
  527. * Test elementExists method
  528. *
  529. * @return void
  530. */
  531. public function testElementExists() {
  532. $result = $this->View->elementExists('test_element');
  533. $this->assertTrue($result);
  534. $result = $this->View->elementExists('TestPlugin.plugin_element');
  535. $this->assertTrue($result);
  536. $result = $this->View->elementExists('non_existent_element');
  537. $this->assertFalse($result);
  538. $result = $this->View->elementExists('TestPlugin.element');
  539. $this->assertFalse($result);
  540. $this->View->plugin = 'TestPlugin';
  541. $result = $this->View->elementExists('test_plugin_element');
  542. $this->assertTrue($result);
  543. }
  544. /**
  545. * Test element method
  546. *
  547. * @return void
  548. */
  549. public function testElement() {
  550. $result = $this->View->element('test_element');
  551. $this->assertEquals('this is the test element', $result);
  552. $result = $this->View->element('TestPlugin.plugin_element');
  553. $this->assertEquals('this is the plugin element using params[plugin]', $result);
  554. $this->View->plugin = 'TestPlugin';
  555. $result = $this->View->element('test_plugin_element');
  556. $this->assertEquals('this is the test set using View::$plugin plugin element', $result);
  557. }
  558. /**
  559. * Test elementInexistent method
  560. *
  561. * @expectedException PHPUnit_Framework_Error_Notice
  562. * @return void
  563. */
  564. public function testElementInexistent() {
  565. $this->View->element('non_existent_element');
  566. }
  567. /**
  568. * Test elementInexistent3 method
  569. *
  570. * @expectedException PHPUnit_Framework_Error_Notice
  571. * @return void
  572. */
  573. public function testElementInexistent3() {
  574. $this->View->element('test_plugin.plugin_element');
  575. }
  576. /**
  577. * Test that elements can have callbacks
  578. *
  579. * @return void
  580. */
  581. public function testElementCallbacks() {
  582. $count = 0;
  583. $callback = function ($event, $file) use (&$count) {
  584. $count++;
  585. };
  586. $events = $this->View->getEventManager();
  587. $events->attach($callback, 'View.beforeRender');
  588. $events->attach($callback, 'View.afterRender');
  589. $this->View->element('test_element', array(), array('callbacks' => true));
  590. $this->assertEquals(2, $count);
  591. }
  592. /**
  593. * Test that additional element viewVars don't get overwritten with helpers.
  594. *
  595. * @return void
  596. */
  597. public function testElementParamsDontOverwriteHelpers() {
  598. $Controller = new ViewPostsController();
  599. $Controller->helpers = array('Form');
  600. $View = $Controller->createView();
  601. $result = $View->element('type_check', array('form' => 'string'), array('callbacks' => true));
  602. $this->assertEquals('string', $result);
  603. $View->set('form', 'string');
  604. $result = $View->element('type_check', array(), array('callbacks' => true));
  605. $this->assertEquals('string', $result);
  606. }
  607. /**
  608. * Test elementCacheHelperNoCache method
  609. *
  610. * @return void
  611. */
  612. public function testElementCacheHelperNoCache() {
  613. $Controller = new ViewPostsController();
  614. $View = $Controller->createView();
  615. $View->loadHelpers();
  616. $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
  617. $this->assertEquals('this is the test element', $result);
  618. }
  619. /**
  620. * Test elementCache method
  621. *
  622. * @return void
  623. */
  624. public function testElementCache() {
  625. Cache::drop('test_view');
  626. Cache::config('test_view', [
  627. 'engine' => 'File',
  628. 'duration' => '+1 day',
  629. 'path' => CACHE . 'views/',
  630. 'prefix' => ''
  631. ]);
  632. Cache::clear(true, 'test_view');
  633. $View = $this->PostsController->createView();
  634. $View->elementCache = 'test_view';
  635. $result = $View->element('test_element', array(), array('cache' => true));
  636. $expected = 'this is the test element';
  637. $this->assertEquals($expected, $result);
  638. $result = Cache::read('element__test_element_cache_callbacks', 'test_view');
  639. $this->assertEquals($expected, $result);
  640. $result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
  641. $this->assertEquals($expected, $result);
  642. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  643. $this->assertEquals($expected, $result);
  644. $View->element('test_element', array(
  645. 'param' => 'one',
  646. 'foo' => 'two'
  647. ), array(
  648. 'cache' => array('key' => 'custom_key')
  649. ));
  650. $result = Cache::read('element_custom_key', 'test_view');
  651. $this->assertEquals($expected, $result);
  652. $View->elementCache = 'default';
  653. $View->element('test_element', array(
  654. 'param' => 'one',
  655. 'foo' => 'two'
  656. ), array(
  657. 'cache' => array('config' => 'test_view'),
  658. ));
  659. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  660. $this->assertEquals($expected, $result);
  661. Cache::clear(true, 'test_view');
  662. Cache::drop('test_view');
  663. }
  664. /**
  665. * Test __get allowing access to helpers.
  666. *
  667. * @return void
  668. */
  669. public function testMagicGetAndAddHelper() {
  670. $View = new View();
  671. $View->addHelper('Html');
  672. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html);
  673. }
  674. /**
  675. * Test loadHelpers method
  676. *
  677. * @return void
  678. */
  679. public function testLoadHelpers() {
  680. $View = new View();
  681. $View->helpers = array('Html', 'Form');
  682. $View->loadHelpers();
  683. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  684. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  685. }
  686. /**
  687. * Test lazy loading helpers
  688. *
  689. * @return void
  690. */
  691. public function testLazyLoadHelpers() {
  692. $View = new View();
  693. $View->helpers = array();
  694. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  695. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  696. }
  697. /**
  698. * Test the correct triggering of helper callbacks
  699. *
  700. * @return void
  701. */
  702. public function testHelperCallbackTriggering() {
  703. $View = $this->PostsController->createView();
  704. $manager = $this->getMock('Cake\Event\EventManager');
  705. $View->setEventManager($manager);
  706. $manager->expects($this->at(0))->method('dispatch')
  707. ->with(
  708. $this->logicalAnd(
  709. $this->isInstanceOf('Cake\Event\Event'),
  710. $this->attributeEqualTo('_name', 'View.beforeRender'),
  711. $this->attributeEqualTo('_subject', $View)
  712. )
  713. );
  714. $manager->expects($this->at(1))->method('dispatch')
  715. ->with(
  716. $this->logicalAnd(
  717. $this->isInstanceOf('Cake\Event\Event'),
  718. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  719. $this->attributeEqualTo('_subject', $View)
  720. )
  721. );
  722. $manager->expects($this->at(2))->method('dispatch')
  723. ->with(
  724. $this->logicalAnd(
  725. $this->isInstanceOf('Cake\Event\Event'),
  726. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  727. $this->attributeEqualTo('_subject', $View)
  728. )
  729. );
  730. $manager->expects($this->at(3))->method('dispatch')
  731. ->with(
  732. $this->logicalAnd(
  733. $this->isInstanceOf('Cake\Event\Event'),
  734. $this->attributeEqualTo('_name', 'View.afterRender'),
  735. $this->attributeEqualTo('_subject', $View)
  736. )
  737. );
  738. $manager->expects($this->at(4))->method('dispatch')
  739. ->with(
  740. $this->logicalAnd(
  741. $this->isInstanceOf('Cake\Event\Event'),
  742. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  743. $this->attributeEqualTo('_subject', $View)
  744. )
  745. );
  746. $manager->expects($this->at(5))->method('dispatch')
  747. ->with(
  748. $this->logicalAnd(
  749. $this->isInstanceOf('Cake\Event\Event'),
  750. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  751. $this->attributeEqualTo('_subject', $View)
  752. )
  753. );
  754. $manager->expects($this->at(6))->method('dispatch')
  755. ->with(
  756. $this->logicalAnd(
  757. $this->isInstanceOf('Cake\Event\Event'),
  758. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  759. $this->attributeEqualTo('_subject', $View)
  760. )
  761. );
  762. $manager->expects($this->at(7))->method('dispatch')
  763. ->with(
  764. $this->logicalAnd(
  765. $this->isInstanceOf('Cake\Event\Event'),
  766. $this->attributeEqualTo('_name', 'View.afterLayout'),
  767. $this->attributeEqualTo('_subject', $View)
  768. )
  769. );
  770. $View->render('index');
  771. }
  772. /**
  773. * Test beforeLayout method
  774. *
  775. * @return void
  776. */
  777. public function testBeforeLayout() {
  778. $this->PostsController->helpers = array(
  779. 'Session',
  780. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  781. 'Html'
  782. );
  783. $View = $this->PostsController->createView();
  784. $View->render('index');
  785. $this->assertEquals('Valuation', $View->helpers()->TestBeforeAfter->property);
  786. }
  787. /**
  788. * Test afterLayout method
  789. *
  790. * @return void
  791. */
  792. public function testAfterLayout() {
  793. $this->PostsController->helpers = array(
  794. 'Session',
  795. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  796. 'Html'
  797. );
  798. $this->PostsController->set('variable', 'values');
  799. $View = $this->PostsController->createView();
  800. $content = 'This is my view output';
  801. $result = $View->renderLayout($content, 'default');
  802. $this->assertRegExp('/modified in the afterlife/', $result);
  803. $this->assertRegExp('/This is my view output/', $result);
  804. }
  805. /**
  806. * Test renderLoadHelper method
  807. *
  808. * @return void
  809. */
  810. public function testRenderLoadHelper() {
  811. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  812. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  813. $result = $View->render('index', false);
  814. $this->assertEquals('posts index', $result);
  815. $attached = $View->helpers()->loaded();
  816. $this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
  817. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  818. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  819. $result = $View->render('index', false);
  820. $this->assertEquals('posts index', $result);
  821. $attached = $View->helpers()->loaded();
  822. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  823. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  824. }
  825. /**
  826. * Test render method
  827. *
  828. * @return void
  829. */
  830. public function testRender() {
  831. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  832. $result = $View->render('index');
  833. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  834. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  835. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  836. $this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
  837. $this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
  838. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  839. $result = $View->render(false, 'ajax2');
  840. $this->assertRegExp('/Ajax\!/', $result);
  841. $this->assertNull($View->render(false, 'ajax2'));
  842. $this->PostsController->helpers = array('Session', 'Cache', 'Html');
  843. $this->PostsController->constructClasses();
  844. $this->PostsController->cacheAction = array('index' => 3600);
  845. $this->PostsController->request->params['action'] = 'index';
  846. Configure::write('Cache.check', true);
  847. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  848. $result = $View->render('index');
  849. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  850. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  851. }
  852. /**
  853. * Test that View::$view works
  854. *
  855. * @return void
  856. */
  857. public function testRenderUsingViewProperty() {
  858. $this->PostsController->view = 'cache_form';
  859. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  860. $this->assertEquals('cache_form', $View->view);
  861. $result = $View->render();
  862. $this->assertRegExp('/Add User/', $result);
  863. }
  864. /**
  865. * Test render()ing a file in a subdir from a custom viewPath
  866. * in a plugin.
  867. *
  868. * @return void
  869. */
  870. public function testGetViewFileNameSubdirWithPluginAndViewPath() {
  871. $this->PostsController->plugin = 'TestPlugin';
  872. $this->PostsController->name = 'Posts';
  873. $this->PostsController->viewPath = 'Element';
  874. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  875. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  876. $result = $View->getViewFileName('sub_dir/sub_element');
  877. $expected = $pluginPath . 'Template' . DS . 'Element' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  878. $this->assertPathEquals($expected, $result);
  879. }
  880. /**
  881. * Test that view vars can replace the local helper variables
  882. * and not overwrite the $this->Helper references
  883. *
  884. * @return void
  885. */
  886. public function testViewVarOverwritingLocalHelperVar() {
  887. $Controller = new ViewPostsController();
  888. $Controller->helpers = array('Session', 'Html');
  889. $Controller->set('html', 'I am some test html');
  890. $View = $Controller->createView();
  891. $result = $View->render('helper_overwrite', false);
  892. $this->assertRegExp('/I am some test html/', $result);
  893. $this->assertRegExp('/Test link/', $result);
  894. }
  895. /**
  896. * Test getViewFileName method
  897. *
  898. * @return void
  899. */
  900. public function testViewFileName() {
  901. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  902. $result = $View->getViewFileName('index');
  903. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  904. $result = $View->getViewFileName('TestPlugin.index');
  905. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  906. $result = $View->getViewFileName('/Pages/home');
  907. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  908. $result = $View->getViewFileName('../Element/test_element');
  909. $this->assertRegExp('/Element(\/|\\\)test_element.ctp/', $result);
  910. $result = $View->getViewFileName('../Themed/TestTheme/Posts/index');
  911. $this->assertRegExp('/Themed(\/|\\\)TestTheme(\/|\\\)Posts(\/|\\\)index.ctp/', $result);
  912. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  913. $result = $View->getViewFileName('../Posts/index');
  914. $this->assertPathEquals($expected, $result);
  915. }
  916. /**
  917. * Test renderCache method
  918. *
  919. * @return void
  920. */
  921. public function testRenderCache() {
  922. $this->skipIf(!is_writable(CACHE . 'views/'), 'CACHE/views dir is not writable, cannot test renderCache.');
  923. $view = 'test_view';
  924. $View = $this->PostsController->createView();
  925. $path = CACHE . 'views/view_cache_' . $view;
  926. $cacheText = '<!--cachetime:' . time() . '-->some cacheText';
  927. $f = fopen($path, 'w+');
  928. fwrite($f, $cacheText);
  929. fclose($f);
  930. $result = $View->renderCache($path, '+1 second');
  931. $this->assertFalse($result);
  932. if (file_exists($path)) {
  933. unlink($path);
  934. }
  935. $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
  936. $f = fopen($path, 'w+');
  937. fwrite($f, $cacheText);
  938. fclose($f);
  939. $result = $View->renderCache($path, '+1 second');
  940. $this->assertRegExp('/^some cacheText/', $result);
  941. if (file_exists($path)) {
  942. unlink($path);
  943. }
  944. }
  945. /**
  946. * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
  947. *
  948. * @return void
  949. */
  950. public function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
  951. Configure::write('Cache.check', false);
  952. $View = $this->PostsController->createView();
  953. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  954. $View->helpers = array('Html', 'Form', 'Cache');
  955. $View->layout = 'cache_layout';
  956. $result = $View->render('index');
  957. $this->assertNotRegExp('/cake:nocache/', $result);
  958. }
  959. /**
  960. * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
  961. *
  962. * @return void
  963. */
  964. public function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
  965. Configure::write('Cache.check', true);
  966. $View = $this->PostsController->createView();
  967. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  968. $View->helpers = array('Html', 'Form');
  969. $View->layout = 'cache_layout';
  970. $result = $View->render('index');
  971. $this->assertNotRegExp('/cake:nocache/', $result);
  972. }
  973. /**
  974. * Test creating a block with capturing output.
  975. *
  976. * @return void
  977. */
  978. public function testBlockCapture() {
  979. $this->View->start('test');
  980. echo 'Block content';
  981. $this->View->end();
  982. $result = $this->View->fetch('test');
  983. $this->assertEquals('Block content', $result);
  984. }
  985. /**
  986. * Test block with startIfEmpty
  987. *
  988. * @return void
  989. */
  990. public function testBlockCaptureStartIfEmpty() {
  991. $this->View->startIfEmpty('test');
  992. echo "Block content 1";
  993. $this->View->end();
  994. $this->View->startIfEmpty('test');
  995. echo "Block content 2";
  996. $this->View->end();
  997. $result = $this->View->fetch('test');
  998. $this->assertEquals('Block content 1', $result);
  999. }
  1000. /**
  1001. * Test block with startIfEmpty
  1002. *
  1003. * @return void
  1004. */
  1005. public function testBlockCaptureStartStartIfEmpty() {
  1006. $this->View->start('test');
  1007. echo "Block content 1";
  1008. $this->View->end();
  1009. $this->View->startIfEmpty('test');
  1010. echo "Block content 2";
  1011. $this->View->end();
  1012. $result = $this->View->fetch('test');
  1013. $this->assertEquals('Block content 1', $result);
  1014. }
  1015. /**
  1016. * Test appending to a block with capturing output.
  1017. *
  1018. * @return void
  1019. */
  1020. public function testBlockCaptureAppend() {
  1021. $this->View->start('test');
  1022. echo 'Block';
  1023. $this->View->end();
  1024. $this->View->append('test');
  1025. echo ' content';
  1026. $this->View->end();
  1027. $result = $this->View->fetch('test');
  1028. $this->assertEquals('Block content', $result);
  1029. }
  1030. /**
  1031. * Test setting a block's content.
  1032. *
  1033. * @return void
  1034. */
  1035. public function testBlockSet() {
  1036. $this->View->assign('test', 'Block content');
  1037. $result = $this->View->fetch('test');
  1038. $this->assertEquals('Block content', $result);
  1039. }
  1040. /**
  1041. * Test resetting a block's content.
  1042. *
  1043. * @return void
  1044. */
  1045. public function testBlockReset() {
  1046. $this->View->assign('test', '');
  1047. $result = $this->View->fetch('test', 'This should not be returned');
  1048. $this->assertSame('', $result);
  1049. }
  1050. /**
  1051. * Test setting a block's content to null
  1052. *
  1053. * @return void
  1054. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
  1055. */
  1056. public function testBlockSetNull() {
  1057. $this->View->assign('testWithNull', null);
  1058. $result = $this->View->fetch('testWithNull');
  1059. $this->assertSame('', $result);
  1060. }
  1061. /**
  1062. * Test setting a block's content to an object with __toString magic method
  1063. *
  1064. * @return void
  1065. */
  1066. public function testBlockSetObjectWithToString() {
  1067. $objectWithToString = new TestObjectWithToString();
  1068. $this->View->assign('testWithObjectWithToString', $objectWithToString);
  1069. $result = $this->View->fetch('testWithObjectWithToString');
  1070. $this->assertSame("I'm ObjectWithToString", $result);
  1071. }
  1072. /**
  1073. * Test setting a block's content to an object without __toString magic method
  1074. *
  1075. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1076. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1077. *
  1078. * @expectedException PHPUnit_Framework_Error
  1079. * @return void
  1080. */
  1081. public function testBlockSetObjectWithoutToString() {
  1082. $objectWithToString = new TestObjectWithoutToString();
  1083. $this->View->assign('testWithObjectWithoutToString', $objectWithToString);
  1084. }
  1085. /**
  1086. * Test setting a block's content to a decimal
  1087. *
  1088. * @return void
  1089. */
  1090. public function testBlockSetDecimal() {
  1091. $this->View->assign('testWithDecimal', 1.23456789);
  1092. $result = $this->View->fetch('testWithDecimal');
  1093. $this->assertEquals('1.23456789', $result);
  1094. }
  1095. /**
  1096. * Data provider for block related tests.
  1097. *
  1098. * @return array
  1099. */
  1100. public static function blockValueProvider() {
  1101. return array(
  1102. 'string' => array('A string value'),
  1103. 'null' => array(null),
  1104. 'decimal' => array(1.23456),
  1105. 'object with __toString' => array(new TestObjectWithToString()),
  1106. );
  1107. }
  1108. /**
  1109. * Test appending to a block with append.
  1110. *
  1111. * @dataProvider blockValueProvider
  1112. * @return void
  1113. */
  1114. public function testBlockAppend($value) {
  1115. $this->View->assign('testBlock', 'Block');
  1116. $this->View->append('testBlock', $value);
  1117. $result = $this->View->fetch('testBlock');
  1118. $this->assertSame('Block' . $value, $result);
  1119. }
  1120. /**
  1121. * Test appending an object without __toString magic method to a block with append.
  1122. *
  1123. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1124. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1125. *
  1126. * @expectedException PHPUnit_Framework_Error
  1127. * @return void
  1128. */
  1129. public function testBlockAppendObjectWithoutToString() {
  1130. $object = new TestObjectWithoutToString();
  1131. $this->View->assign('testBlock', 'Block ');
  1132. $this->View->append('testBlock', $object);
  1133. }
  1134. /**
  1135. * Test prepending to a block with prepend.
  1136. *
  1137. * @dataProvider blockValueProvider
  1138. * @return void
  1139. */
  1140. public function testBlockPrepend($value) {
  1141. $this->View->assign('test', 'Block');
  1142. $this->View->prepend('test', $value);
  1143. $result = $this->View->fetch('test');
  1144. $this->assertEquals($value . 'Block', $result);
  1145. }
  1146. /**
  1147. * Test prepending an object without __toString magic method to a block with prepend.
  1148. *
  1149. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1150. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1151. *
  1152. * @expectedException PHPUnit_Framework_Error
  1153. * @return void
  1154. */
  1155. public function testBlockPrependObjectWithoutToString() {
  1156. $object = new TestObjectWithoutToString();
  1157. $this->View->assign('test', 'Block ');
  1158. $this->View->prepend('test', $object);
  1159. }
  1160. /**
  1161. * You should be able to append to undefined blocks.
  1162. *
  1163. * @return void
  1164. */
  1165. public function testBlockAppendUndefined() {
  1166. $this->View->append('test', 'Unknown');
  1167. $result = $this->View->fetch('test');
  1168. $this->assertEquals('Unknown', $result);
  1169. }
  1170. /**
  1171. * You should be able to prepend to undefined blocks.
  1172. *
  1173. * @return void
  1174. */
  1175. public function testBlockPrependUndefined() {
  1176. $this->View->prepend('test', 'Unknown');
  1177. $result = $this->View->fetch('test');
  1178. $this->assertEquals('Unknown', $result);
  1179. }
  1180. /**
  1181. * Test getting block names
  1182. *
  1183. * @return void
  1184. */
  1185. public function testBlocks() {
  1186. $this->View->append('test', 'one');
  1187. $this->View->assign('test1', 'one');
  1188. $this->assertEquals(array('test', 'test1'), $this->View->blocks());
  1189. }
  1190. /**
  1191. * Test that blocks can be nested.
  1192. *
  1193. * @return void
  1194. */
  1195. public function testNestedBlocks() {
  1196. $this->View->start('first');
  1197. echo 'In first ';
  1198. $this->View->start('second');
  1199. echo 'In second';
  1200. $this->View->end();
  1201. echo 'In first';
  1202. $this->View->end();
  1203. $this->assertEquals('In first In first', $this->View->fetch('first'));
  1204. $this->assertEquals('In second', $this->View->fetch('second'));
  1205. }
  1206. /**
  1207. * Test that starting the same block twice throws an exception
  1208. *
  1209. * @expectedException \Cake\Error\Exception
  1210. * @return void
  1211. */
  1212. public function testStartBlocksTwice() {
  1213. $this->View->start('first');
  1214. $this->View->start('first');
  1215. }
  1216. /**
  1217. * Test that an exception gets thrown when you leave a block open at the end
  1218. * of a view.
  1219. *
  1220. * @expectedException \Cake\Error\Exception
  1221. * @return void
  1222. */
  1223. public function testExceptionOnOpenBlock() {
  1224. $this->View->render('open_block');
  1225. }
  1226. /**
  1227. * Test nested extended views.
  1228. *
  1229. * @return void
  1230. */
  1231. public function testExtendNested() {
  1232. $this->View->layout = false;
  1233. $content = $this->View->render('nested_extends');
  1234. $expected = <<<TEXT
  1235. This is the second parent.
  1236. This is the first parent.
  1237. This is the first template.
  1238. Sidebar Content.
  1239. TEXT;
  1240. $this->assertEquals($expected, $content);
  1241. }
  1242. /**
  1243. * Make sure that extending the current view with itself causes an exception
  1244. *
  1245. * @expectedException LogicException
  1246. * @return void
  1247. */
  1248. public function testExtendSelf() {
  1249. $this->View->layout = false;
  1250. $this->View->render('extend_self');
  1251. }
  1252. /**
  1253. * Make sure that extending in a loop causes an exception
  1254. *
  1255. * @expectedException LogicException
  1256. * @return void
  1257. */
  1258. public function testExtendLoop() {
  1259. $this->View->layout = false;
  1260. $this->View->render('extend_loop');
  1261. }
  1262. /**
  1263. * Test extend() in an element and a view.
  1264. *
  1265. * @return void
  1266. */
  1267. public function testExtendElement() {
  1268. $this->View->layout = false;
  1269. $content = $this->View->render('extend_element');
  1270. $expected = <<<TEXT
  1271. Parent View.
  1272. View content.
  1273. Parent Element.
  1274. Element content.
  1275. TEXT;
  1276. $this->assertEquals($expected, $content);
  1277. }
  1278. /**
  1279. * Extending an element which doesn't exist should throw a missing view exception
  1280. *
  1281. * @expectedException LogicException
  1282. * @return void
  1283. */
  1284. public function testExtendMissingElement() {
  1285. $this->View->layout = false;
  1286. $this->View->render('extend_missing_element');
  1287. }
  1288. /**
  1289. * Test extend() preceeded by an element()
  1290. *
  1291. * @return void
  1292. */
  1293. public function testExtendWithElementBeforeExtend() {
  1294. $this->View->layout = false;
  1295. $result = $this->View->render('extend_with_element');
  1296. $expected = <<<TEXT
  1297. Parent View.
  1298. this is the test elementThe view
  1299. TEXT;
  1300. $this->assertEquals($expected, $result);
  1301. }
  1302. /**
  1303. * Test memory leaks that existed in _paths at one point.
  1304. *
  1305. * @return void
  1306. */
  1307. public function testMemoryLeakInPaths() {
  1308. $this->ThemeController->plugin = null;
  1309. $this->ThemeController->name = 'Posts';
  1310. $this->ThemeController->viewPath = 'Posts';
  1311. $this->ThemeController->layout = 'whatever';
  1312. $this->ThemeController->theme = 'TestTheme';
  1313. $View = $this->ThemeController->createView();
  1314. $View->element('test_element');
  1315. $start = memory_get_usage();
  1316. for ($i = 0; $i < 10; $i++) {
  1317. $View->element('test_element');
  1318. }
  1319. $end = memory_get_usage();
  1320. $this->assertLessThanOrEqual($start + 5000, $end);
  1321. }
  1322. /**
  1323. * Tests that a view block uses default value when not assigned and uses assigned value when it is
  1324. *
  1325. * @return void
  1326. */
  1327. public function testBlockDefaultValue() {
  1328. $default = 'Default';
  1329. $result = $this->View->fetch('title', $default);
  1330. $this->assertEquals($default, $result);
  1331. $expected = 'My Title';
  1332. $this->View->assign('title', $expected);
  1333. $result = $this->View->fetch('title', $default);
  1334. $this->assertEquals($expected, $result);
  1335. }
  1336. /**
  1337. * Tests that a view variable uses default value when not assigned and uses assigned value when it is
  1338. *
  1339. * @return void
  1340. */
  1341. public function testViewVarDefaultValue() {
  1342. $default = 'Default';
  1343. $result = $this->View->get('title', $default);
  1344. $this->assertEquals($default, $result);
  1345. $expected = 'Back to the Future';
  1346. $this->View->set('title', $expected);
  1347. $result = $this->View->get('title', $default);
  1348. $this->assertEquals($expected, $result);
  1349. }
  1350. /**
  1351. * Test the helpers() method.
  1352. *
  1353. * @return void
  1354. */
  1355. public function testHelpers() {
  1356. $this->assertInstanceOf('Cake\View\HelperRegistry', $this->View->helpers());
  1357. $result = $this->View->helpers();
  1358. $this->assertSame($result, $this->View->helpers());
  1359. }
  1360. }