UserControllerTest.php 19KB

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