CellTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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\Controller\Controller;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Event\EventManager;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\View\Cell;
  23. use Cake\View\CellTrait;
  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. * setUp method
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. parent::setUp();
  40. Configure::write('App.namespace', 'TestApp');
  41. Plugin::load(['TestPlugin', 'TestTheme']);
  42. $request = $this->getMock('Cake\Network\Request');
  43. $response = $this->getMock('Cake\Network\Response');
  44. $this->View = new \Cake\View\View($request, $response);
  45. }
  46. /**
  47. * tearDown method
  48. *
  49. * @return void
  50. */
  51. public function tearDown()
  52. {
  53. parent::tearDown();
  54. Plugin::unload('TestPlugin');
  55. Plugin::unload('TestTheme');
  56. unset($this->View);
  57. }
  58. /**
  59. * Tests basic cell rendering.
  60. *
  61. * @return void
  62. */
  63. public function testCellRender()
  64. {
  65. $cell = $this->View->cell('Articles::teaserList');
  66. $render = "{$cell}";
  67. $this->assertEquals('teaser_list', $cell->template);
  68. $this->assertContains('<h2>Lorem ipsum</h2>', $render);
  69. $this->assertContains('<h2>Usectetur adipiscing eli</h2>', $render);
  70. $this->assertContains('<h2>Topis semper blandit eu non</h2>', $render);
  71. $this->assertContains('<h2>Suspendisse gravida neque</h2>', $render);
  72. $cell = $this->View->cell('Cello');
  73. $this->assertInstanceOf('TestApp\View\Cell\CelloCell', $cell);
  74. $this->assertEquals("Cellos\n", $cell->render());
  75. }
  76. /**
  77. * Test __toString() hitting an error when rendering views.
  78. *
  79. * @return void
  80. */
  81. public function testCellImplictRenderWithError()
  82. {
  83. $capture = function ($errno, $msg) {
  84. restore_error_handler();
  85. $this->assertEquals(E_USER_WARNING, $errno);
  86. $this->assertContains('Could not render cell - Cell view file', $msg);
  87. };
  88. set_error_handler($capture);
  89. $cell = $this->View->cell('Articles::teaserList');
  90. $cell->template = 'nope';
  91. $result = "{$cell}";
  92. }
  93. /**
  94. * Tests that we are able pass multiple arguments to cell methods.
  95. *
  96. * This test sets its own error handler, as PHPUnit won't convert
  97. * errors into exceptions when the caller is a __toString() method.
  98. *
  99. * @return void
  100. */
  101. public function testCellWithArguments()
  102. {
  103. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  104. $render = "{$cell}";
  105. $this->assertContains('dummy message', $render);
  106. }
  107. /**
  108. * Tests that cell runs default action when none is provided.
  109. *
  110. * @return void
  111. */
  112. public function testDefaultCellAction()
  113. {
  114. $appCell = $this->View->cell('Articles');
  115. $this->assertEquals('display', $appCell->template);
  116. $this->assertContains('dummy', "{$appCell}");
  117. $pluginCell = $this->View->cell('TestPlugin.Dummy');
  118. $this->assertContains('dummy', "{$pluginCell}");
  119. $this->assertEquals('display', $pluginCell->template);
  120. }
  121. /**
  122. * Tests that cell action setting the template renders the correct template
  123. *
  124. * @return void
  125. */
  126. public function testSettingCellTemplateFromAction()
  127. {
  128. $appCell = $this->View->cell('Articles::customTemplate');
  129. $this->assertContains('This is the alternate template', "{$appCell}");
  130. $this->assertEquals('alternate_teaser_list', $appCell->template);
  131. $appCell = $this->View->cell('Articles::customTemplate');
  132. $this->assertContains('This is the alternate template', $appCell->render());
  133. $this->assertEquals('alternate_teaser_list', $appCell->template);
  134. }
  135. /**
  136. * Tests manual render() invocation.
  137. *
  138. * @return void
  139. */
  140. public function testCellManualRender()
  141. {
  142. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  143. $this->assertContains('dummy message', $cell->render());
  144. $cell->teaserList();
  145. $this->assertContains('<h2>Lorem ipsum</h2>', $cell->render('teaser_list'));
  146. }
  147. /**
  148. * Tests manual render() invocation with error
  149. *
  150. * @expectedException \Cake\View\Exception\MissingCellViewException
  151. * @return void
  152. */
  153. public function testCellManualRenderError()
  154. {
  155. $cell = $this->View->cell('Articles');
  156. $cell->render('derp');
  157. }
  158. /**
  159. * Test rendering a cell with a theme.
  160. *
  161. * @return void
  162. */
  163. public function testCellRenderThemed()
  164. {
  165. $this->View->theme = 'TestTheme';
  166. $cell = $this->View->cell('Articles', ['msg' => 'hello world!']);
  167. $this->assertEquals($this->View->theme, $cell->viewBuilder()->theme());
  168. $this->assertContains('Themed cell content.', $cell->render());
  169. $this->assertEquals($cell->View->theme, $cell->viewBuilder()->theme());
  170. }
  171. /**
  172. * Test that a cell can render a plugin view.
  173. *
  174. * @return void
  175. */
  176. public function testCellRenderPluginTemplate()
  177. {
  178. $cell = $this->View->cell('Articles');
  179. $this->assertContains(
  180. 'TestPlugin Articles/display',
  181. $cell->render('TestPlugin.display')
  182. );
  183. $cell = $this->View->cell('Articles');
  184. $cell->plugin = 'TestPlugin';
  185. $this->assertContains(
  186. 'TestPlugin Articles/display',
  187. $cell->render('display')
  188. );
  189. }
  190. /**
  191. * Tests that using plugin's cells works.
  192. *
  193. * @return void
  194. */
  195. public function testPluginCell()
  196. {
  197. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  198. $this->assertContains('hello world!', "{$cell}");
  199. }
  200. /**
  201. * Test that plugin cells can render other view templates.
  202. *
  203. * @return void
  204. */
  205. public function testPluginCellAlternateTemplate()
  206. {
  207. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  208. $cell->template = '../../Element/translate';
  209. $this->assertContains('This is a translatable string', "{$cell}");
  210. }
  211. /**
  212. * Test that plugin cells can render other view templates.
  213. *
  214. * @return void
  215. */
  216. public function testPluginCellAlternateTemplateRenderParam()
  217. {
  218. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  219. $result = $cell->render('../../Element/translate');
  220. $this->assertContains('This is a translatable string', $result);
  221. }
  222. /**
  223. * Tests that using an unexisting cell throws an exception.
  224. *
  225. * @expectedException \Cake\View\Exception\MissingCellException
  226. * @return void
  227. */
  228. public function testUnexistingCell()
  229. {
  230. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  231. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  232. }
  233. /**
  234. * Tests missing method errors
  235. *
  236. * @expectedException \BadMethodCallException
  237. * @expectedExceptionMessage Class TestApp\View\Cell\ArticlesCell does not have a "nope" method.
  238. * @return void
  239. */
  240. public function testCellMissingMethod()
  241. {
  242. $cell = $this->View->cell('Articles::nope');
  243. $cell->render();
  244. }
  245. /**
  246. * Test that cell options are passed on.
  247. *
  248. * @return void
  249. */
  250. public function testCellOptions()
  251. {
  252. $cell = $this->View->cell('Articles', [], ['limit' => 10, 'nope' => 'nope']);
  253. $this->assertEquals(10, $cell->limit);
  254. $this->assertFalse(property_exists('nope', $cell), 'Not a valid option');
  255. }
  256. /**
  257. * Test that cells get the helper configuration from the view that created them.
  258. *
  259. * @return void
  260. */
  261. public function testCellInheritsHelperConfig()
  262. {
  263. $this->View->helpers = ['Url', 'Form', 'Banana'];
  264. $cell = $this->View->cell('Articles');
  265. $this->assertSame($this->View->helpers, $cell->helpers);
  266. }
  267. /**
  268. * Test that cells the view class name of a custom view passed on.
  269. *
  270. * @return void
  271. */
  272. public function testCellInheritsCustomViewClass()
  273. {
  274. $request = $this->getMock('Cake\Network\Request');
  275. $response = $this->getMock('Cake\Network\Response');
  276. $view = new CustomJsonView($request, $response);
  277. $cell = $view->cell('Articles');
  278. $this->assertSame('TestApp\View\CustomJsonView', $cell->viewClass);
  279. }
  280. /**
  281. * Test cached render.
  282. *
  283. * @return void
  284. */
  285. public function testCachedRenderSimple()
  286. {
  287. $mock = $this->getMock('Cake\Cache\CacheEngine');
  288. $mock->method('init')
  289. ->will($this->returnValue(true));
  290. $mock->method('read')
  291. ->will($this->returnValue(false));
  292. $mock->expects($this->once())
  293. ->method('write')
  294. ->with('cell_test_app_view_cell_articles_cell_display', "dummy\n");
  295. Cache::config('default', $mock);
  296. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  297. $result = $cell->render();
  298. $this->assertEquals("dummy\n", $result);
  299. Cache::drop('default');
  300. }
  301. /**
  302. * Test read cached cell.
  303. *
  304. * @return void
  305. */
  306. public function testReadCachedCell()
  307. {
  308. $mock = $this->getMock('Cake\Cache\CacheEngine');
  309. $mock->method('init')
  310. ->will($this->returnValue(true));
  311. $mock->method('read')
  312. ->will($this->returnValue("dummy\n"));
  313. $mock->expects($this->never())
  314. ->method('write');
  315. Cache::config('default', $mock);
  316. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  317. $result = $cell->render();
  318. $this->assertEquals("dummy\n", $result);
  319. Cache::drop('default');
  320. }
  321. /**
  322. * Test cached render array config
  323. *
  324. * @return void
  325. */
  326. public function testCachedRenderArrayConfig()
  327. {
  328. $mock = $this->getMock('Cake\Cache\CacheEngine');
  329. $mock->method('init')
  330. ->will($this->returnValue(true));
  331. $mock->method('read')
  332. ->will($this->returnValue(false));
  333. $mock->expects($this->once())
  334. ->method('write')
  335. ->with('my_key', "dummy\n");
  336. Cache::config('cell', $mock);
  337. $cell = $this->View->cell('Articles', [], [
  338. 'cache' => ['key' => 'my_key', 'config' => 'cell']
  339. ]);
  340. $result = $cell->render();
  341. $this->assertEquals("dummy\n", $result);
  342. Cache::drop('cell');
  343. }
  344. /**
  345. * Test cached render.
  346. *
  347. * @return void
  348. */
  349. public function testCachedRenderSimpleCustomTemplate()
  350. {
  351. $mock = $this->getMock('Cake\Cache\CacheEngine');
  352. $mock->method('init')
  353. ->will($this->returnValue(true));
  354. $mock->method('read')
  355. ->will($this->returnValue(false));
  356. $mock->expects($this->once())
  357. ->method('write')
  358. ->with('cell_test_app_view_cell_articles_cell_customTemplate', "<h1>This is the alternate template</h1>\n");
  359. Cache::config('default', $mock);
  360. $cell = $this->View->cell('Articles::customTemplate', [], ['cache' => true]);
  361. $result = $cell->render();
  362. $this->assertContains('This is the alternate template', $result);
  363. Cache::drop('default');
  364. }
  365. }