ViewTest.php 42 KB

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