HomeControllerTest.php 31KB

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