ViewTest.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  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.user', 'core.post');
  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. $View->loadHelpers();
  713. $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
  714. $this->assertEquals('this is the test element', $result);
  715. }
  716. /**
  717. * Test elementCache method
  718. *
  719. * @return void
  720. */
  721. public function testElementCache() {
  722. Cache::drop('test_view');
  723. Cache::config('test_view', [
  724. 'engine' => 'File',
  725. 'duration' => '+1 day',
  726. 'path' => CACHE . 'views/',
  727. 'prefix' => ''
  728. ]);
  729. Cache::clear(true, 'test_view');
  730. $View = $this->PostsController->createView();
  731. $View->elementCache = 'test_view';
  732. $result = $View->element('test_element', array(), array('cache' => true));
  733. $expected = 'this is the test element';
  734. $this->assertEquals($expected, $result);
  735. $result = Cache::read('element__test_element_cache_callbacks', 'test_view');
  736. $this->assertEquals($expected, $result);
  737. $result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
  738. $this->assertEquals($expected, $result);
  739. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  740. $this->assertEquals($expected, $result);
  741. $View->element('test_element', array(
  742. 'param' => 'one',
  743. 'foo' => 'two'
  744. ), array(
  745. 'cache' => array('key' => 'custom_key')
  746. ));
  747. $result = Cache::read('element_custom_key', 'test_view');
  748. $this->assertEquals($expected, $result);
  749. $View->elementCache = 'default';
  750. $View->element('test_element', array(
  751. 'param' => 'one',
  752. 'foo' => 'two'
  753. ), array(
  754. 'cache' => array('config' => 'test_view'),
  755. ));
  756. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  757. $this->assertEquals($expected, $result);
  758. Cache::clear(true, 'test_view');
  759. Cache::drop('test_view');
  760. }
  761. /**
  762. * Test element events
  763. *
  764. * @return void
  765. */
  766. public function testViewEvent() {
  767. $View = $this->PostsController->createView();
  768. $View->autoLayout = false;
  769. $listener = new TestViewEventListener();
  770. $View->eventManager()->attach($listener);
  771. $View->render('index');
  772. $this->assertEquals(View::TYPE_VIEW, $listener->beforeRenderViewType);
  773. $this->assertEquals(View::TYPE_VIEW, $listener->afterRenderViewType);
  774. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  775. $View->element('test_element', array(), array('callbacks' => true));
  776. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  777. $this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType);
  778. $this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType);
  779. }
  780. /**
  781. * Test __get allowing access to helpers.
  782. *
  783. * @return void
  784. */
  785. public function testMagicGetAndAddHelper() {
  786. $View = new View();
  787. $View->addHelper('Html');
  788. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html);
  789. }
  790. /**
  791. * Test loadHelpers method
  792. *
  793. * @return void
  794. */
  795. public function testLoadHelpers() {
  796. $View = new View();
  797. $View->helpers = array('Html', 'Form');
  798. $View->loadHelpers();
  799. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  800. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  801. }
  802. /**
  803. * Test lazy loading helpers
  804. *
  805. * @return void
  806. */
  807. public function testLazyLoadHelpers() {
  808. $View = new View();
  809. $View->helpers = array();
  810. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  811. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  812. }
  813. /**
  814. * Test the correct triggering of helper callbacks
  815. *
  816. * @return void
  817. */
  818. public function testHelperCallbackTriggering() {
  819. $View = $this->PostsController->createView();
  820. $manager = $this->getMock('Cake\Event\EventManager');
  821. $View->eventManager($manager);
  822. $manager->expects($this->at(0))->method('dispatch')
  823. ->with(
  824. $this->logicalAnd(
  825. $this->isInstanceOf('Cake\Event\Event'),
  826. $this->attributeEqualTo('_name', 'View.beforeRender'),
  827. $this->attributeEqualTo('_subject', $View)
  828. )
  829. );
  830. $manager->expects($this->at(1))->method('dispatch')
  831. ->with(
  832. $this->logicalAnd(
  833. $this->isInstanceOf('Cake\Event\Event'),
  834. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  835. $this->attributeEqualTo('_subject', $View)
  836. )
  837. );
  838. $manager->expects($this->at(2))->method('dispatch')
  839. ->with(
  840. $this->logicalAnd(
  841. $this->isInstanceOf('Cake\Event\Event'),
  842. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  843. $this->attributeEqualTo('_subject', $View)
  844. )
  845. );
  846. $manager->expects($this->at(3))->method('dispatch')
  847. ->with(
  848. $this->logicalAnd(
  849. $this->isInstanceOf('Cake\Event\Event'),
  850. $this->attributeEqualTo('_name', 'View.afterRender'),
  851. $this->attributeEqualTo('_subject', $View)
  852. )
  853. );
  854. $manager->expects($this->at(4))->method('dispatch')
  855. ->with(
  856. $this->logicalAnd(
  857. $this->isInstanceOf('Cake\Event\Event'),
  858. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  859. $this->attributeEqualTo('_subject', $View)
  860. )
  861. );
  862. $manager->expects($this->at(5))->method('dispatch')
  863. ->with(
  864. $this->logicalAnd(
  865. $this->isInstanceOf('Cake\Event\Event'),
  866. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  867. $this->attributeEqualTo('_subject', $View)
  868. )
  869. );
  870. $manager->expects($this->at(6))->method('dispatch')
  871. ->with(
  872. $this->logicalAnd(
  873. $this->isInstanceOf('Cake\Event\Event'),
  874. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  875. $this->attributeEqualTo('_subject', $View)
  876. )
  877. );
  878. $manager->expects($this->at(7))->method('dispatch')
  879. ->with(
  880. $this->logicalAnd(
  881. $this->isInstanceOf('Cake\Event\Event'),
  882. $this->attributeEqualTo('_name', 'View.afterLayout'),
  883. $this->attributeEqualTo('_subject', $View)
  884. )
  885. );
  886. $View->render('index');
  887. }
  888. /**
  889. * Test beforeLayout method
  890. *
  891. * @return void
  892. */
  893. public function testBeforeLayout() {
  894. $this->PostsController->helpers = array(
  895. 'Session',
  896. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  897. 'Html'
  898. );
  899. $View = $this->PostsController->createView();
  900. $View->render('index');
  901. $this->assertEquals('Valuation', $View->helpers()->TestBeforeAfter->property);
  902. }
  903. /**
  904. * Test afterLayout method
  905. *
  906. * @return void
  907. */
  908. public function testAfterLayout() {
  909. $this->PostsController->helpers = array(
  910. 'Session',
  911. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  912. 'Html'
  913. );
  914. $this->PostsController->set('variable', 'values');
  915. $View = $this->PostsController->createView();
  916. $content = 'This is my view output';
  917. $result = $View->renderLayout($content, 'default');
  918. $this->assertRegExp('/modified in the afterlife/', $result);
  919. $this->assertRegExp('/This is my view output/', $result);
  920. }
  921. /**
  922. * Test renderLoadHelper method
  923. *
  924. * @return void
  925. */
  926. public function testRenderLoadHelper() {
  927. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  928. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  929. $result = $View->render('index', false);
  930. $this->assertEquals('posts index', $result);
  931. $attached = $View->helpers()->loaded();
  932. $this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
  933. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  934. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  935. $result = $View->render('index', false);
  936. $this->assertEquals('posts index', $result);
  937. $attached = $View->helpers()->loaded();
  938. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  939. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  940. }
  941. /**
  942. * Test render method
  943. *
  944. * @return void
  945. */
  946. public function testRender() {
  947. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  948. $result = $View->render('index');
  949. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  950. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  951. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  952. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  953. $result = $View->render(false, 'ajax2');
  954. $this->assertRegExp('/Ajax\!/', $result);
  955. $this->assertNull($View->render(false, 'ajax2'));
  956. $this->PostsController->helpers = array('Session', 'Html');
  957. $this->PostsController->constructClasses();
  958. $this->PostsController->request->params['action'] = 'index';
  959. Configure::write('Cache.check', true);
  960. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  961. $result = $View->render('index');
  962. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  963. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  964. }
  965. /**
  966. * Test that View::$view works
  967. *
  968. * @return void
  969. */
  970. public function testRenderUsingViewProperty() {
  971. $this->PostsController->view = 'cache_form';
  972. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  973. $this->assertEquals('cache_form', $View->view);
  974. $result = $View->render();
  975. $this->assertRegExp('/Add User/', $result);
  976. }
  977. /**
  978. * Test render()ing a file in a subdir from a custom viewPath
  979. * in a plugin.
  980. *
  981. * @return void
  982. */
  983. public function testGetViewFileNameSubdirWithPluginAndViewPath() {
  984. $this->PostsController->plugin = 'TestPlugin';
  985. $this->PostsController->name = 'Posts';
  986. $this->PostsController->viewPath = 'Element';
  987. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  988. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  989. $result = $View->getViewFileName('sub_dir/sub_element');
  990. $expected = $pluginPath . 'src' . DS . 'Template' . DS . 'Element' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  991. $this->assertPathEquals($expected, $result);
  992. }
  993. /**
  994. * Test that view vars can replace the local helper variables
  995. * and not overwrite the $this->Helper references
  996. *
  997. * @return void
  998. */
  999. public function testViewVarOverwritingLocalHelperVar() {
  1000. $Controller = new ViewPostsController();
  1001. $Controller->helpers = array('Session', 'Html');
  1002. $Controller->set('html', 'I am some test html');
  1003. $View = $Controller->createView();
  1004. $result = $View->render('helper_overwrite', false);
  1005. $this->assertRegExp('/I am some test html/', $result);
  1006. $this->assertRegExp('/Test link/', $result);
  1007. }
  1008. /**
  1009. * Test getViewFileName method
  1010. *
  1011. * @return void
  1012. */
  1013. public function testViewFileName() {
  1014. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  1015. $result = $View->getViewFileName('index');
  1016. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1017. $result = $View->getViewFileName('TestPlugin.index');
  1018. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  1019. $result = $View->getViewFileName('/Pages/home');
  1020. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  1021. $result = $View->getViewFileName('../Element/test_element');
  1022. $this->assertRegExp('/Element(\/|\\\)test_element.ctp/', $result);
  1023. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  1024. $result = $View->getViewFileName('../Posts/index');
  1025. $this->assertPathEquals($expected, $result);
  1026. }
  1027. /**
  1028. * Test creating a block with capturing output.
  1029. *
  1030. * @return void
  1031. */
  1032. public function testBlockCaptureOverwrite() {
  1033. $this->View->start('test');
  1034. echo 'Block content';
  1035. $this->View->end();
  1036. $this->View->start('test');
  1037. echo 'New content';
  1038. $this->View->end();
  1039. $result = $this->View->fetch('test');
  1040. $this->assertEquals('New content', $result);
  1041. }
  1042. /**
  1043. * Test that blocks can be fetched inside a block with the same name
  1044. *
  1045. * @return void
  1046. */
  1047. public function testBlockExtend() {
  1048. $this->View->start('test');
  1049. echo 'Block content';
  1050. $this->View->end();
  1051. $this->View->start('test');
  1052. echo $this->View->fetch('test');
  1053. echo 'New content';
  1054. $this->View->end();
  1055. $result = $this->View->fetch('test');
  1056. $this->assertEquals('Block contentNew content', $result);
  1057. }
  1058. /**
  1059. * Test creating a block with capturing output.
  1060. *
  1061. * @return void
  1062. */
  1063. public function testBlockCapture() {
  1064. $this->View->start('test');
  1065. echo 'Block content';
  1066. $this->View->end();
  1067. $result = $this->View->fetch('test');
  1068. $this->assertEquals('Block content', $result);
  1069. }
  1070. /**
  1071. * Test appending to a block with capturing output.
  1072. *
  1073. * @return void
  1074. */
  1075. public function testBlockAppendCapture() {
  1076. $this->View->start('test');
  1077. echo 'Content ';
  1078. $this->View->end();
  1079. $this->View->append('test');
  1080. echo 'appended';
  1081. $this->View->end();
  1082. $result = $this->View->fetch('test');
  1083. $this->assertEquals('Content appended', $result);
  1084. }
  1085. /**
  1086. * Test setting a block's content.
  1087. *
  1088. * @return void
  1089. */
  1090. public function testBlockSet() {
  1091. $this->View->assign('test', 'Block content');
  1092. $result = $this->View->fetch('test');
  1093. $this->assertEquals('Block content', $result);
  1094. }
  1095. /**
  1096. * Test resetting a block's content.
  1097. *
  1098. * @return void
  1099. */
  1100. public function testBlockReset() {
  1101. $this->View->assign('test', '');
  1102. $result = $this->View->fetch('test', 'This should not be returned');
  1103. $this->assertSame('', $result);
  1104. }
  1105. /**
  1106. * Test setting a block's content to null
  1107. *
  1108. * @return void
  1109. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
  1110. */
  1111. public function testBlockSetNull() {
  1112. $this->View->assign('testWithNull', null);
  1113. $result = $this->View->fetch('testWithNull');
  1114. $this->assertSame('', $result);
  1115. }
  1116. /**
  1117. * Test setting a block's content to an object with __toString magic method
  1118. *
  1119. * @return void
  1120. */
  1121. public function testBlockSetObjectWithToString() {
  1122. $objectWithToString = new TestObjectWithToString();
  1123. $this->View->assign('testWithObjectWithToString', $objectWithToString);
  1124. $result = $this->View->fetch('testWithObjectWithToString');
  1125. $this->assertSame("I'm ObjectWithToString", $result);
  1126. }
  1127. /**
  1128. * Test setting a block's content to an object without __toString magic method
  1129. *
  1130. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1131. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1132. *
  1133. * @expectedException PHPUnit_Framework_Error
  1134. * @return void
  1135. */
  1136. public function testBlockSetObjectWithoutToString() {
  1137. $objectWithToString = new TestObjectWithoutToString();
  1138. $this->View->assign('testWithObjectWithoutToString', $objectWithToString);
  1139. }
  1140. /**
  1141. * Test setting a block's content to a decimal
  1142. *
  1143. * @return void
  1144. */
  1145. public function testBlockSetDecimal() {
  1146. $this->View->assign('testWithDecimal', 1.23456789);
  1147. $result = $this->View->fetch('testWithDecimal');
  1148. $this->assertEquals('1.23456789', $result);
  1149. }
  1150. /**
  1151. * Data provider for block related tests.
  1152. *
  1153. * @return array
  1154. */
  1155. public static function blockValueProvider() {
  1156. return array(
  1157. 'string' => array('A string value'),
  1158. 'decimal' => array(1.23456),
  1159. 'object with __toString' => array(new TestObjectWithToString()),
  1160. );
  1161. }
  1162. /**
  1163. * Test appending to a block with append.
  1164. *
  1165. * @param mixed $value Value
  1166. * @return void
  1167. * @dataProvider blockValueProvider
  1168. */
  1169. public function testBlockAppend($value) {
  1170. $this->View->assign('testBlock', 'Block');
  1171. $this->View->append('testBlock', $value);
  1172. $result = $this->View->fetch('testBlock');
  1173. $this->assertSame('Block' . $value, $result);
  1174. }
  1175. /**
  1176. * Test appending an object without __toString magic method to a block with append.
  1177. *
  1178. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1179. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1180. *
  1181. * @expectedException PHPUnit_Framework_Error
  1182. * @return void
  1183. */
  1184. public function testBlockAppendObjectWithoutToString() {
  1185. $object = new TestObjectWithoutToString();
  1186. $this->View->assign('testBlock', 'Block ');
  1187. $this->View->append('testBlock', $object);
  1188. }
  1189. /**
  1190. * Test prepending to a block with prepend.
  1191. *
  1192. * @param mixed $value Value
  1193. * @return void
  1194. * @dataProvider blockValueProvider
  1195. */
  1196. public function testBlockPrepend($value) {
  1197. $this->View->assign('test', 'Block');
  1198. $this->View->prepend('test', $value);
  1199. $result = $this->View->fetch('test');
  1200. $this->assertEquals($value . 'Block', $result);
  1201. }
  1202. /**
  1203. * Test prepending an object without __toString magic method to a block with prepend.
  1204. *
  1205. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1206. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1207. *
  1208. * @expectedException PHPUnit_Framework_Error
  1209. * @return void
  1210. */
  1211. public function testBlockPrependObjectWithoutToString() {
  1212. $object = new TestObjectWithoutToString();
  1213. $this->View->assign('test', 'Block ');
  1214. $this->View->prepend('test', $object);
  1215. }
  1216. /**
  1217. * You should be able to append to undefined blocks.
  1218. *
  1219. * @return void
  1220. */
  1221. public function testBlockAppendUndefined() {
  1222. $this->View->append('test', 'Unknown');
  1223. $result = $this->View->fetch('test');
  1224. $this->assertEquals('Unknown', $result);
  1225. }
  1226. /**
  1227. * You should be able to prepend to undefined blocks.
  1228. *
  1229. * @return void
  1230. */
  1231. public function testBlockPrependUndefined() {
  1232. $this->View->prepend('test', 'Unknown');
  1233. $result = $this->View->fetch('test');
  1234. $this->assertEquals('Unknown', $result);
  1235. }
  1236. /**
  1237. * Test getting block names
  1238. *
  1239. * @return void
  1240. */
  1241. public function testBlocks() {
  1242. $this->View->append('test', 'one');
  1243. $this->View->assign('test1', 'one');
  1244. $this->assertEquals(array('test', 'test1'), $this->View->blocks());
  1245. }
  1246. /**
  1247. * Test that blocks can be nested.
  1248. *
  1249. * @return void
  1250. */
  1251. public function testNestedBlocks() {
  1252. $this->View->start('first');
  1253. echo 'In first ';
  1254. $this->View->start('second');
  1255. echo 'In second';
  1256. $this->View->end();
  1257. echo 'In first';
  1258. $this->View->end();
  1259. $this->assertEquals('In first In first', $this->View->fetch('first'));
  1260. $this->assertEquals('In second', $this->View->fetch('second'));
  1261. }
  1262. /**
  1263. * Test that starting the same block twice throws an exception
  1264. *
  1265. * @expectedException \Cake\Core\Exception\Exception
  1266. * @return void
  1267. */
  1268. public function testStartBlocksTwice() {
  1269. $this->View->start('first');
  1270. $this->View->start('first');
  1271. }
  1272. /**
  1273. * Test that an exception gets thrown when you leave a block open at the end
  1274. * of a view.
  1275. *
  1276. * @expectedException \LogicException
  1277. * @return void
  1278. */
  1279. public function testExceptionOnOpenBlock() {
  1280. $this->View->render('open_block');
  1281. }
  1282. /**
  1283. * Test nested extended views.
  1284. *
  1285. * @return void
  1286. */
  1287. public function testExtendNested() {
  1288. $this->View->layout = false;
  1289. $content = $this->View->render('nested_extends');
  1290. $expected = <<<TEXT
  1291. This is the second parent.
  1292. This is the first parent.
  1293. This is the first template.
  1294. Sidebar Content.
  1295. TEXT;
  1296. $this->assertEquals($expected, $content);
  1297. }
  1298. /**
  1299. * Make sure that extending the current view with itself causes an exception
  1300. *
  1301. * @expectedException LogicException
  1302. * @return void
  1303. */
  1304. public function testExtendSelf() {
  1305. $this->View->layout = false;
  1306. $this->View->render('extend_self');
  1307. }
  1308. /**
  1309. * Make sure that extending in a loop causes an exception
  1310. *
  1311. * @expectedException LogicException
  1312. * @return void
  1313. */
  1314. public function testExtendLoop() {
  1315. $this->View->layout = false;
  1316. $this->View->render('extend_loop');
  1317. }
  1318. /**
  1319. * Test extend() in an element and a view.
  1320. *
  1321. * @return void
  1322. */
  1323. public function testExtendElement() {
  1324. $this->View->layout = false;
  1325. $content = $this->View->render('extend_element');
  1326. $expected = <<<TEXT
  1327. Parent View.
  1328. View content.
  1329. Parent Element.
  1330. Element content.
  1331. TEXT;
  1332. $this->assertEquals($expected, $content);
  1333. }
  1334. /**
  1335. * Extending an element which doesn't exist should throw a missing view exception
  1336. *
  1337. * @expectedException LogicException
  1338. * @return void
  1339. */
  1340. public function testExtendMissingElement() {
  1341. $this->View->layout = false;
  1342. $this->View->render('extend_missing_element');
  1343. }
  1344. /**
  1345. * Test extend() preceeded by an element()
  1346. *
  1347. * @return void
  1348. */
  1349. public function testExtendWithElementBeforeExtend() {
  1350. $this->View->layout = false;
  1351. $result = $this->View->render('extend_with_element');
  1352. $expected = <<<TEXT
  1353. Parent View.
  1354. this is the test elementThe view
  1355. TEXT;
  1356. $this->assertEquals($expected, $result);
  1357. }
  1358. /**
  1359. * Test memory leaks that existed in _paths at one point.
  1360. *
  1361. * @return void
  1362. */
  1363. public function testMemoryLeakInPaths() {
  1364. $this->ThemeController->plugin = null;
  1365. $this->ThemeController->name = 'Posts';
  1366. $this->ThemeController->viewPath = 'Posts';
  1367. $this->ThemeController->layout = 'whatever';
  1368. $this->ThemeController->theme = 'TestTheme';
  1369. $View = $this->ThemeController->createView();
  1370. $View->element('test_element');
  1371. $start = memory_get_usage();
  1372. for ($i = 0; $i < 10; $i++) {
  1373. $View->element('test_element');
  1374. }
  1375. $end = memory_get_usage();
  1376. $this->assertLessThanOrEqual($start + 5000, $end);
  1377. }
  1378. /**
  1379. * Tests that a view block uses default value when not assigned and uses assigned value when it is
  1380. *
  1381. * @return void
  1382. */
  1383. public function testBlockDefaultValue() {
  1384. $default = 'Default';
  1385. $result = $this->View->fetch('title', $default);
  1386. $this->assertEquals($default, $result);
  1387. $expected = 'My Title';
  1388. $this->View->assign('title', $expected);
  1389. $result = $this->View->fetch('title', $default);
  1390. $this->assertEquals($expected, $result);
  1391. }
  1392. /**
  1393. * Tests that a view variable uses default value when not assigned and uses assigned value when it is
  1394. *
  1395. * @return void
  1396. */
  1397. public function testViewVarDefaultValue() {
  1398. $default = 'Default';
  1399. $result = $this->View->get('title', $default);
  1400. $this->assertEquals($default, $result);
  1401. $expected = 'Back to the Future';
  1402. $this->View->set('title', $expected);
  1403. $result = $this->View->get('title', $default);
  1404. $this->assertEquals($expected, $result);
  1405. }
  1406. /**
  1407. * Test the helpers() method.
  1408. *
  1409. * @return void
  1410. */
  1411. public function testHelpers() {
  1412. $this->assertInstanceOf('Cake\View\HelperRegistry', $this->View->helpers());
  1413. $result = $this->View->helpers();
  1414. $this->assertSame($result, $this->View->helpers());
  1415. }
  1416. }