mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
fix tests for user controller
This commit is contained in:
parent
ca7ba90717
commit
d5d12c4a17
@ -10,7 +10,7 @@
|
||||
{% endblock %}
|
||||
{% block table_entities_tbody %}
|
||||
{% for entity in entities %}
|
||||
<tr>
|
||||
<tr data-username="{{ entity.username|e('html_attr') }}">
|
||||
<td>
|
||||
{% if entity.isEnabled %}
|
||||
<i class="fa fa-check chill-green"></i>
|
||||
|
@ -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() ];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user