Преглед изворни кода

Evolution #700: Test nécessaires

Bastien Sevajol пре 11 година
родитељ
комит
cf6c8d93d6

+ 140 - 15
src/Muzich/CoreBundle/Tests/Controller/NoPassTest.php Прегледај датотеку

@@ -45,22 +45,41 @@ class NoPassTest extends FunctionalTest
45 45
   
46 46
   protected function checkUserProhibedActionStatus($match)
47 47
   {
48
-    foreach (array(
48
+    $this->security_context_test->testUserCantMakeActionStatus( 
49 49
       SecurityContext::ACTION_ELEMENT_ADD, 
50
-      SecurityContext::ACTION_ELEMENT_NOTE,
51
-      SecurityContext::ACTION_COMMENT_ALERT,
52
-      SecurityContext::ACTION_ELEMENT_ALERT,
53
-      SecurityContext::ACTION_TAG_ADD,
54
-      SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION,
55
-      SecurityContext::ACTION_GROUP_ADD
56
-    ) as $action)
57
-    {
58
-      $this->security_context_test->testUserCantMakeActionStatus( 
59
-        $action, 
60
-        SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
61
-        $match
62
-      );
63
-    }
50
+      SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
51
+      $match
52
+    );
53
+    $this->security_context_test->testUserCantMakeActionStatus( 
54
+      SecurityContext::ACTION_ELEMENT_NOTE, 
55
+      SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
56
+      $match
57
+    );
58
+    $this->security_context_test->testUserCantMakeActionStatus( 
59
+      SecurityContext::ACTION_COMMENT_ALERT, 
60
+      SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
61
+      $match
62
+    );
63
+    $this->security_context_test->testUserCantMakeActionStatus( 
64
+      SecurityContext::ACTION_ELEMENT_ALERT, 
65
+      SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
66
+      $match
67
+    );
68
+    $this->security_context_test->testUserCantMakeActionStatus( 
69
+      SecurityContext::ACTION_TAG_ADD, 
70
+      SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
71
+      $match
72
+    );
73
+    $this->security_context_test->testUserCantMakeActionStatus( 
74
+      SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION, 
75
+      SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
76
+      $match
77
+    );
78
+    $this->security_context_test->testUserCantMakeActionStatus( 
79
+      SecurityContext::ACTION_GROUP_ADD, 
80
+      SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
81
+      $match
82
+    );
64 83
   }
65 84
   
66 85
   protected function confirmEmail()
@@ -82,12 +101,118 @@ class NoPassTest extends FunctionalTest
82 101
   
83 102
   public function testSetPassword()
84 103
   {
104
+    $this->init();
105
+    $this->registerUser('trolita@mail.com');
106
+    $this->checkUserPasswordHasNotBeenSet();
107
+    $this->updatePasswordMessageExist();
108
+    $this->updatePassword();
109
+    $this->checkUserPasswordHasBeenSet();
110
+    $this->updatePasswordMessageNotExist();
111
+  }
112
+  
113
+  protected function checkUserPasswordHasNotBeenSet()
114
+  {
115
+    $this->assertFalse($this->getUser()->isPasswordSet());
116
+  }
117
+  
118
+  protected function updatePasswordMessageExist()
119
+  {
120
+    $this->goToPage($this->generateUrl('home'));
121
+    $this->exist('div.choose_password');
122
+  }
123
+  
124
+  protected function updatePassword()
125
+  {
126
+    $this->goToPage($this->generateUrl('my_account'));
85 127
     
128
+    $this->exist('form[action="'.($url = $this->generateUrl(
129
+      'change_password', array('open' => 'change_password')
130
+    )).'"]');
131
+    $this->exist('form[action="'.$url.'"] input[id="user_password_plain_password_first"]');
132
+    $this->exist('form[action="'.$url.'"] input[id="user_password_plain_password_second"]');
133
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
134
+    
135
+    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
136
+    $form['user_password[plain_password][first]'] = 'trololo';
137
+    $form['user_password[plain_password][second]'] = 'trololo';
138
+    $this->submit($form);
139
+    
140
+    $this->isResponseRedirection();
141
+    $this->followRedirection();
142
+    $this->isResponseSuccess();
143
+    
144
+    // On se déconnecte
145
+    $this->disconnectUser();
146
+    
147
+    // Et on se connecte avec le nouveau mot de passe
148
+    $this->connectUser('trolita@mail.com', 'trololo');
149
+  }
150
+  
151
+  protected function checkUserPasswordHasBeenSet()
152
+  {
153
+    $this->assertTrue($this->getUser()->isPasswordSet());
154
+  }
155
+  
156
+  protected function updatePasswordMessageNotExist()
157
+  {
158
+    $this->goToPage($this->generateUrl('home'));
159
+    $this->notExist('div.choose_password');
86 160
   }
87 161
   
88 162
   public function testSetUsername()
89 163
   {
164
+    $this->init();
165
+    $this->registerUser('boulouduf@mail.com');
166
+    $this->userHasNotDefinedUsername();
167
+    $this->updateUserNameLinkExist();
168
+    $this->updateUsername('boulouduf');
169
+    $this->userHasDefinedUsername('boulouduf');
170
+    $this->updateUserNameLinkNotExist();
171
+  }
172
+  
173
+  protected function userHasNotDefinedUsername()
174
+  {
175
+    $this->assertTrue($this->getUser()->isUsernameUpdatable());
176
+  }
177
+  
178
+  protected function updateUserNameLinkExist()
179
+  {
180
+    $this->goToPage($this->generateUrl('my_account'));
181
+    $this->exist('a.username_update');
182
+  }
183
+  
184
+  protected function updateUsername($username)
185
+  {
186
+    $this->goToPage($this->generateUrl('change_username'));
187
+    $extract = $this->crawler->filter('input[name="form[_token]"]')
188
+      ->extract(array('value'));
189
+    $csrf = $extract[0];
190
+    $this->crawler = $this->client->request(
191
+      'POST', 
192
+      $this->generateUrl('change_username'), 
193
+      array(
194
+        'form' => array(
195
+          'username' => $username,
196
+          '_token' => $csrf
197
+        )
198
+      ), 
199
+      array(), 
200
+      array()
201
+    );
90 202
     
203
+    $this->isResponseRedirection();
204
+  }
205
+  
206
+  protected function userHasDefinedUsername($username)
207
+  {
208
+    $this->assertEquals($username, $this->getUser()->getUsername());
209
+    $this->assertFalse($this->getUser()->isUsernameUpdatable());
210
+  }
211
+  
212
+  protected function updateUserNameLinkNotExist()
213
+  {
214
+    $this->goToPage($this->generateUrl('my_account'));
215
+    $this->notExist('a.username_update');
91 216
   }
92 217
   
93 218
 }

+ 8 - 1
src/Muzich/CoreBundle/lib/FunctionalTest.php Прегледај датотеку

@@ -120,7 +120,14 @@ class FunctionalTest extends WebTestCase
120 120
     $user = $this->getUser();
121 121
     if ('anon.' != $user)
122 122
     {
123
-      $this->assertEquals($login, $user->getUsername());
123
+      if (strpos($login, '@') === false)
124
+      {
125
+        $this->assertEquals($login, $user->getUsername());
126
+      }
127
+      else
128
+      {
129
+        $this->assertEquals($login, $user->getEmail());
130
+      }
124 131
     }
125 132
     else
126 133
     {

+ 6 - 3
src/Muzich/UserBundle/Controller/UserController.php Прегледај датотеку

@@ -690,12 +690,15 @@ class UserController extends Controller
690 690
   
691 691
   public function changeUsernameAction(Request $request)
692 692
   {
693
-    if (!$this->getUser()->isUsernameUpdatable())
693
+    $user = $this->getUserRefreshed();
694
+    
695
+    if (!$user->isUsernameUpdatable())
694 696
     {
695 697
       return new RedirectResponse($this->generateUrl('my_account'));
696 698
     }
699
+    
697 700
     $errors = array();
698
-    $form = $this->getChangeUsernameForm($this->getUser());
701
+    $form = $this->getChangeUsernameForm($user);
699 702
     if ($request->getMethod() == 'POST')
700 703
     {
701 704
       $form->bind($request);
@@ -703,7 +706,7 @@ class UserController extends Controller
703 706
       if ($form->isValid() && !count($errors))
704 707
       {
705 708
         $form->getData()->setUsernameUpdatable(false);
706
-        $this->persist($form->getData());
709
+        $this->persist($user);
707 710
         $this->flush();
708 711
         $this->setFlash('success', 'user.change_username.success');
709 712
         return new RedirectResponse($this->generateUrl('my_account'));

+ 1 - 1
src/Muzich/UserBundle/Resources/views/User/account.html.twig Прегледај датотеку

@@ -13,7 +13,7 @@
13 13
       <li>
14 14
         <b>{{ 'my_account.username'|trans({}, 'userui') }}</b>: {{ user.name }}
15 15
         {% if app.user.isUsernameUpdatable %}
16
-          (<a href="{{ path('change_username') }}">{{ 'my_account.change_username'|trans({}, 'userui') }}</a>)
16
+          (<a class="username_update" href="{{ path('change_username') }}">{{ 'my_account.change_username'|trans({}, 'userui') }}</a>)
17 17
         {% endif %}
18 18
       </li>
19 19
       <li>