Skip to content
Snippets Groups Projects
Commit ba76e79e authored by Camille Simiand's avatar Camille Simiand
Browse files

Add functional test

parent 96d05593
No related branches found
No related tags found
1 merge request!69tuleap-142-when-editing-email-address-user-should-confirm-it-with-email-validation
...@@ -54,6 +54,13 @@ class UserController extends AbstractController ...@@ -54,6 +54,13 @@ class UserController extends AbstractController
return $this->redirectToRoute('app_logout'); return $this->redirectToRoute('app_logout');
} }
$last_pending_email_address = $this->pending_email_address_repository->findOneBy(
['user' => $current_user->getId()]
);
if ($last_pending_email_address !== null) {
$this->entity_manager->remove($last_pending_email_address);
}
$users_emails = $this->user_repository->getAllEmails(); $users_emails = $this->user_repository->getAllEmails();
$pending_email_addresses = $this->pending_email_address_repository->getAllEmails(); $pending_email_addresses = $this->pending_email_address_repository->getAllEmails();
...@@ -70,13 +77,6 @@ class UserController extends AbstractController ...@@ -70,13 +77,6 @@ class UserController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
if ($current_user->getEmail() !== $form->get('email')->getData()) { if ($current_user->getEmail() !== $form->get('email')->getData()) {
$last_pending_email_address = $this->pending_email_address_repository->findOneBy(
['user' => $current_user->getId()]
);
if ($last_pending_email_address !== null) {
$this->entity_manager->remove($last_pending_email_address);
}
$pending_email_address = new PendingEmailAddress(); $pending_email_address = new PendingEmailAddress();
$pending_email_address->setEmail($form->get('email')->getData()); $pending_email_address->setEmail($form->get('email')->getData());
$pending_email_address->setUser($current_user); $pending_email_address->setUser($current_user);
...@@ -85,7 +85,7 @@ class UserController extends AbstractController ...@@ -85,7 +85,7 @@ class UserController extends AbstractController
$email = (new TemplatedEmail()) $email = (new TemplatedEmail())
->to(new Address($pending_email_address->getEmail())) ->to(new Address($pending_email_address->getEmail()))
->subject('Please Confirm your Email') ->subject($this->translator->trans('user.edit.email.title'))
->htmlTemplate('user/update_email.html.twig') ->htmlTemplate('user/update_email.html.twig')
->context([ ->context([
'expiration_date' => new \DateTime('+1 day') 'expiration_date' => new \DateTime('+1 day')
...@@ -157,7 +157,6 @@ class UserController extends AbstractController ...@@ -157,7 +157,6 @@ class UserController extends AbstractController
public function verifyNewEmailAddress(): Response public function verifyNewEmailAddress(): Response
{ {
$current_user = $this->getUser(); $current_user = $this->getUser();
if (! $current_user instanceof User) { if (! $current_user instanceof User) {
return $this->redirectToRoute('app_logout'); return $this->redirectToRoute('app_logout');
} }
...@@ -169,7 +168,6 @@ class UserController extends AbstractController ...@@ -169,7 +168,6 @@ class UserController extends AbstractController
$current_user->setEmail($pending_email_address->getEmail()); $current_user->setEmail($pending_email_address->getEmail());
$this->entity_manager->persist($current_user); $this->entity_manager->persist($current_user);
$this->entity_manager->remove($pending_email_address); $this->entity_manager->remove($pending_email_address);
$this->entity_manager->flush(); $this->entity_manager->flush();
......
<?php
namespace App\Tests\functional;
use App\Entity\User;
use Exception;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class UserControllerTest extends WebTestCase
{
private KernelBrowser $client;
private User $verified_user;
protected function setUp(): void
{
self::ensureKernelShutdown();
$this->client = static::createClient();
$object_manager = $this->client->getContainer()
->get('doctrine')
->getManager();
$verified_user = $object_manager
->getRepository(User::class)
->findOneBy(['email' => 'defaultUser@localhost.com']);
if (! $verified_user instanceof User) {
throw new \Exception("User does not exist.");
}
$this->verified_user = $verified_user;
}
public function testWhenUserUpdateEmailHeReceivesAConfirmationLinkByEMail(): void
{
$new_email_address = 'defaultuser@localhost.test';
$this->client->loginUser($this->verified_user);
$crawler = $this->client->request('GET', '/edit_profile');
$this->assertResponseIsSuccessful();
$this->client->enableProfiler();
$submit = $crawler->selectButton('Update');
$form = $submit->form();
$form['edit_user_profile_form[email]'] = $new_email_address;
$form['edit_user_profile_form[current_password]'] = "password";
$this->client->submit($form);
$this->assertEmailCount(
1,
null,
'Once the user update his email, the system should send him a confirmation link by email'
);
$emailMessage = $this->getMailerMessage(0);
if (null === $emailMessage) {
throw new Exception("Email message could not be found");
}
$this->assertEmailAddressContains(
$emailMessage,
'To',
$new_email_address
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment