HomeControllerTest.php 31KB

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