fix tests for user controller

This commit is contained in:
Julien Fastré 2021-09-28 15:50:09 +02:00
parent ca7ba90717
commit d5d12c4a17
2 changed files with 49 additions and 40 deletions

View File

@ -10,7 +10,7 @@
{% endblock %} {% endblock %}
{% block table_entities_tbody %} {% block table_entities_tbody %}
{% for entity in entities %} {% for entity in entities %}
<tr> <tr data-username="{{ entity.username|e('html_attr') }}">
<td> <td>
{% if entity.isEnabled %} {% if entity.isEnabled %}
<i class="fa fa-check chill-green"></i> <i class="fa fa-check chill-green"></i>

View File

@ -2,12 +2,17 @@
namespace Chill\MainBundle\Tests\Controller; namespace Chill\MainBundle\Tests\Controller;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class UserControllerTest extends WebTestCase class UserControllerTest extends WebTestCase
{ {
private $client; private $client;
private array $toDelete = [];
public function setUp() public function setUp()
{ {
self::bootKernel(); self::bootKernel();
@ -22,18 +27,14 @@ class UserControllerTest extends WebTestCase
public function testList() public function testList()
{ {
// get the list // 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(), $this->assertEquals(200, $this->client->getResponse()->getStatusCode(),
"Unexpected HTTP status code for GET /admin/user/"); "Unexpected HTTP status code for GET /admin/main/user");
$link = $crawler->selectLink('Ajouter un nouvel utilisateur')->link();
$this->assertInstanceOf('Symfony\Component\DomCrawler\Link', $link);
$this->assertRegExp('|/fr/admin/user/new$|', $link->getUri());
} }
public function testNew() 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(); $username = 'Test_user'. uniqid();
$password = 'Password1234!'; $password = 'Password1234!';
@ -54,22 +55,15 @@ class UserControllerTest extends WebTestCase
$this->assertGreaterThan(0, $crawler->filter('td:contains("Test_user")')->count(), $this->assertGreaterThan(0, $crawler->filter('td:contains("Test_user")')->count(),
'Missing element td:contains("Test user")'); '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 //test the auth of the new client
$this->isPasswordValid($username, $password); $this->isPasswordValid($username, $password);
return $update;
} }
protected function isPasswordValid($username, $password) protected function isPasswordValid($username, $password)
{ {
/* @var $passwordEncoder \Symfony\Component\Security\Core\Encoder\UserPasswordEncoder */ /* @var $passwordEncoder \Symfony\Component\Security\Core\Encoder\UserPasswordEncoder */
$passwordEncoder = self::$kernel->getContainer() $passwordEncoder = self::$container
->get('security.password_encoder'); ->get(UserPasswordEncoderInterface::class);
$user = self::$kernel->getContainer() $user = self::$kernel->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
@ -81,46 +75,33 @@ class UserControllerTest extends WebTestCase
/** /**
* *
* @param \Symfony\Component\DomCrawler\Link $update * @dataProvider dataGenerateUserId
* @depends testNew
*/ */
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(); $username = 'Foo bar '.uniqid();
$form = $crawler->selectButton('Mettre à jour')->form(array( $form = $crawler->selectButton('Enregistrer & fermer')->form(array(
'chill_mainbundle_user[username]' => $username, 'chill_mainbundle_user[username]' => $username,
)); ));
$this->client->submit($form); $this->client->submit($form);
$crawler = $this->client->followRedirect(); $crawler = $this->client->followRedirect();
// Check the element contains an attribute with value equals "Foo" // Check the element contains an attribute with value equals "Foo"
$this->assertGreaterThan(0, $crawler->filter('[value="'.$username.'"]')->count(), $this->assertGreaterThan(0, $crawler->filter('[data-username="'.$username.'"]')->count(),
'Missing element [value="Foo bar"]'); 'Missing element [data-username="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);
} }
/** /**
* *
* @param \Symfony\Component\DomCrawler\Link $updatePassword * @dataProvider dataGenerateUserId
* @depends testUpdate
*/ */
public function testUpdatePassword(array $params) public function testUpdatePassword(int $userId, $username)
{ {
$link = $params['link']; $crawler = $this->client->request('GET', "/fr/admin/user/$userId/edit_password");
$username = $params['username'];
$newPassword = '1234Password!'; $newPassword = '1234Password!';
$crawler = $this->client->click($link);
$form = $crawler->selectButton('Changer le mot de passe')->form(array( $form = $crawler->selectButton('Changer le mot de passe')->form(array(
'chill_mainbundle_user_password[new_password][first]' => $newPassword, 'chill_mainbundle_user_password[new_password][first]' => $newPassword,
'chill_mainbundle_user_password[new_password][second]' => $newPassword, 'chill_mainbundle_user_password[new_password][second]' => $newPassword,
@ -130,10 +111,38 @@ class UserControllerTest extends WebTestCase
$this->assertTrue($this->client->getResponse()->isRedirect(), $this->assertTrue($this->client->getResponse()->isRedirect(),
"the response is a redirection"); "the response is a redirection");
$this->client->followRedirect();
$this->isPasswordValid($username, $newPassword); $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() ];
}
} }