ViewTest.php 55 KB

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