ViewTest.php 41 KB

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