NoPassTest.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace Muzich\CoreBundle\Tests\Controller;
  3. use Muzich\CoreBundle\lib\FunctionalTest;
  4. use Muzich\CoreBundle\Tests\lib\Security\Context as SecurityContextTest;
  5. use Muzich\CoreBundle\Security\Context as SecurityContext;
  6. class NoPassTest extends FunctionalTest
  7. {
  8. protected $security_context_test;
  9. protected function init()
  10. {
  11. $this->client = self::createClient();
  12. $this->security_context_test = new SecurityContextTest($this->client, $this);
  13. }
  14. public function testConfirmationEmail()
  15. {
  16. $this->init();
  17. $this->registerUser('francky@mail.com');
  18. $this->checkUserEmailIsNotConfirmed();
  19. $this->checkUserCantMakeProhibedActionsForEmailNotConfirmed();
  20. $this->confirmEmail();
  21. $this->checkUserEmailIsConfirmed();
  22. $this->checkUserisNotProhibedForActionsBlockedByEmailNotConfirmed();
  23. }
  24. protected function registerUser($email)
  25. {
  26. $this->procedure_registration_success($email);
  27. }
  28. protected function checkUserEmailIsNotConfirmed()
  29. {
  30. $this->security_context_test->userIsInConditionEmailNotConfirmed($this->getUser());
  31. }
  32. protected function checkUserCantMakeProhibedActionsForEmailNotConfirmed()
  33. {
  34. $this->checkUserProhibedActionStatus(true);
  35. }
  36. protected function checkUserProhibedActionStatus($match)
  37. {
  38. $this->security_context_test->testUserCantMakeActionStatus(
  39. SecurityContext::ACTION_ELEMENT_ADD,
  40. SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
  41. $match
  42. );
  43. $this->security_context_test->testUserCantMakeActionStatus(
  44. SecurityContext::ACTION_ELEMENT_NOTE,
  45. SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
  46. $match
  47. );
  48. $this->security_context_test->testUserCantMakeActionStatus(
  49. SecurityContext::ACTION_COMMENT_ALERT,
  50. SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
  51. $match
  52. );
  53. $this->security_context_test->testUserCantMakeActionStatus(
  54. SecurityContext::ACTION_ELEMENT_ALERT,
  55. SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
  56. $match
  57. );
  58. $this->security_context_test->testUserCantMakeActionStatus(
  59. SecurityContext::ACTION_TAG_ADD,
  60. SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
  61. $match
  62. );
  63. $this->security_context_test->testUserCantMakeActionStatus(
  64. SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION,
  65. SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
  66. $match
  67. );
  68. $this->security_context_test->testUserCantMakeActionStatus(
  69. SecurityContext::ACTION_GROUP_ADD,
  70. SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
  71. $match
  72. );
  73. }
  74. protected function confirmEmail()
  75. {
  76. $token = hash('sha256', $this->getUser()->getConfirmationToken().$this->getUser()->getEmail());
  77. $this->goToPage($this->generateUrl('email_confirm', array('token' => $token)));
  78. $this->isResponseRedirection();
  79. }
  80. protected function checkUserEmailIsConfirmed()
  81. {
  82. $this->security_context_test->userIsNotInConditionEmailNotConfirmed($this->getUser());
  83. }
  84. protected function checkUserisNotProhibedForActionsBlockedByEmailNotConfirmed()
  85. {
  86. $this->checkUserProhibedActionStatus(false);
  87. }
  88. public function testSetPassword()
  89. {
  90. $this->init();
  91. $this->registerUser('trolita@mail.com');
  92. $this->checkUserPasswordHasNotBeenSet();
  93. $this->updatePasswordMessageExist();
  94. $this->updatePassword();
  95. $this->checkUserPasswordHasBeenSet();
  96. $this->updatePasswordMessageNotExist();
  97. }
  98. protected function checkUserPasswordHasNotBeenSet()
  99. {
  100. $this->assertFalse($this->getUser()->isPasswordSet());
  101. }
  102. protected function updatePasswordMessageExist()
  103. {
  104. $this->goToPage($this->generateUrl('home'));
  105. $this->exist('div.choose_password');
  106. }
  107. protected function updatePassword()
  108. {
  109. $this->goToPage($this->generateUrl('my_account'));
  110. $this->exist('form[action="'.($url = $this->generateUrl(
  111. 'change_password', array('open' => 'change_password')
  112. )).'"]');
  113. $this->exist('form[action="'.$url.'"] input[id="user_password_plain_password_first"]');
  114. $this->exist('form[action="'.$url.'"] input[id="user_password_plain_password_second"]');
  115. $this->exist('form[action="'.$url.'"] input[type="submit"]');
  116. $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
  117. $form['user_password[plain_password][first]'] = 'trololo';
  118. $form['user_password[plain_password][second]'] = 'trololo';
  119. $this->submit($form);
  120. $this->isResponseRedirection();
  121. $this->followRedirection();
  122. $this->isResponseSuccess();
  123. // On se déconnecte
  124. $this->disconnectUser();
  125. // Et on se connecte avec le nouveau mot de passe
  126. $this->connectUser('trolita@mail.com', 'trololo');
  127. }
  128. protected function checkUserPasswordHasBeenSet()
  129. {
  130. $this->assertTrue($this->getUser()->isPasswordSet());
  131. }
  132. protected function updatePasswordMessageNotExist()
  133. {
  134. $this->goToPage($this->generateUrl('home'));
  135. $this->notExist('div.choose_password');
  136. }
  137. public function testSetUsername()
  138. {
  139. $this->init();
  140. $this->registerUser('boulouduf@mail.com');
  141. $this->userHasNotDefinedUsername();
  142. $this->updateUserNameLinkExist();
  143. $this->updateUsername('boulouduf');
  144. $this->userHasDefinedUsername('boulouduf');
  145. $this->updateUserNameLinkNotExist();
  146. }
  147. protected function userHasNotDefinedUsername()
  148. {
  149. $this->assertTrue($this->getUser()->isUsernameUpdatable());
  150. }
  151. protected function updateUserNameLinkExist()
  152. {
  153. $this->goToPage($this->generateUrl('my_account'));
  154. $this->exist('a.username_update');
  155. }
  156. protected function updateUsername($username)
  157. {
  158. $this->goToPage($this->generateUrl('change_username'));
  159. $extract = $this->crawler->filter('input[name="form[_token]"]')
  160. ->extract(array('value'));
  161. $csrf = $extract[0];
  162. $this->crawler = $this->client->request(
  163. 'POST',
  164. $this->generateUrl('change_username'),
  165. array(
  166. 'form' => array(
  167. 'username' => $username,
  168. '_token' => $csrf
  169. )
  170. ),
  171. array(),
  172. array()
  173. );
  174. $this->isResponseRedirection();
  175. }
  176. protected function userHasDefinedUsername($username)
  177. {
  178. $this->assertEquals($username, $this->getUser()->getUsername());
  179. $this->assertFalse($this->getUser()->isUsernameUpdatable());
  180. }
  181. protected function updateUserNameLinkNotExist()
  182. {
  183. $this->goToPage($this->generateUrl('my_account'));
  184. $this->notExist('a.username_update');
  185. }
  186. }