CellTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View;
  17. use Cake\Cache\Cache;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\View\Cell;
  20. use Cake\View\Exception\MissingCellTemplateException;
  21. use Cake\View\Exception\MissingTemplateException;
  22. use Cake\View\View;
  23. use TestApp\Controller\CellTraitTestController;
  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. * @var \Cake\View\View
  34. */
  35. public $View;
  36. /**
  37. * setUp method
  38. *
  39. * @return void
  40. */
  41. public function setUp()
  42. {
  43. parent::setUp();
  44. static::setAppNamespace();
  45. $this->loadPlugins(['TestPlugin', 'TestTheme']);
  46. $request = $this->getMockBuilder('Cake\Http\ServerRequest')->getMock();
  47. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  48. $this->View = new View($request, $response);
  49. }
  50. /**
  51. * tearDown method
  52. *
  53. * @return void
  54. */
  55. public function tearDown()
  56. {
  57. parent::tearDown();
  58. $this->clearPlugins();
  59. unset($this->View);
  60. }
  61. /**
  62. * Tests basic cell rendering.
  63. *
  64. * @return void
  65. */
  66. public function testCellRender()
  67. {
  68. $cell = $this->View->cell('Articles::teaserList');
  69. $render = "{$cell}";
  70. $this->assertEquals('teaser_list', $cell->viewBuilder()->getTemplate());
  71. $this->assertContains('<h2>Lorem ipsum</h2>', $render);
  72. $this->assertContains('<h2>Usectetur adipiscing eli</h2>', $render);
  73. $this->assertContains('<h2>Topis semper blandit eu non</h2>', $render);
  74. $this->assertContains('<h2>Suspendisse gravida neque</h2>', $render);
  75. $cell = $this->View->cell('Cello');
  76. $this->assertInstanceOf('TestApp\View\Cell\CelloCell', $cell);
  77. $this->assertEquals("Cellos\n", $cell->render());
  78. }
  79. /**
  80. * Tests debug output.
  81. *
  82. * @return void
  83. */
  84. public function testDebugInfo()
  85. {
  86. $cell = $this->View->cell('Articles::teaserList');
  87. $data = $cell->__debugInfo();
  88. $this->assertArrayHasKey('request', $data);
  89. $this->assertArrayHasKey('response', $data);
  90. $this->assertEquals('teaserList', $data['action']);
  91. $this->assertEquals([], $data['args']);
  92. }
  93. /**
  94. * Test __toString() hitting an error when rendering views.
  95. *
  96. * @return void
  97. */
  98. public function testCellImplictRenderWithError()
  99. {
  100. $capture = function ($errno, $msg) {
  101. restore_error_handler();
  102. $this->assertEquals(E_USER_WARNING, $errno);
  103. $this->assertContains('Could not render cell - Cell view file', $msg);
  104. };
  105. set_error_handler($capture);
  106. $cell = $this->View->cell('Articles::teaserList');
  107. $cell->viewBuilder()->setTemplate('nope');
  108. $result = "{$cell}";
  109. }
  110. /**
  111. * Tests that we are able pass multiple arguments to cell methods.
  112. *
  113. * This test sets its own error handler, as PHPUnit won't convert
  114. * errors into exceptions when the caller is a __toString() method.
  115. *
  116. * @return void
  117. */
  118. public function testCellWithArguments()
  119. {
  120. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  121. $render = "{$cell}";
  122. $this->assertContains('dummy message', $render);
  123. }
  124. /**
  125. * Tests that cell runs default action when none is provided.
  126. *
  127. * @return void
  128. */
  129. public function testDefaultCellAction()
  130. {
  131. $appCell = $this->View->cell('Articles');
  132. $this->assertEquals('display', $appCell->viewBuilder()->getTemplate());
  133. $this->assertContains('dummy', "{$appCell}");
  134. $pluginCell = $this->View->cell('TestPlugin.Dummy');
  135. $this->assertContains('dummy', "{$pluginCell}");
  136. $this->assertEquals('display', $pluginCell->viewBuilder()->getTemplate());
  137. }
  138. /**
  139. * Tests that cell action setting the templatePath
  140. *
  141. * @return void
  142. */
  143. public function testSettingCellTemplatePathFromAction()
  144. {
  145. $appCell = $this->View->cell('Articles::customTemplatePath');
  146. $this->assertContains('Articles subdir custom_template_path template', "{$appCell}");
  147. $this->assertEquals('custom_template_path', $appCell->viewBuilder()->getTemplate());
  148. $this->assertEquals(Cell::TEMPLATE_FOLDER . '/Articles/Subdir', $appCell->viewBuilder()->getTemplatePath());
  149. }
  150. /**
  151. * Tests that cell action setting the template using the ViewBuilder renders the correct template
  152. *
  153. * @return void
  154. */
  155. public function testSettingCellTemplateFromActionViewBuilder()
  156. {
  157. $appCell = $this->View->cell('Articles::customTemplateViewBuilder');
  158. $this->assertContains('This is the alternate template', "{$appCell}");
  159. $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->getTemplate());
  160. }
  161. /**
  162. * Tests manual render() invocation.
  163. *
  164. * @return void
  165. */
  166. public function testCellManualRender()
  167. {
  168. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  169. $this->assertContains('dummy message', $cell->render());
  170. $cell->teaserList();
  171. $this->assertContains('<h2>Lorem ipsum</h2>', $cell->render('teaser_list'));
  172. }
  173. /**
  174. * Tests manual render() invocation with error
  175. *
  176. * @return void
  177. */
  178. public function testCellManualRenderError()
  179. {
  180. $cell = $this->View->cell('Articles');
  181. $e = null;
  182. try {
  183. $cell->render('derp');
  184. } catch (MissingCellTemplateException $e) {
  185. }
  186. $this->assertNotNull($e);
  187. $message = $e->getMessage();
  188. $this->assertContains("Cell view file 'derp' could not be found.", $message);
  189. $this->assertContains('The following paths', $message);
  190. $this->assertContains(ROOT . DS . 'templates', $message);
  191. $this->assertInstanceOf(MissingTemplateException::class, $e->getPrevious());
  192. }
  193. /**
  194. * Test rendering a cell with a theme.
  195. *
  196. * @return void
  197. */
  198. public function testCellRenderThemed()
  199. {
  200. $this->View->setTheme('TestTheme');
  201. $cell = $this->View->cell('Articles', ['msg' => 'hello world!']);
  202. $this->assertEquals($this->View->getTheme(), $cell->viewBuilder()->getTheme());
  203. $this->assertContains('Themed cell content.', $cell->render());
  204. }
  205. /**
  206. * Test that a cell can render a plugin view.
  207. *
  208. * @return void
  209. */
  210. public function testCellRenderPluginTemplate()
  211. {
  212. $cell = $this->View->cell('Articles');
  213. $this->assertContains(
  214. 'TestPlugin Articles/display',
  215. $cell->render('TestPlugin.display')
  216. );
  217. $cell = $this->View->cell('Articles');
  218. $cell->viewBuilder()->setPlugin('TestPlugin');
  219. $this->assertContains(
  220. 'TestPlugin Articles/display',
  221. $cell->render('display')
  222. );
  223. }
  224. /**
  225. * Tests that using plugin's cells works.
  226. *
  227. * @return void
  228. */
  229. public function testPluginCell()
  230. {
  231. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  232. $this->assertContains('hello world!', "{$cell}");
  233. }
  234. /**
  235. * Tests that using namespaced cells works.
  236. *
  237. * @return void
  238. */
  239. public function testNamespacedCell()
  240. {
  241. $cell = $this->View->cell('Admin/Menu');
  242. $this->assertContains('Admin Menu Cell', $cell->render());
  243. }
  244. /**
  245. * Tests that using namespaced cells in plugins works
  246. *
  247. * @return void
  248. */
  249. public function testPluginNamespacedCell()
  250. {
  251. $cell = $this->View->cell('TestPlugin.Admin/Menu');
  252. $this->assertContains('Test Plugin Admin Menu Cell', $cell->render());
  253. }
  254. /**
  255. * Test that plugin cells can render other view templates.
  256. *
  257. * @return void
  258. */
  259. public function testPluginCellAlternateTemplate()
  260. {
  261. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  262. $cell->viewBuilder()->setTemplate('../../element/translate');
  263. $this->assertContains('This is a translatable string', "{$cell}");
  264. }
  265. /**
  266. * Test that plugin cells can render other view templates.
  267. *
  268. * @return void
  269. */
  270. public function testPluginCellAlternateTemplateRenderParam()
  271. {
  272. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  273. $result = $cell->render('../../element/translate');
  274. $this->assertContains('This is a translatable string', $result);
  275. }
  276. /**
  277. * Tests that using an non-existent cell throws an exception.
  278. *
  279. * @return void
  280. */
  281. public function testNonExistentCell()
  282. {
  283. $this->expectException(\Cake\View\Exception\MissingCellException::class);
  284. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  285. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  286. }
  287. /**
  288. * Tests missing method errors
  289. *
  290. * @return void
  291. */
  292. public function testCellMissingMethod()
  293. {
  294. $this->expectException(\BadMethodCallException::class);
  295. $this->expectExceptionMessage('Class TestApp\View\Cell\ArticlesCell does not have a "nope" method.');
  296. $cell = $this->View->cell('Articles::nope');
  297. $cell->render();
  298. }
  299. /**
  300. * Test that cell options are passed on.
  301. *
  302. * @return void
  303. */
  304. public function testCellOptions()
  305. {
  306. $cell = $this->View->cell('Articles', [], ['limit' => 10, 'nope' => 'nope']);
  307. $this->assertEquals(10, $cell->limit);
  308. $this->assertObjectNotHasAttribute('nope', $cell, 'Not a valid option');
  309. }
  310. /**
  311. * Test that cells get the helper configuration from the view that created them.
  312. *
  313. * @return void
  314. */
  315. public function testCellInheritsHelperConfig()
  316. {
  317. $request = $this->getMockBuilder('Cake\Http\ServerRequest')->getMock();
  318. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  319. $helpers = ['Url', 'Form', 'Banana'];
  320. $view = new View($request, $response, null, ['helpers' => $helpers]);
  321. $cell = $view->cell('Articles');
  322. $this->assertSame($helpers, $cell->viewBuilder()->getHelpers());
  323. }
  324. /**
  325. * Test that cells the view class name of a custom view passed on.
  326. *
  327. * @return void
  328. */
  329. public function testCellInheritsCustomViewClass()
  330. {
  331. $request = $this->getMockBuilder('Cake\Http\ServerRequest')->getMock();
  332. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  333. $view = new CustomJsonView($request, $response);
  334. $view->setTheme('Pretty');
  335. $cell = $view->cell('Articles');
  336. $this->assertSame('TestApp\View\CustomJsonView', $cell->viewClass);
  337. $this->assertSame('TestApp\View\CustomJsonView', $cell->viewBuilder()->getClassName());
  338. $this->assertSame('Pretty', $cell->viewBuilder()->getTheme());
  339. }
  340. /**
  341. * Test that cells the view class name of a controller passed on.
  342. *
  343. * @return void
  344. */
  345. public function testCellInheritsController()
  346. {
  347. $request = $this->getMockBuilder('Cake\Http\ServerRequest')->getMock();
  348. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  349. $controller = new CellTraitTestController($request, $response);
  350. $controller->viewBuilder()->setTheme('Pretty');
  351. $controller->viewClass = 'Json';
  352. $cell = $controller->cell('Articles');
  353. $this->assertSame('Json', $cell->viewClass);
  354. $this->assertSame('Json', $cell->viewBuilder()->getClassName());
  355. $this->assertSame('Pretty', $cell->viewBuilder()->getTheme());
  356. }
  357. /**
  358. * Test cached render.
  359. *
  360. * @return void
  361. */
  362. public function testCachedRenderSimple()
  363. {
  364. $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  365. $mock->method('init')
  366. ->will($this->returnValue(true));
  367. $mock->method('get')
  368. ->will($this->returnValue(null));
  369. $mock->expects($this->once())
  370. ->method('set')
  371. ->with('cell_test_app_view_cell_articles_cell_display_default', "dummy\n")
  372. ->will($this->returnValue(true));
  373. Cache::setConfig('default', $mock);
  374. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  375. $result = $cell->render();
  376. $this->assertEquals("dummy\n", $result);
  377. Cache::drop('default');
  378. }
  379. /**
  380. * Test read cached cell.
  381. *
  382. * @return void
  383. */
  384. public function testReadCachedCell()
  385. {
  386. $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  387. $mock->method('init')
  388. ->will($this->returnValue(true));
  389. $mock->method('get')
  390. ->will($this->returnValue("dummy\n"));
  391. $mock->expects($this->never())
  392. ->method('set');
  393. Cache::setConfig('default', $mock);
  394. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  395. $result = $cell->render();
  396. $this->assertEquals("dummy\n", $result);
  397. Cache::drop('default');
  398. }
  399. /**
  400. * Test cached render array config
  401. *
  402. * @return void
  403. */
  404. public function testCachedRenderArrayConfig()
  405. {
  406. $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  407. $mock->method('init')
  408. ->will($this->returnValue(true));
  409. $mock->method('get')
  410. ->will($this->returnValue(null));
  411. $mock->expects($this->once())
  412. ->method('set')
  413. ->with('my_key', "dummy\n")
  414. ->will($this->returnValue(true));
  415. Cache::setConfig('cell', $mock);
  416. $cell = $this->View->cell('Articles', [], [
  417. 'cache' => ['key' => 'my_key', 'config' => 'cell'],
  418. ]);
  419. $result = $cell->render();
  420. $this->assertEquals("dummy\n", $result);
  421. Cache::drop('cell');
  422. }
  423. /**
  424. * Test cached render when using an action changing the template used
  425. *
  426. * @return void
  427. */
  428. public function testCachedRenderSimpleCustomTemplate()
  429. {
  430. $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  431. $mock->method('init')
  432. ->will($this->returnValue(true));
  433. $mock->method('get')
  434. ->will($this->returnValue(null));
  435. $mock->expects($this->once())
  436. ->method('set')
  437. ->with('cell_test_app_view_cell_articles_cell_customTemplateViewBuilder_default', "<h1>This is the alternate template</h1>\n")
  438. ->will($this->returnValue(true));
  439. Cache::setConfig('default', $mock);
  440. $cell = $this->View->cell('Articles::customTemplateViewBuilder', [], ['cache' => true]);
  441. $result = $cell->render();
  442. $this->assertContains('This is the alternate template', $result);
  443. Cache::drop('default');
  444. }
  445. /**
  446. * Test that when the cell cache is enabled, the cell action is only invoke the first
  447. * time the cell is rendered
  448. *
  449. * @return void
  450. */
  451. public function testCachedRenderSimpleCustomTemplateViewBuilder()
  452. {
  453. Cache::setConfig('default', [
  454. 'className' => 'File',
  455. 'path' => CACHE,
  456. ]);
  457. $cell = $this->View->cell('Articles::customTemplateViewBuilder', [], ['cache' => ['key' => 'celltest']]);
  458. $result = $cell->render();
  459. $this->assertEquals(1, $cell->counter);
  460. $cell->render();
  461. $this->assertEquals(1, $cell->counter);
  462. $this->assertContains('This is the alternate template', $result);
  463. Cache::delete('celltest');
  464. Cache::drop('default');
  465. }
  466. /**
  467. * Test that when the cell cache is enabled, the cell action is only invoke the first
  468. * time the cell is rendered
  469. *
  470. * @return void
  471. */
  472. public function testACachedViewCellReRendersWhenGivenADifferentTemplate()
  473. {
  474. Cache::setConfig('default', [
  475. 'className' => 'File',
  476. 'path' => CACHE,
  477. ]);
  478. $cell = $this->View->cell('Articles::customTemplateViewBuilder', [], ['cache' => true]);
  479. $result = $cell->render('alternate_teaser_list');
  480. $result2 = $cell->render('not_the_alternate_teaser_list');
  481. $this->assertContains('This is the alternate template', $result);
  482. $this->assertContains('This is NOT the alternate template', $result2);
  483. Cache::delete('celltest');
  484. Cache::drop('default');
  485. }
  486. }