TagsTest.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace Muzich\CoreBundle\Tests\Controller;
  3. use Muzich\CoreBundle\lib\FunctionalTest;
  4. class TagsTest extends FunctionalTest
  5. {
  6. /**
  7. * Test du listing de ses favoris
  8. */
  9. public function testAddTag()
  10. {
  11. $this->client = self::createClient();
  12. $this->connectUser('paul', 'toor');
  13. // On commence par ajouter un tag
  14. $url = $this->generateUrl('ajax_add_tag');
  15. $crawler = $this->client->request('POST', $url, array('tag_name' => 'Mon Beau Tag'), array(), array(
  16. 'HTTP_X-Requested-With' => 'XMLHttpRequest',
  17. ));
  18. $this->isResponseSuccess();
  19. $response = json_decode($this->client->getResponse()->getContent(), true);
  20. $this->assertEquals($response['status'], 'success');
  21. $tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  22. ->findOneByName('Mon beau tag');
  23. $this->assertTrue(!is_null($tag));
  24. // Paul ajoute un élément avec ce tag
  25. $this->procedure_add_element(
  26. 'Un nouvel élément',
  27. 'http://www.youtube.com/watch?v=WC8qb_of04E',
  28. array($tag->getId())
  29. );
  30. $this->isResponseRedirection();
  31. $this->followRedirection();
  32. $this->isResponseSuccess();
  33. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  34. ->findOneByName('Un nouvel élément')
  35. ;
  36. $paul = $this->getUser();
  37. // Il ajoute cet élément en favoris
  38. $url = $this->generateUrl('favorite_add', array(
  39. 'id' => $element->getId(),
  40. 'token' => $paul->getPersonalHash()
  41. ));
  42. $crawler = $this->client->request('GET', $url, array(), array(), array(
  43. 'HTTP_X-Requested-With' => 'XMLHttpRequest',
  44. ));
  45. $this->isResponseSuccess();
  46. // On contrôle la présence du favoris
  47. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  48. ->findOneBy(array(
  49. 'user' => $paul->getId(),
  50. 'element' => $element->getId()
  51. ));
  52. $this->assertTrue(!is_null($fav));
  53. /*
  54. * Contrôle de la vue de ce tag
  55. */
  56. // Paul, l'ayant ajouté peut le voir.
  57. // sur la page home (l'élément qu'il vient d'envoyer)
  58. $this->client->request('GET', $this->generateUrl('home'));
  59. $this->isResponseSuccess();
  60. $this->exist('li.element_tag');
  61. $this->exist('li.element_tag:contains("Mon beau tag")');
  62. // sur sa page de favoris
  63. $this->client->request('GET', $this->generateUrl('favorites_my_list'));
  64. $this->isResponseSuccess();
  65. $this->exist('li.element_tag');
  66. $this->exist('li.element_tag:contains("Mon beau tag")');
  67. $this->exist('body:contains("Mon beau tag")');
  68. // Lors d'une recherche de tag
  69. $url = $this->generateUrl('search_tag', array(
  70. 'timestamp' => time()
  71. ));
  72. $crawler = $this->client->request('POST', $url, array('string_search' => 'mon beau tag'), array(), array(
  73. 'HTTP_X-Requested-With' => 'XMLHttpRequest'
  74. ));
  75. $this->isResponseSuccess();
  76. $response = json_decode($this->client->getResponse()->getContent(), true);
  77. $this->assertTrue($this->findTagNameInResponse($response, 'Mon beau tag', 'data'));
  78. // En revanche, bux ne pourra pas les voirs lui.
  79. // Sur la page home, sur la page des favoris de paul, et sur le profil de paul
  80. $this->disconnectUser();
  81. $this->client = self::createClient();
  82. $this->connectUser('bux', 'toor');
  83. // On enlève les tags pour être sur d'avoir l'élément ajouté par paul
  84. $this->client->request('GET', $this->generateUrl('filter_clear'));
  85. $this->client->request('GET', $this->generateUrl('home'));
  86. $this->isResponseSuccess();
  87. $this->exist('li.element_tag');
  88. $this->notExist('li.element_tag:contains("Mon beau tag")');
  89. // sur la page de favoris de paul
  90. $this->client->request('GET', $this->generateUrl('favorite_user_list', array('slug' => $paul->getSlug())));
  91. $this->isResponseSuccess();
  92. $this->exist('li.element_tag');
  93. $this->notExist('li.element_tag:contains("Mon beau tag")');
  94. $this->notExist('body:contains("Mon beau tag")');
  95. // sur la page de profil de paul
  96. $this->client->request('GET', $this->generateUrl('show_user', array('slug' => $paul->getSlug())));
  97. $this->isResponseSuccess();
  98. $this->exist('li.element_tag');
  99. $this->notExist('li.element_tag:contains("Mon beau tag")');
  100. $this->notExist('body:contains("Mon beau tag")');
  101. // Lors d'une recherche de tag
  102. $url = $this->generateUrl('search_tag', array(
  103. 'string_search' => 'mon beau tag',
  104. 'timestamp' => time()
  105. ));
  106. $crawler = $this->client->request('GET', $url, array(), array(), array(
  107. 'HTTP_X-Requested-With' => 'XMLHttpRequest'
  108. ));
  109. $this->isResponseSuccess();
  110. $response = json_decode($this->client->getResponse()->getContent(), true);
  111. $this->assertFalse($this->findTagNameInResponse($response, 'Mon beau tag', 'data'));
  112. }
  113. protected function findTagNameInResponse($response = array(), $name = null, $i = null)
  114. {
  115. if (count($response))
  116. {
  117. if (array_key_exists($i, $response))
  118. {
  119. foreach ($response[$i] as $tag)
  120. {
  121. if ($tag['name'] == $name)
  122. {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. }
  129. }
  130. }