CellTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * setUp method
  33. *
  34. * @return void
  35. */
  36. public function setUp() {
  37. parent::setUp();
  38. Configure::write('App.namespace', 'TestApp');
  39. Plugin::load(['TestPlugin', 'TestTheme']);
  40. $request = $this->getMock('Cake\Network\Request');
  41. $response = $this->getMock('Cake\Network\Response');
  42. $this->View = new \Cake\View\View($request, $response);
  43. }
  44. /**
  45. * tearDown method
  46. *
  47. * @return void
  48. */
  49. public function tearDown() {
  50. parent::tearDown();
  51. Plugin::unload('TestPlugin');
  52. Plugin::unload('TestTheme');
  53. unset($this->View);
  54. }
  55. /**
  56. * Tests basic cell rendering.
  57. *
  58. * @return void
  59. */
  60. public function testCellRender() {
  61. $cell = $this->View->cell('Articles::teaserList');
  62. $render = "{$cell}";
  63. $this->assertEquals('teaser_list', $cell->template);
  64. $this->assertContains('<h2>Lorem ipsum</h2>', $render);
  65. $this->assertContains('<h2>Usectetur adipiscing eli</h2>', $render);
  66. $this->assertContains('<h2>Topis semper blandit eu non</h2>', $render);
  67. $this->assertContains('<h2>Suspendisse gravida neque</h2>', $render);
  68. }
  69. /**
  70. * Test __toString() hitting an error when rendering views.
  71. *
  72. * @return void
  73. */
  74. public function testCellImplictRenderWithError() {
  75. $capture = function ($errno, $msg) {
  76. restore_error_handler();
  77. $this->assertEquals(E_USER_WARNING, $errno);
  78. $this->assertContains('Could not render cell - Cell view file', $msg);
  79. };
  80. set_error_handler($capture);
  81. $cell = $this->View->cell('Articles::teaserList');
  82. $cell->template = 'nope';
  83. $result = "{$cell}";
  84. }
  85. /**
  86. * Tests that we are able pass multiple arguments to cell methods.
  87. *
  88. * This test sets its own error handler, as PHPUnit won't convert
  89. * errors into exceptions when the caller is a __toString() method.
  90. *
  91. * @return void
  92. */
  93. public function testCellWithArguments() {
  94. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  95. $render = "{$cell}";
  96. $this->assertContains('dummy message', $render);
  97. }
  98. /**
  99. * Tests that cell runs default action when none is provided.
  100. *
  101. * @return void
  102. */
  103. public function testDefaultCellAction() {
  104. $appCell = $this->View->cell('Articles');
  105. $this->assertEquals('display', $appCell->template);
  106. $this->assertContains('dummy', "{$appCell}");
  107. $pluginCell = $this->View->cell('TestPlugin.Dummy');
  108. $this->assertContains('dummy', "{$pluginCell}");
  109. $this->assertEquals('display', $pluginCell->template);
  110. }
  111. /**
  112. * Tests manual render() invocation.
  113. *
  114. * @return void
  115. */
  116. public function testCellManualRender() {
  117. $cell = $this->View->cell('Articles::doEcho', ['msg1' => 'dummy', 'msg2' => ' message']);
  118. $this->assertContains('dummy message', $cell->render());
  119. $cell->teaserList();
  120. $this->assertContains('<h2>Lorem ipsum</h2>', $cell->render('teaser_list'));
  121. }
  122. /**
  123. * Tests manual render() invocation with error
  124. *
  125. * @expectedException \Cake\View\Exception\MissingCellViewException
  126. * @return void
  127. */
  128. public function testCellManualRenderError() {
  129. $cell = $this->View->cell('Articles');
  130. $cell->render('derp');
  131. }
  132. /**
  133. * Test rendering a cell with a theme.
  134. *
  135. * @return void
  136. */
  137. public function testCellRenderThemed() {
  138. $this->View->theme = 'TestTheme';
  139. $cell = $this->View->cell('Articles', ['msg' => 'hello world!']);
  140. $this->assertEquals($this->View->theme, $cell->theme);
  141. $this->assertContains('Themed cell content.', $cell->render());
  142. $this->assertEquals($cell->View->theme, $cell->theme);
  143. }
  144. /**
  145. * Tests that using plugin's cells works.
  146. *
  147. * @return void
  148. */
  149. public function testPluginCell() {
  150. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  151. $this->assertContains('hello world!', "{$cell}");
  152. }
  153. /**
  154. * Test that plugin cells can render other view templates.
  155. *
  156. * @return void
  157. */
  158. public function testPluginCellAlternateTemplate() {
  159. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  160. $cell->template = '../../Element/translate';
  161. $this->assertContains('This is a translatable string', "{$cell}");
  162. }
  163. /**
  164. * Test that plugin cells can render other view templates.
  165. *
  166. * @return void
  167. */
  168. public function testPluginCellAlternateTemplateRenderParam() {
  169. $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
  170. $result = $cell->render('../../Element/translate');
  171. $this->assertContains('This is a translatable string', $result);
  172. }
  173. /**
  174. * Tests that using an unexisting cell throws an exception.
  175. *
  176. * @expectedException \Cake\View\Exception\MissingCellException
  177. * @return void
  178. */
  179. public function testUnexistingCell() {
  180. $cell = $this->View->cell('TestPlugin.Void::echoThis', ['arg1' => 'v1']);
  181. $cell = $this->View->cell('Void::echoThis', ['arg1' => 'v1', 'arg2' => 'v2']);
  182. }
  183. /**
  184. * Tests missing method errors
  185. *
  186. * @expectedException \BadMethodCallException
  187. * @expectedExceptionMessage Class TestApp\View\Cell\ArticlesCell does not have a "nope" method.
  188. * @return void
  189. */
  190. public function testCellMissingMethod() {
  191. $this->View->cell('Articles::nope');
  192. }
  193. /**
  194. * Test that cell options are passed on.
  195. *
  196. * @return void
  197. */
  198. public function testCellOptions() {
  199. $cell = $this->View->cell('Articles', [], ['limit' => 10, 'nope' => 'nope']);
  200. $this->assertEquals(10, $cell->limit);
  201. $this->assertFalse(property_exists('nope', $cell), 'Not a valid option');
  202. }
  203. /**
  204. * Test that cells get the helper configuration from the view that created them.
  205. *
  206. * @return void
  207. */
  208. public function testCellInheritsHelperConfig() {
  209. $this->View->helpers = ['Url', 'Form', 'Banana'];
  210. $cell = $this->View->cell('Articles');
  211. $this->assertSame($this->View->helpers, $cell->helpers);
  212. }
  213. /**
  214. * Test that cells the view class name of a custom view passed on.
  215. *
  216. * @return void
  217. */
  218. public function testCellInheritsCustomViewClass() {
  219. $request = $this->getMock('Cake\Network\Request');
  220. $response = $this->getMock('Cake\Network\Response');
  221. $view = new CustomJsonView($request, $response);
  222. $cell = $view->cell('Articles');
  223. $this->assertSame('TestApp\View\CustomJsonView', $cell->viewClass);
  224. }
  225. /**
  226. * Test cached render.
  227. *
  228. * @return void
  229. */
  230. public function testCachedRenderSimple() {
  231. $mock = $this->getMock('Cake\Cache\CacheEngine');
  232. $mock->method('init')
  233. ->will($this->returnValue(true));
  234. $mock->method('read')
  235. ->will($this->returnValue(false));
  236. $mock->expects($this->once())
  237. ->method('write')
  238. ->with('cell_test_app_view_cell_articles_cell_display', "dummy\n");
  239. Cache::config('default', $mock);
  240. $cell = $this->View->cell('Articles', [], ['cache' => true]);
  241. $result = $cell->render();
  242. $this->assertEquals("dummy\n", $result);
  243. Cache::drop('default');
  244. }
  245. /**
  246. * Test cached render array config
  247. *
  248. * @return void
  249. */
  250. public function testCachedRenderArrayConfig() {
  251. $mock = $this->getMock('Cake\Cache\CacheEngine');
  252. $mock->method('init')
  253. ->will($this->returnValue(true));
  254. $mock->method('read')
  255. ->will($this->returnValue(false));
  256. $mock->expects($this->once())
  257. ->method('write')
  258. ->with('my_key', "dummy\n");
  259. Cache::config('cell', $mock);
  260. $cell = $this->View->cell('Articles', [], [
  261. 'cache' => ['key' => 'my_key', 'config' => 'cell']
  262. ]);
  263. $result = $cell->render();
  264. $this->assertEquals("dummy\n", $result);
  265. Cache::drop('cell');
  266. }
  267. }