CellTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. * Test that a cell can render a plugin view.
  159. *
  160. * @return void
  161. */
  162. public function testCellRenderPluginTemplate()
  163. {
  164. $cell = $this->View->cell('Articles');
  165. $this->assertContains(
  166. 'TestPlugin Articles/display',
  167. $cell->render('TestPlugin.display')
  168. );
  169. $cell = $this->View->cell('Articles');
  170. $cell->plugin = 'TestPlugin';
  171. $this->assertContains(
  172. 'TestPlugin Articles/display',
  173. $cell->render('display')
  174. );
  175. }
  176. /**
  177. * Tests that using plugin's cells works.
  178. *
  179. * @return void
  180. */
  181. public function testPluginCell()
  182. {
  183. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  184. $this->assertContains('hello world!', "{$cell}");
  185. }
  186. /**
  187. * Test that plugin cells can render other view templates.
  188. *
  189. * @return void
  190. */
  191. public function testPluginCellAlternateTemplate()
  192. {
  193. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  194. $cell->template = '../../Element/translate';
  195. $this->assertContains('This is a translatable string', "{$cell}");
  196. }
  197. /**
  198. * Test that plugin cells can render other view templates.
  199. *
  200. * @return void
  201. */
  202. public function testPluginCellAlternateTemplateRenderParam()
  203. {
  204. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  205. $result = $cell->render('../../Element/translate');
  206. $this->assertContains('This is a translatable string', $result);
  207. }
  208. /**
  209. * Tests that using an unexisting cell throws an exception.
  210. *
  211. * @expectedException \Cake\View\Exception\MissingCellException
  212. * @return void
  213. */
  214. public function testUnexistingCell()
  215. {
  216. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  217. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  218. }
  219. /**
  220. * Tests missing method errors
  221. *
  222. * @expectedException \BadMethodCallException
  223. * @expectedExceptionMessage Class TestApp\View\Cell\ArticlesCell does not have a "nope" method.
  224. * @return void
  225. */
  226. public function testCellMissingMethod()
  227. {
  228. $this->View->cell('Articles::nope');
  229. }
  230. /**
  231. * Test that cell options are passed on.
  232. *
  233. * @return void
  234. */
  235. public function testCellOptions()
  236. {
  237. $cell = $this->View->cell('Articles', [], ['limit' => 10, 'nope' => 'nope']);
  238. $this->assertEquals(10, $cell->limit);
  239. $this->assertFalse(property_exists('nope', $cell), 'Not a valid option');
  240. }
  241. /**
  242. * Test that cells get the helper configuration from the view that created them.
  243. *
  244. * @return void
  245. */
  246. public function testCellInheritsHelperConfig()
  247. {
  248. $this->View->helpers = ['Url', 'Form', 'Banana'];
  249. $cell = $this->View->cell('Articles');
  250. $this->assertSame($this->View->helpers, $cell->helpers);
  251. }
  252. /**
  253. * Test that cells the view class name of a custom view passed on.
  254. *
  255. * @return void
  256. */
  257. public function testCellInheritsCustomViewClass()
  258. {
  259. $request = $this->getMock('Cake\Network\Request');
  260. $response = $this->getMock('Cake\Network\Response');
  261. $view = new CustomJsonView($request, $response);
  262. $cell = $view->cell('Articles');
  263. $this->assertSame('TestApp\View\CustomJsonView', $cell->viewClass);
  264. }
  265. /**
  266. * Test cached render.
  267. *
  268. * @return void
  269. */
  270. public function testCachedRenderSimple()
  271. {
  272. $mock = $this->getMock('Cake\Cache\CacheEngine');
  273. $mock->method('init')
  274. ->will($this->returnValue(true));
  275. $mock->method('read')
  276. ->will($this->returnValue(false));
  277. $mock->expects($this->once())
  278. ->method('write')
  279. ->with('cell_test_app_view_cell_articles_cell_display', "dummy\n");
  280. Cache::config('default', $mock);
  281. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  282. $result = $cell->render();
  283. $this->assertEquals("dummy\n", $result);
  284. Cache::drop('default');
  285. }
  286. /**
  287. * Test cached render array config
  288. *
  289. * @return void
  290. */
  291. public function testCachedRenderArrayConfig()
  292. {
  293. $mock = $this->getMock('Cake\Cache\CacheEngine');
  294. $mock->method('init')
  295. ->will($this->returnValue(true));
  296. $mock->method('read')
  297. ->will($this->returnValue(false));
  298. $mock->expects($this->once())
  299. ->method('write')
  300. ->with('my_key', "dummy\n");
  301. Cache::config('cell', $mock);
  302. $cell = $this->View->cell('Articles', [], [
  303. 'cache' => ['key' => 'my_key', 'config' => 'cell']
  304. ]);
  305. $result = $cell->render();
  306. $this->assertEquals("dummy\n", $result);
  307. Cache::drop('cell');
  308. }
  309. }