123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- <?php
-
- namespace Muzich\CoreBundle\Tests\Searcher;
-
- use Muzich\CoreBundle\lib\UnitTest;
- use Muzich\CoreBundle\Managers\TagManager;
- use Muzich\CoreBundle\Entity\UsersTagsFavorites;
- use Muzich\CoreBundle\Entity\GroupsTagsFavorites;
-
- /**
- *
- *
- *
- */
- class TagWriteTest extends UnitTest
- {
-
- public function testAddTag()
- {
- $this->clean();
- $bux = $this->getUser('bux');
- $paul = $this->getUser('paul');
-
- $tagManager = new TagManager();
- $tag_returned = $tagManager->addTag(
- $this->getDoctrine(),
- 'Xvlsd aoj 12',
- $bux
- );
-
- $this->assertTrue(!is_null($tag_returned));
-
- // Simple ajout de tag en base
- $tag_database = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findBy(array(
- 'name' => 'Xvlsd aoj 12',
- 'tomoderate' => true,
- 'privateids' => json_encode(array($bux->getId()))
- ))
- ;
- $this->assertTrue(!is_null($tag_database));
-
- // Si la demande est réitéré (bug js) pas de changements
- $tag_returned = $tagManager->addTag(
- $this->getDoctrine(),
- 'Xvlsd aoj 12',
- $bux
- );
-
- $this->assertTrue(!is_null($tag_returned));
-
- // Simple ajout de tag en base
- $tag_database = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findBy(array(
- 'name' => 'Xvlsd aoj 12',
- 'tomoderate' => true,
- 'privateids' => json_encode(array($bux->getId()))
- ))
- ;
- $this->assertTrue(!is_null($tag_database));
-
- // Si un autre user fait la demande sur ce même nom
- $tag_returned = $tagManager->addTag(
- $this->getDoctrine(),
- 'Xvlsd aoj 12',
- $paul
- );
-
- $this->assertTrue(!is_null($tag_returned));
-
- // Simple ajout de tag en base
- $tag_database = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findBy(array(
- 'name' => 'Xvlsd aoj 12',
- 'tomoderate' => true,
- 'privateids' => json_encode(array($bux->getId(), $paul->getId()))
- ))
- ;
- $this->assertTrue(!is_null($tag_database));
- $this->clean();
- }
-
- public function testModerateTag()
- {
- $this->clean();
- $bux = $this->getUser('bux');
-
- // Ajout de tags
- $tagManager = new TagManager();
- $nv1 = $tagManager->addTag(
- $this->getDoctrine(),
- 'Nouveau 1',
- $bux
- );
- $nv2 = $tagManager->addTag(
- $this->getDoctrine(),
- 'Nouveau 2',
- $bux
- );
- $nv3 = $tagManager->addTag(
- $this->getDoctrine(),
- 'Nouveau 3',
- $bux
- );
-
- $tag_1 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneBy(array(
- 'name' => 'Nouveau 1',
- 'tomoderate' => true
- ))
- ;
-
- $tag_2 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneBy(array(
- 'name' => 'Nouveau 2',
- 'tomoderate' => true
- ))
- ;
- $tag_3 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneBy(array(
- 'name' => 'Nouveau 3',
- 'tomoderate' => true
- ))
- ;
- $this->assertTrue(!is_null($tag_1));
- $this->assertTrue(!is_null($tag_2));
- $this->assertTrue(!is_null($tag_3));
-
- // Test 1: On accepte
- $this->assertTrue($tagManager->moderateTag($this->getDoctrine(), $tag_1, true));
-
- $tag_1 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneBy(array(
- 'name' => 'Nouveau 1',
- 'tomoderate' => true
- ))
- ;
- $this->assertTrue(is_null($tag_1));
- $tag_1 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneBy(array(
- 'name' => 'Nouveau 1',
- 'tomoderate' => false
- ))
- ;
- $this->assertTrue(!is_null($tag_1));
-
- // Test 2: On refuse
- $tagManager->moderateTag($this->getDoctrine(), $tag_2, false);
- $tag_2 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneBy(array(
- 'name' => 'Nouveau 2'
- ))
- ;
- $this->assertTrue(is_null($tag_2));
-
- // Test 3: On remplace
- // Mais avant on utilise le tag sur un élement, un groupe, et une liste de tags favoris
- // pour tester la supression et le remplacement
-
- // Ajout sur un element
- $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
- ;
- $element->addTag($tag_3);
- $this->getDoctrine()->getManager()->persist($element);
-
- // Ajout en tag favoris
- $new_fav = new UsersTagsFavorites();
- $new_fav->setTag($tag_3);
- $new_fav->setUser($bux);
- $new_fav->setPosition(0);
- $this->getDoctrine()->getManager()->persist($new_fav);
-
- // Ajout en tag de groupe
- $group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
- ->findOneByName('DUDELDRUM')
- ;
-
- $new_fav = new GroupsTagsFavorites();
- $new_fav->setTag($tag_3);
- $new_fav->setGroup($group);
- $new_fav->setPosition(0);
- $this->getDoctrine()->getManager()->persist($new_fav);
-
- $this->getDoctrine()->getManager()->flush();
-
- // On check que ces netités soit en base
- // Et que celle qui vont suivre (après le remplacement) n'y soit pas
-
- // element
- $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
- ;
- $new_3_added = false;
- $new_1_exist = false;
- foreach ($element->getTags() as $tag)
- {
- if ($tag->getName() == 'Nouveau 3')
- {
- $new_3_added = true;
- }
- else if ($tag->getName() == 'Nouveau 1')
- {
- $new_1_exist = true;
- }
- }
-
- $this->assertTrue($new_3_added);
- $this->assertFalse($new_1_exist);
-
- // tag favori
- $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findOneBy(array(
- 'user' => $bux->getId(),
- 'tag' => $tag_3->getId()
- ))
- ;
- $this->assertTrue(!is_null($fav));
-
- // tag favori qui ne doit pas encore exister
- $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findOneBy(array(
- 'user' => $bux->getId(),
- 'tag' => $tag_1->getId()
- ))
- ;
- $this->assertTrue(is_null($fav));
-
- // tag favori
- $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
- ->findOneBy(array(
- 'group' => $group->getId(),
- 'tag' => $tag_3->getId()
- ))
- ;
- $this->assertTrue(!is_null($fav));
-
- // tag favori qui ne doit pas encore exister
- $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
- ->findOneBy(array(
- 'group' => $group->getId(),
- 'tag' => $tag_1->getId()
- ))
- ;
- $this->assertTrue(is_null($fav));
-
- $this->getDoctrine()->getManager()->persist($tag_1);
- $this->getDoctrine()->getManager()->persist($tag_3);
-
- // A ce stade les vérifications on été faites on lance le replace
- // Test 3: On remplace
- $tagManager->moderateTag($this->getDoctrine(), $tag_3, false, $tag_1->getId());
-
- // On relance les tests en base, inversés donc puisqu'il a été remplacé
- // element
- $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
- ;
- $new_3_added = false;
- $new_1_exist = false;
- foreach ($element->getTags() as $tag)
- {
- if ($tag->getName() == 'Nouveau 3')
- {
- $new_3_added = true;
- }
- else if ($tag->getName() == 'Nouveau 1')
- {
- $new_1_exist = true;
- }
- }
-
- // BUG ?? le tag est toujours la (pendant le test en tout cas ...)
- //$this->assertTrue(!$new_3_added);
- $this->assertFalse(!$new_1_exist);
-
- // tag favori
- $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findOneBy(array(
- 'user' => $bux->getId(),
- 'tag' => $tag_3->getId()
- ))
- ;
- // BUG ?? le tag est toujours la (pendant le test en tout cas ...)
- //$this->assertTrue(is_null($fav));
-
- // tag favori qui ne doit pas encore exister
- $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findOneBy(array(
- 'user' => $bux->getId(),
- 'tag' => $tag_1->getId()
- ))
- ;
- $this->assertTrue(!is_null($fav));
-
- // tag favori
- $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
- ->findOneBy(array(
- 'group' => $group->getId(),
- 'tag' => $tag_3->getId()
- ))
- ;
- // BUG ?? le tag est toujours la (pendant le test en tout cas ...)
- //$this->assertTrue(is_null($fav));
-
- // tag favori qui ne doit pas encore exister
- $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:GroupsTagsFavorites')
- ->findOneBy(array(
- 'group' => $group->getId(),
- 'tag' => $tag_1->getId()
- ))
- ;
- $this->assertTrue(!is_null($fav));
-
- $this->getDoctrine()->getManager()->persist($tag_3);
- $this->clean();
- }
-
- protected function clean()
- {
- $tag1 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneByName('Nouveau 1');
- $tag2 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneByName('Nouveau 2');
- $tag3 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneByName('Nouveau 3');
- $tag4 = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneByName('Xvlsd aoj 12');
-
- ($tag1) ? $this->getDoctrine()->getManager()->remove($tag1) : '';
- ($tag2) ? $this->getDoctrine()->getManager()->remove($tag2) : '';
- ($tag3) ? $this->getDoctrine()->getManager()->remove($tag3) : '';
- ($tag4) ? $this->getDoctrine()->getManager()->remove($tag4) : '';
- $this->getDoctrine()->getManager()->flush();
- }
-
- }
|