PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Comments/Test/Case/Controller/CommentsControllerTest.php

https://github.com/kareypowell/croogo
PHP | 387 lines | 296 code | 34 blank | 57 comment | 0 complexity | 77cdf7684dd5237d9c6ee9ae4e1698e5 MD5 | raw file
  1. <?php
  2. App::uses('CommentsController', 'Comments.Controller');
  3. App::uses('CroogoControllerTestCase', 'Croogo.TestSuite');
  4. App::uses('CroogoTestFixture', 'Croogo.TestSuite');
  5. class CommentsControllerTest extends CroogoControllerTestCase {
  6. public $fixtures = array(
  7. 'plugin.users.aco',
  8. 'plugin.users.aro',
  9. 'plugin.users.aros_aco',
  10. 'plugin.blocks.block',
  11. 'plugin.comments.comment',
  12. 'plugin.contacts.contact',
  13. 'plugin.translate.i18n',
  14. 'plugin.settings.language',
  15. 'plugin.menus.link',
  16. 'plugin.menus.menu',
  17. 'plugin.contacts.message',
  18. 'plugin.meta.meta',
  19. 'plugin.nodes.node',
  20. 'plugin.taxonomy.model_taxonomy',
  21. 'plugin.blocks.region',
  22. 'plugin.users.role',
  23. 'plugin.settings.setting',
  24. 'plugin.taxonomy.taxonomy',
  25. 'plugin.taxonomy.term',
  26. 'plugin.taxonomy.type',
  27. 'plugin.taxonomy.types_vocabulary',
  28. 'plugin.users.user',
  29. 'plugin.taxonomy.vocabulary',
  30. );
  31. protected $_level;
  32. /**
  33. * setUp
  34. *
  35. * @return void
  36. */
  37. public function setUp() {
  38. parent::setUp();
  39. $this->_level = Configure::write('Comment.level');
  40. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  41. $_SERVER['SERVER_NAME'] = 'localhost';
  42. $this->CommentsController = $this->generate('Comments.Comments', array(
  43. 'methods' => array(
  44. 'redirect',
  45. ),
  46. 'components' => array(
  47. 'Auth' => array('user'),
  48. 'Session',
  49. ),
  50. ));
  51. $this->CommentsController->Auth
  52. ->staticExpects($this->any())
  53. ->method('user')
  54. ->will($this->returnCallback(array($this, 'authUserCallback')));
  55. }
  56. /**
  57. * tearDown
  58. *
  59. * @return void
  60. */
  61. public function tearDown() {
  62. parent::tearDown();
  63. Configure::write('Comment.level', $this->_level);
  64. unset($this->CommentsController);
  65. }
  66. /**
  67. * testAdminIndex
  68. *
  69. * @return void
  70. */
  71. public function testAdminIndex() {
  72. $this->testAction('/admin/comments/index');
  73. $this->assertNotEmpty($this->vars['comments']);
  74. }
  75. /**
  76. * testAdminEdit
  77. *
  78. * @return void
  79. */
  80. public function testAdminEdit() {
  81. $this->expectFlashAndRedirect('The Comment has been saved');
  82. $this->testAction('/admin/comments/comments/edit/1', array(
  83. 'data' => array(
  84. 'Comment' => array(
  85. 'id' => 1, // Mr Croogo
  86. 'name' => 'Mr Croogo [modified]',
  87. 'email' => 'contact@example.com',
  88. 'body' => 'lots of text...',
  89. ),
  90. ),
  91. ));
  92. $result = $this->CommentsController->Comment->findById(1);
  93. $this->assertEquals('Mr Croogo [modified]', $result['Comment']['name']);
  94. }
  95. /**
  96. * testAdminDelete
  97. *
  98. * @return void
  99. */
  100. public function testAdminDelete() {
  101. $this->expectFlashAndRedirect('Comment deleted');
  102. $this->testAction('/admin/comments/comments/delete/1');
  103. $hasAny = $this->CommentsController->Comment->hasAny(array(
  104. 'Comment.id' => 1,
  105. ));
  106. $this->assertFalse($hasAny);
  107. }
  108. /**
  109. * testAdminProcessDelete
  110. *
  111. * @return void
  112. */
  113. public function testAdminProcessDelete() {
  114. $this->expectFlashAndRedirect('Comments deleted');
  115. $this->testAction('/admin/comments/comments/process', array(
  116. 'data' => array(
  117. 'Comment' => array(
  118. 'action' => 'delete',
  119. '1' => array('id' => 1),
  120. ),
  121. ),
  122. ));
  123. $list = $this->CommentsController->Comment->find('list', array(
  124. 'fields' => array(
  125. 'id',
  126. 'name',
  127. ),
  128. 'order' => 'Comment.lft ASC',
  129. ));
  130. $this->assertEqual($list, array(2 => 'Mrs Croogo'));
  131. }
  132. /**
  133. * testAdminProcessPublish
  134. *
  135. * @return void
  136. */
  137. public function testAdminProcessPublish() {
  138. // unpublish a Comment for testing
  139. $this->CommentsController->Comment->id = 1;
  140. $this->CommentsController->Comment->saveField('status', 0);
  141. $this->CommentsController->Comment->id = false;
  142. $comment = $this->CommentsController->Comment->hasAny(array(
  143. 'id' => 1,
  144. 'status' => 0,
  145. ));
  146. $relevantNode = $this->CommentsController->Comment->Node->findById(1);
  147. $this->assertTrue($comment);
  148. //Ensure that the counterCache field has been updated for the rest of the test
  149. $this->assertEqual($relevantNode['Node']['comment_count'], 0);
  150. $this->expectFlashAndRedirect('Comments published');
  151. $this->testAction('/admin/comments/comments/process', array(
  152. 'data' => array(
  153. 'Comment' => array(
  154. 'action' => 'publish',
  155. '1' => array('id' => 1),
  156. ),
  157. ),
  158. ));
  159. $list = $this->CommentsController->Comment->find('list', array(
  160. 'conditions' => array(
  161. 'Comment.status' => 1,
  162. ),
  163. 'fields' => array(
  164. 'id',
  165. 'name',
  166. ),
  167. 'order' => 'Comment.lft ASC',
  168. ));
  169. $this->assertEqual($list, array(
  170. '1' => 'Mr Croogo',
  171. ));
  172. $relevantNode = $this->CommentsController->Comment->Node->findById(1);
  173. $this->assertEqual($relevantNode['Node']['comment_count'], 1);
  174. }
  175. /**
  176. * testAdminProcessUnpublish
  177. *
  178. * @return void
  179. */
  180. public function testAdminProcessUnpublish() {
  181. $this->expectFlashAndRedirect('Comments unpublished');
  182. $this->testAction('/admin/comments/comments/process', array(
  183. 'data' => array(
  184. 'Comment' => array(
  185. 'action' => 'unpublish',
  186. '1' => array('id' => 1),
  187. ),
  188. ),
  189. ));
  190. $list = $this->CommentsController->Comment->find('list', array(
  191. 'conditions' => array(
  192. 'Comment.status' => 1,
  193. ),
  194. 'fields' => array(
  195. 'id',
  196. 'name',
  197. ),
  198. 'order' => 'Comment.lft ASC',
  199. ));
  200. $this->assertEqual($list, array());
  201. $relevantNode = $this->CommentsController->Comment->Node->findById(1);
  202. $this->assertEqual($relevantNode['Node']['comment_count'], 0);
  203. }
  204. /**
  205. * testAdd
  206. */
  207. public function testAdd() {
  208. Configure::write('Comment.email_notification', 1);
  209. $Comments = $this->generate('Comments', array(
  210. 'methods' => array(
  211. '_sendEmail',
  212. ),
  213. 'components' => array(
  214. 'Session',
  215. ),
  216. ));
  217. $Comments->plugin = 'Comments';
  218. $Comments
  219. ->expects($this->once())
  220. ->method('_sendEmail')
  221. ->will($this->returnValue(true));
  222. $Comments->request->params['action'] = 'add';
  223. $Comments->request->params['url']['url'] = 'comments/comments/add';
  224. $Comments->Components->trigger('initialize', array(&$Comments));
  225. $Comments->Components->trigger('startup', array(&$Comments));
  226. $Comments->request->data['Comment'] = array(
  227. 'name' => 'John Smith',
  228. 'email' => 'john.smith@example.com',
  229. 'website' => 'http://example.com',
  230. 'body' => 'text here...',
  231. );
  232. $node = $Comments->Comment->Node->findBySlug('hello-world');
  233. $Comments->add('Node', $node['Node']['id']);
  234. $comments = $Comments->Comment->generateTreeList(array('Comment.foreign_key' => $node['Node']['id'], 'Comment.model' => 'Node'), '{n}.Comment.id', '{n}.Comment.name');
  235. $commenters = array_values($comments);
  236. $this->assertEqual($commenters, array('Mr Croogo', 'Mrs Croogo', 'John Smith'));
  237. $Comments->testView = true;
  238. $model = 'Node';
  239. $data = $node;
  240. $Comments->set(compact('model', 'data'));
  241. $output = $Comments->render('add');
  242. $this->assertFalse(strpos($output, '<pre class="cake-debug">'));
  243. }
  244. /**
  245. * testAddWithParent
  246. */
  247. public function testAddWithParent() {
  248. Configure::write('Comment.email_notification', 1);
  249. $Comments = $this->generate('Comments', array(
  250. 'methods' => array('_sendEmail'),
  251. ));
  252. $Comments->plugin = 'Comments';
  253. $Comments
  254. ->expects($this->once())
  255. ->method('_sendEmail')
  256. ->will($this->returnValue(true));
  257. $Comments->request->params['action'] = 'add';
  258. $Comments->request->params['url']['url'] = 'comments/comments/add';
  259. $Comments->Components->trigger('initialize', array(&$Comments));
  260. $Comments->Components->trigger('startup', array(&$Comments));
  261. $Comments->request->data['Comment'] = array(
  262. 'name' => 'John Smith',
  263. 'email' => 'john.smith@example.com',
  264. 'website' => 'http://example.com',
  265. 'body' => 'text here...',
  266. );
  267. $node = $Comments->Comment->Node->findBySlug('hello-world');
  268. Configure::write('Comment.level', 2);
  269. $Comments->add('Node', $node['Node']['id'], 1); // under the comment by Mr Croogo
  270. $comments = $Comments->Comment->generateTreeList(array('Comment.foreign_key' => $node['Node']['id'], 'Comment.model' => 'Node'), '{n}.Comment.id', '{n}.Comment.name');
  271. $commenters = array_values($comments);
  272. $this->assertEqual($commenters, array('Mr Croogo', '_John Smith', 'Mrs Croogo'));
  273. $Comments->testView = true;
  274. $model = 'Node';
  275. $data = $node;
  276. $Comments->set(compact('model', 'data'));
  277. $output = $Comments->render('add');
  278. $this->assertFalse(strpos($output, '<pre class="cake-debug">'));
  279. }
  280. /**
  281. * testAddWithoutEmailNotification
  282. */
  283. public function testAddWithoutEmailNotification() {
  284. Configure::write('Comment.email_notification', 0);
  285. $Comments = $this->generate('Comments', array(
  286. 'components' => array(
  287. 'Session',
  288. ),
  289. ));
  290. $Comments->plugin = 'Comments';
  291. $Comments->request->params['action'] = 'add';
  292. $Comments->request->params['url']['url'] = 'comments/comments/add';
  293. $Comments->startupProcess();
  294. $Comments->request->data['Comment'] = array(
  295. 'name' => 'John Smith',
  296. 'email' => 'john.smith@example.com',
  297. 'website' => 'http://example.com',
  298. 'body' => 'text here...',
  299. );
  300. $node = $Comments->Comment->Node->findBySlug('hello-world');
  301. $Comments->add('Node', $node['Node']['id']);
  302. $comments = $Comments->Comment->generateTreeList(array('Comment.foreign_key' => $node['Node']['id'], 'Comment.model' => 'Node'), '{n}.Comment.id', '{n}.Comment.name');
  303. $commenters = array_values($comments);
  304. $this->assertEqual($commenters, array('Mr Croogo', 'Mrs Croogo', 'John Smith'));
  305. $Comments->testView = true;
  306. $model = 'Node';
  307. $data = $node;
  308. $Comments->set(compact('model', 'data'));
  309. $output = $Comments->render('add');
  310. $this->assertFalse(strpos($output, '<pre class="cake-debug">'));
  311. }
  312. /**
  313. * testAddNotAllowedByType
  314. */
  315. public function testAddNotAllowedByType() {
  316. $Type = ClassRegistry::init('Taxonomy.Type');
  317. $Type->id = 2;
  318. $Type->saveField('comment_status', 0);
  319. $this->CommentsController->Session
  320. ->expects($this->once())
  321. ->method('setFlash')
  322. ->with(
  323. $this->equalTo('Comments are not allowed.'),
  324. $this->equalTo('default'),
  325. $this->equalTo(array('class' => 'error'))
  326. );
  327. $this->CommentsController->request->params['action'] = 'add';
  328. $this->CommentsController->request->params['url']['url'] = 'comments/add';
  329. $this->CommentsController->request->data['Comment'] = array(
  330. 'name' => 'John Smith',
  331. 'email' => 'john.smith@example.com',
  332. 'website' => 'http://example.com',
  333. 'body' => 'text here...',
  334. );
  335. $this->CommentsController->add('Node', 1);
  336. }
  337. /**
  338. * testAddShouldWorkWhenLoggedIn
  339. */
  340. public function testAddShouldWorkWhenLoggedIn() {
  341. Configure::write('Comment.email_notification', 0);
  342. $this->CommentsController->request->params['action'] = 'add';
  343. $this->CommentsController->request->params['url']['url'] = 'comments/add';
  344. $this->expectFlashAndRedirect('Your comment has been added successfully.');
  345. $this->CommentsController->request->data['Comment'] = array(
  346. 'name' => 'John Smith',
  347. 'email' => 'john.smith@example.com',
  348. 'website' => 'http://example.com',
  349. 'body' => 'text here...',
  350. );
  351. $this->CommentsController->add('Node', 1);
  352. }
  353. }