ViewTest.php 42 KB

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