TagWriteTest.php 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace Muzich\CoreBundle\Tests\Searcher;
  3. use Muzich\CoreBundle\lib\UnitTest;
  4. use Muzich\CoreBundle\Managers\TagManager;
  5. use Muzich\CoreBundle\Entity\UsersTagsFavorites;
  6. use Muzich\CoreBundle\Entity\GroupsTagsFavorites;
  7. /**
  8. *
  9. *
  10. *
  11. */
  12. class TagWriteTest extends UnitTest
  13. {
  14. public function testAddTag()
  15. {
  16. $this->clean();
  17. $bux = $this->getUser('bux');
  18. $paul = $this->getUser('paul');
  19. $tagManager = new TagManager();
  20. $tag_returned = $tagManager->addTag(
  21. $this->getDoctrine(),
  22. 'Xvlsd aoj 12',
  23. $bux
  24. );
  25. $this->assertTrue(!is_null($tag_returned));
  26. // Simple ajout de tag en base
  27. $tag_database = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  28. ->findBy(array(
  29. 'name' => 'Xvlsd aoj 12',
  30. 'tomoderate' => true,
  31. 'privateids' => json_encode(array($bux->getId()))
  32. ))
  33. ;
  34. $this->assertTrue(!is_null($tag_database));
  35. // Si la demande est réitéré (bug js) pas de changements
  36. $tag_returned = $tagManager->addTag(
  37. $this->getDoctrine(),
  38. 'Xvlsd aoj 12',
  39. $bux
  40. );
  41. $this->assertTrue(!is_null($tag_returned));
  42. // Simple ajout de tag en base
  43. $tag_database = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  44. ->findBy(array(
  45. 'name' => 'Xvlsd aoj 12',
  46. 'tomoderate' => true,
  47. 'privateids' => json_encode(array($bux->getId()))
  48. ))
  49. ;
  50. $this->assertTrue(!is_null($tag_database));
  51. // Si un autre user fait la demande sur ce même nom
  52. $tag_returned = $tagManager->addTag(
  53. $this->getDoctrine(),
  54. 'Xvlsd aoj 12',
  55. $paul
  56. );
  57. $this->assertTrue(!is_null($tag_returned));
  58. // Simple ajout de tag en base
  59. $tag_database = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  60. ->findBy(array(
  61. 'name' => 'Xvlsd aoj 12',
  62. 'tomoderate' => true,
  63. 'privateids' => json_encode(array($bux->getId(), $paul->getId()))
  64. ))
  65. ;
  66. $this->assertTrue(!is_null($tag_database));
  67. $this->clean();
  68. }
  69. public function testModerateTag()
  70. {
  71. $this->clean();
  72. $bux = $this->getUser('bux');
  73. // Ajout de tags
  74. $tagManager = new TagManager();
  75. $nv1 = $tagManager->addTag(
  76. $this->getDoctrine(),
  77. 'Nouveau 1',
  78. $bux
  79. );
  80. $nv2 = $tagManager->addTag(
  81. $this->getDoctrine(),
  82. 'Nouveau 2',
  83. $bux
  84. );
  85. $nv3 = $tagManager->addTag(
  86. $this->getDoctrine(),
  87. 'Nouveau 3',
  88. $bux
  89. );
  90. $tag_1 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  91. ->findOneBy(array(
  92. 'name' => 'Nouveau 1',
  93. 'tomoderate' => true
  94. ))
  95. ;
  96. $tag_2 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  97. ->findOneBy(array(
  98. 'name' => 'Nouveau 2',
  99. 'tomoderate' => true
  100. ))
  101. ;
  102. $tag_3 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  103. ->findOneBy(array(
  104. 'name' => 'Nouveau 3',
  105. 'tomoderate' => true
  106. ))
  107. ;
  108. $this->assertTrue(!is_null($tag_1));
  109. $this->assertTrue(!is_null($tag_2));
  110. $this->assertTrue(!is_null($tag_3));
  111. // Test 1: On accepte
  112. $this->assertTrue($tagManager->moderateTag($this->getDoctrine(), $tag_1, true));
  113. $tag_1 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  114. ->findOneBy(array(
  115. 'name' => 'Nouveau 1',
  116. 'tomoderate' => true
  117. ))
  118. ;
  119. $this->assertTrue(is_null($tag_1));
  120. $tag_1 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  121. ->findOneBy(array(
  122. 'name' => 'Nouveau 1',
  123. 'tomoderate' => false
  124. ))
  125. ;
  126. $this->assertTrue(!is_null($tag_1));
  127. // Test 2: On refuse
  128. $tagManager->moderateTag($this->getDoctrine(), $tag_2, false);
  129. $tag_2 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  130. ->findOneBy(array(
  131. 'name' => 'Nouveau 2'
  132. ))
  133. ;
  134. $this->assertTrue(is_null($tag_2));
  135. // Test 3: On remplace
  136. // Mais avant on utilise le tag sur un élement, un groupe, et une liste de tags favoris
  137. // pour tester la supression et le remplacement
  138. // Ajout sur un element
  139. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  140. ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
  141. ;
  142. $element->addTag($tag_3);
  143. $this->getDoctrine()->getManager()->persist($element);
  144. // Ajout en tag favoris
  145. $new_fav = new UsersTagsFavorites();
  146. $new_fav->setTag($tag_3);
  147. $new_fav->setUser($bux);
  148. $new_fav->setPosition(0);
  149. $this->getDoctrine()->getManager()->persist($new_fav);
  150. // Ajout en tag de groupe
  151. $group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  152. ->findOneByName('DUDELDRUM')
  153. ;
  154. $new_fav = new GroupsTagsFavorites();
  155. $new_fav->setTag($tag_3);
  156. $new_fav->setGroup($group);
  157. $new_fav->setPosition(0);
  158. $this->getDoctrine()->getManager()->persist($new_fav);
  159. $this->getDoctrine()->getManager()->flush();
  160. // On check que ces netités soit en base
  161. // Et que celle qui vont suivre (après le remplacement) n'y soit pas
  162. // element
  163. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  164. ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
  165. ;
  166. $new_3_added = false;
  167. $new_1_exist = false;
  168. foreach ($element->getTags() as $tag)
  169. {
  170. if ($tag->getName() == 'Nouveau 3')
  171. {
  172. $new_3_added = true;
  173. }
  174. else if ($tag->getName() == 'Nouveau 1')
  175. {
  176. $new_1_exist = true;
  177. }
  178. }
  179. $this->assertTrue($new_3_added);
  180. $this->assertFalse($new_1_exist);
  181. // tag favori
  182. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  183. ->findOneBy(array(
  184. 'user' => $bux->getId(),
  185. 'tag' => $tag_3->getId()
  186. ))
  187. ;
  188. $this->assertTrue(!is_null($fav));
  189. // tag favori qui ne doit pas encore exister
  190. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  191. ->findOneBy(array(
  192. 'user' => $bux->getId(),
  193. 'tag' => $tag_1->getId()
  194. ))
  195. ;
  196. $this->assertTrue(is_null($fav));
  197. // tag favori
  198. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
  199. ->findOneBy(array(
  200. 'group' => $group->getId(),
  201. 'tag' => $tag_3->getId()
  202. ))
  203. ;
  204. $this->assertTrue(!is_null($fav));
  205. // tag favori qui ne doit pas encore exister
  206. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
  207. ->findOneBy(array(
  208. 'group' => $group->getId(),
  209. 'tag' => $tag_1->getId()
  210. ))
  211. ;
  212. $this->assertTrue(is_null($fav));
  213. $this->getDoctrine()->getManager()->persist($tag_1);
  214. $this->getDoctrine()->getManager()->persist($tag_3);
  215. // A ce stade les vérifications on été faites on lance le replace
  216. // Test 3: On remplace
  217. $tagManager->moderateTag($this->getDoctrine(), $tag_3, false, $tag_1->getId());
  218. // On relance les tests en base, inversés donc puisqu'il a été remplacé
  219. // element
  220. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  221. ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
  222. ;
  223. $new_3_added = false;
  224. $new_1_exist = false;
  225. foreach ($element->getTags() as $tag)
  226. {
  227. if ($tag->getName() == 'Nouveau 3')
  228. {
  229. $new_3_added = true;
  230. }
  231. else if ($tag->getName() == 'Nouveau 1')
  232. {
  233. $new_1_exist = true;
  234. }
  235. }
  236. // BUG ?? le tag est toujours la (pendant le test en tout cas ...)
  237. //$this->assertTrue(!$new_3_added);
  238. $this->assertFalse(!$new_1_exist);
  239. // tag favori
  240. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  241. ->findOneBy(array(
  242. 'user' => $bux->getId(),
  243. 'tag' => $tag_3->getId()
  244. ))
  245. ;
  246. // BUG ?? le tag est toujours la (pendant le test en tout cas ...)
  247. //$this->assertTrue(is_null($fav));
  248. // tag favori qui ne doit pas encore exister
  249. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  250. ->findOneBy(array(
  251. 'user' => $bux->getId(),
  252. 'tag' => $tag_1->getId()
  253. ))
  254. ;
  255. $this->assertTrue(!is_null($fav));
  256. // tag favori
  257. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
  258. ->findOneBy(array(
  259. 'group' => $group->getId(),
  260. 'tag' => $tag_3->getId()
  261. ))
  262. ;
  263. // BUG ?? le tag est toujours la (pendant le test en tout cas ...)
  264. //$this->assertTrue(is_null($fav));
  265. // tag favori qui ne doit pas encore exister
  266. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
  267. ->findOneBy(array(
  268. 'group' => $group->getId(),
  269. 'tag' => $tag_1->getId()
  270. ))
  271. ;
  272. $this->assertTrue(!is_null($fav));
  273. $this->getDoctrine()->getManager()->persist($tag_3);
  274. $this->clean();
  275. }
  276. protected function clean()
  277. {
  278. $tag1 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  279. ->findOneByName('Nouveau 1');
  280. $tag2 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  281. ->findOneByName('Nouveau 2');
  282. $tag3 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  283. ->findOneByName('Nouveau 3');
  284. $tag4 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  285. ->findOneByName('Xvlsd aoj 12');
  286. ($tag1) ? $this->getDoctrine()->getManager()->remove($tag1) : '';
  287. ($tag2) ? $this->getDoctrine()->getManager()->remove($tag2) : '';
  288. ($tag3) ? $this->getDoctrine()->getManager()->remove($tag3) : '';
  289. ($tag4) ? $this->getDoctrine()->getManager()->remove($tag4) : '';
  290. $this->getDoctrine()->getManager()->flush();
  291. }
  292. }