CellTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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->getMockBuilder('Cake\Network\Request')->getMock();
  39. $response = $this->getMockBuilder('Cake\Network\Response')->getMock();
  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. * Tests that using namespaced cells works.
  219. *
  220. * @return void
  221. */
  222. public function testNamespacedCell()
  223. {
  224. $cell = $this->View->cell('Admin/Menu');
  225. $this->assertContains('Admin Menu Cell', $cell->render());
  226. }
  227. /**
  228. * Tests that using namespaced cells in plugins works
  229. *
  230. * @return void
  231. */
  232. public function testPluginNamespacedCell()
  233. {
  234. $cell = $this->View->cell('TestPlugin.Admin/Menu');
  235. $this->assertContains('Test Plugin Admin Menu Cell', $cell->render());
  236. }
  237. /**
  238. * Test that plugin cells can render other view templates.
  239. *
  240. * @return void
  241. */
  242. public function testPluginCellAlternateTemplate()
  243. {
  244. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  245. $cell->template = '../../Element/translate';
  246. $this->assertContains('This is a translatable string', "{$cell}");
  247. }
  248. /**
  249. * Test that plugin cells can render other view templates.
  250. *
  251. * @return void
  252. */
  253. public function testPluginCellAlternateTemplateRenderParam()
  254. {
  255. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  256. $result = $cell->render('../../Element/translate');
  257. $this->assertContains('This is a translatable string', $result);
  258. }
  259. /**
  260. * Tests that using an unexisting cell throws an exception.
  261. *
  262. * @expectedException \Cake\View\Exception\MissingCellException
  263. * @return void
  264. */
  265. public function testUnexistingCell()
  266. {
  267. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  268. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  269. }
  270. /**
  271. * Tests missing method errors
  272. *
  273. * @expectedException \BadMethodCallException
  274. * @expectedExceptionMessage Class TestApp\View\Cell\ArticlesCell does not have a "nope" method.
  275. * @return void
  276. */
  277. public function testCellMissingMethod()
  278. {
  279. $cell = $this->View->cell('Articles::nope');
  280. $cell->render();
  281. }
  282. /**
  283. * Test that cell options are passed on.
  284. *
  285. * @return void
  286. */
  287. public function testCellOptions()
  288. {
  289. $cell = $this->View->cell('Articles', [], ['limit' => 10, 'nope' => 'nope']);
  290. $this->assertEquals(10, $cell->limit);
  291. $this->assertFalse(property_exists('nope', $cell), 'Not a valid option');
  292. }
  293. /**
  294. * Test that cells get the helper configuration from the view that created them.
  295. *
  296. * @return void
  297. */
  298. public function testCellInheritsHelperConfig()
  299. {
  300. $this->View->helpers = ['Url', 'Form', 'Banana'];
  301. $cell = $this->View->cell('Articles');
  302. $this->assertSame($this->View->helpers, $cell->helpers);
  303. }
  304. /**
  305. * Test that cells the view class name of a custom view passed on.
  306. *
  307. * @return void
  308. */
  309. public function testCellInheritsCustomViewClass()
  310. {
  311. $request = $this->getMockBuilder('Cake\Network\Request')->getMock();
  312. $response = $this->getMockBuilder('Cake\Network\Response')->getMock();
  313. $view = new CustomJsonView($request, $response);
  314. $cell = $view->cell('Articles');
  315. $this->assertSame('TestApp\View\CustomJsonView', $cell->viewClass);
  316. }
  317. /**
  318. * Test cached render.
  319. *
  320. * @return void
  321. */
  322. public function testCachedRenderSimple()
  323. {
  324. $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  325. $mock->method('init')
  326. ->will($this->returnValue(true));
  327. $mock->method('read')
  328. ->will($this->returnValue(false));
  329. $mock->expects($this->once())
  330. ->method('write')
  331. ->with('cell_test_app_view_cell_articles_cell_display', "dummy\n");
  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 read cached cell.
  340. *
  341. * @return void
  342. */
  343. public function testReadCachedCell()
  344. {
  345. $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  346. $mock->method('init')
  347. ->will($this->returnValue(true));
  348. $mock->method('read')
  349. ->will($this->returnValue("dummy\n"));
  350. $mock->expects($this->never())
  351. ->method('write');
  352. Cache::config('default', $mock);
  353. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  354. $result = $cell->render();
  355. $this->assertEquals("dummy\n", $result);
  356. Cache::drop('default');
  357. }
  358. /**
  359. * Test cached render array config
  360. *
  361. * @return void
  362. */
  363. public function testCachedRenderArrayConfig()
  364. {
  365. $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  366. $mock->method('init')
  367. ->will($this->returnValue(true));
  368. $mock->method('read')
  369. ->will($this->returnValue(false));
  370. $mock->expects($this->once())
  371. ->method('write')
  372. ->with('my_key', "dummy\n");
  373. Cache::config('cell', $mock);
  374. $cell = $this->View->cell('Articles', [], [
  375. 'cache' => ['key' => 'my_key', 'config' => 'cell']
  376. ]);
  377. $result = $cell->render();
  378. $this->assertEquals("dummy\n", $result);
  379. Cache::drop('cell');
  380. }
  381. /**
  382. * Test cached render when using an action changing the template used
  383. *
  384. * @return void
  385. */
  386. public function testCachedRenderSimpleCustomTemplate()
  387. {
  388. $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  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_customTemplate', "<h1>This is the alternate template</h1>\n");
  396. Cache::config('default', $mock);
  397. $cell = $this->View->cell('Articles::customTemplate', [], ['cache' => true]);
  398. $result = $cell->render();
  399. $this->assertContains('This is the alternate template', $result);
  400. Cache::drop('default');
  401. }
  402. /**
  403. * Test that when the cell cache is enabled, the cell action is only invoke the first
  404. * time the cell is rendered
  405. *
  406. * @return void
  407. */
  408. public function testCachedRenderSimpleCustomTemplateViewBuilder()
  409. {
  410. Cache::config('default', [
  411. 'className' => 'File',
  412. 'path' => CACHE,
  413. ]);
  414. $cell = $this->View->cell('Articles::customTemplateViewBuilder', [], ['cache' => ['key' => 'celltest']]);
  415. $result = $cell->render();
  416. $this->assertEquals(1, $cell->counter);
  417. $cell->render();
  418. $this->assertEquals(1, $cell->counter);
  419. $this->assertContains('This is the alternate template', $result);
  420. Cache::delete('celltest');
  421. Cache::drop('default');
  422. }
  423. }