CellTest.php 13 KB

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