PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/cases/helpers/comment_widget.test.php

http://github.com/CakeDC/comments
PHP | 465 lines | 288 code | 53 blank | 124 comment | 4 complexity | 7a41a5ed261db1f4ab3c691aca6f9705 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
  4. *
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @copyright Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
  9. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  10. */
  11. App::import('Core', array('ClassRegistry', 'Controller', 'View', 'Model', 'Security'));
  12. App::import('Helper', array('Comments.CommentWidget', 'Html', 'Form', 'Session'));
  13. App::import('Component', array('Comments.Comments'));
  14. Mock::generatePartial('AppHelper', 'JsHelper', array('link', 'get', 'effect'));
  15. if (!class_exists('Article')) {
  16. class Article extends CakeTestModel {
  17. /**
  18. *
  19. */
  20. public $name = 'Article';
  21. }
  22. }
  23. if (!class_exists('ArticlesTestController')) {
  24. class ArticlesTestController extends Controller {
  25. /**
  26. * @var string
  27. */
  28. public $name = 'ArticlesTest';
  29. /**
  30. * @var array
  31. */
  32. public $uses = array('Article');
  33. /**
  34. * @var array
  35. */
  36. public $components = array('Comments.Comments');
  37. /**
  38. * Overrides Controller::redirect() to log the redirected url
  39. * (non-PHPdoc)
  40. * @see cake/libs/controller/Controller#redirect($url, $status, $exit)
  41. */
  42. public function redirect($url, $status = NULL, $exit = true) {
  43. $this->redirectUrl = $url;
  44. }
  45. }
  46. }
  47. /**
  48. * Comment Widget Helper Test
  49. *
  50. * @package comments
  51. * @subpackage comment.tests.cases.helpers
  52. */
  53. class CommentWidgetHelperTest extends CakeTestCase {
  54. /**
  55. * fixtures property
  56. *
  57. * @var array
  58. */
  59. public $fixtures = array(
  60. 'plugin.comments.comment',
  61. 'plugin.comments.user',
  62. 'plugin.comments.article');
  63. /**
  64. * Helper being tested
  65. * @var CommentWidgetHelper
  66. */
  67. public $CommentWidget = null;
  68. /**
  69. * Controller with commentable related actions for testing purpose
  70. * @var ArticlesTestController
  71. */
  72. public $Controller = null;
  73. /**
  74. * Current view object
  75. * @var View
  76. */
  77. public $View = null;
  78. /**
  79. * Mock object for Js helper
  80. * @var JsHelper
  81. */
  82. public $Js = null;
  83. /**
  84. * Start test method
  85. *
  86. * @return void
  87. */
  88. public function startTest($method) {
  89. parent::startTest($method);
  90. $this->CommentWidget = new CommentWidgetHelper();
  91. $this->CommentWidget->Form = new FormHelper();
  92. $this->CommentWidget->Html = new HtmlHelper();
  93. $this->Js = new JsHelper();
  94. $this->CommentWidget->Js = $this->Js;
  95. $this->CommentWidget->params['action'] = 'view';
  96. $this->Controller = ClassRegistry::init('ArticlesTestController');
  97. $this->View = new View($this->Controller);
  98. ClassRegistry::addObject('view', $this->View);
  99. if (!in_array($method, array('testInitialize', 'testOptions'))) {
  100. $this->CommentWidget->initialize();
  101. }
  102. }
  103. /**
  104. * Test helper instance
  105. *
  106. * @return void
  107. */
  108. public function testInstance() {
  109. $this->assertTrue(is_a($this->CommentWidget, 'CommentWidgetHelper'));
  110. }
  111. /**
  112. * Test initialize
  113. *
  114. * @return void
  115. */
  116. public function testInitialize() {
  117. $this->assertTrue(empty($this->CommentWidget->globalParams));
  118. $this->CommentWidget->initialize();
  119. $this->assertFalse(empty($this->CommentWidget->globalParams));
  120. }
  121. /**
  122. * Test beforeRender callback
  123. *
  124. * @return void
  125. */
  126. public function testBeforeRender() {
  127. $this->assertTrue(empty($this->View->viewVars));
  128. $this->CommentWidget->beforeRender();
  129. $this->assertFalse($this->CommentWidget->enabled);
  130. $this->View->viewVars['commentParams'] = array(
  131. 'displayType' => 'flat');
  132. $this->CommentWidget->beforeRender();
  133. $this->assertFalse($this->CommentWidget->enabled);
  134. $this->View->viewVars['commentParams'] = array(
  135. 'displayType' => 'flat',
  136. 'viewComments' => 'commentsData');
  137. $this->CommentWidget->beforeRender();
  138. $this->assertTrue($this->CommentWidget->enabled);
  139. }
  140. /**
  141. * Test options method
  142. *
  143. * @return void
  144. */
  145. public function testOptions() {
  146. $this->assertTrue(empty($this->CommentWidget->globalParams));
  147. $this->Js->setReturnValue('get', $this->Js);
  148. $this->Js->setReturnValue('effect', '');
  149. $options = array(
  150. 'target' => 'test',
  151. 'foo' => 'bar');
  152. $this->CommentWidget->options($options);
  153. $this->assertEqual(count($this->CommentWidget->globalParams), 9);
  154. $this->assertEqual($this->CommentWidget->globalParams['target'], 'test');
  155. $this->assertEqual($this->CommentWidget->globalParams['foo'], 'bar');
  156. $this->CommentWidget->options(array());
  157. $this->assertEqual(count($this->CommentWidget->globalParams), 9);
  158. $this->assertFalse($this->CommentWidget->globalParams['target']);
  159. $this->assertEqual($this->CommentWidget->globalParams['foo'], 'bar');
  160. }
  161. /**
  162. * Test display method
  163. *
  164. * @return void
  165. */
  166. public function testDisplay() {
  167. $this->CommentWidget->enabled = false;
  168. $this->assertEqual($this->CommentWidget->display(), '');
  169. $this->CommentWidget->enabled = true;
  170. $this->__mockView();
  171. $countElementCall = 0;
  172. $initialParams = $this->CommentWidget->globalParams;
  173. $Article = ClassRegistry::init('Article');
  174. Configure::write('Routing.admin', 'admin');
  175. // Test a basic display call
  176. $currArticle = $Article->findById(1);
  177. $this->View->passedArgs = array(
  178. 'foo' => 'bar',
  179. 'article-slug');
  180. $this->View->viewVars = array(
  181. 'article' => $currArticle,
  182. 'commentParams' => array(
  183. 'viewComments' => 'commentsData',
  184. 'modelName' => 'Article',
  185. 'userModel' => 'User'),
  186. );
  187. $expectedParams = array(
  188. 'comments/flat/main',
  189. array_merge(
  190. $initialParams,
  191. array(
  192. 'viewRecord' => $currArticle['Article'],
  193. 'viewRecordFull' => $currArticle),
  194. $this->View->viewVars['commentParams'],
  195. array(
  196. 'url' => array('article-slug'),
  197. 'allowAddByAuth' => false,
  198. 'allowAddByModel' => 1,
  199. 'adminRoute' => 'admin',
  200. 'isAddMode' => false,
  201. 'theme' => 'flat')
  202. )
  203. );
  204. $this->View->expectAt($countElementCall, 'element', $expectedParams);
  205. $expected = 'Here are your comments!';
  206. $this->View->setReturnValueAt($countElementCall++, 'element', $expected);
  207. $result = $this->CommentWidget->display();
  208. $this->assertEqual($result, $expected);
  209. // Test a display call with options
  210. $expectedParams[0] = 'comments/threaded_custom/main';
  211. $expectedParams[1] = array_merge($expectedParams[1], array(
  212. 'theme' => 'threaded_custom',
  213. 'displayType' => 'threaded',
  214. 'subtheme' => 'custom'));
  215. $this->View->expectAt($countElementCall, 'element', $expectedParams);
  216. $this->View->setReturnValueAt($countElementCall++, 'element', $expected);
  217. $options = array(
  218. 'displayType' => 'threaded',
  219. 'subtheme' => 'custom');
  220. $result = $this->CommentWidget->display($options);
  221. $this->assertEqual($result, $expected);
  222. // Test other cases
  223. $this->CommentWidget->initialize();
  224. $this->View->params['userslug'] = 'example-user';
  225. unset($this->View->viewVars['article']);
  226. $expectedParams[1] = array_merge($expectedParams[1], array(
  227. 'url' => array('example-user', 'article-slug'),
  228. 'viewRecord' => array(),
  229. 'viewRecordFull' => array()));
  230. $this->View->expectAt($countElementCall, 'element', $expectedParams);
  231. $this->View->setReturnValueAt($countElementCall++, 'element', $expected);
  232. $result = $this->CommentWidget->display($options);
  233. $this->assertEqual($result, $expected);
  234. $this->View->expectCallCount('element', $countElementCall);
  235. }
  236. /**
  237. * Test display method with a custom url
  238. *
  239. * @return void
  240. */
  241. public function testDisplayCustomUrl() {
  242. $this->__mockView();
  243. $countElementCall = 0;
  244. $initialParams = $this->CommentWidget->globalParams;
  245. $Article = ClassRegistry::init('Article');
  246. Configure::write('Routing.admin', 'admin');
  247. // Test a basic display call
  248. $currArticle = $Article->findById(1);
  249. $this->View->passedArgs = array(
  250. 'foo' => 'bar',
  251. 'article-slug');
  252. $this->View->viewVars = array(
  253. 'article' => $currArticle,
  254. 'commentParams' => array(
  255. 'viewComments' => 'commentsData',
  256. 'modelName' => 'Article',
  257. 'userModel' => 'User'),
  258. );
  259. $expectedParams = array(
  260. 'comments/flat/main',
  261. array_merge(
  262. $initialParams,
  263. array(
  264. 'viewRecord' => $currArticle['Article'],
  265. 'viewRecordFull' => $currArticle),
  266. $this->View->viewVars['commentParams'],
  267. array(
  268. 'url' => array('action' => 'other', 'param1'),
  269. 'allowAddByAuth' => false,
  270. 'allowAddByModel' => 1,
  271. 'adminRoute' => 'admin',
  272. 'isAddMode' => false,
  273. 'theme' => 'flat')
  274. )
  275. );
  276. $this->CommentWidget->options(array('url' => array('action' => 'other', 'param1')));
  277. $this->View->expectAt($countElementCall, 'element', $expectedParams);
  278. $expected = 'Here are your comments!';
  279. $this->View->setReturnValueAt($countElementCall++, 'element', $expected);
  280. $result = $this->CommentWidget->display();
  281. $this->assertEqual($result, $expected);
  282. }
  283. /**
  284. * Test link method
  285. *
  286. * @return void
  287. */
  288. public function testLink() {
  289. $result = $this->CommentWidget->link('Foobar', '/foo', array('class' => 'bar'));
  290. $expected = array(
  291. 'a' => array('href' => '/foo', 'class' => 'bar'),
  292. 'Foobar',
  293. '/a');
  294. $this->assertTags($result, $expected);
  295. $this->Js->setReturnValue('get', $this->Js);
  296. $this->Js->setReturnValue('effect', '');
  297. $this->CommentWidget->options(array('target' => 'wrapper', 'ajaxOptions' => array('update' => 'wrapper')));
  298. $this->Js->expectOnce('link', array(
  299. 'Foobar',
  300. '/foo',
  301. array(
  302. 'update' => 'wrapper',
  303. 'class' => 'bar'))
  304. );
  305. $this->Js->setReturnValueAt(0, 'link', '/ajaxFoo');
  306. $result = $this->CommentWidget->link('Foobar', '/foo', array('class' => 'bar'));
  307. $this->assertEqual($result, '/ajaxFoo');
  308. }
  309. /**
  310. * Test prepareUrl method
  311. *
  312. * @return void
  313. */
  314. public function testPrepareUrl() {
  315. $expected = $url = array(
  316. 'controller' => 'articles',
  317. 'action' => 'view',
  318. 'my-first-article');
  319. $this->assertEqual($this->CommentWidget->prepareUrl($url), $expected);
  320. $this->Js->setReturnValue('get', $this->Js);
  321. $this->Js->setReturnValue('effect', '');
  322. $this->CommentWidget->options(array(
  323. 'target' => 'placeholder',
  324. 'ajaxAction' => 'add'));
  325. $expected['action'] = 'add';
  326. $this->assertEqual($this->CommentWidget->prepareUrl($url), $expected);
  327. $this->CommentWidget->options(array(
  328. 'target' => 'placeholder',
  329. 'ajaxAction' => array(
  330. 'controller' => 'comments',
  331. 'action' => 'add')));
  332. $expected = array(
  333. 'controller' => 'comments',
  334. 'action' => 'add',
  335. 'my-first-article');
  336. $this->assertEqual($this->CommentWidget->prepareUrl($url), $expected);
  337. }
  338. /**
  339. * Test allowAnonymousComment method
  340. *
  341. * @return void
  342. */
  343. public function testAllowAnonymousComment() {
  344. $this->assertFalse($this->CommentWidget->globalParams['allowAnonymousComment']);
  345. $this->CommentWidget->options(array('allowAnonymousComment' => true));
  346. $this->assertTrue($this->CommentWidget->globalParams['allowAnonymousComment']);
  347. }
  348. /**
  349. * Test element method
  350. *
  351. * @return void
  352. */
  353. public function testElement() {
  354. $this->__mockView();
  355. $this->CommentWidget->options(array('theme' => 'flat'));
  356. $countElementCall = 0;
  357. $expectedParams = array(
  358. 'comments/flat/view',
  359. array(
  360. 'target' => false,
  361. 'ajaxAction' => false,
  362. 'displayUrlToComment' => false,
  363. 'urlToComment' => '',
  364. 'allowAnonymousComment' => false,
  365. 'url' => null,
  366. 'ajaxOptions' => array(),
  367. 'viewInstance' => null,
  368. 'theme' => 'flat')
  369. );
  370. $this->View->expectAt($countElementCall, 'element', $expectedParams);
  371. $expected = 'Comment element content';
  372. $this->View->setReturnValueAt($countElementCall++, 'element', $expected);
  373. $this->assertEqual($this->CommentWidget->element('view'), $expected);
  374. // Test missing element in project elements path. The helper must try to search the element from the comments plugin
  375. $this->View->expectAt($countElementCall, 'element', $expectedParams);
  376. $this->View->setReturnValueAt($countElementCall++, 'element', 'Not Found: /path/to/project/views/elements/comments/flat/view.ctp');
  377. $expectedParams[1]['plugin'] = 'comments';
  378. $this->View->expectAt($countElementCall, 'element', $expectedParams);
  379. $this->View->setReturnValueAt($countElementCall++, 'element', $expected);
  380. $this->assertEqual($this->CommentWidget->element('view'), $expected);
  381. unset($expectedParams[1]['plugin']);
  382. // Test params: they must be passed to the element "as is". Note that the theme has not effect on the element being fetched
  383. $expectedParams[1]['target'] = 'wrapper';
  384. $expectedParams[1]['theme'] = 'threaded';
  385. $this->View->expectAt($countElementCall, 'element', $expectedParams);
  386. $this->View->setReturnValueAt($countElementCall++, 'element', $expected);
  387. $this->assertEqual($this->CommentWidget->element('view', array('target' => 'wrapper', 'theme' => 'threaded')), $expected);
  388. $this->View->expectCallCount('element', $countElementCall);
  389. }
  390. /**
  391. * End test method
  392. *
  393. * @return void
  394. */
  395. public function endTest($method) {
  396. unset($this->CommentWidget, $this->Controller, $this->View);
  397. ClassRegistry::flush();
  398. }
  399. /**
  400. * Mock the view object, update the CR and the testCase attribute with the mock object
  401. *
  402. * @return void
  403. */
  404. private function __mockView() {
  405. if (!class_exists('MockView')) {
  406. Mock::generate('View');
  407. }
  408. $this->View = new MockView();
  409. ClassRegistry::removeObject('view');
  410. ClassRegistry::addObject('view', $this->View);
  411. }
  412. }