HomeControllerTest.php 33KB

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