UserControllerTest.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <?php
  2. namespace Muzich\CoreBundle\Tests\Controller;
  3. use Muzich\CoreBundle\lib\FunctionalTest;
  4. use Muzich\CoreBundle\Entity\RegistrationToken;
  5. class UserControllerTest extends FunctionalTest
  6. {
  7. public function testTagsFavoritesSuccess()
  8. {
  9. /**
  10. * Inscription d'un utilisateur
  11. */
  12. $this->client = self::createClient();
  13. $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
  14. $tribe_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
  15. $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
  16. $this->isResponseSuccess();
  17. // On a besoin d'un token pour le moment
  18. $token = new RegistrationToken();
  19. $token->setToken('hekt78yl789dzafdfz');
  20. $em = $this->getDoctrine()->getEntityManager();
  21. $em->persist($token);
  22. $em->flush();
  23. $this->procedure_registration_success(
  24. 'raoulc',
  25. 'raoulc.def4v65sds@gmail.com',
  26. 'toor',
  27. 'toor',
  28. 'hekt78yl789dzafdfz'
  29. );
  30. // Il ne doit y avoir aucun enregistrements de tags favoris
  31. $Favorites = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  32. ->findBy(array(
  33. 'user' => $this->getUser()->getId()
  34. ))
  35. ;
  36. $this->assertEquals(0, count($Favorites));
  37. // On a attérit sur la page de présentation et de sleection des tags favoris
  38. $this->exist('form[action="'.($url = $this->generateUrl('update_tag_favorites')).'"]');
  39. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  40. $form['tag_favorites_form[tags]'] = json_encode(array($hardtek_id,$tribe_id));
  41. $this->submit($form);
  42. $this->isResponseRedirection();
  43. $this->followRedirection();
  44. $this->isResponseSuccess();
  45. // Désormais il y a deux tags favoris pour cet utilisateur
  46. $Favorites = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  47. ->findBy(array(
  48. 'user' => $this->getUser()->getId()
  49. ))
  50. ;
  51. $this->assertEquals(2, count($Favorites));
  52. $Favorites = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  53. ->findBy(array(
  54. 'user' => $this->getUser()->getId(),
  55. 'tag' => $hardtek_id
  56. ))
  57. ;
  58. $this->assertEquals(1, count($Favorites));
  59. $Favorites = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  60. ->findBy(array(
  61. 'user' => $this->getUser()->getId(),
  62. 'tag' => $tribe_id
  63. ))
  64. ;
  65. $this->assertEquals(1, count($Favorites));
  66. }
  67. /**
  68. * Test du changement de mot de passe par le baisis de la page 'Mon compte'
  69. */
  70. public function testChangePassword()
  71. {
  72. $this->client = self::createClient();
  73. $this->connectUser('bux', 'toor');
  74. // Ouverture de la page Mon compte
  75. $this->crawler = $this->client->request('GET', $this->generateUrl('my_account'));
  76. $this->exist('form[action="'.($url = $this->generateUrl(
  77. 'change_password'
  78. )).'"]');
  79. $this->exist('form[action="'.$url.'"] input[id="fos_user_change_password_form_current"]');
  80. $this->exist('form[action="'.$url.'"] input[id="fos_user_change_password_form_new_first"]');
  81. $this->exist('form[action="'.$url.'"] input[id="fos_user_change_password_form_new_second"]');
  82. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  83. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  84. $form['fos_user_change_password_form[current]'] = 'toor';
  85. $form['fos_user_change_password_form[new][first]'] = 'trololo';
  86. $form['fos_user_change_password_form[new][second]'] = 'trololo';
  87. $this->submit($form);
  88. $this->isResponseRedirection();
  89. $this->followRedirection();
  90. $this->isResponseSuccess();
  91. // On se déconnecte
  92. $this->disconnectUser();
  93. // Et on se connecte avec le nouveau mot de passe
  94. $this->connectUser('bux', 'trololo');
  95. }
  96. /**
  97. * Test du formulaire de mise a jour des tags par le baisis de la page 'Mon compte'
  98. */
  99. public function testUpdateFavoriteTags()
  100. {
  101. $this->client = self::createClient();
  102. $this->connectUser('bob', 'toor');
  103. // Ouverture de la page Mon compte
  104. $this->crawler = $this->client->request('GET', $this->generateUrl('my_account'));
  105. $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
  106. $tribe_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
  107. // Bob n'a aucun tag préféré
  108. $prefereds = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  109. ->findBy(array('user' => $this->getUser()->getId()))
  110. ;
  111. $this->assertEquals(0, count($prefereds));
  112. $this->exist('form[action="'.($url = $this->generateUrl(
  113. 'update_tag_favorites', array('redirect' => 'account')
  114. )).'"]');
  115. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  116. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  117. $form['tag_favorites_form[tags]'] = json_encode(array($hardtek_id,$tribe_id));
  118. $this->submit($form);
  119. $this->isResponseRedirection();
  120. $this->followRedirection();
  121. $this->isResponseSuccess();
  122. // On a été redirigé sur la page Mon compte
  123. $this->exist('form[action="'.$url.'"]');
  124. // On vérifie la présence en base des enregistrements
  125. $prefereds = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  126. ->findBy(array('user' => $this->getUser()->getId()))
  127. ;
  128. $this->assertEquals(2, count($prefereds));
  129. // On vérifie la présence en base des enregistrements
  130. $prefereds = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  131. ->findBy(array(
  132. 'user' => $this->getUser()->getId(),
  133. 'tag' => $hardtek_id
  134. ))
  135. ;
  136. $this->assertEquals(1, count($prefereds));
  137. // On vérifie la présence en base des enregistrements
  138. $prefereds = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  139. ->findBy(array(
  140. 'user' => $this->getUser()->getId(),
  141. 'tag' => $tribe_id
  142. ))
  143. ;
  144. $this->assertEquals(1, count($prefereds));
  145. }
  146. /**
  147. * Test de al procédure de changement d'email.
  148. */
  149. public function testChangeEmail()
  150. {
  151. $this->client = self::createClient();
  152. $this->connectUser('bob', 'toor');
  153. $bob = $this->findUserByUsername('bob');
  154. // Ouverture de la page Mon compte
  155. $this->crawler = $this->client->request('GET', $this->generateUrl('my_account'));
  156. // Le mail en cours n'est pas celui que nous voulons mettre
  157. $this->assertFalse($bob->getEmail() == 'trololololooo@trolo.com');
  158. // Nous n'avons pas encore demandé de nouveau mail
  159. $this->assertTrue($bob->getEmailRequested() == null);
  160. $this->assertTrue($bob->getEmailRequestedDatetime() == null);
  161. // On fait un premier essaie avec une email mal formulé
  162. $this->exist('form[action="'.($url = $this->generateUrl(
  163. 'change_email_request'
  164. )).'"]');
  165. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  166. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  167. $form['form[email]'] = 'trololololooo@trolo';
  168. $this->submit($form);
  169. // Il n'y as pas de redirection
  170. $this->isResponseSuccess();
  171. $bob = $this->findUserByUsername('bob');
  172. // Les champs n'ont pas bougés
  173. $this->assertFalse($bob->getEmail() == 'trololololooo@trolo.com');
  174. // Nous n'avons pas encore demandé de nouveau mail
  175. $this->assertTrue($bob->getEmailRequested() == null);
  176. $this->assertTrue($bob->getEmailRequestedDatetime() == null);
  177. $this->exist('form[action="'.($url = $this->generateUrl(
  178. 'change_email_request'
  179. )).'"]');
  180. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  181. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  182. $form['form[email]'] = 'trololololooo@trolo.com';
  183. $this->submit($form);
  184. // Ce coup-ci c'est bien une redirection
  185. $this->isResponseRedirection();
  186. // Un mail a été envoyé
  187. $mc = $this->getMailerMessageDataCollector();
  188. $this->assertEquals(1, $mc->getMessageCount());
  189. $mails = $mc->getMessages();
  190. $mail = $mails[0];
  191. // Les champs ont bougés
  192. $bob = $this->findUserByUsername('bob');
  193. $this->assertFalse($bob->getEmail() == 'trololololooo@trolo.com');
  194. $this->assertFalse($bob->getEmailRequested() == null);
  195. $this->assertTrue($bob->getEmailRequested() == 'trololololooo@trolo.com');
  196. $this->assertFalse($bob->getEmailRequestedDatetime() == null);
  197. $this->followRedirection();
  198. $this->isResponseSuccess();
  199. // On ouvre un lien erroné
  200. $badurl = $this->generateUrl(
  201. 'change_email_confirm',
  202. array('token' => $this->getUser()->getConfirmationToken()),
  203. true
  204. );
  205. $this->crawler = $this->client->request('GET', $badurl);
  206. $this->isResponseRedirection();
  207. $this->followRedirection();
  208. $this->isResponseSuccess();
  209. $this->exist('div.error');
  210. // Et les champs ont pas bougés
  211. $bob = $this->findUserByUsername('bob');
  212. $this->assertFalse($bob->getEmail() == 'trololololooo@trolo.com');
  213. $this->assertFalse($bob->getEmailRequested() == null);
  214. $this->assertTrue($bob->getEmailRequested() == 'trololololooo@trolo.com');
  215. $this->assertFalse($bob->getEmailRequestedDatetime() == null);
  216. $this->assertTrue(!is_null(strpos($mail->getBody(), ($url = $this->generateUrl(
  217. 'change_email_confirm',
  218. array('token' => $token = hash('sha256', $bob->getConfirmationToken().'trololololooo@trolo.com')),
  219. true
  220. )))));
  221. // On ouvre le bon lien
  222. $this->crawler = $this->client->request('GET', $url);
  223. // C'est un succés
  224. $this->isResponseRedirection();
  225. $this->followRedirection();
  226. $this->isResponseSuccess();
  227. $this->notExist('div.error');
  228. // Et les champs ont bougés
  229. $bob = $this->findUserByUsername('bob');
  230. $this->assertTrue($bob->getEmail() == 'trololololooo@trolo.com');
  231. $this->assertTrue($bob->getEmailRequested() == null);
  232. $this->assertFalse($bob->getEmailRequestedDatetime() == null);
  233. // Par contre si on refait une demande maintenant ca échoue (délais entre demandes)
  234. $this->exist('form[action="'.($url = $this->generateUrl(
  235. 'change_email_request'
  236. )).'"]');
  237. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  238. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  239. $form['form[email]'] = 'trololololooo222@trolo.com';
  240. $this->submit($form);
  241. // Il n'y as pas de redirection
  242. $this->isResponseRedirection();
  243. $this->followRedirection();
  244. $this->isResponseSuccess();
  245. $this->exist('div.error');
  246. // Et les champs ont bougés
  247. $bob = $this->findUserByUsername('bob');
  248. $this->assertTrue($bob->getEmail() == 'trololololooo@trolo.com');
  249. $this->assertTrue($bob->getEmailRequested() == null);
  250. $this->assertFalse($bob->getEmailRequestedDatetime() == null);
  251. // Si par contre on manipule le dateTime on pourra
  252. $bob = $this->findUserByUsername('bob');
  253. $bob->setEmailRequestedDatetime(
  254. $this->getUser()->getEmailRequestedDatetime()
  255. - $this->getContainer()->getParameter('changeemail_security_delay')
  256. );
  257. $this->getDoctrine()->getEntityManager()->flush();
  258. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  259. $form['form[email]'] = 'trololololooo222@trolo.com';
  260. $this->submit($form);
  261. // Ce coup-ci c'est bien une redirection
  262. $this->isResponseRedirection();
  263. // Un mail a été envoyé
  264. $mc = $this->getMailerMessageDataCollector();
  265. $this->assertEquals(1, $mc->getMessageCount());
  266. $mails = $mc->getMessages();
  267. $mail = $mails[0];
  268. $this->assertTrue(!is_null(strpos($mail->getBody(), ($url = $this->generateUrl(
  269. 'change_email_confirm',
  270. array('token' => hash('sha256', $this->getUser()->getConfirmationToken().'trololololooo222@trolo.com')),
  271. true
  272. )))));
  273. // Les champs ont bougés
  274. $bob = $this->findUserByUsername('bob');
  275. $this->assertFalse($bob->getEmail() == 'trololololooo222@trolo.com');
  276. $this->assertFalse($bob->getEmailRequested() == null);
  277. $this->assertTrue($bob->getEmailRequested() == 'trololololooo222@trolo.com');
  278. $this->assertFalse($bob->getEmailRequestedDatetime() == null);
  279. $this->followRedirection();
  280. $this->isResponseSuccess();
  281. }
  282. public function testAddElementTagToFavorites()
  283. {
  284. $this->client = self::createClient();
  285. $this->connectUser('paul', 'toor');
  286. $paul = $this->getUser();
  287. // D'après les fixtures paul n'a pas de tags favoris
  288. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  289. ->findBy(array(
  290. 'user' => $paul->getId()
  291. ))
  292. ;
  293. $this->assertEquals(0, count($fav));
  294. // Ajout d'un tag en favoris (ajax)
  295. $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  296. ->findOneByName('Tribe')
  297. ;
  298. $url = $this->generateUrl('ajax_tag_add_to_favorites', array(
  299. 'tag_id' => $tribe->getId(),
  300. 'token' => $paul->getPersonalHash()
  301. ));
  302. $crawler = $this->client->request('GET', $url, array(), array(), array(
  303. 'HTTP_X-Requested-With' => 'XMLHttpRequest',
  304. ));
  305. $this->isResponseSuccess();
  306. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  307. ->findBy(array(
  308. 'user' => $paul->getId()
  309. ))
  310. ;
  311. $this->assertEquals(1, count($fav));
  312. $this->assertEquals('Tribe', $fav[0]->getTag()->getName());
  313. // Si on rajoute le même tag il ne doit pas y avoir de changement
  314. $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  315. ->findOneByName('Tribe')
  316. ;
  317. $url = $this->generateUrl('ajax_tag_add_to_favorites', array(
  318. 'tag_id' => $tribe->getId(),
  319. 'token' => $paul->getPersonalHash()
  320. ));
  321. $crawler = $this->client->request('GET', $url, array(), array(), array(
  322. 'HTTP_X-Requested-With' => 'XMLHttpRequest',
  323. ));
  324. $this->isResponseSuccess();
  325. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  326. ->findBy(array(
  327. 'user' => $paul->getId()
  328. ))
  329. ;
  330. $this->assertEquals(1, count($fav));
  331. $this->assertEquals('Tribe', $fav[0]->getTag()->getName());
  332. // Si on ajoute un nouveau tag
  333. $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  334. ->findOneByName('Hardtek')
  335. ;
  336. $url = $this->generateUrl('ajax_tag_add_to_favorites', array(
  337. 'tag_id' => $hardtek->getId(),
  338. 'token' => $paul->getPersonalHash()
  339. ));
  340. $crawler = $this->client->request('GET', $url, array(), array(), array(
  341. 'HTTP_X-Requested-With' => 'XMLHttpRequest',
  342. ));
  343. $this->isResponseSuccess();
  344. $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
  345. ->findBy(array(
  346. 'user' => $paul->getId()
  347. ))
  348. ;
  349. $this->assertEquals(2, count($fav));
  350. $this->assertEquals('Tribe', $fav[0]->getTag()->getName());
  351. $this->assertEquals('Hardtek', $fav[1]->getTag()->getName());
  352. }
  353. public function testUpdateAddress()
  354. {
  355. $this->client = self::createClient();
  356. $this->connectUser('paul', 'toor');
  357. $paul = $this->getUser();
  358. // D'après les fixtures, pas d'adresse pour paul
  359. $this->assertEquals($paul->getTown(), null);
  360. $this->assertEquals($paul->getCountry(), null);
  361. $crawler = $this->client->request(
  362. 'POST',
  363. $this->generateUrl('update_address', array('token' => $this->getUser()->getPersonalHash())),
  364. array(
  365. 'town' => '',
  366. 'country' => ''
  367. ),
  368. array(),
  369. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  370. );
  371. $this->isResponseSuccess();
  372. $response = json_decode($this->client->getResponse()->getContent(), true);
  373. $this->assertEquals($response['status'], 'error');
  374. $this->assertEquals(count($response['errors']), '2');
  375. $this->assertEquals($response['errors'], array(
  376. $this->getContainer()->get('translator')->trans('my_account.address.form.errors.notown', array(), 'userui'),
  377. $this->getContainer()->get('translator')->trans('my_account.address.form.errors.nocountry', array(), 'userui')
  378. ));
  379. $paul = $this->getUser();
  380. $this->assertEquals($paul->getTown(), null);
  381. $this->assertEquals($paul->getCountry(), null);
  382. /////
  383. $crawler = $this->client->request(
  384. 'POST',
  385. $this->generateUrl('update_address', array('token' => $this->getUser()->getPersonalHash())),
  386. array(
  387. 'town' => 'peyruis',
  388. 'country' => ''
  389. ),
  390. array(),
  391. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  392. );
  393. $this->isResponseSuccess();
  394. $response = json_decode($this->client->getResponse()->getContent(), true);
  395. $this->assertEquals($response['status'], 'error');
  396. $this->assertEquals(count($response['errors']), '1');
  397. $this->assertEquals($response['errors'], array(
  398. $this->getContainer()->get('translator')->trans('my_account.address.form.errors.nocountry', array(), 'userui')
  399. ));
  400. $paul = $this->getUser();
  401. $this->assertEquals($paul->getTown(), null);
  402. $this->assertEquals($paul->getCountry(), null);
  403. /////
  404. $crawler = $this->client->request(
  405. 'POST',
  406. $this->generateUrl('update_address', array('token' => $this->getUser()->getPersonalHash())),
  407. array(
  408. 'town' => 'peyruis',
  409. 'country' => 'france'
  410. ),
  411. array(),
  412. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  413. );
  414. $this->isResponseSuccess();
  415. $response = json_decode($this->client->getResponse()->getContent(), true);
  416. $paul = $this->getUser();
  417. $this->assertEquals($response['status'], 'success');
  418. $this->assertEquals($paul->getTown(), 'peyruis');
  419. $this->assertEquals($paul->getCountry(), 'france');
  420. }
  421. }