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

Fix tests

parent 0e46c11f
Branches
Tags
1 merge request!32tuleap-50-create-a-capsule-for-an-unexisting-project-in-the-legacy
......@@ -70,7 +70,8 @@
"symfony/maker-bundle": "^1.0",
"symfony/phpunit-bridge": "^5.3",
"symfony/stopwatch": "5.3.*",
"symfony/web-profiler-bundle": "5.3.*"
"symfony/web-profiler-bundle": "5.3.*",
"ext-dom": "*"
},
"config": {
"optimize-autoloader": true,
......
......@@ -24,7 +24,8 @@ class UserFixtures extends Fixture
"Test",
"Test LastName",
"",
["ROLE_USER"]
["ROLE_USER"],
false
);
$this->createUserWithData(
......@@ -33,7 +34,8 @@ class UserFixtures extends Fixture
"Test",
"Test LastName",
"",
["ROLE_USER"]
["ROLE_USER"],
true
);
$this->createUserWithData(
......@@ -42,7 +44,8 @@ class UserFixtures extends Fixture
"Test",
"Test LastName",
"",
["ROLE_USER"]
["ROLE_USER"],
true
);
$manager->flush();
......@@ -54,7 +57,8 @@ class UserFixtures extends Fixture
string $first_name,
string $lastname,
string $salt,
array $roles
array $roles,
bool $is_verified
): void {
$user = new User();
$user->setEmail($email);
......@@ -62,6 +66,7 @@ class UserFixtures extends Fixture
$user->setLastName($lastname);
$user->setSalt($salt);
$user->setRoles($roles);
$user->setIsVerified($is_verified);
$password = $this->passwordHasher->hashPassword($user, 'password');
$user->setPassword($password);
......
......@@ -2,9 +2,15 @@
namespace App\Repository;
use App\Entity\Capsule;
use App\Entity\User;
use Doctrine\ORM\EntityRepository;
/**
* @method Capsule|null find($id, $lockMode = null, $lockVersion = null)
* @method Capsule|null findOneBy(array $criteria, array $orderBy = null)
* @method Capsule[] findAll()
* @method Capsule[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
* @template CapsuleEntity of object
* @extends EntityRepository<CapsuleEntity>
*/
......
......@@ -13,6 +13,7 @@ class LoginTest extends WebTestCase
protected function setUp(): void
{
self::ensureKernelShutdown();
$this->client = static::createClient();
$crawler = $this->client->request('GET', '/login');
$this->assertResponseIsSuccessful();
......@@ -32,7 +33,7 @@ class LoginTest extends WebTestCase
$this->assertResponseRedirects(
'/my_capsules',
302,
'Once the user is logged in, He should be redirected to its capsules lists'
'Once the user is logged in, he should be redirected to its capsules lists'
);
$this->client->followRedirect();
......
......@@ -4,8 +4,8 @@ namespace App\Tests\functional;
use App\Entity\Capsule;
use App\Entity\User;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DomCrawler\Form;
......@@ -14,20 +14,28 @@ use Symfony\Component\Filesystem\Filesystem;
class ProjectControllerTest extends WebTestCase
{
private KernelBrowser $client;
private $manager;
private EntityManager $entity_manager;
private Form $form;
private const TEST_DIR_PATH = __DIR__ . '/../../legacy/';
private const CAPSULE_NAME = 'TestCapsuleName';
private const CAPSULE_DIR_PATH = self::TEST_DIR_PATH . self::CAPSULE_NAME;
private const XML_FILE_PATH = self::CAPSULE_DIR_PATH . '/file/project.xml';
protected function setUp(): void
{
chdir(self::TEST_DIR_PATH);
self::ensureKernelShutdown();
$this->client = static::createClient();
$this->manager = $this->client->getContainer()->get('doctrine')->getManager();
$user_repository = $this->manager->getRepository(User::class);
$verified_user = $user_repository->findOneByEmail('defaultUser@localhost.com');
$this->entity_manager = $this->client->getContainer()
->get('doctrine')
->getManager();
$verified_user = $this->entity_manager
->getRepository(User::class)
->findOneBy(['email' => 'defaultUser@localhost.com'])
;
$this->client->loginUser($verified_user);
$crawler = $this->client->request('GET', '/create');
......@@ -45,11 +53,20 @@ class ProjectControllerTest extends WebTestCase
$file_system->remove(self::TEST_DIR_PATH . self::CAPSULE_NAME);
}
private function getDOMDocument(): \DOMDocument
{
$dom_xml = new \DOMDocument();
$dom_xml->preserveWhiteSpace = false;
$dom_xml->load(self::XML_FILE_PATH);
return $dom_xml;
}
public function testProjectDirectoryWithCorrespondingXMLFileIsCreatedWhenCapsuleCreationIsSuccessful(): void
{
$video_url = "https://TestUrl";
$this->form['create_capsule_form[name]'] = self::CAPSULE_NAME;
$this->form['create_capsule_form[video_url]'] = "https://TestUrl";
$this->form['create_capsule_form[video_url]'] = $video_url;
$this->client->submit($this->form);
......@@ -61,31 +78,21 @@ class ProjectControllerTest extends WebTestCase
$this->client->followRedirect();
$capsule_repository = $this->manager->getRepository(Capsule::class);
$capsule_in_db = $capsule_repository->findOneByName(self::CAPSULE_NAME);
$capsule_repository = $this->entity_manager->getRepository(Capsule::class);
$capsule_in_db = $capsule_repository->findOneBy(['name' => self::CAPSULE_NAME]);
self::assertInstanceOf(Capsule::class, $capsule_in_db);
$capsule_name_in_db = $capsule_in_db->getName();
$dom_xml = self::getDomDocument();
$video_node = $dom_xml->getElementsByTagName('video')->item(0);
$video_url_in_xml_file = $video_node->getAttribute('url');
$this->assertResponseIsSuccessful('/my_capsules');
$this->assertEquals($video_url, $video_url_in_xml_file);
$this->assertDirectoryExists(self::CAPSULE_NAME);
$this->assertDirectoryIsReadable(self::CAPSULE_NAME);
$this->assertSame(self::CAPSULE_NAME, $capsule_name_in_db);
}
// public function testNoProjectDirectoryIsCreatedWhenCapsuleCreationFail(): void
// {
// $capsule_name = 'TestCapsuleName';
// $this->form['name'] = $capsule_name;
// $this->form['video_url'] = "https://TestUrl";
//
// $this->client->submit($this->form);
//
// $this->assertResponseRedirects(
// '/my_capsules',
// 302,
// 'Once the capsule is created, the user should be redirected to its capsules lists'
// );
//
// $this->client->followRedirect();
//
// $this->assertDirectoryDoesNotExist($capsule_name);
// }
}
......@@ -11,22 +11,28 @@ use Symfony\Component\Mime\RawMessage;
class RegistrationControllerTest extends WebTestCase
{
private KernelBrowser $client;
protected function setUp(): void
{
self::ensureKernelShutdown();
$this->client = static::createClient();
}
public function testNewUserRegistrationShouldBeNotifiedOfAccountValidationByMail(): void
{
$userEmail = 'newUser@localhost.com';
$client = static::createClient();
$this->registerUser($userEmail, $client);
$this->registerUser($userEmail, $this->client);
$this->assertUserIsRedirectedToTheEmailNotificationPage($client);
$this->assertUserIsRedirectedToTheEmailNotificationPage($this->client);
}
public function testNewUserRegistrationShouldHaveUnverifiedAccount(): void
{
$userEmail = 'newUser@localhost.com';
$client = static::createClient();
$this->registerUser($userEmail, $client);
$this->registerUser($userEmail, $this->client);
$user = $this->getUserByEmail($userEmail);
......@@ -39,9 +45,8 @@ class RegistrationControllerTest extends WebTestCase
public function testWhenNewUserRegistersThenTheSystemShouldSendAnEmailToTheUser(): void
{
$userEmail = 'newUser@localhost.com';
$client = static::createClient();
$this->registerUser($userEmail, $client);
$this->registerUser($userEmail, $this->client);
$this->checkEmailHasBeenSentAndGetEmailMessage($userEmail);
}
......@@ -49,13 +54,12 @@ class RegistrationControllerTest extends WebTestCase
public function testEmailValidationRegistrationShouldEnableUser(): void
{
$userEmail = 'newUser@localhost.com';
$client = static::createClient();
$this->registerUser($userEmail, $client);
$this->registerUser($userEmail, $this->client);
$emailMessage = $this->checkEmailHasBeenSentAndGetEmailMessage($userEmail);
$client->followRedirect();
$this->client->followRedirect();
$this->clickOnEmailMessageLink($emailMessage, $client);
$this->clickOnEmailMessageLink($emailMessage, $this->client);
// Check the user has been redirected on login page
$this->assertResponseRedirects(
......@@ -72,30 +76,28 @@ class RegistrationControllerTest extends WebTestCase
);
// Check the redirection is successful
$client->followRedirect();
$this->client->followRedirect();
$this->assertResponseIsSuccessful('Once the user has validated his email, he should be on the login page');
}
public function testAnAuthenticatedUserShouldNotBeAbleToAccessTheRegisterPage(): void
{
$client = static::createClient();
$userRepository = static::getContainer()->get(UserRepository::class);
$testUser = $userRepository->findOneByEmail('defaultUser@localhost.com');
$client->loginUser($testUser);
$client->request('GET', '/register');
$this->client->loginUser($testUser);
$this->client->request('GET', '/register');
$this->assertResponseRedirects('/my_capsules', 302);
}
public function testAnAuthenticatedUserShouldNotBeAbleToAccessTheLoginPage(): void
{
$client = static::createClient();
$userRepository = static::getContainer()->get(UserRepository::class);
$testUser = $userRepository->findOneByEmail('defaultUser@localhost.com');
$client->loginUser($testUser);
$client->request('GET', '/login');
$this->client->loginUser($testUser);
$this->client->request('GET', '/login');
$this->assertResponseRedirects('/my_capsules', 302);
}
......@@ -103,9 +105,8 @@ class RegistrationControllerTest extends WebTestCase
public function testSubmittingTheRegisterFormWithAnInvalidEmailAddressShouldDisplayAFeedbackError(): void
{
$userEmail = 'invalidEmailAddress';
$client = static::createClient();
$crawler = $this->registerUser($userEmail, $client);
$crawler = $this->registerUser($userEmail, $this->client);
$html = $crawler->outerHtml();
$this->assertStringContainsString("is not a valid email", $html);
......
......@@ -16,6 +16,7 @@ class ResetPasswordControllerTest extends WebTestCase
protected function setUp(): void
{
self::ensureKernelShutdown();
$this->client = static::createClient();
$this->client->enableProfiler();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment