IndexControllerTest.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Muzich\CoreBundle\Tests\Controller;
  3. use Muzich\CoreBundle\lib\FunctionalTest;
  4. use Muzich\CoreBundle\Entity\RegistrationToken;
  5. class IndexControllerTest extends FunctionalTest
  6. {
  7. public function testIdentificationSuccess()
  8. {
  9. /**
  10. * Test de l'identification de paul
  11. */
  12. $this->client = self::createClient();
  13. $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
  14. $this->isResponseSuccess();
  15. $this->assertEquals('anon.', $this->getUser());
  16. $this->exist('div.login');
  17. $this->exist('form[action="'.($url = $this->generateUrl('fos_user_security_check')).'"]');
  18. $this->exist('form[action="'.$url.'"] input[id="username"]');
  19. $this->exist('form[action="'.$url.'"] input[id="password"]');
  20. $this->exist('form[action="'.$url.'"] input[id="remember_me"]');
  21. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  22. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  23. $form['_username'] = 'paul';
  24. $form['_password'] = 'toor';
  25. $form['_remember_me'] = true;
  26. $this->submit($form);
  27. $this->isResponseRedirection();
  28. $this->followRedirection();
  29. $this->isResponseSuccess();
  30. $user = $this->getUser();
  31. $this->assertEquals('paul', $user->getUsername());
  32. }
  33. public function testIdentificationFail()
  34. {
  35. /**
  36. * Test de l'identification de paul, avec erreur
  37. */
  38. $this->client = self::createClient();
  39. $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
  40. $this->isResponseSuccess();
  41. $this->assertEquals('anon.', $this->getUser());
  42. $form = $this->selectForm('form[action="'.$this->generateUrl('fos_user_security_check').'"] input[type="submit"]');
  43. $form['_username'] = 'paul';
  44. $form['_password'] = 'toorr';
  45. $form['_remember_me'] = true;
  46. $this->submit($form);
  47. $this->isResponseRedirection();
  48. $this->followRedirection();
  49. $this->isResponseSuccess();
  50. $this->assertEquals('anon.', $this->getUser());
  51. }
  52. public function testRegistrationSuccess()
  53. {
  54. /**
  55. * Inscription d'un utilisateur
  56. */
  57. $this->client = self::createClient();
  58. $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
  59. $this->isResponseSuccess();
  60. $this->assertEquals('anon.', $this->getUser());
  61. // On a besoin d'un token pour le moment
  62. $token = new RegistrationToken();
  63. $token->setToken('4vcsdv54svqcc3q1v54sdv6qs');
  64. $em = $this->getDoctrine()->getManager();
  65. $em->persist($token);
  66. $em->flush();
  67. $this->exist('div.register');
  68. $this->exist('form[action="'.($url = $this->generateUrl('register')).'"]');
  69. $this->exist('form[action="'.$url.'"] input[id="muzich_user_registration_email"]');
  70. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  71. $this->procedure_registration_success(
  72. 'raoula.def4v65sds@gmail.com'
  73. );
  74. }
  75. public function testRegistrationFailure()
  76. {
  77. /**
  78. * Inscription d'un utilisateur
  79. */
  80. $this->client = self::createClient();
  81. // On a besoin d'un token pour le moment
  82. $token = new RegistrationToken();
  83. $token_name = '45gf645jgf6xqz4dc'.time();
  84. $token->setToken($token_name);
  85. $em = $this->getDoctrine()->getManager();
  86. $em->persist($token);
  87. $em->flush();
  88. // Pseudo trop long
  89. $this->procedure_registration_failure(
  90. 'raoulb.def4v65sdsgmail.com'
  91. );
  92. }
  93. /**
  94. * Test du changement de mot de passe
  95. */
  96. public function testPasswordLost()
  97. {
  98. $this->client = self::createClient();
  99. $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
  100. $bux = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneByUsername('bux');
  101. // On peux voir le lien vers al page de demande de mot de passe
  102. $this->exist('a[href="'.($url = $this->generateUrl('fos_user_resetting_request')).'"]');
  103. $link = $this->selectLink('a[href="'.$url.'"]');
  104. $this->clickOnLink($link);
  105. $this->isResponseSuccess();
  106. // On trouve le formulaire
  107. $this->exist('form[action="'.($url = $this->generateUrl('fos_user_resetting_send_email')).'"]');
  108. $this->exist('form[action="'.$url.'"] input[id="username"]');
  109. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  110. // On selectionne le form
  111. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  112. $form['username'] = 'bux';
  113. $this->submit($form);
  114. $mc = $this->getMailerMessageDataCollector();
  115. $this->assertEquals(1, $mc->getMessageCount());
  116. $mails = $mc->getMessages();
  117. $mail = $mails[0];
  118. // $mail = new Swift_Message();
  119. $bux = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneByUsername('bux');
  120. $this->assertTrue(!is_null(strpos($mail->getBody(), ($url = $this->generateUrl(
  121. 'fos_user_resetting_reset',
  122. array('token' => $bux->getConfirmationToken()),
  123. true
  124. )))));
  125. $keys = array_keys($mail->getTo());
  126. $this->assertEquals($bux->getEmail(), $keys[0]);
  127. $this->isResponseRedirection();
  128. $this->followRedirection();
  129. $this->isResponseSuccess();
  130. // On se rend sur le lien envoyé dans le mail
  131. $this->crawler = $this->client->request('GET', $url);
  132. $this->exist('form[action="'.($url = $this->generateUrl(
  133. 'fos_user_resetting_reset',
  134. array('token' => $bux->getConfirmationToken())
  135. )).'"]');
  136. $this->exist('form[action="'.$url.'"] input[id="fos_user_resetting_form_new_first"]');
  137. $this->exist('form[action="'.$url.'"] input[id="fos_user_resetting_form_new_second"]');
  138. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  139. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  140. $form['fos_user_resetting_form[new][first]'] = 'trololo';
  141. $form['fos_user_resetting_form[new][second]'] = 'trololo';
  142. $this->submit($form);
  143. $this->isResponseRedirection();
  144. $this->followRedirection();
  145. $this->isResponseSuccess();
  146. // A ce stade on a été connecté
  147. $this->assertEquals('bux', $this->getUser()->getUsername());
  148. // On se déconnecte pour aller tester ce nouveau mot de passe
  149. $this->disconnectUser();
  150. $this->connectUser('bux', 'trololo');
  151. }
  152. }