CellTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.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\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Event\EventManager;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\View\Cell;
  23. use Cake\View\CellTrait;
  24. use TestApp\View\CustomJsonView;
  25. /**
  26. * CellTest class.
  27. *
  28. * For testing both View\Cell & Utility\CellTrait
  29. */
  30. class CellTest extends TestCase
  31. {
  32. /**
  33. * setUp method
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. parent::setUp();
  40. Configure::write('App.namespace', 'TestApp');
  41. Plugin::load(['TestPlugin', 'TestTheme']);
  42. $request = $this->getMock('Cake\Network\Request');
  43. $response = $this->getMock('Cake\Network\Response');
  44. $this->View = new \Cake\View\View($request, $response);
  45. }
  46. /**
  47. * tearDown method
  48. *
  49. * @return void
  50. */
  51. public function tearDown()
  52. {
  53. parent::tearDown();
  54. Plugin::unload('TestPlugin');
  55. Plugin::unload('TestTheme');
  56. unset($this->View);
  57. }
  58. /**
  59. * Tests basic cell rendering.
  60. *
  61. * @return void
  62. */
  63. public function testCellRender()
  64. {
  65. $cell = $this->View->cell('Articles::teaserList');
  66. $render = "{$cell}";
  67. $this->assertEquals('teaser_list', $cell->template);
  68. $this->assertContains('<h2>Lorem ipsum</h2>', $render);
  69. $this->assertContains('<h2>Usectetur adipiscing eli</h2>', $render);
  70. $this->assertContains('<h2>Topis semper blandit eu non</h2>', $render);
  71. $this->assertContains('<h2>Suspendisse gravida neque</h2>', $render);
  72. $cell = $this->View->cell('Cello');
  73. $this->assertInstanceOf('TestApp\View\Cell\CelloCell', $cell);
  74. $this->assertEquals("Cellos\n", $cell->render());
  75. }
  76. /**
  77. * Test __toString() hitting an error when rendering views.
  78. *
  79. * @return void
  80. */
  81. public function testCellImplictRenderWithError()
  82. {
  83. $capture = function ($errno, $msg) {
  84. restore_error_handler();
  85. $this->assertEquals(E_USER_WARNING, $errno);
  86. $this->assertContains('Could not render cell - Cell view file', $msg);
  87. };
  88. set_error_handler($capture);
  89. $cell = $this->View->cell('Articles::teaserList');
  90. $cell->template = 'nope';
  91. $result = "{$cell}";
  92. }
  93. /**
  94. * Tests that we are able pass multiple arguments to cell methods.
  95. *
  96. * This test sets its own error handler, as PHPUnit won't convert
  97. * errors into exceptions when the caller is a __toString() method.
  98. *
  99. * @return void
  100. */
  101. public function testCellWithArguments()
  102. {
  103. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  104. $render = "{$cell}";
  105. $this->assertContains('dummy message', $render);
  106. }
  107. /**
  108. * Tests that cell runs default action when none is provided.
  109. *
  110. * @return void
  111. */
  112. public function testDefaultCellAction()
  113. {
  114. $appCell = $this->View->cell('Articles');
  115. $this->assertEquals('display', $appCell->template);
  116. $this->assertContains('dummy', "{$appCell}");
  117. $pluginCell = $this->View->cell('TestPlugin.Dummy');
  118. $this->assertContains('dummy', "{$pluginCell}");
  119. $this->assertEquals('display', $pluginCell->template);
  120. }
  121. /**
  122. * Tests that cell action setting the template using the property renders the correct template
  123. *
  124. * @return void
  125. */
  126. public function testSettingCellTemplateFromAction()
  127. {
  128. $appCell = $this->View->cell('Articles::customTemplate');
  129. $this->assertContains('This is the alternate template', "{$appCell}");
  130. $this->assertEquals('alternate_teaser_list', $appCell->template);
  131. $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template());
  132. $appCell = $this->View->cell('Articles::customTemplate');
  133. $this->assertContains('This is the alternate template', $appCell->render());
  134. $this->assertEquals('alternate_teaser_list', $appCell->template);
  135. $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template());
  136. }
  137. /**
  138. * Tests that cell action setting the template using the ViewBuilder renders the correct template
  139. *
  140. * @return void
  141. */
  142. public function testSettingCellTemplateFromActionViewBuilder()
  143. {
  144. $appCell = $this->View->cell('Articles::customTemplateViewBuilder');
  145. $this->assertContains('This is the alternate template', "{$appCell}");
  146. $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template());
  147. $appCell = $this->View->cell('Articles::customTemplateViewBuilder');
  148. $this->assertContains('This is the alternate template', $appCell->render());
  149. $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template());
  150. }
  151. /**
  152. * Tests manual render() invocation.
  153. *
  154. * @return void
  155. */
  156. public function testCellManualRender()
  157. {
  158. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  159. $this->assertContains('dummy message', $cell->render());
  160. $cell->teaserList();
  161. $this->assertContains('<h2>Lorem ipsum</h2>', $cell->render('teaser_list'));
  162. }
  163. /**
  164. * Tests manual render() invocation with error
  165. *
  166. * @expectedException \Cake\View\Exception\MissingCellViewException
  167. * @return void
  168. */
  169. public function testCellManualRenderError()
  170. {
  171. $cell = $this->View->cell('Articles');
  172. $cell->render('derp');
  173. }
  174. /**
  175. * Test rendering a cell with a theme.
  176. *
  177. * @return void
  178. */
  179. public function testCellRenderThemed()
  180. {
  181. $this->View->theme = 'TestTheme';
  182. $cell = $this->View->cell('Articles', ['msg' => 'hello world!']);
  183. $this->assertEquals($this->View->theme, $cell->viewBuilder()->theme());
  184. $this->assertContains('Themed cell content.', $cell->render());
  185. $this->assertEquals($cell->View->theme, $cell->viewBuilder()->theme());
  186. }
  187. /**
  188. * Test that a cell can render a plugin view.
  189. *
  190. * @return void
  191. */
  192. public function testCellRenderPluginTemplate()
  193. {
  194. $cell = $this->View->cell('Articles');
  195. $this->assertContains(
  196. 'TestPlugin Articles/display',
  197. $cell->render('TestPlugin.display')
  198. );
  199. $cell = $this->View->cell('Articles');
  200. $cell->plugin = 'TestPlugin';
  201. $this->assertContains(
  202. 'TestPlugin Articles/display',
  203. $cell->render('display')
  204. );
  205. }
  206. /**
  207. * Tests that using plugin's cells works.
  208. *
  209. * @return void
  210. */
  211. public function testPluginCell()
  212. {
  213. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  214. $this->assertContains('hello world!', "{$cell}");
  215. }
  216. /**
  217. * Test that plugin cells can render other view templates.
  218. *
  219. * @return void
  220. */
  221. public function testPluginCellAlternateTemplate()
  222. {
  223. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  224. $cell->template = '../../Element/translate';
  225. $this->assertContains('This is a translatable string', "{$cell}");
  226. }
  227. /**
  228. * Test that plugin cells can render other view templates.
  229. *
  230. * @return void
  231. */
  232. public function testPluginCellAlternateTemplateRenderParam()
  233. {
  234. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  235. $result = $cell->render('../../Element/translate');
  236. $this->assertContains('This is a translatable string', $result);
  237. }
  238. /**
  239. * Tests that using an unexisting cell throws an exception.
  240. *
  241. * @expectedException \Cake\View\Exception\MissingCellException
  242. * @return void
  243. */
  244. public function testUnexistingCell()
  245. {
  246. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  247. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  248. }
  249. /**
  250. * Tests missing method errors
  251. *
  252. * @expectedException \BadMethodCallException
  253. * @expectedExceptionMessage Class TestApp\View\Cell\ArticlesCell does not have a "nope" method.
  254. * @return void
  255. */
  256. public function testCellMissingMethod()
  257. {
  258. $cell = $this->View->cell('Articles::nope');
  259. $cell->render();
  260. }
  261. /**
  262. * Test that cell options are passed on.
  263. *
  264. * @return void
  265. */
  266. public function testCellOptions()
  267. {
  268. $cell = $this->View->cell('Articles', [], ['limit' => 10, 'nope' => 'nope']);
  269. $this->assertEquals(10, $cell->limit);
  270. $this->assertFalse(property_exists('nope', $cell), 'Not a valid option');
  271. }
  272. /**
  273. * Test that cells get the helper configuration from the view that created them.
  274. *
  275. * @return void
  276. */
  277. public function testCellInheritsHelperConfig()
  278. {
  279. $this->View->helpers = ['Url', 'Form', 'Banana'];
  280. $cell = $this->View->cell('Articles');
  281. $this->assertSame($this->View->helpers, $cell->helpers);
  282. }
  283. /**
  284. * Test that cells the view class name of a custom view passed on.
  285. *
  286. * @return void
  287. */
  288. public function testCellInheritsCustomViewClass()
  289. {
  290. $request = $this->getMock('Cake\Network\Request');
  291. $response = $this->getMock('Cake\Network\Response');
  292. $view = new CustomJsonView($request, $response);
  293. $cell = $view->cell('Articles');
  294. $this->assertSame('TestApp\View\CustomJsonView', $cell->viewClass);
  295. }
  296. /**
  297. * Test cached render.
  298. *
  299. * @return void
  300. */
  301. public function testCachedRenderSimple()
  302. {
  303. $mock = $this->getMock('Cake\Cache\CacheEngine');
  304. $mock->method('init')
  305. ->will($this->returnValue(true));
  306. $mock->method('read')
  307. ->will($this->returnValue(false));
  308. $mock->expects($this->once())
  309. ->method('write')
  310. ->with('cell_test_app_view_cell_articles_cell_display', "dummy\n");
  311. Cache::config('default', $mock);
  312. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  313. $result = $cell->render();
  314. $this->assertEquals("dummy\n", $result);
  315. Cache::drop('default');
  316. }
  317. /**
  318. * Test read cached cell.
  319. *
  320. * @return void
  321. */
  322. public function testReadCachedCell()
  323. {
  324. $mock = $this->getMock('Cake\Cache\CacheEngine');
  325. $mock->method('init')
  326. ->will($this->returnValue(true));
  327. $mock->method('read')
  328. ->will($this->returnValue("dummy\n"));
  329. $mock->expects($this->never())
  330. ->method('write');
  331. Cache::config('default', $mock);
  332. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  333. $result = $cell->render();
  334. $this->assertEquals("dummy\n", $result);
  335. Cache::drop('default');
  336. }
  337. /**
  338. * Test cached render array config
  339. *
  340. * @return void
  341. */
  342. public function testCachedRenderArrayConfig()
  343. {
  344. $mock = $this->getMock('Cake\Cache\CacheEngine');
  345. $mock->method('init')
  346. ->will($this->returnValue(true));
  347. $mock->method('read')
  348. ->will($this->returnValue(false));
  349. $mock->expects($this->once())
  350. ->method('write')
  351. ->with('my_key', "dummy\n");
  352. Cache::config('cell', $mock);
  353. $cell = $this->View->cell('Articles', [], [
  354. 'cache' => ['key' => 'my_key', 'config' => 'cell']
  355. ]);
  356. $result = $cell->render();
  357. $this->assertEquals("dummy\n", $result);
  358. Cache::drop('cell');
  359. }
  360. /**
  361. * Test cached render.
  362. *
  363. * @return void
  364. */
  365. public function testCachedRenderSimpleCustomTemplate()
  366. {
  367. $mock = $this->getMock('Cake\Cache\CacheEngine');
  368. $mock->method('init')
  369. ->will($this->returnValue(true));
  370. $mock->method('read')
  371. ->will($this->returnValue(false));
  372. $mock->expects($this->once())
  373. ->method('write')
  374. ->with('cell_test_app_view_cell_articles_cell_customTemplate', "<h1>This is the alternate template</h1>\n");
  375. Cache::config('default', $mock);
  376. $cell = $this->View->cell('Articles::customTemplate', [], ['cache' => true]);
  377. $result = $cell->render();
  378. $this->assertContains('This is the alternate template', $result);
  379. Cache::drop('default');
  380. }
  381. /**
  382. * Test cached render.
  383. *
  384. * @return void
  385. */
  386. public function testCachedRenderSimpleCustomTemplateViewBuilder()
  387. {
  388. $mock = $this->getMock('Cake\Cache\CacheEngine');
  389. $mock->method('init')
  390. ->will($this->returnValue(true));
  391. $mock->method('read')
  392. ->will($this->returnValue(false));
  393. $mock->expects($this->once())
  394. ->method('write')
  395. ->with('cell_test_app_view_cell_articles_cell_customTemplateViewBuilder', "<h1>This is the alternate template</h1>\n");
  396. Cache::config('default', $mock);
  397. $cell = $this->View->cell('Articles::customTemplateViewBuilder', [], ['cache' => true]);
  398. $result = $cell->render();
  399. $this->assertContains('This is the alternate template', $result);
  400. Cache::drop('default');
  401. }
  402. }