ViewTest.php 57 KB

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