HomeControllerTest.php 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. <?php
  2. namespace Muzich\CoreBundle\Tests\Controller;
  3. use Muzich\CoreBundle\lib\FunctionalTest;
  4. use Muzich\CoreBundle\Searcher\ElementSearcher;
  5. use Muzich\CoreBundle\Entity\Element;
  6. class HomeControllerTest extends FunctionalTest
  7. {
  8. /**
  9. * Ce test contrôle l'affichage des elements sur la page d'accueil
  10. * Il modifie egallement le filtre d'éléments
  11. */
  12. public function testFilter()
  13. {
  14. $this->client = self::createClient();
  15. $this->connectUser('bux', 'toor');
  16. // Présence du formulaire d'ajout d'un élément
  17. $this->exist('form[action="'.($url = $this->generateUrl('element_add')).'"]');
  18. $this->exist('form[action="'.$url.'"] input[id="element_add_name"]');
  19. $this->exist('form[action="'.$url.'"] input[id="element_add_url"]');
  20. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  21. // Présence du formulaire de filtrage
  22. $this->exist('form[action="'.($url = $this->generateUrl('search_elements', array('context'=>'home'))).'"]');
  23. $this->exist('form[action="'.$url.'"] select[id="element_search_form_network"]');
  24. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  25. $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
  26. $tribe_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
  27. // On récupére le formulaire de filtrage
  28. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  29. // On met ce que l'on veut dans le form
  30. $form['element_search_form[network]'] = ElementSearcher::NETWORK_PUBLIC;
  31. $form['element_search_form[tags]'] = json_encode(array($hardtek_id, $tribe_id));
  32. $this->submit($form);
  33. $this->client->submit($form);
  34. $this->isResponseRedirection();
  35. $this->followRedirection();
  36. $this->isResponseSuccess();
  37. $this->assertTrue($this->getSession()->has('user.element_search.params'));
  38. $this->assertEquals(array(
  39. 'network' => ElementSearcher::NETWORK_PUBLIC,
  40. 'tags' => array(
  41. $hardtek_id => 'Hardtek',
  42. $tribe_id => 'Tribe'
  43. ),
  44. 'count' => $this->getContainer()->getParameter('search_default_count'),
  45. 'user_id' => null,
  46. 'group_id' => null,
  47. 'favorite' => false,
  48. 'ids' => null,
  49. 'ids_display' => null,
  50. 'tag_strict' => false,
  51. 'string' => null,
  52. 'need_tags' => false,
  53. ), $this->getSession()->get('user.element_search.params'));
  54. // On fabrique l'ElementSearcher correspondant
  55. $es = new ElementSearcher();
  56. $es->init($this->getSession()->get('user.element_search.params'));
  57. foreach ($es->getElements($this->getDoctrine(), $this->getUser()->getId()) as $element)
  58. {
  59. $this->exist('html:contains("'.$element->getName().'")');
  60. }
  61. }
  62. /**
  63. * Test de la présence des elements sur la page d'un utilisateur
  64. */
  65. public function testUserPage()
  66. {
  67. $this->client = self::createClient();
  68. $this->connectUser('bux', 'toor');
  69. $jean = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  70. ->findOneByUsername('jean')
  71. ;
  72. $this->crawler = $this->client->request(
  73. 'GET',
  74. $this->generateUrl('show_user', array('slug' => $jean->getSlug()))
  75. );
  76. $this->isResponseSuccess();
  77. $this->exist('h1:contains("'.$jean->getName().'")');
  78. $es = new ElementSearcher();
  79. $es->init(array(
  80. 'user_id' => $jean->getId()
  81. ));
  82. foreach ($es->getElements($this->getDoctrine(), $this->getUser()->getId()) as $element)
  83. {
  84. $this->exist('html:contains("'.$element->getName().'")');
  85. }
  86. }
  87. /**
  88. * Test de la présence des elements sur la page d'un utilisateur
  89. */
  90. public function testGroupPage()
  91. {
  92. $this->client = self::createClient();
  93. $this->connectUser('bux', 'toor');
  94. $fdp = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  95. ->findOneBySlug('fans-de-psytrance')
  96. ->getSingleResult()
  97. ;
  98. $this->crawler = $this->client->request(
  99. 'GET',
  100. $this->generateUrl('show_group', array('slug' => $fdp->getSlug()))
  101. );
  102. $this->isResponseSuccess();
  103. $this->exist('h1:contains("'.$fdp->getName().'")');
  104. $es = new ElementSearcher();
  105. $es->init(array(
  106. 'group_id' => $fdp->getId()
  107. ));
  108. foreach ($es->getElements($this->getDoctrine(), $this->getUser()->getId()) as $element)
  109. {
  110. $this->exist('html:contains("'.$element->getName().'")');
  111. }
  112. }
  113. /**
  114. * Ajouts d'éléments et tests de cas refusés
  115. */
  116. public function testAddElementSuccess()
  117. {
  118. $this->client = self::createClient();
  119. $this->connectUser('bux', 'toor');
  120. $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
  121. $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
  122. /*
  123. * Ajout d'un élément avec succés
  124. */
  125. $this->procedure_add_element(
  126. 'Mon bel element',
  127. 'http://www.youtube.com/watch?v=WC8qb_of04E',
  128. array($hardtek->getId(), $tribe->getId()),
  129. null, true
  130. );
  131. $this->isResponseRedirection();
  132. $this->followRedirection();
  133. $this->isResponseSuccess();
  134. $this->exist('li:contains("Mon bel element")');
  135. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  136. ->findOneByName('Mon bel element')
  137. ;
  138. $this->assertTrue(!is_null($element));
  139. }
  140. /**
  141. * Ajouts d'éléments et tests de cas refusés
  142. */
  143. public function testAddElementFailure()
  144. {
  145. $this->client = self::createClient();
  146. $this->connectUser('bux', 'toor');
  147. $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
  148. $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
  149. /*
  150. * Ajouts d'éléments avec echec
  151. */
  152. // Nom trop court
  153. $this->procedure_add_element(
  154. 'Mo',
  155. 'http://www.youtube.com/watch?v=WC8qb_of04E',
  156. array($hardtek->getId(), $tribe->getId())
  157. );
  158. $this->isResponseSuccess();
  159. $this->notExist('li:contains("Mon bel element a4er563a1r")');
  160. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  161. ->findOneByName('Mon bel element a4er563a1r')
  162. ;
  163. $this->assertTrue(is_null($element));
  164. // Nom trop long
  165. $this->procedure_add_element(
  166. 'Mon bel element mais qui a un nom trop court la vache oui trop long hohoho la vache oui trop long hohoho la vache oui trop long hohoho',
  167. 'http://www.youtube.com/watch?v=WC8qb_of04E',
  168. array($hardtek->getId(), $tribe->getId())
  169. );
  170. $this->isResponseSuccess();
  171. $this->notExist('li:contains("Mon bel element mais qui a un nom trop court la vache oui trop long hohoho")');
  172. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  173. ->findOneByName('Mon bel element mais qui a un nom trop court la vache oui trop long hohoho')
  174. ;
  175. $this->assertTrue(is_null($element));
  176. // Pas d'url
  177. $this->procedure_add_element(
  178. 'Mon bel element',
  179. '',
  180. array($hardtek->getId(), $tribe->getId())
  181. );
  182. $this->isResponseSuccess();
  183. $this->notExist('li:contains("Mon bel element gfez7f")');
  184. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  185. ->findOneByName('Mon bel element gfez7f')
  186. ;
  187. $this->assertTrue(is_null($element));
  188. // url non conforme
  189. $this->procedure_add_element(
  190. 'Mon bel element 789e',
  191. 'http://',
  192. array($hardtek->getId(), $tribe->getId())
  193. );
  194. $this->isResponseSuccess();
  195. $this->notExist('li:contains("Mon bel element 789e")');
  196. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  197. ->findOneByName('Mon bel element 789e')
  198. ;
  199. $this->assertTrue(is_null($element));
  200. // url non conforme
  201. $this->procedure_add_element(
  202. 'Mon bel element 789f',
  203. 'http://youtube',
  204. array($hardtek->getId(), $tribe->getId())
  205. );
  206. $this->isResponseSuccess();
  207. $this->notExist('li:contains("Mon bel element 789f")');
  208. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  209. ->findOneByName('Mon bel element 789f')
  210. ;
  211. $this->assertTrue(is_null($element));
  212. // url non conforme
  213. $this->procedure_add_element(
  214. 'Mon bel element 789g',
  215. 'youtube.com?lalala',
  216. array($hardtek->getId(), $tribe->getId())
  217. );
  218. $this->isResponseSuccess();
  219. $this->notExist('li:contains("Mon bel element 789g")');
  220. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  221. ->findOneByName('Mon bel element 789g')
  222. ;
  223. $this->assertTrue(is_null($element));
  224. // Pas de nom
  225. $this->procedure_add_element(
  226. '',
  227. 'http://www.youtube.com/watch?v=WC8qb_of04E',
  228. array($hardtek->getId(), $tribe->getId())
  229. );
  230. $this->isResponseSuccess();
  231. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  232. ->findOneByName('')
  233. ;
  234. $this->assertTrue(is_null($element));
  235. }
  236. /**
  237. * L'ajout d'un Element a un de ses groupe ne doit pas poser de problème
  238. */
  239. public function testAddElementAtMyGroupSuccess()
  240. {
  241. $this->client = self::createClient();
  242. $this->connectUser('bux', 'toor');
  243. // Un groupe open, donc pas de soucis
  244. $fan_de_psy = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
  245. $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
  246. $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
  247. $this->isResponseSuccess();
  248. $this->procedure_add_element(
  249. 'Element mis dans le groupe de psytrance',
  250. 'http://www.youtube.com/watch?v=WC8qb_of04E',
  251. array($hardtek->getId(), $tribe->getId()),
  252. $fan_de_psy->getSlug()
  253. );
  254. $this->isResponseRedirection();
  255. $this->followRedirection();
  256. $this->isResponseSuccess();
  257. $this->exist('li:contains("Element mis dans le groupe de psytrance")');
  258. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  259. ->findOneByName('Element mis dans le groupe de psytrance')
  260. ;
  261. $this->assertTrue(!is_null($element));
  262. if (!is_null($element))
  263. {
  264. $this->assertEquals($fan_de_psy->getId(), $element->getGroup()->getId());
  265. }
  266. else
  267. {
  268. $this->assertTrue(false);
  269. }
  270. $this->disconnectUser();
  271. /*
  272. * Ajout d'un element dans un groupe que l'on posséde.
  273. */
  274. $this->client = self::createClient();
  275. $this->connectUser('joelle', 'toor');
  276. $this->isResponseSuccess();
  277. // Ce groupe appartient a joelle
  278. $groupe_de_joelle = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Le groupe de joelle');
  279. $this->procedure_add_element(
  280. 'Element mis dans le groupe de joelle',
  281. 'http://www.youtube.com/watch?v=WC8qb_of04E',
  282. array($hardtek->getId(), $tribe->getId()),
  283. $groupe_de_joelle->getSlug()
  284. );
  285. $this->isResponseRedirection();
  286. $this->followRedirection();
  287. $this->isResponseSuccess();
  288. $this->exist('li:contains("Element mis dans le groupe de joelle")');
  289. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  290. ->findOneByName('Element mis dans le groupe de joelle')
  291. ;
  292. $this->assertTrue(!is_null($element));
  293. if (!is_null($element))
  294. {
  295. $this->assertEquals($groupe_de_joelle->getId(), $element->getGroup()->getId());
  296. }
  297. else
  298. {
  299. $this->assertTrue(false);
  300. }
  301. }
  302. /**
  303. * L'ajout a un group qui n'est pas a sois, ou qui n'est pas open
  304. * doit être impossible.
  305. */
  306. public function testAddElementAtGroupFailure()
  307. {
  308. $this->client = self::createClient();
  309. $this->connectUser('bux', 'toor');
  310. // Un groupe no open
  311. $dudeldrum = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('DUDELDRUM');
  312. $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
  313. $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
  314. // Nous tentons d'ouvrir l'url d'ajout d'élément avec un groupe qui n'est pas ouvert
  315. // et qui n'appartient pas a l'utilisateur connecté
  316. $this->crawler = $this->client->request(
  317. 'POST',
  318. $this->generateUrl('element_add', array('group_slug' => $dudeldrum->getSlug())),
  319. array(
  320. 'element_add[name]' => 'Yohoho trululu',
  321. 'element_add[url]' => 'http://www.youtube.com/watch?v=WC8qb_of04E',
  322. 'element_add[tags]['.$hardtek->getId().']' => $hardtek->getId(),
  323. 'element_add[tags]['.$tribe->getId().']' => $tribe->getId()
  324. )
  325. );
  326. $this->isResponseNotFound();
  327. }
  328. /**
  329. * Test de la fonction ajax de récupération de plus d'éléments sur la page
  330. * de profil
  331. */
  332. public function testFilterUserElements()
  333. {
  334. $this->client = self::createClient();
  335. $this->connectUser('paul', 'toor');
  336. $paul = $this->getUser();
  337. $bux = $this->getUser('bux');
  338. $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  339. ->findOneByName('Hardtek')->getId();
  340. $hardcore_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  341. ->findOneByName('Hardcore')->getId();
  342. $electro_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  343. ->findOneByName('Electro')->getId();
  344. $metal_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  345. ->findOneByName('Metal')->getId();
  346. // Ouverture de la page favoris
  347. $this->crawler = $this->client->request('GET', $this->generateUrl('show_user', array('slug' => $bux->getSlug())));
  348. // On doit voir deux elements pour paul
  349. $this->exist('span.element_name:contains("Ed Cox - La fanfare des teuffeurs (Hardcordian)")');
  350. $this->exist('span.element_name:contains("Babylon Pression - Des Tasers et des Pauvres")');
  351. $this->exist('span.element_name:contains("AZYD AZYLUM Live au Café Provisoire")');
  352. $this->exist('span.element_name:contains("SOULFLY - Prophecy")');
  353. $this->exist('span.element_name:contains("KoinkOin - H5N1")');
  354. $this->exist('span.element_name:contains("Antropod - Polakatek")');
  355. $this->exist('span.element_name:contains("Dtc che passdrop")');
  356. $this->exist('span.element_name:contains("Heretik System Popof - Resistance")');
  357. // Récupération de la liste avec la kekete ajax pour Tribe
  358. $url = $this->generateUrl('show_elements_get', array(
  359. 'type' => 'user',
  360. 'object_id' => $bux->getId(),
  361. 'tags_ids_json' => json_encode(array($hardtek_id))
  362. ));
  363. $this->crawler = $this->client->request('GET', $url, array(), array(), array(
  364. 'HTTP_X-Requested-With' => 'XMLHttpRequest',
  365. ));
  366. $response_content = json_decode($this->client->getResponse()->getContent(), true);
  367. $html = $response_content['html'];
  368. $this->assertTrue(strpos($html, 'Ed Cox - La fanfare des teuffeurs (Hardcordian)') === false);
  369. $this->assertTrue(strpos($html, 'Babylon Pression - Des Tasers et des Pauvres') === false);
  370. $this->assertTrue(strpos($html, 'AZYD AZYLUM Live au Café Provisoire') === false);
  371. $this->assertTrue(strpos($html, 'SOULFLY - Prophecy') === false);
  372. $this->assertTrue(strpos($html, 'KoinkOin - H5N1') !== false);
  373. $this->assertTrue(strpos($html, 'Antropod - Polakatek') !== false);
  374. $this->assertTrue(strpos($html, 'Dtc che passdrop') !== false);
  375. $this->assertTrue(strpos($html, 'Heretik System Popof - Resistance') !== false);
  376. // Récupération de la liste avec la kekete ajax pour Hardtek
  377. $url = $this->generateUrl('show_elements_get', array(
  378. 'type' => 'user',
  379. 'object_id' => $bux->getId(),
  380. 'tags_ids_json' => json_encode(array($metal_id))
  381. ));
  382. $this->crawler = $this->client->request('GET', $url, array(), array(), array(
  383. 'HTTP_X-Requested-With' => 'XMLHttpRequest',
  384. ));
  385. $this->isResponseSuccess();
  386. $response_content = json_decode($this->client->getResponse()->getContent(), true);
  387. $html = $response_content['html'];
  388. $this->assertTrue(strpos($html, 'Ed Cox - La fanfare des teuffeurs (Hardcordian)') === false);
  389. $this->assertTrue(strpos($html, 'Babylon Pression - Des Tasers et des Pauvres') !== false);
  390. $this->assertTrue(strpos($html, 'AZYD AZYLUM Live au Café Provisoire') !== false);
  391. $this->assertTrue(strpos($html, 'SOULFLY - Prophecy') !== false);
  392. $this->assertTrue(strpos($html, 'KoinkOin - H5N1') === false);
  393. $this->assertTrue(strpos($html, 'Antropod - Polakatek') === false);
  394. $this->assertTrue(strpos($html, 'Dtc che passdrop') === false);
  395. $this->assertTrue(strpos($html, 'Heretik System Popof - Resistance') === false);
  396. // Récupération de la liste avec la kekete ajax pour Tribe + Hardtek
  397. $url = $this->generateUrl('show_elements_get', array(
  398. 'type' => 'user',
  399. 'object_id' => $bux->getId(),
  400. 'tags_ids_json' => json_encode(array($hardtek_id, $hardcore_id, $electro_id, $metal_id))
  401. ));
  402. $this->crawler = $this->client->request('GET', $url, array(), array(), array(
  403. 'HTTP_X-Requested-With' => 'XMLHttpRequest',
  404. ));
  405. $this->isResponseSuccess();
  406. $response_content = json_decode($this->client->getResponse()->getContent(), true);
  407. $html = $response_content['html'];
  408. $this->assertTrue(strpos($html, 'Ed Cox - La fanfare des teuffeurs (Hardcordian)') !== false);
  409. $this->assertTrue(strpos($html, 'Babylon Pression - Des Tasers et des Pauvres') !== false);
  410. $this->assertTrue(strpos($html, 'AZYD AZYLUM Live au Café Provisoire') !== false);
  411. $this->assertTrue(strpos($html, 'SOULFLY - Prophecy') !== false);
  412. $this->assertTrue(strpos($html, 'KoinkOin - H5N1') !== false);
  413. $this->assertTrue(strpos($html, 'Antropod - Polakatek') !== false);
  414. $this->assertTrue(strpos($html, 'Dtc che passdrop') !== false);
  415. $this->assertTrue(strpos($html, 'Heretik System Popof - Resistance') !== false);
  416. }
  417. /**
  418. * Test de la fonction ajax de récupération de plus d'éléments sur la page
  419. * de profil
  420. */
  421. public function testFilterGroupElements()
  422. {
  423. $this->client = self::createClient();
  424. $this->connectUser('paul', 'toor');
  425. $paul = $this->getUser();
  426. $group = $this->getGroup('dudeldrum');
  427. $medieval_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  428. ->findOneByName('Medieval')->getId();
  429. // Ouverture de la page favoris
  430. $this->crawler = $this->client->request('GET', $this->generateUrl('show_group', array('slug' => $group->getSlug())));
  431. // On doit voir deux elements pour paul
  432. $this->exist('span.element_name:contains("DUDELDRUM")');
  433. // Récupération de la liste avec la kekete ajax pour Tribe
  434. $url = $this->generateUrl('show_elements_get', array(
  435. 'type' => 'group',
  436. 'object_id' => $group->getId(),
  437. 'tags_ids_json' => json_encode(array($medieval_id))
  438. ));
  439. $this->crawler = $this->client->request('GET', $url, array(), array(), array(
  440. 'HTTP_X-Requested-With' => 'XMLHttpRequest',
  441. ));
  442. $response_content = json_decode($this->client->getResponse()->getContent(), true);
  443. $html = $response_content['html'];
  444. $this->assertTrue(strpos($html, 'DUDELDRUM') !== false);
  445. }
  446. /**
  447. * Test de la récupération de nouveaux éléments
  448. *
  449. */
  450. public function testSeeNew()
  451. {
  452. $this->client = self::createClient();
  453. $this->connectUser('bux', 'toor');
  454. $bux = $this->getUser();
  455. $bob = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  456. ->findOneByUsername('bob')
  457. ;
  458. $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
  459. $tribe_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
  460. // On récupère l'id du dernier element affiché
  461. $extract = $this->crawler->filter('ul.elements li.element')
  462. ->extract(array('id'));
  463. $first_id = (int)str_replace('element_', '', $extract[0]);
  464. $url = $this->generateUrl('element_new_count', array('refid' => $first_id));
  465. // On effectue la kekete ajax
  466. $crawler = $this->client->request(
  467. 'POST',
  468. $url,
  469. array('element_search_form' => array(
  470. 'network' => ElementSearcher::NETWORK_PUBLIC,
  471. 'tags' => json_encode(array($hardtek_id, $tribe_id))
  472. )),
  473. array(),
  474. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  475. );
  476. $this->isResponseSuccess();
  477. $response = json_decode($this->client->getResponse()->getContent(), true);
  478. $this->assertEquals($response['status'], 'success');
  479. $this->assertEquals($response['count'], '0');
  480. $this->assertEquals($response['message'], '');
  481. $this->addElementAjax('NewElement One', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
  482. $this->addElementAjax('NewElement Two', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
  483. // On refait la même demande, deux éléments sont nouveaux
  484. $url = $this->generateUrl('element_new_count', array('refid' => $first_id));
  485. // On effectue la kekete ajax
  486. $crawler = $this->client->request(
  487. 'POST',
  488. $url,
  489. array('element_search_form' => array(
  490. 'network' => ElementSearcher::NETWORK_PUBLIC,
  491. 'tags' => json_encode(array($hardtek_id, $tribe_id))
  492. )),
  493. array(),
  494. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  495. );
  496. $response = json_decode($this->client->getResponse()->getContent(), true);
  497. $this->assertEquals($response['status'], 'success');
  498. $this->assertEquals($response['count'], '2');
  499. // Si on demande la récupération des nouveaux éléments on doit les obtenirs
  500. $url = $this->generateUrl('element_new_get', array('refid' => $first_id));
  501. // On effectue la kekete ajax
  502. $crawler = $this->client->request(
  503. 'POST',
  504. $url,
  505. array('element_search_form' => array(
  506. 'network' => ElementSearcher::NETWORK_PUBLIC,
  507. 'tags' => json_encode(array($hardtek_id, $tribe_id))
  508. )),
  509. array(),
  510. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  511. );
  512. $this->isResponseSuccess();
  513. $response = json_decode($this->client->getResponse()->getContent(), true);
  514. $this->assertEquals($response['status'], 'success');
  515. $this->assertEquals($response['count'], '0');
  516. $this->assertTrue(!is_null($response['html']));
  517. $this->assertTrue(strpos($response['html'], 'NewElement One') !== false);
  518. $this->assertTrue(strpos($response['html'], 'NewElement Two') !== false);
  519. // On ajoute 10 autres éléments (NOTE: le 10 est hardcodé dans ce test
  520. // , c'est la limite d'affichage d'éléments)
  521. $this->addElementAjax('NewElement 3', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
  522. $this->addElementAjax('NewElement 4', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
  523. $this->addElementAjax('NewElement 5', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
  524. $this->addElementAjax('NewElement 6', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
  525. $this->addElementAjax('NewElement 7', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
  526. $this->addElementAjax('NewElement 8', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
  527. $this->addElementAjax('NewElement 9', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
  528. $this->addElementAjax('NewElement 10', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
  529. $this->addElementAjax('NewElement 11', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
  530. $this->addElementAjax('NewElement 12', 'http://labas.com', json_encode(array($hardtek_id, $tribe_id)));
  531. // On va refaire un count des nouveaux éléments
  532. // Ca devrat nous répondree 12 puisque on utilise l'id de référence du début
  533. // On refait la même demande, deux éléments sont nouveaux
  534. $url = $this->generateUrl('element_new_count', array('refid' => $first_id));
  535. // On effectue la kekete ajax
  536. $crawler = $this->client->request(
  537. 'POST',
  538. $url,
  539. array('element_search_form' => array(
  540. 'network' => ElementSearcher::NETWORK_PUBLIC,
  541. 'tags' => json_encode(array($hardtek_id, $tribe_id))
  542. )),
  543. array(),
  544. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  545. );
  546. $response = json_decode($this->client->getResponse()->getContent(), true);
  547. $this->assertEquals($response['status'], 'success');
  548. $this->assertEquals($response['count'], '12');
  549. // Si on demande la récupération des nouveaux éléments on doit en obtenir 10
  550. // et en rester 2
  551. $url = $this->generateUrl('element_new_get', array('refid' => $first_id));
  552. // On effectue la kekete ajax
  553. $crawler = $this->client->request(
  554. 'POST',
  555. $url,
  556. array('element_search_form' => array(
  557. 'network' => ElementSearcher::NETWORK_PUBLIC,
  558. 'tags' => json_encode(array($hardtek_id, $tribe_id))
  559. )),
  560. array(),
  561. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  562. );
  563. $this->isResponseSuccess();
  564. $response = json_decode($this->client->getResponse()->getContent(), true);
  565. $this->assertEquals($response['status'], 'success');
  566. $this->assertEquals($response['count'], '2');
  567. $this->assertTrue(!is_null($response['html']));
  568. $this->assertTrue(strpos($response['html'], 'NewElement One') !== false);
  569. $this->assertTrue(strpos($response['html'], 'NewElement Two') !== false);
  570. $this->assertTrue(strpos($response['html'], 'NewElement 3') !== false);
  571. $this->assertTrue(strpos($response['html'], 'NewElement 4') !== false);
  572. $this->assertTrue(strpos($response['html'], 'NewElement 5') !== false);
  573. $this->assertTrue(strpos($response['html'], 'NewElement 6') !== false);
  574. $this->assertTrue(strpos($response['html'], 'NewElement 7') !== false);
  575. $this->assertTrue(strpos($response['html'], 'NewElement 8') !== false);
  576. $this->assertTrue(strpos($response['html'], 'NewElement 9') !== false);
  577. $this->assertTrue(strpos($response['html'], 'NewElement 10') !== false);
  578. $this->assertTrue(strpos($response['html'], 'NewElement 11') === false);
  579. $this->assertTrue(strpos($response['html'], 'NewElement 12') === false);
  580. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  581. ->findOneByName('NewElement 10')
  582. ;
  583. // notre nouvel id référent en celui de NewElement 10
  584. // On renouvelle la demande, il ne doit y avoir que 2 élément nouveau a afficher
  585. $url = $this->generateUrl('element_new_count', array('refid' => $element->getId()));
  586. // On effectue la kekete ajax
  587. $crawler = $this->client->request(
  588. 'POST',
  589. $url,
  590. array('element_search_form' => array(
  591. 'network' => ElementSearcher::NETWORK_PUBLIC,
  592. 'tags' => json_encode(array($hardtek_id, $tribe_id))
  593. )),
  594. array(),
  595. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  596. );
  597. $response = json_decode($this->client->getResponse()->getContent(), true);
  598. $this->assertEquals($response['status'], 'success');
  599. $this->assertEquals($response['count'], '2');
  600. }
  601. public function testMoreElements()
  602. {
  603. $this->client = self::createClient();
  604. $this->connectUser('bux', 'toor');
  605. $bux = $this->getUser();
  606. // On récupère l'id du dernier element affiché
  607. $extract = $this->crawler->filter('ul.elements li.element')
  608. ->extract(array('id'));
  609. // !!!!!! NOTE !!!!! : 9 est hardcodé ici: la config est de 10 éléments en affichage
  610. $id_limit = (int)str_replace('element_', '', $extract[9]);
  611. $url = $this->generateUrl('search_elements_more', array(
  612. 'context' => 'home',
  613. 'id_limit' => $id_limit
  614. ));
  615. // We want mooooore
  616. // On effectue la kekete ajax
  617. $crawler = $this->client->request(
  618. 'GET',
  619. $url,
  620. array(),
  621. array(),
  622. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  623. );
  624. $response = json_decode($this->client->getResponse()->getContent(), true);
  625. $this->assertEquals($response['status'], 'success');
  626. // TODO: améliorer ce test !!
  627. // $this->assertEquals($response['count'], '4'); // HARDCODE fixtures
  628. // $this->assertEquals($response['end'], false); // HARDCODE fixtures
  629. }
  630. public function testAddedElementToGroup()
  631. {
  632. $this->client = self::createClient();
  633. $this->connectUser('bob', 'toor');
  634. // bob administre le groupe fans de psytrance
  635. $bob = $this->getUser();
  636. $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
  637. $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
  638. $psytrance = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Psytrance');
  639. $fan_de_psy = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
  640. // On envoie d'abord un élément sans tags associé
  641. // On ne devra pas avoir de proposition de groupe
  642. $url = $this->generateUrl('element_add');
  643. $extract = $this->crawler->filter('input[name="element_add[_token]"]')
  644. ->extract(array('value'));
  645. $csrf = $extract[0];
  646. $crawler = $this->client->request(
  647. 'POST',
  648. $url,
  649. array(
  650. 'element_add' => array(
  651. '_token' => $csrf,
  652. 'name' => 'Musique 1976824673',
  653. 'url' => 'http://www.youtube.com/watch?v=WC8qb_of04E',
  654. 'tags' => json_encode(array($hardtek->getId(), $tribe->getId()))
  655. )
  656. ),
  657. array(),
  658. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  659. );
  660. $this->isResponseSuccess();
  661. $response = json_decode($this->client->getResponse()->getContent(), true);
  662. $this->assertEquals($response['status'], 'success');
  663. $this->assertEquals($response['groups'], array());
  664. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  665. ->findOneByName('Musique 1976824673')
  666. ;
  667. $this->assertTrue(!is_null($element));
  668. // Maintenant on ajout un élément qui a comme tag psytrance
  669. $url = $this->generateUrl('element_add');
  670. $extract = $this->crawler->filter('input[name="element_add[_token]"]')
  671. ->extract(array('value'));
  672. $csrf = $extract[0];
  673. $crawler = $this->client->request(
  674. 'POST',
  675. $url,
  676. array(
  677. 'element_add' => array(
  678. '_token' => $csrf,
  679. 'name' => 'Musique 4gbz65g4afa',
  680. 'url' => 'http://www.youtube.com/watch?v=WC8qb_of04E',
  681. 'tags' => json_encode(array($psytrance->getId()))
  682. )
  683. ),
  684. array(),
  685. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  686. );
  687. $this->isResponseSuccess();
  688. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  689. ->findOneByName('Musique 4gbz65g4afa')
  690. ;
  691. $this->assertTrue(!is_null($element));
  692. $response = json_decode($this->client->getResponse()->getContent(), true);
  693. $this->assertEquals($response['status'], 'success');
  694. $this->assertEquals($response['groups'], array(
  695. array(
  696. 'name' => $fan_de_psy->getName(),
  697. 'id' => $fan_de_psy->getId(),
  698. 'url' => $this->generateUrl('ajax_set_element_group', array(
  699. 'token' => $this->getUser()->getPersonalHash($element->getId()),
  700. 'element_id' => $element->getId(),
  701. 'group_id' => $fan_de_psy->getId()
  702. ))
  703. )
  704. ));
  705. // Du coup on effectue la diffusion de l'élément dans le groupe
  706. $crawler = $this->client->request(
  707. 'POST',
  708. $this->generateUrl('ajax_set_element_group', array(
  709. 'element_id' => $element->getId(),
  710. 'group_id' => $fan_de_psy->getId(),
  711. 'token' => $this->getUser()->getPersonalHash($element->getId())
  712. )),
  713. array(),
  714. array(),
  715. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  716. );
  717. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  718. ->findOneBy(array(
  719. 'name' => 'Musique 4gbz65g4afa',
  720. 'group' => $fan_de_psy->getId()
  721. ))
  722. ;
  723. $this->assertTrue(!is_null($element));
  724. }
  725. public function testElementNeedTags()
  726. {
  727. $this->client = self::createClient();
  728. $this->connectUser('bux', 'toor');
  729. $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
  730. $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
  731. /*
  732. * Ajout d'un élément avec succés
  733. */
  734. $this->procedure_add_element(
  735. 'Mon bel element 98he4gez',
  736. 'http://www.youtube.com/watch?v=WC8qb_of04E',
  737. array($hardtek->getId(), $tribe->getId()),
  738. null,
  739. true
  740. );
  741. $this->isResponseRedirection();
  742. $this->followRedirection();
  743. $this->isResponseSuccess();
  744. $this->exist('li:contains("Mon bel element 98he4gez")');
  745. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  746. ->findOneByName('Mon bel element 98he4gez')
  747. ;
  748. $this->assertTrue(!is_null($element));
  749. $this->assertTrue($element->getNeedTags());
  750. // On va voir si elle se trouve dans la liste des demandes avec paul
  751. $this->disconnectUser();
  752. $this->connectUser('paul', 'toor');
  753. $paul = $this->getUser();
  754. // On ouvre la page
  755. $this->goToPage($this->generateUrl('element_show_need_tags'));
  756. // On y voir le partage
  757. $this->exist('li:contains("Mon bel element 98he4gez")');
  758. // On fait une proposition
  759. $crawler = $this->client->request(
  760. 'POST',
  761. $this->generateUrl('ajax_element_propose_tags_proceed',
  762. array('element_id' => $element->getId(), 'token' => $paul->getPersonalHash())
  763. ),
  764. array(
  765. 'element_tag_proposition_'.$element->getId() => array(
  766. 'tags' => json_encode(array($hardtek->getId(), $tribe->getId()))
  767. )
  768. ),
  769. array(),
  770. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  771. );
  772. $this->isResponseSuccess();
  773. $response = json_decode($this->client->getResponse()->getContent(), true);
  774. $this->assertEquals($response['status'], 'success');
  775. // On se reconnecte avec bux pour l'accepter
  776. $this->disconnectUser();
  777. $this->connectUser('bux', 'toor');
  778. $bux = $this->getUser();
  779. // On récupère la proposition
  780. $propositions = $this->getDoctrine()->getEntityManager()
  781. ->createQuery('SELECT p, t FROM MuzichCoreBundle:ElementTagsProposition p'
  782. .' JOIN p.tags t WHERE p.element = :eid AND p.user = :uid')
  783. ->setParameters(array(
  784. 'eid' => $element->getId(),
  785. 'uid' => $paul->getId()
  786. ))
  787. ->getResult()
  788. ;
  789. $this->assertEquals(1, count($propositions));
  790. $proposition_paul = $propositions[0];
  791. // On accepte la proposition de paul
  792. $url_accept_paul = $this->generateUrl('ajax_element_proposed_tags_accept', array(
  793. 'proposition_id' => $proposition_paul->getId(),
  794. 'token' => $bux->getPersonalHash($proposition_paul->getId())
  795. ));
  796. $crawler = $this->client->request(
  797. 'GET',
  798. $url_accept_paul,
  799. array(),
  800. array(),
  801. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  802. );
  803. $this->isResponseSuccess();
  804. $response = json_decode($this->client->getResponse()->getContent(), true);
  805. $this->assertEquals($response['status'], 'success');
  806. // On check l'élémetn pour voir si il est bien plsuen demande de tags
  807. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  808. ->findOneByName('Mon bel element 98he4gez')
  809. ;
  810. $this->assertTrue(!is_null($element));
  811. $this->assertFalse($element->getNeedTags());
  812. }
  813. }