CellTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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 manual render() invocation.
  123. *
  124. * @return void
  125. */
  126. public function testCellManualRender()
  127. {
  128. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  129. $this->assertContains('dummy message', $cell->render());
  130. $cell->teaserList();
  131. $this->assertContains('<h2>Lorem ipsum</h2>', $cell->render('teaser_list'));
  132. }
  133. /**
  134. * Tests manual render() invocation with error
  135. *
  136. * @expectedException \Cake\View\Exception\MissingCellViewException
  137. * @return void
  138. */
  139. public function testCellManualRenderError()
  140. {
  141. $cell = $this->View->cell('Articles');
  142. $cell->render('derp');
  143. }
  144. /**
  145. * Test rendering a cell with a theme.
  146. *
  147. * @return void
  148. */
  149. public function testCellRenderThemed()
  150. {
  151. $this->View->theme = 'TestTheme';
  152. $cell = $this->View->cell('Articles', ['msg' => 'hello world!']);
  153. $this->assertEquals($this->View->theme, $cell->theme);
  154. $this->assertContains('Themed cell content.', $cell->render());
  155. $this->assertEquals($cell->View->theme, $cell->theme);
  156. }
  157. /**
  158. * Tests that using plugin's cells works.
  159. *
  160. * @return void
  161. */
  162. public function testPluginCell()
  163. {
  164. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  165. $this->assertContains('hello world!', "{$cell}");
  166. }
  167. /**
  168. * Test that plugin cells can render other view templates.
  169. *
  170. * @return void
  171. */
  172. public function testPluginCellAlternateTemplate()
  173. {
  174. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  175. $cell->template = '../../Element/translate';
  176. $this->assertContains('This is a translatable string', "{$cell}");
  177. }
  178. /**
  179. * Test that plugin cells can render other view templates.
  180. *
  181. * @return void
  182. */
  183. public function testPluginCellAlternateTemplateRenderParam()
  184. {
  185. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  186. $result = $cell->render('../../Element/translate');
  187. $this->assertContains('This is a translatable string', $result);
  188. }
  189. /**
  190. * Tests that using an unexisting cell throws an exception.
  191. *
  192. * @expectedException \Cake\View\Exception\MissingCellException
  193. * @return void
  194. */
  195. public function testUnexistingCell()
  196. {
  197. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  198. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  199. }
  200. /**
  201. * Tests missing method errors
  202. *
  203. * @expectedException \BadMethodCallException
  204. * @expectedExceptionMessage Class TestApp\View\Cell\ArticlesCell does not have a "nope" method.
  205. * @return void
  206. */
  207. public function testCellMissingMethod()
  208. {
  209. $this->View->cell('Articles::nope');
  210. }
  211. /**
  212. * Test that cell options are passed on.
  213. *
  214. * @return void
  215. */
  216. public function testCellOptions()
  217. {
  218. $cell = $this->View->cell('Articles', [], ['limit' => 10, 'nope' => 'nope']);
  219. $this->assertEquals(10, $cell->limit);
  220. $this->assertFalse(property_exists('nope', $cell), 'Not a valid option');
  221. }
  222. /**
  223. * Test that cells get the helper configuration from the view that created them.
  224. *
  225. * @return void
  226. */
  227. public function testCellInheritsHelperConfig()
  228. {
  229. $this->View->helpers = ['Url', 'Form', 'Banana'];
  230. $cell = $this->View->cell('Articles');
  231. $this->assertSame($this->View->helpers, $cell->helpers);
  232. }
  233. /**
  234. * Test that cells the view class name of a custom view passed on.
  235. *
  236. * @return void
  237. */
  238. public function testCellInheritsCustomViewClass()
  239. {
  240. $request = $this->getMock('Cake\Network\Request');
  241. $response = $this->getMock('Cake\Network\Response');
  242. $view = new CustomJsonView($request, $response);
  243. $cell = $view->cell('Articles');
  244. $this->assertSame('TestApp\View\CustomJsonView', $cell->viewClass);
  245. }
  246. /**
  247. * Test cached render.
  248. *
  249. * @return void
  250. */
  251. public function testCachedRenderSimple()
  252. {
  253. $mock = $this->getMock('Cake\Cache\CacheEngine');
  254. $mock->method('init')
  255. ->will($this->returnValue(true));
  256. $mock->method('read')
  257. ->will($this->returnValue(false));
  258. $mock->expects($this->once())
  259. ->method('write')
  260. ->with('cell_test_app_view_cell_articles_cell_display', "dummy\n");
  261. Cache::config('default', $mock);
  262. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  263. $result = $cell->render();
  264. $this->assertEquals("dummy\n", $result);
  265. Cache::drop('default');
  266. }
  267. /**
  268. * Test cached render array config
  269. *
  270. * @return void
  271. */
  272. public function testCachedRenderArrayConfig()
  273. {
  274. $mock = $this->getMock('Cake\Cache\CacheEngine');
  275. $mock->method('init')
  276. ->will($this->returnValue(true));
  277. $mock->method('read')
  278. ->will($this->returnValue(false));
  279. $mock->expects($this->once())
  280. ->method('write')
  281. ->with('my_key', "dummy\n");
  282. Cache::config('cell', $mock);
  283. $cell = $this->View->cell('Articles', [], [
  284. 'cache' => ['key' => 'my_key', 'config' => 'cell']
  285. ]);
  286. $result = $cell->render();
  287. $this->assertEquals("dummy\n", $result);
  288. Cache::drop('cell');
  289. }
  290. }