ViewTest.php 44 KB

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