ViewTest.php 46 KB

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