CellTest.php 16 KB

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