GroupControllerTest.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace Muzich\CoreBundle\Tests\Controller;
  3. use Muzich\CoreBundle\lib\FunctionalTest;
  4. class GroupControllerTest extends FunctionalTest
  5. {
  6. /**
  7. * Test de création d'un groupe
  8. */
  9. public function testGroupAddAndDelete()
  10. {
  11. $this->client = self::createClient();
  12. $this->connectUser('bob', 'toor');
  13. $Fans_de_psytrance = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
  14. $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
  15. $tribe_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
  16. $this->crawler = $this->client->request('GET', $this->generateUrl('groups_own_list'));
  17. // Le groupe que nous voulons créer n'existe pas dans la base
  18. $group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  19. ->findOneByName('HardtekMania')
  20. ;
  21. $this->assertTrue(is_null($group));
  22. // bob administre le groupe Fan de psutrance
  23. $this->exist('a[href="'.$this->generateUrl('show_group', array('slug' => $Fans_de_psytrance->getSlug())).'"]');
  24. // On a le formulaire de création sur la page
  25. $this->exist('form[action="'.($url = $this->generateUrl('group_add')).'"]');
  26. $this->exist('form[action="'.$url.'"] input[id="group_name"]');
  27. $this->exist('form[action="'.$url.'"] textarea[id="group_description"]');
  28. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  29. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  30. $form['group[name]'] = 'HardtekMania';
  31. $form['group[description]'] = 'Des bass, des bpm, des gros caissons !';
  32. $form['group[tags]'] = json_encode(array($hardtek_id,$tribe_id));
  33. $this->submit($form);
  34. $this->isResponseRedirection();
  35. $this->followRedirection();
  36. $this->isResponseSuccess();
  37. // Le groupe que créé existe bien dans la base
  38. $group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  39. ->findOneBy(array(
  40. 'name' => 'HardtekMania',
  41. 'description' => 'Des bass, des bpm, des gros caissons !'
  42. ))
  43. ;
  44. $this->assertTrue(!is_null($group));
  45. // On vérifie egallement le lien avec les tags
  46. if (!is_null($group))
  47. {
  48. $group_tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
  49. ->findBy(array(
  50. 'group' => $group->getId(),
  51. 'tag' => $hardtek_id
  52. ))
  53. ;
  54. $this->assertEquals(1, count($group_tags));
  55. $group_tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
  56. ->findBy(array(
  57. 'group' => $group->getId(),
  58. 'tag' => $tribe_id
  59. ))
  60. ;
  61. $this->assertEquals(1, count($group_tags));
  62. }
  63. // Maintenant on va supprimer le groupe fans de psytrance
  64. $this->crawler = $this->client->request('GET', $this->generateUrl('groups_own_list'));
  65. // test du lien de suppression
  66. $this->exist('a[href="'.($url = $this->generateUrl('group_delete', array(
  67. 'group_id' => $Fans_de_psytrance->getId(),
  68. 'token' => $this->getUser()->getPersonalHash()
  69. ))).'"]');
  70. $this->crawler = $this->client->request('GET', $url);
  71. $this->isResponseRedirection();
  72. $this->followRedirection();
  73. $this->isResponseSuccess();
  74. $fangrp = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
  75. $this->assertTrue(is_null($fangrp));
  76. }
  77. public function testGroupAddFail()
  78. {
  79. $this->client = self::createClient();
  80. $this->connectUser('bob', 'toor');
  81. $Fans_de_psytrance = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
  82. $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
  83. $tribe_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
  84. $this->crawler = $this->client->request('GET', $this->generateUrl('groups_own_list'));
  85. // Le groupe que nous voulons créer n'existe pas dans la base
  86. $group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  87. ->findOneByName('Ha')
  88. ;
  89. $this->assertTrue(is_null($group));
  90. // bob administre le groupe Fan de psytrance
  91. $this->exist('a[href="'.$this->generateUrl('show_group', array('slug' => $Fans_de_psytrance->getSlug())).'"]');
  92. // On a le formulaire de création sur la page
  93. $this->exist('form[action="'.($url = $this->generateUrl('group_add')).'"]');
  94. $this->exist('form[action="'.$url.'"] input[id="group_name"]');
  95. $this->exist('form[action="'.$url.'"] textarea[id="group_description"]');
  96. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  97. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  98. $form['group[name]'] = 'Ha';
  99. $form['group[description]'] = '';
  100. $form['group[tags]'] = json_encode(array($hardtek_id,$tribe_id));
  101. $this->submit($form);
  102. // Pas de redirection, la création a échoué (nom trop court) $this->outputDebug();
  103. $this->isResponseSuccess();
  104. // Le groupe que créé existe bien dans la base
  105. $group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  106. ->findOneByName('Ha');
  107. $this->assertTrue(is_null($group));
  108. }
  109. /**
  110. * Test de la mise a jour d'un groupe
  111. */
  112. public function testGroupUpdate()
  113. {
  114. $this->client = self::createClient();
  115. $this->connectUser('bob', 'toor');
  116. $Fans_de_psytrance = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
  117. $psytrance_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Psytrance')->getId();
  118. $electro_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Electro')->getId();
  119. // Le groupe n'a pas encore les modifs que nous voulons effectuer
  120. $group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  121. ->findOneBy(array(
  122. 'name' => 'Les Fans de Psytrance'
  123. ))
  124. ;
  125. $this->assertTrue(is_null($group));
  126. // On se rend sur la page de listing des groupes possédés
  127. $this->crawler = $this->client->request('GET', $this->generateUrl('groups_own_list'));
  128. // Il y a bien le lien vers son groupe
  129. $this->exist('a[href="'.($url = $this->generateUrl('show_group', array('slug' => $Fans_de_psytrance->getSlug()))).'"]');
  130. $link = $this->selectLink('a[href="'.$url.'"]');
  131. // On clique dessus
  132. $this->clickOnLink($link);
  133. $this->isResponseSuccess();
  134. // On est sur la page du groupe, bob doit voir le lien vers la page d'edition
  135. $this->exist('a[href="'.($url = $this->generateUrl('group_edit', array('slug' => $Fans_de_psytrance->getSlug()))).'"]');
  136. $link = $this->selectLink('a[href="'.$url.'"]');
  137. // On clique dessus
  138. $this->clickOnLink($link);
  139. $this->isResponseSuccess();
  140. // On vérifie al présence du forumaire
  141. $this->exist('form[action="'.($url = $this->generateUrl('group_update', array('slug' => $Fans_de_psytrance->getSlug()))).'"]');
  142. $this->exist('form[action="'.$url.'"] input[id="group_name"]');
  143. $this->exist('form[action="'.$url.'"] textarea[id="group_description"]');
  144. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  145. // On valide ce formulaire
  146. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  147. $form['group[name]'] = 'Les Fans de Psytrance';
  148. $form['group[description]'] = 'Ca va swiguer !';
  149. $form['group[tags]'] = json_encode(array($psytrance_id,$electro_id));
  150. $this->submit($form);
  151. $this->isResponseRedirection();
  152. $this->followRedirection();
  153. $this->isResponseSuccess();
  154. // Le groupe est bien modifié en base
  155. $group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  156. ->findOneBy(array(
  157. 'name' => 'Les Fans de Psytrance',
  158. 'description' => 'Ca va swiguer !'
  159. ))
  160. ;
  161. $this->assertTrue(!is_null($group));
  162. // On vérifie egallement le lien avec les tags
  163. if (!is_null($group))
  164. {
  165. $group_tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
  166. ->findBy(array(
  167. 'group' => $group->getId(),
  168. 'tag' => $psytrance_id
  169. ))
  170. ;
  171. $this->assertEquals(1, count($group_tags));
  172. $group_tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
  173. ->findBy(array(
  174. 'group' => $group->getId(),
  175. 'tag' => $electro_id
  176. ))
  177. ;
  178. $this->assertEquals(1, count($group_tags));
  179. }
  180. // Et sur la page aussi
  181. $this->exist('h2:contains("Les Fans de Psytrance")');
  182. }
  183. }