From d5d12c4a17e1f52e797727833ba1e37dfc437601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 28 Sep 2021 15:50:09 +0200 Subject: [PATCH] fix tests for user controller --- .../Resources/views/User/index.html.twig | 2 +- .../Tests/Controller/UserControllerTest.php | 87 ++++++++++--------- 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Resources/views/User/index.html.twig b/src/Bundle/ChillMainBundle/Resources/views/User/index.html.twig index c57d9ed5f..53bd89482 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/User/index.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/User/index.html.twig @@ -10,7 +10,7 @@ {% endblock %} {% block table_entities_tbody %} {% for entity in entities %} - + {% if entity.isEnabled %} diff --git a/src/Bundle/ChillMainBundle/Tests/Controller/UserControllerTest.php b/src/Bundle/ChillMainBundle/Tests/Controller/UserControllerTest.php index 1953439dd..0017fbb4e 100644 --- a/src/Bundle/ChillMainBundle/Tests/Controller/UserControllerTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Controller/UserControllerTest.php @@ -2,12 +2,17 @@ namespace Chill\MainBundle\Tests\Controller; +use Chill\MainBundle\Entity\User; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; class UserControllerTest extends WebTestCase { private $client; + private array $toDelete = []; + public function setUp() { self::bootKernel(); @@ -22,18 +27,14 @@ class UserControllerTest extends WebTestCase public function testList() { // get the list - $crawler = $this->client->request('GET', '/fr/admin/user/'); + $crawler = $this->client->request('GET', '/fr/admin/main/user'); $this->assertEquals(200, $this->client->getResponse()->getStatusCode(), - "Unexpected HTTP status code for GET /admin/user/"); - - $link = $crawler->selectLink('Ajouter un nouvel utilisateur')->link(); - $this->assertInstanceOf('Symfony\Component\DomCrawler\Link', $link); - $this->assertRegExp('|/fr/admin/user/new$|', $link->getUri()); + "Unexpected HTTP status code for GET /admin/main/user"); } public function testNew() { - $crawler = $this->client->request('GET', '/fr/admin/user/new'); + $crawler = $this->client->request('GET', '/fr/admin/main/user/new'); $username = 'Test_user'. uniqid(); $password = 'Password1234!'; @@ -54,22 +55,15 @@ class UserControllerTest extends WebTestCase $this->assertGreaterThan(0, $crawler->filter('td:contains("Test_user")')->count(), 'Missing element td:contains("Test user")'); - $update = $crawler->selectLink('Modifier')->link(); - - $this->assertInstanceOf('Symfony\Component\DomCrawler\Link', $update); - $this->assertRegExp('|/fr/admin/user/[0-9]{1,}/edit$|', $update->getUri()); - //test the auth of the new client $this->isPasswordValid($username, $password); - - return $update; } protected function isPasswordValid($username, $password) { /* @var $passwordEncoder \Symfony\Component\Security\Core\Encoder\UserPasswordEncoder */ - $passwordEncoder = self::$kernel->getContainer() - ->get('security.password_encoder'); + $passwordEncoder = self::$container + ->get(UserPasswordEncoderInterface::class); $user = self::$kernel->getContainer() ->get('doctrine.orm.entity_manager') @@ -81,46 +75,33 @@ class UserControllerTest extends WebTestCase /** * - * @param \Symfony\Component\DomCrawler\Link $update - * @depends testNew + * @dataProvider dataGenerateUserId */ - public function testUpdate(\Symfony\Component\DomCrawler\Link $update) + public function testUpdate(int $userId, string $username) { - $crawler = $this->client->click($update); + $crawler = $this->client->request('GET', "/fr/admin/main/user/$userId/edit"); $username = 'Foo bar '.uniqid(); - $form = $crawler->selectButton('Mettre à jour')->form(array( + $form = $crawler->selectButton('Enregistrer & fermer')->form(array( 'chill_mainbundle_user[username]' => $username, )); $this->client->submit($form); $crawler = $this->client->followRedirect(); // Check the element contains an attribute with value equals "Foo" - $this->assertGreaterThan(0, $crawler->filter('[value="'.$username.'"]')->count(), - 'Missing element [value="Foo bar"]'); - - $updatePassword = $crawler->selectLink('Modifier le mot de passe')->link(); - - $this->assertInstanceOf('Symfony\Component\DomCrawler\Link', $updatePassword); - $this->assertRegExp('|/fr/admin/user/[0-9]{1,}/edit_password$|', - $updatePassword->getUri()); - - return array('link' => $updatePassword, 'username' => $username); + $this->assertGreaterThan(0, $crawler->filter('[data-username="'.$username.'"]')->count(), + 'Missing element [data-username="Foo bar"]'); } /** * - * @param \Symfony\Component\DomCrawler\Link $updatePassword - * @depends testUpdate + * @dataProvider dataGenerateUserId */ - public function testUpdatePassword(array $params) + public function testUpdatePassword(int $userId, $username) { - $link = $params['link']; - $username = $params['username']; + $crawler = $this->client->request('GET', "/fr/admin/user/$userId/edit_password"); $newPassword = '1234Password!'; - $crawler = $this->client->click($link); - $form = $crawler->selectButton('Changer le mot de passe')->form(array( 'chill_mainbundle_user_password[new_password][first]' => $newPassword, 'chill_mainbundle_user_password[new_password][second]' => $newPassword, @@ -130,10 +111,38 @@ class UserControllerTest extends WebTestCase $this->assertTrue($this->client->getResponse()->isRedirect(), "the response is a redirection"); - $this->client->followRedirect(); $this->isPasswordValid($username, $newPassword); } + protected function tearDown() + { + self::bootKernel(); + $em = self::$container->get(EntityManagerInterface::class); + foreach ($this->toDelete as list($class, $id)) { + $obj = $em->getRepository($class)->find($id); + $em->remove($obj); + } + + $em->flush(); + } + + public function dataGenerateUserId() + { + self::bootKernel(); + $em = self::$container->get(EntityManagerInterface::class); + + $user = new User(); + $user->setUsername('Test_user '.uniqid()); + $user->setPassword(self::$container->get(UserPasswordEncoderInterface::class)->encodePassword($user, + 'password')); + + $em->persist($user); + $em->flush(); + + $this->toDelete[] = [User::class, $user->getId()]; + + yield [ $user->getId(), $user->getUsername() ]; + } }