ViewTest.php 44 KB

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