CommentControllerTest.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace Muzich\CoreBundle\Tests\Controller;
  3. use Muzich\CoreBundle\lib\FunctionalTest;
  4. use Muzich\CoreBundle\Managers\CommentsManager;
  5. /**
  6. * Test des commentaires. Doit couvrir:
  7. *
  8. * * La consultation de commentaires en fixtures
  9. * * L'ajout de commentaire
  10. * * La modification d'un commentaire
  11. * * La suppression d'un commentaire
  12. *
  13. */
  14. class CommentControllerTest extends FunctionalTest
  15. {
  16. public function testView()
  17. {
  18. $this->client = self::createClient();
  19. $this->connectUser('paul', 'toor');
  20. // On est sur la page home, d'après les fixtures on a des coms en dom
  21. $this->exist('div.comments:contains("C\'est trop bon hein ?")');
  22. $this->exist('div.comments:contains("C\'est pas mal en effet")');
  23. $this->exist('li.element a.display_comments');
  24. $this->exist('li.element a.hide_comments');
  25. $this->exist('li.element a.add_comment');
  26. $this->exist('div.comments a.add_comment');
  27. }
  28. public function testAddEditDelete()
  29. {
  30. $this->client = self::createClient();
  31. $this->connectUser('paul', 'toor');
  32. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  33. ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
  34. ;
  35. // On va ajouter un commentaire a la toute dernière musique posté par bux
  36. $this->crawler = $this->client->request(
  37. 'POST',
  38. $this->generateUrl('ajax_add_comment', array(
  39. 'element_id' => $element->getId(),
  40. 'token' => $this->getUser()->getPersonalHash()
  41. )),
  42. array(
  43. 'comment' => "J'ai réécouté et ouaa je kiff BrOOO"
  44. ),
  45. array(),
  46. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  47. );
  48. $this->isResponseSuccess();
  49. $response = json_decode($this->client->getResponse()->getContent(), true);
  50. $this->assertEquals($response['status'], 'success');
  51. // On ré-affiche la page home pour voir si le commentaire y est
  52. $this->crawler = $this->client->request('GET', $this->generateUrl('home'));
  53. $this->exist('div.comments:contains("J\'ai réécouté et ouaa je kiff BrOOO")');
  54. $extract = $this->crawler->filter('div.comments li:contains("J\'ai réécouté et ouaa je kiff BrOOO")')
  55. ->extract(array('id'));
  56. $id = $extract[0];
  57. // Faut que l'on récupère la date
  58. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  59. ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
  60. ;
  61. $cm = new CommentsManager($element->getComments());
  62. $comment = $cm->getLast();
  63. $this->assertEquals($comment['c'], "J'ai réécouté et ouaa je kiff BrOOO");
  64. // On effectue une modification de ce commentaire
  65. $this->crawler = $this->client->request(
  66. 'POST',
  67. $this->generateUrl('ajax_update_comment', array(
  68. 'element_id' => $element->getId(),
  69. 'date' => $comment['d'],
  70. 'dom_id' => $id,
  71. 'token' => $this->getUser()->getPersonalHash()
  72. )),
  73. array(
  74. 'comment' => "Je me modifie mon com kwaa"
  75. ),
  76. array(),
  77. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  78. );
  79. $this->isResponseSuccess();
  80. $response = json_decode($this->client->getResponse()->getContent(), true);
  81. $this->assertEquals($response['status'], 'success');
  82. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  83. ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
  84. ;
  85. $cm = new CommentsManager($element->getComments());
  86. $comment = $cm->getLast();
  87. $this->assertEquals($comment['c'], "Je me modifie mon com kwaa");
  88. // Il y a une date d'edition
  89. $this->assertTrue(array_key_exists('e', $comment));
  90. // On ré-affiche la page home pour voir si le commentaire modifié y est
  91. $this->crawler = $this->client->request('GET', $this->generateUrl('home'));
  92. $this->exist('div.comments:contains("Je me modifie mon com kwaa")');
  93. // maintenant on le supprime
  94. $this->crawler = $this->client->request(
  95. 'GET',
  96. $this->generateUrl('ajax_delete_comment', array(
  97. 'element_id' => $element->getId(),
  98. 'date' => $comment['d'],
  99. 'token' => $this->getUser()->getPersonalHash()
  100. )),
  101. array(),
  102. array(),
  103. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  104. );
  105. $this->isResponseSuccess();
  106. $response = json_decode($this->client->getResponse()->getContent(), true);
  107. $this->assertEquals($response['status'], 'success');
  108. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  109. ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
  110. ;
  111. $cm = new CommentsManager($element->getComments());
  112. $comment = $cm->getLast();
  113. $this->assertNotEquals($comment['c'], "Je me modifie mon com kwaa");
  114. // On ré-affiche la page home pour voir si le commentaire modifié y est
  115. $this->crawler = $this->client->request('GET', $this->generateUrl('home'));
  116. $this->notExist('div.comments:contains("Je me modifie mon com kwaa")');
  117. }
  118. /**
  119. * On test ici la sécurité nous empêchant de modifier / supprimer le comm
  120. * d'un autre.
  121. */
  122. public function testFails()
  123. {
  124. $this->client = self::createClient();
  125. $this->connectUser('paul', 'toor');
  126. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  127. ->findOneByName('Babylon Pression - Des Tasers et des Pauvres')
  128. ;
  129. $cm = new CommentsManager($element->getComments());
  130. // D'après fixtures: Ce dernier commentaire est à bux
  131. $comment = $cm->getLast();
  132. $this->assertEquals($comment['c'], "Je répond 13");
  133. // On récupère l'id dom
  134. $extract = $this->crawler->filter('div.comments li:contains("Je répond 13")')
  135. ->extract(array('id'));
  136. $id = $extract[0];
  137. // On essaie de le modifier
  138. $this->crawler = $this->client->request(
  139. 'POST',
  140. $this->generateUrl('ajax_update_comment', array(
  141. 'element_id' => $element->getId(),
  142. 'date' => $comment['d'],
  143. 'dom_id' => $id,
  144. 'token' => $this->getUser()->getPersonalHash()
  145. )),
  146. array(
  147. 'comment' => "Je répond 13 HACKED"
  148. ),
  149. array(),
  150. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  151. );
  152. $this->isResponseSuccess();
  153. $response = json_decode($this->client->getResponse()->getContent(), true);
  154. $this->assertEquals($response['status'], 'error');
  155. // On essaie de le supprimer
  156. $this->crawler = $this->client->request(
  157. 'GET',
  158. $this->generateUrl('ajax_delete_comment', array(
  159. 'element_id' => $element->getId(),
  160. 'date' => $comment['d'],
  161. 'token' => $this->getUser()->getPersonalHash()
  162. )),
  163. array(),
  164. array(),
  165. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  166. );
  167. $this->isResponseSuccess();
  168. $response = json_decode($this->client->getResponse()->getContent(), true);
  169. $this->assertEquals($response['status'], 'error');
  170. }
  171. }