ViewTest.php 58 KB

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