Browse Source

Evolution #48: Ajout de tests: Changement d'email de contact

bastien 13 years ago
parent
commit
c777e8e687

+ 171 - 0
src/Muzich/CoreBundle/Tests/Controller/UserControllerTest.php View File

@@ -175,4 +175,175 @@ class UserControllerTest extends FunctionalTest
175 175
     $this->assertEquals(1, count($prefereds));
176 176
   }
177 177
   
178
+  /**
179
+   * Test de al procédure de changement d'email.
180
+   */
181
+  public function testChangeEmail()
182
+  {
183
+    $this->client = self::createClient();
184
+    $this->connectUser('bob', 'toor');
185
+    $bob = $this->findUserByUsername('bob');
186
+    
187
+    // Ouverture de la page Mon compte
188
+    $this->crawler = $this->client->request('GET', $this->generateUrl('my_account'));
189
+    
190
+    // Le mail en cours n'est pas celui que nous voulons mettre
191
+    $this->assertFalse($bob->getEmail() == 'trololololooo@trolo.com');
192
+    // Nous n'avons pas encore demandé de nouveau mail
193
+    $this->assertTrue($bob->getEmailRequested() == null);
194
+    $this->assertTrue($bob->getEmailRequestedDatetime() == null);
195
+    
196
+    // On fait un premier essaie avec une email mal formulé
197
+    $this->exist('form[action="'.($url = $this->generateUrl(
198
+      'change_email_request'
199
+    )).'"]');
200
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
201
+    
202
+    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
203
+    $form['form[email]'] = 'trololololooo@trolo';
204
+    $this->submit($form);
205
+    
206
+    // Il n'y as pas de redirection
207
+    $this->isResponseSuccess();
208
+    
209
+    $bob = $this->findUserByUsername('bob');
210
+    // Les champs n'ont pas bougés
211
+    $this->assertFalse($bob->getEmail() == 'trololololooo@trolo.com');
212
+    // Nous n'avons pas encore demandé de nouveau mail
213
+    $this->assertTrue($bob->getEmailRequested() == null);
214
+    $this->assertTrue($bob->getEmailRequestedDatetime() == null);
215
+    
216
+    $this->exist('form[action="'.($url = $this->generateUrl(
217
+      'change_email_request'
218
+    )).'"]');
219
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
220
+    
221
+    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
222
+    $form['form[email]'] = 'trololololooo@trolo.com';
223
+    $this->submit($form);
224
+    
225
+    // Ce coup-ci c'est bien une redirection
226
+    $this->isResponseRedirection();
227
+    
228
+    // Un mail a été envoyé
229
+    $mc = $this->getMailerMessageDataCollector();
230
+    $this->assertEquals(1, $mc->getMessageCount());
231
+    
232
+    $mails = $mc->getMessages();
233
+    $mail = $mails[0];
234
+    
235
+    // Les champs ont bougés
236
+    $bob = $this->findUserByUsername('bob');
237
+    $this->assertFalse($bob->getEmail() == 'trololololooo@trolo.com');
238
+    $this->assertFalse($bob->getEmailRequested() == null);
239
+    $this->assertTrue($bob->getEmailRequested() == 'trololololooo@trolo.com');
240
+    $this->assertFalse($bob->getEmailRequestedDatetime() == null);
241
+    
242
+    $this->followRedirection();
243
+    $this->isResponseSuccess();
244
+    
245
+    // On ouvre un lien erroné
246
+    $badurl = $this->generateUrl(
247
+      'change_email_confirm', 
248
+      array('token' => $this->getUser()->getConfirmationToken()), 
249
+      true
250
+    );
251
+    $this->crawler = $this->client->request('GET', $badurl);
252
+    $this->isResponseRedirection();
253
+    $this->followRedirection();
254
+    $this->isResponseSuccess();
255
+    $this->exist('div.error');
256
+    
257
+    // Et les champs ont pas bougés
258
+    $bob = $this->findUserByUsername('bob');
259
+    $this->assertFalse($bob->getEmail() == 'trololololooo@trolo.com');
260
+    $this->assertFalse($bob->getEmailRequested() == null);
261
+    $this->assertTrue($bob->getEmailRequested() == 'trololololooo@trolo.com');
262
+    $this->assertFalse($bob->getEmailRequestedDatetime() == null);
263
+    
264
+    $this->assertTrue(!is_null(strpos($mail->getBody(), ($url = $this->generateUrl(
265
+      'change_email_confirm', 
266
+      array('token' => $token = hash('sha256', $bob->getConfirmationToken().'trololololooo@trolo.com')), 
267
+      true
268
+    )))));
269
+    
270
+    // On ouvre le bon lien
271
+    $this->crawler = $this->client->request('GET', $url);
272
+    
273
+    // C'est un succés
274
+    $this->isResponseRedirection();
275
+    $this->followRedirection();
276
+    $this->isResponseSuccess();
277
+    
278
+    $this->outputDebug();
279
+    $this->notExist('div.error');
280
+    
281
+    // Et les champs ont bougés
282
+    $bob = $this->findUserByUsername('bob');
283
+    $this->assertTrue($bob->getEmail() == 'trololololooo@trolo.com');
284
+    $this->assertTrue($bob->getEmailRequested() == null);
285
+    $this->assertFalse($bob->getEmailRequestedDatetime() == null);
286
+    
287
+    // Par contre si on refait une demande maintenant ca échoue (délais entre demandes)
288
+    $this->exist('form[action="'.($url = $this->generateUrl(
289
+      'change_email_request'
290
+    )).'"]');
291
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
292
+    
293
+    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
294
+    $form['form[email]'] = 'trololololooo222@trolo.com';
295
+    $this->submit($form);
296
+    
297
+    // Il n'y as pas de redirection
298
+    $this->isResponseRedirection();
299
+    $this->followRedirection();
300
+    $this->isResponseSuccess();
301
+    $this->exist('div.error');
302
+    
303
+    // Et les champs ont bougés
304
+    $bob = $this->findUserByUsername('bob');
305
+    $this->assertTrue($bob->getEmail() == 'trololololooo@trolo.com');
306
+    $this->assertTrue($bob->getEmailRequested() == null);
307
+    $this->assertFalse($bob->getEmailRequestedDatetime() == null);
308
+    
309
+    // Si par contre on manipule le dateTime on pourra
310
+    $bob = $this->findUserByUsername('bob');
311
+    $bob->setEmailRequestedDatetime(
312
+      $this->getUser()->getEmailRequestedDatetime() 
313
+      - $this->getContainer()->getParameter('changeemail_security_delay')
314
+    );
315
+    
316
+    $this->getDoctrine()->getEntityManager()->flush();
317
+    
318
+    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
319
+    $form['form[email]'] = 'trololololooo222@trolo.com';
320
+    $this->submit($form);
321
+    
322
+    // Ce coup-ci c'est bien une redirection
323
+    $this->isResponseRedirection();
324
+    
325
+    // Un mail a été envoyé
326
+    $mc = $this->getMailerMessageDataCollector();
327
+    $this->assertEquals(1, $mc->getMessageCount());
328
+    
329
+    $mails = $mc->getMessages();
330
+    $mail = $mails[0];
331
+       
332
+    $this->assertTrue(!is_null(strpos($mail->getBody(), ($url = $this->generateUrl(
333
+      'change_email_confirm', 
334
+      array('token' => hash('sha256', $this->getUser()->getConfirmationToken().'trololololooo222@trolo.com')), 
335
+      true
336
+    )))));
337
+    
338
+    // Les champs ont bougés
339
+    $bob = $this->findUserByUsername('bob');
340
+    $this->assertFalse($bob->getEmail() == 'trololololooo222@trolo.com');
341
+    $this->assertFalse($bob->getEmailRequested() == null);
342
+    $this->assertTrue($bob->getEmailRequested() == 'trololololooo222@trolo.com');
343
+    $this->assertFalse($bob->getEmailRequestedDatetime() == null);
344
+    
345
+    $this->followRedirection();
346
+    $this->isResponseSuccess();
347
+  }
348
+  
178 349
 }

+ 13 - 0
src/Muzich/CoreBundle/lib/FunctionalTest.php View File

@@ -197,6 +197,19 @@ class FunctionalTest extends WebTestCase
197 197
   }
198 198
   
199 199
   /**
200
+   * Retourne un utilisateur en allant le chercher en base.
201
+   * 
202
+   * @param string $username
203
+   * @return \Muzich\CoreBundle\Entity\User 
204
+   */
205
+  protected function findUserByUsername($username)
206
+  {
207
+    return $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:User')
208
+      ->findOneByUsername($username)
209
+    ;
210
+  }
211
+  
212
+  /**
200 213
    * Generates a URL from the given parameters.
201 214
    *
202 215
    * @param string $route

+ 27 - 0
src/Muzich/UserBundle/Controller/UserController.php View File

@@ -339,6 +339,19 @@ class UserController extends Controller
339 339
   {
340 340
     $em = $this->getDoctrine()->getEntityManager();
341 341
     $user = $this->getUser();
342
+    
343
+    /**
344
+     * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
345
+     * Docrine le voit si on faire une requete directe.
346
+     */
347
+    if ($this->container->getParameter('env') == 'test')
348
+    {
349
+      $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
350
+        $this->container->get('security.context')->getToken()->getUser()->getId(),
351
+        array()
352
+      )->getSingleResult();
353
+    }
354
+    
342 355
     $request = $this->getRequest();
343 356
     $change_email_form = $this->getChangeEmailForm();
344 357
     
@@ -388,6 +401,7 @@ class UserController extends Controller
388 401
       
389 402
       $this->setFlash('info', 'user.changeemail.mail_send');
390 403
       $em->flush();
404
+      return new RedirectResponse($this->generateUrl('my_account'));
391 405
     }
392 406
     
393 407
     // En cas d'échec
@@ -415,6 +429,19 @@ class UserController extends Controller
415 429
     $em = $this->getDoctrine()->getEntityManager();
416 430
     $um = $this->get('muzich_user_manager');
417 431
     $user = $this->getUser();
432
+    
433
+    /**
434
+     * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
435
+     * Docrine le voit si on faire une requete directe.
436
+     */
437
+    if ($this->container->getParameter('env') == 'test')
438
+    {
439
+      $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
440
+        $this->container->get('security.context')->getToken()->getUser()->getId(),
441
+        array()
442
+      )->getSingleResult();
443
+    }
444
+    
418 445
     $token_ = hash('sha256', $user->getConfirmationToken().($email = $user->getEmailRequested()));
419 446
     
420 447
     // Le token est-il valide