CellTest.php 14 KB

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