IndexControllerTest.php 6.7KB

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