mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Tests\Controller;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
@@ -7,6 +14,10 @@ use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class UserControllerTest extends WebTestCase
|
||||
{
|
||||
private $client;
|
||||
@@ -17,102 +28,11 @@ class UserControllerTest extends WebTestCase
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
$this->client = static::createClient(array(), array(
|
||||
'PHP_AUTH_USER' => 'admin',
|
||||
'PHP_AUTH_PW' => 'password',
|
||||
'HTTP_ACCEPT_LANGUAGE' => 'fr_FR'
|
||||
));
|
||||
}
|
||||
|
||||
public function testList()
|
||||
{
|
||||
// get the list
|
||||
$crawler = $this->client->request('GET', '/fr/admin/main/user');
|
||||
$this->assertEquals(200, $this->client->getResponse()->getStatusCode(),
|
||||
"Unexpected HTTP status code for GET /admin/main/user");
|
||||
}
|
||||
|
||||
public function testNew()
|
||||
{
|
||||
$crawler = $this->client->request('GET', '/fr/admin/main/user/new');
|
||||
|
||||
$username = 'Test_user'. uniqid();
|
||||
$password = 'Password1234!';
|
||||
// Fill in the form and submit it
|
||||
$form = $crawler->selectButton('Créer')->form(array(
|
||||
'chill_mainbundle_user[username]' => $username,
|
||||
'chill_mainbundle_user[plainPassword][first]' => $password,
|
||||
'chill_mainbundle_user[plainPassword][second]' => $password,
|
||||
'chill_mainbundle_user[email]' => $username.'@gmail.com',
|
||||
'chill_mainbundle_user[label]' => $username,
|
||||
|
||||
));
|
||||
|
||||
$this->client->submit($form);
|
||||
$crawler = $this->client->followRedirect();
|
||||
|
||||
// Check data in the show view
|
||||
$this->assertGreaterThan(0, $crawler->filter('td:contains("Test_user")')->count(),
|
||||
'Missing element td:contains("Test user")');
|
||||
|
||||
//test the auth of the new client
|
||||
$this->isPasswordValid($username, $password);
|
||||
}
|
||||
|
||||
protected function isPasswordValid($username, $password)
|
||||
{
|
||||
/* @var $passwordEncoder \Symfony\Component\Security\Core\Encoder\UserPasswordEncoder */
|
||||
$passwordEncoder = self::$container
|
||||
->get(UserPasswordEncoderInterface::class);
|
||||
|
||||
$user = self::$kernel->getContainer()
|
||||
->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillMainBundle:User')
|
||||
->findOneBy(array('username' => $username));
|
||||
|
||||
$this->assertTrue($passwordEncoder->isPasswordValid($user, $password));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider dataGenerateUserId
|
||||
*/
|
||||
public function testUpdate(int $userId, string $username)
|
||||
{
|
||||
$crawler = $this->client->request('GET', "/fr/admin/main/user/$userId/edit");
|
||||
|
||||
$username = 'Foo bar '.uniqid();
|
||||
$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('[data-username="'.$username.'"]')->count(),
|
||||
'Missing element [data-username="Foo bar"]');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider dataGenerateUserId
|
||||
*/
|
||||
public function testUpdatePassword(int $userId, $username)
|
||||
{
|
||||
$crawler = $this->client->request('GET', "/fr/admin/user/$userId/edit_password");
|
||||
$newPassword = '1234Password!';
|
||||
|
||||
$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,
|
||||
));
|
||||
|
||||
$this->client->submit($form);
|
||||
|
||||
$this->assertTrue($this->client->getResponse()->isRedirect(),
|
||||
"the response is a redirection");
|
||||
|
||||
$this->isPasswordValid($username, $newPassword);
|
||||
$this->client = static::createClient([], [
|
||||
'PHP_AUTH_USER' => 'admin',
|
||||
'PHP_AUTH_PW' => 'password',
|
||||
'HTTP_ACCEPT_LANGUAGE' => 'fr_FR',
|
||||
]);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
@@ -120,7 +40,7 @@ class UserControllerTest extends WebTestCase
|
||||
self::bootKernel();
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
|
||||
foreach ($this->toDelete as list($class, $id)) {
|
||||
foreach ($this->toDelete as [$class, $id]) {
|
||||
$obj = $em->getRepository($class)->find($id);
|
||||
$em->remove($obj);
|
||||
}
|
||||
@@ -134,15 +54,118 @@ class UserControllerTest extends WebTestCase
|
||||
$em = self::$container->get(EntityManagerInterface::class);
|
||||
|
||||
$user = new User();
|
||||
$user->setUsername('Test_user '.uniqid());
|
||||
$user->setPassword(self::$container->get(UserPasswordEncoderInterface::class)->encodePassword($user,
|
||||
'password'));
|
||||
$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() ];
|
||||
yield [$user->getId(), $user->getUsername()];
|
||||
}
|
||||
|
||||
public function testList()
|
||||
{
|
||||
// get the list
|
||||
$crawler = $this->client->request('GET', '/fr/admin/main/user');
|
||||
$this->assertEquals(
|
||||
200,
|
||||
$this->client->getResponse()->getStatusCode(),
|
||||
'Unexpected HTTP status code for GET /admin/main/user'
|
||||
);
|
||||
}
|
||||
|
||||
public function testNew()
|
||||
{
|
||||
$crawler = $this->client->request('GET', '/fr/admin/main/user/new');
|
||||
|
||||
$username = 'Test_user' . uniqid();
|
||||
$password = 'Password1234!';
|
||||
// Fill in the form and submit it
|
||||
$form = $crawler->selectButton('Créer')->form([
|
||||
'chill_mainbundle_user[username]' => $username,
|
||||
'chill_mainbundle_user[plainPassword][first]' => $password,
|
||||
'chill_mainbundle_user[plainPassword][second]' => $password,
|
||||
'chill_mainbundle_user[email]' => $username . '@gmail.com',
|
||||
'chill_mainbundle_user[label]' => $username,
|
||||
]);
|
||||
|
||||
$this->client->submit($form);
|
||||
$crawler = $this->client->followRedirect();
|
||||
|
||||
// Check data in the show view
|
||||
$this->assertGreaterThan(
|
||||
0,
|
||||
$crawler->filter('td:contains("Test_user")')->count(),
|
||||
'Missing element td:contains("Test user")'
|
||||
);
|
||||
|
||||
//test the auth of the new client
|
||||
$this->isPasswordValid($username, $password);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataGenerateUserId
|
||||
*/
|
||||
public function testUpdate(int $userId, string $username)
|
||||
{
|
||||
$crawler = $this->client->request('GET', "/fr/admin/main/user/{$userId}/edit");
|
||||
|
||||
$username = 'Foo bar ' . uniqid();
|
||||
$form = $crawler->selectButton('Enregistrer & fermer')->form([
|
||||
'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('[data-username="' . $username . '"]')->count(),
|
||||
'Missing element [data-username="Foo bar"]'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataGenerateUserId
|
||||
*
|
||||
* @param mixed $username
|
||||
*/
|
||||
public function testUpdatePassword(int $userId, $username)
|
||||
{
|
||||
$crawler = $this->client->request('GET', "/fr/admin/user/{$userId}/edit_password");
|
||||
$newPassword = '1234Password!';
|
||||
|
||||
$form = $crawler->selectButton('Changer le mot de passe')->form([
|
||||
'chill_mainbundle_user_password[new_password][first]' => $newPassword,
|
||||
'chill_mainbundle_user_password[new_password][second]' => $newPassword,
|
||||
]);
|
||||
|
||||
$this->client->submit($form);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->client->getResponse()->isRedirect(),
|
||||
'the response is a redirection'
|
||||
);
|
||||
|
||||
$this->isPasswordValid($username, $newPassword);
|
||||
}
|
||||
|
||||
protected function isPasswordValid($username, $password)
|
||||
{
|
||||
/* @var $passwordEncoder \Symfony\Component\Security\Core\Encoder\UserPasswordEncoder */
|
||||
$passwordEncoder = self::$container
|
||||
->get(UserPasswordEncoderInterface::class);
|
||||
|
||||
$user = self::$kernel->getContainer()
|
||||
->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillMainBundle:User')
|
||||
->findOneBy(['username' => $username]);
|
||||
|
||||
$this->assertTrue($passwordEncoder->isPasswordValid($user, $password));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user