IndexControllerTest.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. $user = $this->getUser();
  51. $this->assertEquals('anon.', $this->getUser());
  52. }
  53. public function testRegistrationSuccess()
  54. {
  55. /**
  56. * Inscription d'un utilisateur
  57. */
  58. $this->client = self::createClient();
  59. $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
  60. $this->isResponseSuccess();
  61. $this->assertEquals('anon.', $this->getUser());
  62. // On a besoin d'un token pour le moment
  63. $token = new RegistrationToken();
  64. $token->setToken('4vcsdv54svqcc3q1v54sdv6qs');
  65. $em = $this->getDoctrine()->getEntityManager();
  66. $em->persist($token);
  67. $em->flush();
  68. $this->exist('div.register');
  69. $this->exist('form[action="'.($url = $this->generateUrl('register')).'"]');
  70. $this->exist('form[action="'.$url.'"] input[id="fos_user_registration_form_username"]');
  71. $this->exist('form[action="'.$url.'"] input[id="fos_user_registration_form_email"]');
  72. $this->exist('form[action="'.$url.'"] input[id="fos_user_registration_form_plainPassword_first"]');
  73. $this->exist('form[action="'.$url.'"] input[id="fos_user_registration_form_plainPassword_second"]');
  74. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  75. $this->procedure_registration_success(
  76. 'raoula',
  77. 'raoula.def4v65sds@gmail.com',
  78. 'toor',
  79. 'toor',
  80. '4vcsdv54svqcc3q1v54sdv6qs'
  81. );
  82. }
  83. public function testRegistrationFailure()
  84. {
  85. /**
  86. * Inscription d'un utilisateur
  87. */
  88. $this->client = self::createClient();
  89. // On a besoin d'un token pour le moment
  90. $token = new RegistrationToken();
  91. $token->setToken('45gf645jgf6xqz4dc');
  92. $em = $this->getDoctrine()->getEntityManager();
  93. $em->persist($token);
  94. $em->flush();
  95. // Mots de passe différents
  96. $this->procedure_registration_failure(
  97. 'raoulb',
  98. 'raoulb.def4v65sds@gmail.com',
  99. 'toor',
  100. 'toorr',
  101. '45gf645jgf6xqz4dc'
  102. );
  103. // Pseudo trop court
  104. $this->procedure_registration_failure(
  105. 'ra',
  106. 'raoulb.def4v65sds@gmail.com',
  107. 'toor',
  108. 'toor',
  109. '45gf645jgf6xqz4dc'
  110. );
  111. // Pseudo trop long
  112. $this->procedure_registration_failure(
  113. 'raouuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu'
  114. .'uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu'
  115. .'uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuul',
  116. 'raoulb.def4v65sds@gmail.com',
  117. 'toor',
  118. 'toor',
  119. '45gf645jgf6xqz4dc'
  120. );
  121. // Email invalide
  122. $this->procedure_registration_failure(
  123. 'raoulc',
  124. 'raoulb.def4v65sds@gmail',
  125. 'toor',
  126. 'toor',
  127. '45gf645jgf6xqz4dc'
  128. );
  129. }
  130. /**
  131. * Test du changement de mot de passe
  132. */
  133. public function testPasswordLost()
  134. {
  135. $this->client = self::createClient();
  136. $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
  137. $bux = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneByUsername('bux');
  138. // On peux voir le lien vers al page de demande de mot de passe
  139. $this->exist('a[href="'.($url = $this->generateUrl('fos_user_resetting_request')).'"]');
  140. $link = $this->selectLink('a[href="'.$url.'"]');
  141. $this->clickOnLink($link);
  142. $this->isResponseSuccess();
  143. // On trouve le formulaire
  144. $this->exist('form[action="'.($url = $this->generateUrl('fos_user_resetting_send_email')).'"]');
  145. $this->exist('form[action="'.$url.'"] input[id="username"]');
  146. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  147. // On selectionne le form
  148. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  149. $form['username'] = 'bux';
  150. $this->submit($form);
  151. $mc = $this->getMailerMessageDataCollector();
  152. $this->assertEquals(1, $mc->getMessageCount());
  153. $mails = $mc->getMessages();
  154. $mail = $mails[0];
  155. // $mail = new Swift_Message();
  156. $this->assertTrue(!is_null(strpos($mail->getBody(), ($url = $this->generateUrl(
  157. 'fos_user_resetting_reset',
  158. array('token' => $bux->getConfirmationToken()),
  159. true
  160. )))));
  161. $keys = array_keys($mail->getTo());
  162. $this->assertEquals($bux->getEmail(), $keys[0]);
  163. $this->isResponseRedirection();
  164. $this->followRedirection();
  165. $this->isResponseSuccess();
  166. // On se rend sur le lien envoyé dans le mail
  167. $this->crawler = $this->client->request('GET', $url);
  168. $this->exist('form[action="'.($url = $this->generateUrl(
  169. 'fos_user_resetting_reset',
  170. array('token' => $bux->getConfirmationToken())
  171. )).'"]');
  172. $this->exist('form[action="'.$url.'"] input[id="fos_user_resetting_form_new_first"]');
  173. $this->exist('form[action="'.$url.'"] input[id="fos_user_resetting_form_new_second"]');
  174. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  175. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  176. $form['fos_user_resetting_form[new][first]'] = 'trololo';
  177. $form['fos_user_resetting_form[new][second]'] = 'trololo';
  178. $this->submit($form);
  179. $this->isResponseRedirection();
  180. $this->followRedirection();
  181. $this->isResponseSuccess();
  182. // A ce stade on a été connecté
  183. $this->assertEquals('bux', $this->getUser()->getUsername());
  184. // On se déconnecte pour aller tester ce nouveau mot de passe
  185. $this->disconnectUser();
  186. $this->connectUser('bux', 'trololo');
  187. }
  188. }