ViewTest.php 45 KB

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