ViewTest.php 44 KB

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