ViewTest.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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. ob_start();
  476. $View->getViewFileName('does_not_exist');
  477. ob_get_clean();
  478. }
  479. /**
  480. * Test for missing layouts
  481. *
  482. * @expectedException \Cake\View\Error\MissingLayoutException
  483. * @return void
  484. */
  485. public function testMissingLayout() {
  486. $viewOptions = ['plugin' => null,
  487. 'name' => 'Pages',
  488. 'viewPath' => 'Pages',
  489. 'layout' => 'whatever'
  490. ];
  491. $View = new TestView(null, null, null, $viewOptions);
  492. ob_start();
  493. $View->getLayoutFileName();
  494. ob_get_clean();
  495. }
  496. /**
  497. * Test viewVars method
  498. *
  499. * @return void
  500. */
  501. public function testViewVars() {
  502. $this->assertEquals(array('testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data'), $this->View->viewVars);
  503. }
  504. /**
  505. * Test generation of UUIDs method
  506. *
  507. * @return void
  508. */
  509. public function testUUIDGeneration() {
  510. Router::connect('/:controller', ['action' => 'index']);
  511. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  512. $this->assertEquals('form5988016017', $result);
  513. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  514. $this->assertEquals('formc3dc6be854', $result);
  515. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  516. $this->assertEquals('form28f92cc87f', $result);
  517. }
  518. /**
  519. * Test elementExists method
  520. *
  521. * @return void
  522. */
  523. public function testElementExists() {
  524. $result = $this->View->elementExists('test_element');
  525. $this->assertTrue($result);
  526. $result = $this->View->elementExists('TestPlugin.plugin_element');
  527. $this->assertTrue($result);
  528. $result = $this->View->elementExists('non_existent_element');
  529. $this->assertFalse($result);
  530. $result = $this->View->elementExists('TestPlugin.element');
  531. $this->assertFalse($result);
  532. $this->View->plugin = 'TestPlugin';
  533. $result = $this->View->elementExists('test_plugin_element');
  534. $this->assertTrue($result);
  535. }
  536. /**
  537. * Test element method
  538. *
  539. * @return void
  540. */
  541. public function testElement() {
  542. $result = $this->View->element('test_element');
  543. $this->assertEquals('this is the test element', $result);
  544. $result = $this->View->element('TestPlugin.plugin_element');
  545. $this->assertEquals('this is the plugin element using params[plugin]', $result);
  546. $this->View->plugin = 'TestPlugin';
  547. $result = $this->View->element('test_plugin_element');
  548. $this->assertEquals('this is the test set using View::$plugin plugin element', $result);
  549. }
  550. /**
  551. * Test elementInexistent method
  552. *
  553. * @expectedException Cake\View\Error\MissingElementException
  554. * @return void
  555. */
  556. public function testElementInexistent() {
  557. $this->View->element('non_existent_element');
  558. }
  559. /**
  560. * Test elementInexistent3 method
  561. *
  562. * @expectedException Cake\View\Error\MissingElementException
  563. * @return void
  564. */
  565. public function testElementInexistent3() {
  566. $this->View->element('test_plugin.plugin_element');
  567. }
  568. /**
  569. * Test that elements can have callbacks
  570. *
  571. * @return void
  572. */
  573. public function testElementCallbacks() {
  574. $count = 0;
  575. $callback = function ($event, $file) use (&$count) {
  576. $count++;
  577. };
  578. $events = $this->View->eventManager();
  579. $events->attach($callback, 'View.beforeRender');
  580. $events->attach($callback, 'View.afterRender');
  581. $this->View->element('test_element', array(), array('callbacks' => true));
  582. $this->assertEquals(2, $count);
  583. }
  584. /**
  585. * Test that additional element viewVars don't get overwritten with helpers.
  586. *
  587. * @return void
  588. */
  589. public function testElementParamsDontOverwriteHelpers() {
  590. $Controller = new ViewPostsController();
  591. $Controller->helpers = array('Form');
  592. $View = $Controller->createView();
  593. $result = $View->element('type_check', array('form' => 'string'), array('callbacks' => true));
  594. $this->assertEquals('string', $result);
  595. $View->set('form', 'string');
  596. $result = $View->element('type_check', array(), array('callbacks' => true));
  597. $this->assertEquals('string', $result);
  598. }
  599. /**
  600. * Test elementCacheHelperNoCache method
  601. *
  602. * @return void
  603. */
  604. public function testElementCacheHelperNoCache() {
  605. $Controller = new ViewPostsController();
  606. $View = $Controller->createView();
  607. $View->loadHelpers();
  608. $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
  609. $this->assertEquals('this is the test element', $result);
  610. }
  611. /**
  612. * Test elementCache method
  613. *
  614. * @return void
  615. */
  616. public function testElementCache() {
  617. Cache::drop('test_view');
  618. Cache::config('test_view', [
  619. 'engine' => 'File',
  620. 'duration' => '+1 day',
  621. 'path' => CACHE . 'views/',
  622. 'prefix' => ''
  623. ]);
  624. Cache::clear(true, 'test_view');
  625. $View = $this->PostsController->createView();
  626. $View->elementCache = 'test_view';
  627. $result = $View->element('test_element', array(), array('cache' => true));
  628. $expected = 'this is the test element';
  629. $this->assertEquals($expected, $result);
  630. $result = Cache::read('element__test_element_cache_callbacks', 'test_view');
  631. $this->assertEquals($expected, $result);
  632. $result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
  633. $this->assertEquals($expected, $result);
  634. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  635. $this->assertEquals($expected, $result);
  636. $View->element('test_element', array(
  637. 'param' => 'one',
  638. 'foo' => 'two'
  639. ), array(
  640. 'cache' => array('key' => 'custom_key')
  641. ));
  642. $result = Cache::read('element_custom_key', 'test_view');
  643. $this->assertEquals($expected, $result);
  644. $View->elementCache = 'default';
  645. $View->element('test_element', array(
  646. 'param' => 'one',
  647. 'foo' => 'two'
  648. ), array(
  649. 'cache' => array('config' => 'test_view'),
  650. ));
  651. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  652. $this->assertEquals($expected, $result);
  653. Cache::clear(true, 'test_view');
  654. Cache::drop('test_view');
  655. }
  656. /**
  657. * Test __get allowing access to helpers.
  658. *
  659. * @return void
  660. */
  661. public function testMagicGetAndAddHelper() {
  662. $View = new View();
  663. $View->addHelper('Html');
  664. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html);
  665. }
  666. /**
  667. * Test loadHelpers method
  668. *
  669. * @return void
  670. */
  671. public function testLoadHelpers() {
  672. $View = new View();
  673. $View->helpers = array('Html', 'Form');
  674. $View->loadHelpers();
  675. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  676. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  677. }
  678. /**
  679. * Test lazy loading helpers
  680. *
  681. * @return void
  682. */
  683. public function testLazyLoadHelpers() {
  684. $View = new View();
  685. $View->helpers = array();
  686. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  687. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  688. }
  689. /**
  690. * Test the correct triggering of helper callbacks
  691. *
  692. * @return void
  693. */
  694. public function testHelperCallbackTriggering() {
  695. $View = $this->PostsController->createView();
  696. $manager = $this->getMock('Cake\Event\EventManager');
  697. $View->eventManager($manager);
  698. $manager->expects($this->at(0))->method('dispatch')
  699. ->with(
  700. $this->logicalAnd(
  701. $this->isInstanceOf('Cake\Event\Event'),
  702. $this->attributeEqualTo('_name', 'View.beforeRender'),
  703. $this->attributeEqualTo('_subject', $View)
  704. )
  705. );
  706. $manager->expects($this->at(1))->method('dispatch')
  707. ->with(
  708. $this->logicalAnd(
  709. $this->isInstanceOf('Cake\Event\Event'),
  710. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  711. $this->attributeEqualTo('_subject', $View)
  712. )
  713. );
  714. $manager->expects($this->at(2))->method('dispatch')
  715. ->with(
  716. $this->logicalAnd(
  717. $this->isInstanceOf('Cake\Event\Event'),
  718. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  719. $this->attributeEqualTo('_subject', $View)
  720. )
  721. );
  722. $manager->expects($this->at(3))->method('dispatch')
  723. ->with(
  724. $this->logicalAnd(
  725. $this->isInstanceOf('Cake\Event\Event'),
  726. $this->attributeEqualTo('_name', 'View.afterRender'),
  727. $this->attributeEqualTo('_subject', $View)
  728. )
  729. );
  730. $manager->expects($this->at(4))->method('dispatch')
  731. ->with(
  732. $this->logicalAnd(
  733. $this->isInstanceOf('Cake\Event\Event'),
  734. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  735. $this->attributeEqualTo('_subject', $View)
  736. )
  737. );
  738. $manager->expects($this->at(5))->method('dispatch')
  739. ->with(
  740. $this->logicalAnd(
  741. $this->isInstanceOf('Cake\Event\Event'),
  742. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  743. $this->attributeEqualTo('_subject', $View)
  744. )
  745. );
  746. $manager->expects($this->at(6))->method('dispatch')
  747. ->with(
  748. $this->logicalAnd(
  749. $this->isInstanceOf('Cake\Event\Event'),
  750. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  751. $this->attributeEqualTo('_subject', $View)
  752. )
  753. );
  754. $manager->expects($this->at(7))->method('dispatch')
  755. ->with(
  756. $this->logicalAnd(
  757. $this->isInstanceOf('Cake\Event\Event'),
  758. $this->attributeEqualTo('_name', 'View.afterLayout'),
  759. $this->attributeEqualTo('_subject', $View)
  760. )
  761. );
  762. $View->render('index');
  763. }
  764. /**
  765. * Test beforeLayout method
  766. *
  767. * @return void
  768. */
  769. public function testBeforeLayout() {
  770. $this->PostsController->helpers = array(
  771. 'Session',
  772. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  773. 'Html'
  774. );
  775. $View = $this->PostsController->createView();
  776. $View->render('index');
  777. $this->assertEquals('Valuation', $View->helpers()->TestBeforeAfter->property);
  778. }
  779. /**
  780. * Test afterLayout method
  781. *
  782. * @return void
  783. */
  784. public function testAfterLayout() {
  785. $this->PostsController->helpers = array(
  786. 'Session',
  787. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  788. 'Html'
  789. );
  790. $this->PostsController->set('variable', 'values');
  791. $View = $this->PostsController->createView();
  792. $content = 'This is my view output';
  793. $result = $View->renderLayout($content, 'default');
  794. $this->assertRegExp('/modified in the afterlife/', $result);
  795. $this->assertRegExp('/This is my view output/', $result);
  796. }
  797. /**
  798. * Test renderLoadHelper method
  799. *
  800. * @return void
  801. */
  802. public function testRenderLoadHelper() {
  803. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  804. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  805. $result = $View->render('index', false);
  806. $this->assertEquals('posts index', $result);
  807. $attached = $View->helpers()->loaded();
  808. $this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
  809. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  810. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  811. $result = $View->render('index', false);
  812. $this->assertEquals('posts index', $result);
  813. $attached = $View->helpers()->loaded();
  814. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  815. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  816. }
  817. /**
  818. * Test render method
  819. *
  820. * @return void
  821. */
  822. public function testRender() {
  823. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  824. $result = $View->render('index');
  825. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  826. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  827. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  828. $this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
  829. $this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
  830. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  831. $result = $View->render(false, 'ajax2');
  832. $this->assertRegExp('/Ajax\!/', $result);
  833. $this->assertNull($View->render(false, 'ajax2'));
  834. $this->PostsController->helpers = array('Session', 'Cache', 'Html');
  835. $this->PostsController->constructClasses();
  836. $this->PostsController->cacheAction = array('index' => 3600);
  837. $this->PostsController->request->params['action'] = 'index';
  838. Configure::write('Cache.check', true);
  839. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  840. $result = $View->render('index');
  841. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  842. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  843. }
  844. /**
  845. * Test that View::$view works
  846. *
  847. * @return void
  848. */
  849. public function testRenderUsingViewProperty() {
  850. $this->PostsController->view = 'cache_form';
  851. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  852. $this->assertEquals('cache_form', $View->view);
  853. $result = $View->render();
  854. $this->assertRegExp('/Add User/', $result);
  855. }
  856. /**
  857. * Test render()ing a file in a subdir from a custom viewPath
  858. * in a plugin.
  859. *
  860. * @return void
  861. */
  862. public function testGetViewFileNameSubdirWithPluginAndViewPath() {
  863. $this->PostsController->plugin = 'TestPlugin';
  864. $this->PostsController->name = 'Posts';
  865. $this->PostsController->viewPath = 'Element';
  866. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  867. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  868. $result = $View->getViewFileName('sub_dir/sub_element');
  869. $expected = $pluginPath . 'src' . DS . 'Template' . DS . 'Element' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  870. $this->assertPathEquals($expected, $result);
  871. }
  872. /**
  873. * Test that view vars can replace the local helper variables
  874. * and not overwrite the $this->Helper references
  875. *
  876. * @return void
  877. */
  878. public function testViewVarOverwritingLocalHelperVar() {
  879. $Controller = new ViewPostsController();
  880. $Controller->helpers = array('Session', 'Html');
  881. $Controller->set('html', 'I am some test html');
  882. $View = $Controller->createView();
  883. $result = $View->render('helper_overwrite', false);
  884. $this->assertRegExp('/I am some test html/', $result);
  885. $this->assertRegExp('/Test link/', $result);
  886. }
  887. /**
  888. * Test getViewFileName method
  889. *
  890. * @return void
  891. */
  892. public function testViewFileName() {
  893. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  894. $result = $View->getViewFileName('index');
  895. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  896. $result = $View->getViewFileName('TestPlugin.index');
  897. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  898. $result = $View->getViewFileName('/Pages/home');
  899. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  900. $result = $View->getViewFileName('../Element/test_element');
  901. $this->assertRegExp('/Element(\/|\\\)test_element.ctp/', $result);
  902. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  903. $result = $View->getViewFileName('../Posts/index');
  904. $this->assertPathEquals($expected, $result);
  905. }
  906. /**
  907. * Test renderCache method
  908. *
  909. * @return void
  910. */
  911. public function testRenderCache() {
  912. $this->skipIf(!is_writable(CACHE . 'views/'), 'CACHE/views dir is not writable, cannot test renderCache.');
  913. $view = 'test_view';
  914. $View = $this->PostsController->createView();
  915. $path = CACHE . 'views/view_cache_' . $view;
  916. $cacheText = '<!--cachetime:' . time() . '-->some cacheText';
  917. $f = fopen($path, 'w+');
  918. fwrite($f, $cacheText);
  919. fclose($f);
  920. $result = $View->renderCache($path, '+1 second');
  921. $this->assertFalse($result);
  922. if (file_exists($path)) {
  923. unlink($path);
  924. }
  925. $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
  926. $f = fopen($path, 'w+');
  927. fwrite($f, $cacheText);
  928. fclose($f);
  929. $result = $View->renderCache($path, '+1 second');
  930. $this->assertRegExp('/^some cacheText/', $result);
  931. if (file_exists($path)) {
  932. unlink($path);
  933. }
  934. }
  935. /**
  936. * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
  937. *
  938. * @return void
  939. */
  940. public function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
  941. Configure::write('Cache.check', false);
  942. $View = $this->PostsController->createView();
  943. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  944. $View->helpers = array('Html', 'Form', 'Cache');
  945. $View->layout = 'cache_layout';
  946. $result = $View->render('index');
  947. $this->assertNotRegExp('/cake:nocache/', $result);
  948. }
  949. /**
  950. * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
  951. *
  952. * @return void
  953. */
  954. public function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
  955. Configure::write('Cache.check', true);
  956. $View = $this->PostsController->createView();
  957. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  958. $View->helpers = array('Html', 'Form');
  959. $View->layout = 'cache_layout';
  960. $result = $View->render('index');
  961. $this->assertNotRegExp('/cake:nocache/', $result);
  962. }
  963. /**
  964. * Test creating a block with capturing output.
  965. *
  966. * @return void
  967. */
  968. public function testBlockCaptureOverwrite() {
  969. $this->View->start('test');
  970. echo 'Block content';
  971. $this->View->end();
  972. $this->View->start('test');
  973. echo 'New content';
  974. $this->View->end();
  975. $result = $this->View->fetch('test');
  976. $this->assertEquals('New content', $result);
  977. }
  978. /**
  979. * Test that blocks can be fetched inside a block with the same name
  980. *
  981. * @return void
  982. */
  983. public function testBlockExtend() {
  984. $this->View->start('test');
  985. echo 'Block content';
  986. $this->View->end();
  987. $this->View->start('test');
  988. echo $this->View->fetch('test');
  989. echo 'New content';
  990. $this->View->end();
  991. $result = $this->View->fetch('test');
  992. $this->assertEquals('Block contentNew content', $result);
  993. }
  994. /**
  995. * Test creating a block with capturing output.
  996. *
  997. * @return void
  998. */
  999. public function testBlockCapture() {
  1000. $this->View->start('test');
  1001. echo 'Block content';
  1002. $this->View->end();
  1003. $result = $this->View->fetch('test');
  1004. $this->assertEquals('Block content', $result);
  1005. }
  1006. /**
  1007. * Test appending to a block with capturing output.
  1008. *
  1009. * @return void
  1010. */
  1011. public function testBlockCaptureAppend() {
  1012. $this->View->start('test');
  1013. echo 'Block';
  1014. $this->View->end();
  1015. $this->View->append('test', ' content');
  1016. $result = $this->View->fetch('test');
  1017. $this->assertEquals('Block content', $result);
  1018. }
  1019. /**
  1020. * Test setting a block's content.
  1021. *
  1022. * @return void
  1023. */
  1024. public function testBlockSet() {
  1025. $this->View->assign('test', 'Block content');
  1026. $result = $this->View->fetch('test');
  1027. $this->assertEquals('Block content', $result);
  1028. }
  1029. /**
  1030. * Test resetting a block's content.
  1031. *
  1032. * @return void
  1033. */
  1034. public function testBlockReset() {
  1035. $this->View->assign('test', '');
  1036. $result = $this->View->fetch('test', 'This should not be returned');
  1037. $this->assertSame('', $result);
  1038. }
  1039. /**
  1040. * Test setting a block's content to null
  1041. *
  1042. * @return void
  1043. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
  1044. */
  1045. public function testBlockSetNull() {
  1046. $this->View->assign('testWithNull', null);
  1047. $result = $this->View->fetch('testWithNull');
  1048. $this->assertSame('', $result);
  1049. }
  1050. /**
  1051. * Test setting a block's content to an object with __toString magic method
  1052. *
  1053. * @return void
  1054. */
  1055. public function testBlockSetObjectWithToString() {
  1056. $objectWithToString = new TestObjectWithToString();
  1057. $this->View->assign('testWithObjectWithToString', $objectWithToString);
  1058. $result = $this->View->fetch('testWithObjectWithToString');
  1059. $this->assertSame("I'm ObjectWithToString", $result);
  1060. }
  1061. /**
  1062. * Test setting a block's content to an object without __toString magic method
  1063. *
  1064. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1065. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1066. *
  1067. * @expectedException PHPUnit_Framework_Error
  1068. * @return void
  1069. */
  1070. public function testBlockSetObjectWithoutToString() {
  1071. $objectWithToString = new TestObjectWithoutToString();
  1072. $this->View->assign('testWithObjectWithoutToString', $objectWithToString);
  1073. }
  1074. /**
  1075. * Test setting a block's content to a decimal
  1076. *
  1077. * @return void
  1078. */
  1079. public function testBlockSetDecimal() {
  1080. $this->View->assign('testWithDecimal', 1.23456789);
  1081. $result = $this->View->fetch('testWithDecimal');
  1082. $this->assertEquals('1.23456789', $result);
  1083. }
  1084. /**
  1085. * Data provider for block related tests.
  1086. *
  1087. * @return array
  1088. */
  1089. public static function blockValueProvider() {
  1090. return array(
  1091. 'string' => array('A string value'),
  1092. 'null' => array(null),
  1093. 'decimal' => array(1.23456),
  1094. 'object with __toString' => array(new TestObjectWithToString()),
  1095. );
  1096. }
  1097. /**
  1098. * Test appending to a block with append.
  1099. *
  1100. * @param mixed $value Value
  1101. * @return void
  1102. * @dataProvider blockValueProvider
  1103. */
  1104. public function testBlockAppend($value) {
  1105. $this->View->assign('testBlock', 'Block');
  1106. $this->View->append('testBlock', $value);
  1107. $result = $this->View->fetch('testBlock');
  1108. $this->assertSame('Block' . $value, $result);
  1109. }
  1110. /**
  1111. * Test appending an object without __toString magic method to a block with append.
  1112. *
  1113. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1114. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1115. *
  1116. * @expectedException PHPUnit_Framework_Error
  1117. * @return void
  1118. */
  1119. public function testBlockAppendObjectWithoutToString() {
  1120. $object = new TestObjectWithoutToString();
  1121. $this->View->assign('testBlock', 'Block ');
  1122. $this->View->append('testBlock', $object);
  1123. }
  1124. /**
  1125. * Test prepending to a block with prepend.
  1126. *
  1127. * @param mixed $value Value
  1128. * @return void
  1129. * @dataProvider blockValueProvider
  1130. */
  1131. public function testBlockPrepend($value) {
  1132. $this->View->assign('test', 'Block');
  1133. $this->View->prepend('test', $value);
  1134. $result = $this->View->fetch('test');
  1135. $this->assertEquals($value . 'Block', $result);
  1136. }
  1137. /**
  1138. * Test prepending an object without __toString magic method to a block with prepend.
  1139. *
  1140. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1141. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1142. *
  1143. * @expectedException PHPUnit_Framework_Error
  1144. * @return void
  1145. */
  1146. public function testBlockPrependObjectWithoutToString() {
  1147. $object = new TestObjectWithoutToString();
  1148. $this->View->assign('test', 'Block ');
  1149. $this->View->prepend('test', $object);
  1150. }
  1151. /**
  1152. * You should be able to append to undefined blocks.
  1153. *
  1154. * @return void
  1155. */
  1156. public function testBlockAppendUndefined() {
  1157. $this->View->append('test', 'Unknown');
  1158. $result = $this->View->fetch('test');
  1159. $this->assertEquals('Unknown', $result);
  1160. }
  1161. /**
  1162. * You should be able to prepend to undefined blocks.
  1163. *
  1164. * @return void
  1165. */
  1166. public function testBlockPrependUndefined() {
  1167. $this->View->prepend('test', 'Unknown');
  1168. $result = $this->View->fetch('test');
  1169. $this->assertEquals('Unknown', $result);
  1170. }
  1171. /**
  1172. * Test getting block names
  1173. *
  1174. * @return void
  1175. */
  1176. public function testBlocks() {
  1177. $this->View->append('test', 'one');
  1178. $this->View->assign('test1', 'one');
  1179. $this->assertEquals(array('test', 'test1'), $this->View->blocks());
  1180. }
  1181. /**
  1182. * Test that blocks can be nested.
  1183. *
  1184. * @return void
  1185. */
  1186. public function testNestedBlocks() {
  1187. $this->View->start('first');
  1188. echo 'In first ';
  1189. $this->View->start('second');
  1190. echo 'In second';
  1191. $this->View->end();
  1192. echo 'In first';
  1193. $this->View->end();
  1194. $this->assertEquals('In first In first', $this->View->fetch('first'));
  1195. $this->assertEquals('In second', $this->View->fetch('second'));
  1196. }
  1197. /**
  1198. * Test that starting the same block twice throws an exception
  1199. *
  1200. * @expectedException \Cake\Error\Exception
  1201. * @return void
  1202. */
  1203. public function testStartBlocksTwice() {
  1204. $this->View->start('first');
  1205. $this->View->start('first');
  1206. }
  1207. /**
  1208. * Test that an exception gets thrown when you leave a block open at the end
  1209. * of a view.
  1210. *
  1211. * @expectedException \Cake\Error\Exception
  1212. * @return void
  1213. */
  1214. public function testExceptionOnOpenBlock() {
  1215. $this->View->render('open_block');
  1216. }
  1217. /**
  1218. * Test nested extended views.
  1219. *
  1220. * @return void
  1221. */
  1222. public function testExtendNested() {
  1223. $this->View->layout = false;
  1224. $content = $this->View->render('nested_extends');
  1225. $expected = <<<TEXT
  1226. This is the second parent.
  1227. This is the first parent.
  1228. This is the first template.
  1229. Sidebar Content.
  1230. TEXT;
  1231. $this->assertEquals($expected, $content);
  1232. }
  1233. /**
  1234. * Make sure that extending the current view with itself causes an exception
  1235. *
  1236. * @expectedException LogicException
  1237. * @return void
  1238. */
  1239. public function testExtendSelf() {
  1240. $this->View->layout = false;
  1241. $this->View->render('extend_self');
  1242. }
  1243. /**
  1244. * Make sure that extending in a loop causes an exception
  1245. *
  1246. * @expectedException LogicException
  1247. * @return void
  1248. */
  1249. public function testExtendLoop() {
  1250. $this->View->layout = false;
  1251. $this->View->render('extend_loop');
  1252. }
  1253. /**
  1254. * Test extend() in an element and a view.
  1255. *
  1256. * @return void
  1257. */
  1258. public function testExtendElement() {
  1259. $this->View->layout = false;
  1260. $content = $this->View->render('extend_element');
  1261. $expected = <<<TEXT
  1262. Parent View.
  1263. View content.
  1264. Parent Element.
  1265. Element content.
  1266. TEXT;
  1267. $this->assertEquals($expected, $content);
  1268. }
  1269. /**
  1270. * Extending an element which doesn't exist should throw a missing view exception
  1271. *
  1272. * @expectedException LogicException
  1273. * @return void
  1274. */
  1275. public function testExtendMissingElement() {
  1276. $this->View->layout = false;
  1277. $this->View->render('extend_missing_element');
  1278. }
  1279. /**
  1280. * Test extend() preceeded by an element()
  1281. *
  1282. * @return void
  1283. */
  1284. public function testExtendWithElementBeforeExtend() {
  1285. $this->View->layout = false;
  1286. $result = $this->View->render('extend_with_element');
  1287. $expected = <<<TEXT
  1288. Parent View.
  1289. this is the test elementThe view
  1290. TEXT;
  1291. $this->assertEquals($expected, $result);
  1292. }
  1293. /**
  1294. * Test memory leaks that existed in _paths at one point.
  1295. *
  1296. * @return void
  1297. */
  1298. public function testMemoryLeakInPaths() {
  1299. $this->ThemeController->plugin = null;
  1300. $this->ThemeController->name = 'Posts';
  1301. $this->ThemeController->viewPath = 'Posts';
  1302. $this->ThemeController->layout = 'whatever';
  1303. $this->ThemeController->theme = 'TestTheme';
  1304. $View = $this->ThemeController->createView();
  1305. $View->element('test_element');
  1306. $start = memory_get_usage();
  1307. for ($i = 0; $i < 10; $i++) {
  1308. $View->element('test_element');
  1309. }
  1310. $end = memory_get_usage();
  1311. $this->assertLessThanOrEqual($start + 5000, $end);
  1312. }
  1313. /**
  1314. * Tests that a view block uses default value when not assigned and uses assigned value when it is
  1315. *
  1316. * @return void
  1317. */
  1318. public function testBlockDefaultValue() {
  1319. $default = 'Default';
  1320. $result = $this->View->fetch('title', $default);
  1321. $this->assertEquals($default, $result);
  1322. $expected = 'My Title';
  1323. $this->View->assign('title', $expected);
  1324. $result = $this->View->fetch('title', $default);
  1325. $this->assertEquals($expected, $result);
  1326. }
  1327. /**
  1328. * Tests that a view variable uses default value when not assigned and uses assigned value when it is
  1329. *
  1330. * @return void
  1331. */
  1332. public function testViewVarDefaultValue() {
  1333. $default = 'Default';
  1334. $result = $this->View->get('title', $default);
  1335. $this->assertEquals($default, $result);
  1336. $expected = 'Back to the Future';
  1337. $this->View->set('title', $expected);
  1338. $result = $this->View->get('title', $default);
  1339. $this->assertEquals($expected, $result);
  1340. }
  1341. /**
  1342. * Test the helpers() method.
  1343. *
  1344. * @return void
  1345. */
  1346. public function testHelpers() {
  1347. $this->assertInstanceOf('Cake\View\HelperRegistry', $this->View->helpers());
  1348. $result = $this->View->helpers();
  1349. $this->assertSame($result, $this->View->helpers());
  1350. }
  1351. }