ViewTest.php 42 KB

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