CellTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Cache\Cache;
  17. use Cake\Core\Plugin;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\View\View;
  20. use TestApp\Controller\CellTraitTestController;
  21. use TestApp\View\CustomJsonView;
  22. /**
  23. * CellTest class.
  24. *
  25. * For testing both View\Cell & Utility\CellTrait
  26. */
  27. class CellTest extends TestCase
  28. {
  29. /**
  30. * @var \Cake\View\View
  31. */
  32. public $View;
  33. /**
  34. * setUp method
  35. *
  36. * @return void
  37. */
  38. public function setUp()
  39. {
  40. parent::setUp();
  41. static::setAppNamespace();
  42. Plugin::load(['TestPlugin', 'TestTheme']);
  43. $request = $this->getMockBuilder('Cake\Http\ServerRequest')->getMock();
  44. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  45. $this->View = new 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()->getTemplate());
  133. }
  134. /**
  135. * Tests that cell action setting the templatePath
  136. *
  137. * @return void
  138. */
  139. public function testSettingCellTemplatePathFromAction()
  140. {
  141. $appCell = $this->View->cell('Articles::customTemplatePath');
  142. $this->assertContains('Articles subdir custom_template_path template', "{$appCell}");
  143. $this->assertEquals('custom_template_path', $appCell->template);
  144. $this->assertEquals('Cell/Articles/Subdir', $appCell->viewBuilder()->getTemplatePath());
  145. }
  146. /**
  147. * Tests that cell action setting the template using the ViewBuilder renders the correct template
  148. *
  149. * @return void
  150. */
  151. public function testSettingCellTemplateFromActionViewBuilder()
  152. {
  153. $appCell = $this->View->cell('Articles::customTemplateViewBuilder');
  154. $this->assertContains('This is the alternate template', "{$appCell}");
  155. $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->getTemplate());
  156. }
  157. /**
  158. * Tests manual render() invocation.
  159. *
  160. * @return void
  161. */
  162. public function testCellManualRender()
  163. {
  164. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  165. $this->assertContains('dummy message', $cell->render());
  166. $cell->teaserList();
  167. $this->assertContains('<h2>Lorem ipsum</h2>', $cell->render('teaser_list'));
  168. }
  169. /**
  170. * Tests manual render() invocation with error
  171. *
  172. * @return void
  173. */
  174. public function testCellManualRenderError()
  175. {
  176. $this->expectException(\Cake\View\Exception\MissingCellViewException::class);
  177. $cell = $this->View->cell('Articles');
  178. $cell->render('derp');
  179. }
  180. /**
  181. * Test rendering a cell with a theme.
  182. *
  183. * @return void
  184. */
  185. public function testCellRenderThemed()
  186. {
  187. $this->View->theme = 'TestTheme';
  188. $cell = $this->View->cell('Articles', ['msg' => 'hello world!']);
  189. $this->assertEquals($this->View->theme, $cell->viewBuilder()->getTheme());
  190. $this->assertContains('Themed cell content.', $cell->render());
  191. $this->assertEquals($cell->View->theme, $cell->viewBuilder()->getTheme());
  192. }
  193. /**
  194. * Test that a cell can render a plugin view.
  195. *
  196. * @return void
  197. */
  198. public function testCellRenderPluginTemplate()
  199. {
  200. $cell = $this->View->cell('Articles');
  201. $this->assertContains(
  202. 'TestPlugin Articles/display',
  203. $cell->render('TestPlugin.display')
  204. );
  205. $cell = $this->View->cell('Articles');
  206. $cell->plugin = 'TestPlugin';
  207. $this->assertContains(
  208. 'TestPlugin Articles/display',
  209. $cell->render('display')
  210. );
  211. }
  212. /**
  213. * Tests that using plugin's cells works.
  214. *
  215. * @return void
  216. */
  217. public function testPluginCell()
  218. {
  219. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  220. $this->assertContains('hello world!', "{$cell}");
  221. }
  222. /**
  223. * Tests that using namespaced cells works.
  224. *
  225. * @return void
  226. */
  227. public function testNamespacedCell()
  228. {
  229. $cell = $this->View->cell('Admin/Menu');
  230. $this->assertContains('Admin Menu Cell', $cell->render());
  231. }
  232. /**
  233. * Tests that using namespaced cells in plugins works
  234. *
  235. * @return void
  236. */
  237. public function testPluginNamespacedCell()
  238. {
  239. $cell = $this->View->cell('TestPlugin.Admin/Menu');
  240. $this->assertContains('Test Plugin Admin Menu Cell', $cell->render());
  241. }
  242. /**
  243. * Test that plugin cells can render other view templates.
  244. *
  245. * @return void
  246. */
  247. public function testPluginCellAlternateTemplate()
  248. {
  249. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  250. $cell->template = '../../Element/translate';
  251. $this->assertContains('This is a translatable string', "{$cell}");
  252. }
  253. /**
  254. * Test that plugin cells can render other view templates.
  255. *
  256. * @return void
  257. */
  258. public function testPluginCellAlternateTemplateRenderParam()
  259. {
  260. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  261. $result = $cell->render('../../Element/translate');
  262. $this->assertContains('This is a translatable string', $result);
  263. }
  264. /**
  265. * Tests that using an non-existent cell throws an exception.
  266. *
  267. * @return void
  268. */
  269. public function testNonExistentCell()
  270. {
  271. $this->expectException(\Cake\View\Exception\MissingCellException::class);
  272. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  273. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  274. }
  275. /**
  276. * Tests missing method errors
  277. *
  278. * @return void
  279. */
  280. public function testCellMissingMethod()
  281. {
  282. $this->expectException(\BadMethodCallException::class);
  283. $this->expectExceptionMessage('Class TestApp\View\Cell\ArticlesCell does not have a "nope" method.');
  284. $cell = $this->View->cell('Articles::nope');
  285. $cell->render();
  286. }
  287. /**
  288. * Test that cell options are passed on.
  289. *
  290. * @return void
  291. */
  292. public function testCellOptions()
  293. {
  294. $cell = $this->View->cell('Articles', [], ['limit' => 10, 'nope' => 'nope']);
  295. $this->assertEquals(10, $cell->limit);
  296. $this->assertFalse(property_exists('nope', $cell), 'Not a valid option');
  297. }
  298. /**
  299. * Test that cells get the helper configuration from the view that created them.
  300. *
  301. * @return void
  302. */
  303. public function testCellInheritsHelperConfig()
  304. {
  305. $this->View->helpers = ['Url', 'Form', 'Banana'];
  306. $cell = $this->View->cell('Articles');
  307. $this->assertSame($this->View->helpers, $cell->helpers);
  308. }
  309. /**
  310. * Test that cells the view class name of a custom view passed on.
  311. *
  312. * @return void
  313. */
  314. public function testCellInheritsCustomViewClass()
  315. {
  316. $request = $this->getMockBuilder('Cake\Http\ServerRequest')->getMock();
  317. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  318. $view = new CustomJsonView($request, $response);
  319. $view->theme = 'Pretty';
  320. $cell = $view->cell('Articles');
  321. $this->assertSame('TestApp\View\CustomJsonView', $cell->viewClass);
  322. $this->assertSame('TestApp\View\CustomJsonView', $cell->viewBuilder()->getClassName());
  323. $this->assertSame('Pretty', $cell->viewBuilder()->getTheme());
  324. }
  325. /**
  326. * Test that cells the view class name of a controller passed on.
  327. *
  328. * @return void
  329. */
  330. public function testCellInheritsController()
  331. {
  332. $request = $this->getMockBuilder('Cake\Http\ServerRequest')->getMock();
  333. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  334. $controller = new CellTraitTestController($request, $response);
  335. $controller->viewBuilder()->setTheme('Pretty');
  336. $controller->viewClass = 'Json';
  337. $cell = $controller->cell('Articles');
  338. $this->assertSame('Json', $cell->viewClass);
  339. $this->assertSame('Json', $cell->viewBuilder()->getClassName());
  340. $this->assertSame('Pretty', $cell->viewBuilder()->getTheme());
  341. }
  342. /**
  343. * Test cached render.
  344. *
  345. * @return void
  346. */
  347. public function testCachedRenderSimple()
  348. {
  349. $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  350. $mock->method('init')
  351. ->will($this->returnValue(true));
  352. $mock->method('read')
  353. ->will($this->returnValue(false));
  354. $mock->expects($this->once())
  355. ->method('write')
  356. ->with('cell_test_app_view_cell_articles_cell_display_default', "dummy\n");
  357. Cache::config('default', $mock);
  358. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  359. $result = $cell->render();
  360. $this->assertEquals("dummy\n", $result);
  361. Cache::drop('default');
  362. }
  363. /**
  364. * Test read cached cell.
  365. *
  366. * @return void
  367. */
  368. public function testReadCachedCell()
  369. {
  370. $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  371. $mock->method('init')
  372. ->will($this->returnValue(true));
  373. $mock->method('read')
  374. ->will($this->returnValue("dummy\n"));
  375. $mock->expects($this->never())
  376. ->method('write');
  377. Cache::config('default', $mock);
  378. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  379. $result = $cell->render();
  380. $this->assertEquals("dummy\n", $result);
  381. Cache::drop('default');
  382. }
  383. /**
  384. * Test cached render array config
  385. *
  386. * @return void
  387. */
  388. public function testCachedRenderArrayConfig()
  389. {
  390. $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  391. $mock->method('init')
  392. ->will($this->returnValue(true));
  393. $mock->method('read')
  394. ->will($this->returnValue(false));
  395. $mock->expects($this->once())
  396. ->method('write')
  397. ->with('my_key', "dummy\n");
  398. Cache::config('cell', $mock);
  399. $cell = $this->View->cell('Articles', [], [
  400. 'cache' => ['key' => 'my_key', 'config' => 'cell']
  401. ]);
  402. $result = $cell->render();
  403. $this->assertEquals("dummy\n", $result);
  404. Cache::drop('cell');
  405. }
  406. /**
  407. * Test cached render when using an action changing the template used
  408. *
  409. * @return void
  410. */
  411. public function testCachedRenderSimpleCustomTemplate()
  412. {
  413. $mock = $this->getMockBuilder('Cake\Cache\CacheEngine')->getMock();
  414. $mock->method('init')
  415. ->will($this->returnValue(true));
  416. $mock->method('read')
  417. ->will($this->returnValue(false));
  418. $mock->expects($this->once())
  419. ->method('write')
  420. ->with('cell_test_app_view_cell_articles_cell_customTemplate_default', "<h1>This is the alternate template</h1>\n");
  421. Cache::config('default', $mock);
  422. $cell = $this->View->cell('Articles::customTemplate', [], ['cache' => true]);
  423. $result = $cell->render();
  424. $this->assertContains('This is the alternate template', $result);
  425. Cache::drop('default');
  426. }
  427. /**
  428. * Test that when the cell cache is enabled, the cell action is only invoke the first
  429. * time the cell is rendered
  430. *
  431. * @return void
  432. */
  433. public function testCachedRenderSimpleCustomTemplateViewBuilder()
  434. {
  435. Cache::config('default', [
  436. 'className' => 'File',
  437. 'path' => CACHE,
  438. ]);
  439. $cell = $this->View->cell('Articles::customTemplateViewBuilder', [], ['cache' => ['key' => 'celltest']]);
  440. $result = $cell->render();
  441. $this->assertEquals(1, $cell->counter);
  442. $cell->render();
  443. $this->assertEquals(1, $cell->counter);
  444. $this->assertContains('This is the alternate template', $result);
  445. Cache::delete('celltest');
  446. Cache::drop('default');
  447. }
  448. /**
  449. * Test that when the cell cache is enabled, the cell action is only invoke the first
  450. * time the cell is rendered
  451. *
  452. * @return void
  453. */
  454. public function testACachedViewCellReRendersWhenGivenADifferentTemplate()
  455. {
  456. Cache::config('default', [
  457. 'className' => 'File',
  458. 'path' => CACHE,
  459. ]);
  460. $cell = $this->View->cell('Articles::customTemplateViewBuilder', [], ['cache' => true]);
  461. $result = $cell->render('alternate_teaser_list');
  462. $result2 = $cell->render('not_the_alternate_teaser_list');
  463. $this->assertContains('This is the alternate template', $result);
  464. $this->assertContains('This is NOT the alternate template', $result2);
  465. Cache::delete('celltest');
  466. Cache::drop('default');
  467. }
  468. }