ViewTest.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  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 CakePHP(tm) v 1.2.0.4206
  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 = $this->getMock('Cake\Network\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', 2);
  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 that ctp is used as a fallback file extension for elements
  678. *
  679. * @return void
  680. */
  681. public function testElementCtpFallback() {
  682. $View = new TestView($this->PostsController);
  683. $View->ext = '.missing';
  684. $element = 'test_element';
  685. $expected = 'this is the test element';
  686. $result = $View->element($element);
  687. $this->assertEquals($expected, $result);
  688. }
  689. /**
  690. * Test loadHelpers method
  691. *
  692. * @return void
  693. */
  694. public function testLoadHelpers() {
  695. $View = new View($this->PostsController);
  696. $View->helpers = array('Html', 'Form');
  697. $View->loadHelpers();
  698. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  699. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  700. }
  701. /**
  702. * Test lazy loading helpers
  703. *
  704. * @return void
  705. */
  706. public function testLazyLoadHelpers() {
  707. $View = new View($this->PostsController);
  708. $View->helpers = array();
  709. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  710. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  711. }
  712. /**
  713. * Test the correct triggering of helper callbacks
  714. *
  715. * @return void
  716. */
  717. public function testHelperCallbackTriggering() {
  718. $View = new View($this->PostsController);
  719. $manager = $this->getMock('Cake\Event\EventManager');
  720. $View->setEventManager($manager);
  721. $manager->expects($this->at(0))->method('dispatch')
  722. ->with(
  723. $this->logicalAnd(
  724. $this->isInstanceOf('Cake\Event\Event'),
  725. $this->attributeEqualTo('_name', 'View.beforeRender'),
  726. $this->attributeEqualTo('_subject', $View)
  727. )
  728. );
  729. $manager->expects($this->at(1))->method('dispatch')
  730. ->with(
  731. $this->logicalAnd(
  732. $this->isInstanceOf('Cake\Event\Event'),
  733. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  734. $this->attributeEqualTo('_subject', $View)
  735. )
  736. );
  737. $manager->expects($this->at(2))->method('dispatch')
  738. ->with(
  739. $this->logicalAnd(
  740. $this->isInstanceOf('Cake\Event\Event'),
  741. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  742. $this->attributeEqualTo('_subject', $View)
  743. )
  744. );
  745. $manager->expects($this->at(3))->method('dispatch')
  746. ->with(
  747. $this->logicalAnd(
  748. $this->isInstanceOf('Cake\Event\Event'),
  749. $this->attributeEqualTo('_name', 'View.afterRender'),
  750. $this->attributeEqualTo('_subject', $View)
  751. )
  752. );
  753. $manager->expects($this->at(4))->method('dispatch')
  754. ->with(
  755. $this->logicalAnd(
  756. $this->isInstanceOf('Cake\Event\Event'),
  757. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  758. $this->attributeEqualTo('_subject', $View)
  759. )
  760. );
  761. $manager->expects($this->at(5))->method('dispatch')
  762. ->with(
  763. $this->logicalAnd(
  764. $this->isInstanceOf('Cake\Event\Event'),
  765. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  766. $this->attributeEqualTo('_subject', $View)
  767. )
  768. );
  769. $manager->expects($this->at(6))->method('dispatch')
  770. ->with(
  771. $this->logicalAnd(
  772. $this->isInstanceOf('Cake\Event\Event'),
  773. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  774. $this->attributeEqualTo('_subject', $View)
  775. )
  776. );
  777. $manager->expects($this->at(7))->method('dispatch')
  778. ->with(
  779. $this->logicalAnd(
  780. $this->isInstanceOf('Cake\Event\Event'),
  781. $this->attributeEqualTo('_name', 'View.afterLayout'),
  782. $this->attributeEqualTo('_subject', $View)
  783. )
  784. );
  785. $View->render('index');
  786. }
  787. /**
  788. * Test beforeLayout method
  789. *
  790. * @return void
  791. */
  792. public function testBeforeLayout() {
  793. $this->PostsController->helpers = array(
  794. 'Session',
  795. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  796. 'Html'
  797. );
  798. $View = new View($this->PostsController);
  799. $View->render('index');
  800. $this->assertEquals('Valuation', $View->Helpers->TestBeforeAfter->property);
  801. }
  802. /**
  803. * Test afterLayout method
  804. *
  805. * @return void
  806. */
  807. public function testAfterLayout() {
  808. $this->PostsController->helpers = array(
  809. 'Session',
  810. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  811. 'Html'
  812. );
  813. $this->PostsController->set('variable', 'values');
  814. $View = new View($this->PostsController);
  815. $content = 'This is my view output';
  816. $result = $View->renderLayout($content, 'default');
  817. $this->assertRegExp('/modified in the afterlife/', $result);
  818. $this->assertRegExp('/This is my view output/', $result);
  819. }
  820. /**
  821. * Test renderLoadHelper method
  822. *
  823. * @return void
  824. */
  825. public function testRenderLoadHelper() {
  826. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  827. $View = new TestView($this->PostsController);
  828. $result = $View->render('index', false);
  829. $this->assertEquals('posts index', $result);
  830. $attached = $View->Helpers->loaded();
  831. $this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
  832. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  833. $View = new TestView($this->PostsController);
  834. $result = $View->render('index', false);
  835. $this->assertEquals('posts index', $result);
  836. $attached = $View->Helpers->loaded();
  837. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  838. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  839. }
  840. /**
  841. * Test render method
  842. *
  843. * @return void
  844. */
  845. public function testRender() {
  846. $View = new TestView($this->PostsController);
  847. $result = $View->render('index');
  848. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  849. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  850. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  851. $this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
  852. $this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
  853. $this->PostsController->helpers = array('Session', 'Cache', 'Html');
  854. $this->PostsController->constructClasses();
  855. $this->PostsController->cacheAction = array('index' => 3600);
  856. $this->PostsController->request->params['action'] = 'index';
  857. Configure::write('Cache.check', true);
  858. $View = new TestView($this->PostsController);
  859. $result = $View->render('index');
  860. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  861. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  862. }
  863. /**
  864. * Test that View::$view works
  865. *
  866. * @return void
  867. */
  868. public function testRenderUsingViewProperty() {
  869. $this->PostsController->view = 'cache_form';
  870. $View = new TestView($this->PostsController);
  871. $this->assertEquals('cache_form', $View->view);
  872. $result = $View->render();
  873. $this->assertRegExp('/Add User/', $result);
  874. }
  875. /**
  876. * Test render()ing a file in a subdir from a custom viewPath
  877. * in a plugin.
  878. *
  879. * @return void
  880. */
  881. public function testGetViewFileNameSubdirWithPluginAndViewPath() {
  882. $this->PostsController->plugin = 'TestPlugin';
  883. $this->PostsController->viewPath = 'Element';
  884. $this->PostsController->name = 'Posts';
  885. $View = new TestView($this->PostsController);
  886. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  887. $result = $View->getViewFileName('sub_dir/sub_element');
  888. $expected = $pluginPath . 'Template' . DS . 'Element' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  889. $this->assertEquals($expected, $result);
  890. }
  891. /**
  892. * Test that view vars can replace the local helper variables
  893. * and not overwrite the $this->Helper references
  894. *
  895. * @return void
  896. */
  897. public function testViewVarOverwritingLocalHelperVar() {
  898. $Controller = new ViewPostsController();
  899. $Controller->helpers = array('Session', 'Html');
  900. $Controller->set('html', 'I am some test html');
  901. $View = new View($Controller);
  902. $result = $View->render('helper_overwrite', false);
  903. $this->assertRegExp('/I am some test html/', $result);
  904. $this->assertRegExp('/Test link/', $result);
  905. }
  906. /**
  907. * Test getViewFileName method
  908. *
  909. * @return void
  910. */
  911. public function testViewFileName() {
  912. $View = new TestView($this->PostsController);
  913. $result = $View->getViewFileName('index');
  914. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  915. $result = $View->getViewFileName('TestPlugin.index');
  916. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  917. $result = $View->getViewFileName('/Pages/home');
  918. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  919. $result = $View->getViewFileName('../Element/test_element');
  920. $this->assertRegExp('/Element(\/|\\\)test_element.ctp/', $result);
  921. $result = $View->getViewFileName('../Themed/TestTheme/Posts/index');
  922. $this->assertRegExp('/Themed(\/|\\\)TestTheme(\/|\\\)Posts(\/|\\\)index.ctp/', $result);
  923. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  924. $result = $View->getViewFileName('../Posts/index');
  925. $this->assertEquals($expected, $result);
  926. }
  927. /**
  928. * Test renderCache method
  929. *
  930. * @return void
  931. */
  932. public function testRenderCache() {
  933. $this->skipIf(!is_writable(CACHE . 'views/'), 'CACHE/views dir is not writable, cannot test renderCache.');
  934. $view = 'test_view';
  935. $View = new View($this->PostsController);
  936. $path = CACHE . 'views/view_cache_' . $view;
  937. $cacheText = '<!--cachetime:' . time() . '-->some cacheText';
  938. $f = fopen($path, 'w+');
  939. fwrite($f, $cacheText);
  940. fclose($f);
  941. $result = $View->renderCache($path, '+1 second');
  942. $this->assertFalse($result);
  943. if (file_exists($path)) {
  944. unlink($path);
  945. }
  946. $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
  947. $f = fopen($path, 'w+');
  948. fwrite($f, $cacheText);
  949. fclose($f);
  950. $result = $View->renderCache($path, '+1 second');
  951. $this->assertRegExp('/^some cacheText/', $result);
  952. if (file_exists($path)) {
  953. unlink($path);
  954. }
  955. }
  956. /**
  957. * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
  958. *
  959. * @return void
  960. */
  961. public function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
  962. Configure::write('Cache.check', false);
  963. $View = new View($this->PostsController);
  964. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  965. $View->helpers = array('Html', 'Form', 'Cache');
  966. $View->layout = 'cache_layout';
  967. $result = $View->render('index');
  968. $this->assertNotRegExp('/cake:nocache/', $result);
  969. }
  970. /**
  971. * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
  972. *
  973. * @return void
  974. */
  975. public function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
  976. Configure::write('Cache.check', true);
  977. $View = new View($this->PostsController);
  978. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  979. $View->helpers = array('Html', 'Form');
  980. $View->layout = 'cache_layout';
  981. $result = $View->render('index');
  982. $this->assertNotRegExp('/cake:nocache/', $result);
  983. }
  984. /**
  985. * testBadExt method
  986. *
  987. * @expectedException Cake\Error\MissingViewException
  988. * @return void
  989. */
  990. public function testBadExt() {
  991. $this->PostsController->action = 'something';
  992. $this->PostsController->ext = '.whatever';
  993. $View = new TestView($this->PostsController);
  994. $View->render('this_is_missing');
  995. }
  996. /**
  997. * testAltExt method
  998. *
  999. * @return void
  1000. */
  1001. public function testAltExt() {
  1002. $this->PostsController->ext = '.alt';
  1003. $View = new TestView($this->PostsController);
  1004. $result = $View->render('alt_ext', false);
  1005. $this->assertEquals('alt ext', $result);
  1006. }
  1007. /**
  1008. * testAltBadExt method
  1009. *
  1010. * @expectedException Cake\Error\MissingViewException
  1011. * @return void
  1012. */
  1013. public function testAltBadExt() {
  1014. $View = new TestView($this->PostsController);
  1015. $View->render('alt_ext');
  1016. }
  1017. /**
  1018. * Test creating a block with capturing output.
  1019. *
  1020. * @return void
  1021. */
  1022. public function testBlockCapture() {
  1023. $this->View->start('test');
  1024. echo 'Block content';
  1025. $this->View->end();
  1026. $result = $this->View->fetch('test');
  1027. $this->assertEquals('Block content', $result);
  1028. }
  1029. /**
  1030. * Test block with startIfEmpty
  1031. *
  1032. * @return void
  1033. */
  1034. public function testBlockCaptureStartIfEmpty() {
  1035. $this->View->startIfEmpty('test');
  1036. echo "Block content 1";
  1037. $this->View->end();
  1038. $this->View->startIfEmpty('test');
  1039. echo "Block content 2";
  1040. $this->View->end();
  1041. $result = $this->View->fetch('test');
  1042. $this->assertEquals('Block content 1', $result);
  1043. }
  1044. /**
  1045. * Test block with startIfEmpty
  1046. *
  1047. * @return void
  1048. */
  1049. public function testBlockCaptureStartStartIfEmpty() {
  1050. $this->View->start('test');
  1051. echo "Block content 1";
  1052. $this->View->end();
  1053. $this->View->startIfEmpty('test');
  1054. echo "Block content 2";
  1055. $this->View->end();
  1056. $result = $this->View->fetch('test');
  1057. $this->assertEquals('Block content 1', $result);
  1058. }
  1059. /**
  1060. * Test appending to a block with capturing output.
  1061. *
  1062. * @return void
  1063. */
  1064. public function testBlockCaptureAppend() {
  1065. $this->View->start('test');
  1066. echo 'Block';
  1067. $this->View->end();
  1068. $this->View->append('test');
  1069. echo ' content';
  1070. $this->View->end();
  1071. $result = $this->View->fetch('test');
  1072. $this->assertEquals('Block content', $result);
  1073. }
  1074. /**
  1075. * Test setting a block's content.
  1076. *
  1077. * @return void
  1078. */
  1079. public function testBlockSet() {
  1080. $this->View->assign('test', 'Block content');
  1081. $result = $this->View->fetch('test');
  1082. $this->assertEquals('Block content', $result);
  1083. }
  1084. /**
  1085. * Test resetting a block's content.
  1086. *
  1087. * @return void
  1088. */
  1089. public function testBlockReset() {
  1090. $this->View->assign('test', '');
  1091. $result = $this->View->fetch('test', 'This should not be returned');
  1092. $this->assertSame('', $result);
  1093. }
  1094. /**
  1095. * Test setting a block's content to null
  1096. *
  1097. * @return void
  1098. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
  1099. */
  1100. public function testBlockSetNull() {
  1101. $this->View->assign('testWithNull', null);
  1102. $result = $this->View->fetch('testWithNull');
  1103. $this->assertSame('', $result);
  1104. }
  1105. /**
  1106. * Test setting a block's content to an object with __toString magic method
  1107. *
  1108. * @return void
  1109. */
  1110. public function testBlockSetObjectWithToString() {
  1111. $objectWithToString = new TestObjectWithToString();
  1112. $this->View->assign('testWithObjectWithToString', $objectWithToString);
  1113. $result = $this->View->fetch('testWithObjectWithToString');
  1114. $this->assertSame("I'm ObjectWithToString", $result);
  1115. }
  1116. /**
  1117. * Test setting a block's content to an object without __toString magic method
  1118. *
  1119. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1120. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1121. *
  1122. * @expectedException PHPUnit_Framework_Error
  1123. * @return void
  1124. */
  1125. public function testBlockSetObjectWithoutToString() {
  1126. $objectWithToString = new TestObjectWithoutToString();
  1127. $this->View->assign('testWithObjectWithoutToString', $objectWithToString);
  1128. }
  1129. /**
  1130. * Test setting a block's content to a decimal
  1131. *
  1132. * @return void
  1133. */
  1134. public function testBlockSetDecimal() {
  1135. $this->View->assign('testWithDecimal', 1.23456789);
  1136. $result = $this->View->fetch('testWithDecimal');
  1137. $this->assertEquals('1.23456789', $result);
  1138. }
  1139. /**
  1140. * Data provider for block related tests.
  1141. *
  1142. * @return array
  1143. */
  1144. public static function blockValueProvider() {
  1145. return array(
  1146. 'string' => array('A string value'),
  1147. 'null' => array(null),
  1148. 'decimal' => array(1.23456),
  1149. 'object with __toString' => array(new TestObjectWithToString()),
  1150. );
  1151. }
  1152. /**
  1153. * Test appending to a block with append.
  1154. *
  1155. * @dataProvider blockValueProvider
  1156. * @return void
  1157. */
  1158. public function testBlockAppend($value) {
  1159. $this->View->assign('testBlock', 'Block');
  1160. $this->View->append('testBlock', $value);
  1161. $result = $this->View->fetch('testBlock');
  1162. $this->assertSame('Block' . $value, $result);
  1163. }
  1164. /**
  1165. * Test appending an object without __toString magic method to a block with append.
  1166. *
  1167. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1168. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1169. *
  1170. * @expectedException PHPUnit_Framework_Error
  1171. * @return void
  1172. */
  1173. public function testBlockAppendObjectWithoutToString() {
  1174. $object = new TestObjectWithoutToString();
  1175. $this->View->assign('testBlock', 'Block ');
  1176. $this->View->append('testBlock', $object);
  1177. }
  1178. /**
  1179. * Test prepending to a block with prepend.
  1180. *
  1181. * @dataProvider blockValueProvider
  1182. * @return void
  1183. */
  1184. public function testBlockPrepend($value) {
  1185. $this->View->assign('test', 'Block');
  1186. $this->View->prepend('test', $value);
  1187. $result = $this->View->fetch('test');
  1188. $this->assertEquals($value . 'Block', $result);
  1189. }
  1190. /**
  1191. * Test prepending an object without __toString magic method to a block with prepend.
  1192. *
  1193. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1194. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1195. *
  1196. * @expectedException PHPUnit_Framework_Error
  1197. * @return void
  1198. */
  1199. public function testBlockPrependObjectWithoutToString() {
  1200. $object = new TestObjectWithoutToString();
  1201. $this->View->assign('test', 'Block ');
  1202. $this->View->prepend('test', $object);
  1203. }
  1204. /**
  1205. * You should be able to append to undefined blocks.
  1206. *
  1207. * @return void
  1208. */
  1209. public function testBlockAppendUndefined() {
  1210. $this->View->append('test', 'Unknown');
  1211. $result = $this->View->fetch('test');
  1212. $this->assertEquals('Unknown', $result);
  1213. }
  1214. /**
  1215. * You should be able to prepend to undefined blocks.
  1216. *
  1217. * @return void
  1218. */
  1219. public function testBlockPrependUndefined() {
  1220. $this->View->prepend('test', 'Unknown');
  1221. $result = $this->View->fetch('test');
  1222. $this->assertEquals('Unknown', $result);
  1223. }
  1224. /**
  1225. * Test getting block names
  1226. *
  1227. * @return void
  1228. */
  1229. public function testBlocks() {
  1230. $this->View->append('test', 'one');
  1231. $this->View->assign('test1', 'one');
  1232. $this->assertEquals(array('test', 'test1'), $this->View->blocks());
  1233. }
  1234. /**
  1235. * Test that blocks can be nested.
  1236. *
  1237. * @return void
  1238. */
  1239. public function testNestedBlocks() {
  1240. $this->View->start('first');
  1241. echo 'In first ';
  1242. $this->View->start('second');
  1243. echo 'In second';
  1244. $this->View->end();
  1245. echo 'In first';
  1246. $this->View->end();
  1247. $this->assertEquals('In first In first', $this->View->fetch('first'));
  1248. $this->assertEquals('In second', $this->View->fetch('second'));
  1249. }
  1250. /**
  1251. * Test that starting the same block twice throws an exception
  1252. *
  1253. * @expectedException Cake\Error\Exception
  1254. * @return void
  1255. */
  1256. public function testStartBlocksTwice() {
  1257. $this->View->start('first');
  1258. $this->View->start('first');
  1259. }
  1260. /**
  1261. * Test that an exception gets thrown when you leave a block open at the end
  1262. * of a view.
  1263. *
  1264. * @expectedException Cake\Error\Exception
  1265. * @return void
  1266. */
  1267. public function testExceptionOnOpenBlock() {
  1268. $this->View->render('open_block');
  1269. }
  1270. /**
  1271. * Test nested extended views.
  1272. *
  1273. * @return void
  1274. */
  1275. public function testExtendNested() {
  1276. $this->View->layout = false;
  1277. $content = $this->View->render('nested_extends');
  1278. $expected = <<<TEXT
  1279. This is the second parent.
  1280. This is the first parent.
  1281. This is the first template.
  1282. Sidebar Content.
  1283. TEXT;
  1284. $this->assertEquals($expected, $content);
  1285. }
  1286. /**
  1287. * Make sure that extending the current view with itself causes an exception
  1288. *
  1289. * @expectedException LogicException
  1290. * @return void
  1291. */
  1292. public function testExtendSelf() {
  1293. $this->View->layout = false;
  1294. $this->View->render('extend_self');
  1295. }
  1296. /**
  1297. * Make sure that extending in a loop causes an exception
  1298. *
  1299. * @expectedException LogicException
  1300. * @return void
  1301. */
  1302. public function testExtendLoop() {
  1303. $this->View->layout = false;
  1304. $this->View->render('extend_loop');
  1305. }
  1306. /**
  1307. * Test extend() in an element and a view.
  1308. *
  1309. * @return void
  1310. */
  1311. public function testExtendElement() {
  1312. $this->View->layout = false;
  1313. $content = $this->View->render('extend_element');
  1314. $expected = <<<TEXT
  1315. Parent View.
  1316. View content.
  1317. Parent Element.
  1318. Element content.
  1319. TEXT;
  1320. $this->assertEquals($expected, $content);
  1321. }
  1322. /**
  1323. * Extending an element which doesn't exist should throw a missing view exception
  1324. *
  1325. * @expectedException LogicException
  1326. * @return void
  1327. */
  1328. public function testExtendMissingElement() {
  1329. $this->View->layout = false;
  1330. $this->View->render('extend_missing_element');
  1331. }
  1332. /**
  1333. * Test extend() preceeded by an element()
  1334. *
  1335. * @return void
  1336. */
  1337. public function testExtendWithElementBeforeExtend() {
  1338. $this->View->layout = false;
  1339. $result = $this->View->render('extend_with_element');
  1340. $expected = <<<TEXT
  1341. Parent View.
  1342. this is the test elementThe view
  1343. TEXT;
  1344. $this->assertEquals($expected, $result);
  1345. }
  1346. /**
  1347. * Test memory leaks that existed in _paths at one point.
  1348. *
  1349. * @return void
  1350. */
  1351. public function testMemoryLeakInPaths() {
  1352. $this->ThemeController->plugin = null;
  1353. $this->ThemeController->name = 'Posts';
  1354. $this->ThemeController->viewPath = 'posts';
  1355. $this->ThemeController->layout = 'whatever';
  1356. $this->ThemeController->theme = 'TestTheme';
  1357. $View = new View($this->ThemeController);
  1358. $View->element('test_element');
  1359. $start = memory_get_usage();
  1360. for ($i = 0; $i < 10; $i++) {
  1361. $View->element('test_element');
  1362. }
  1363. $end = memory_get_usage();
  1364. $this->assertLessThanOrEqual($start + 5000, $end);
  1365. }
  1366. /**
  1367. * Tests that a view block uses default value when not assigned and uses assigned value when it is
  1368. *
  1369. * @return void
  1370. */
  1371. public function testBlockDefaultValue() {
  1372. $default = 'Default';
  1373. $result = $this->View->fetch('title', $default);
  1374. $this->assertEquals($default, $result);
  1375. $expected = 'My Title';
  1376. $this->View->assign('title', $expected);
  1377. $result = $this->View->fetch('title', $default);
  1378. $this->assertEquals($expected, $result);
  1379. }
  1380. /**
  1381. * Tests that a view variable uses default value when not assigned and uses assigned value when it is
  1382. *
  1383. * @return void
  1384. */
  1385. public function testViewVarDefaultValue() {
  1386. $default = 'Default';
  1387. $result = $this->View->get('title', $default);
  1388. $this->assertEquals($default, $result);
  1389. $expected = 'Back to the Future';
  1390. $this->View->set('title', $expected);
  1391. $result = $this->View->get('title', $default);
  1392. $this->assertEquals($expected, $result);
  1393. }
  1394. }