CellTest.php 17 KB

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