ViewTest.php 56 KB

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