ViewTest.php 60 KB

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