diff --git a/composer.json b/composer.json index 46b6ee85370f86dda6906c051fdd94c35772227a..cc5076ba54e16c1708714fa0ce79dd936d665dcc 100644 --- a/composer.json +++ b/composer.json @@ -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, diff --git a/src/DataFixtures/UserFixtures.php b/src/DataFixtures/UserFixtures.php index 694186b4182ea566b46304f816cd00a8901d7474..c7605f1b7a7f1ac2b46d2df2a068093dd3bf5904 100644 --- a/src/DataFixtures/UserFixtures.php +++ b/src/DataFixtures/UserFixtures.php @@ -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); diff --git a/src/Repository/CapsuleRepository.php b/src/Repository/CapsuleRepository.php index 3b09b96672a810b72fbebeed1326f7dece47819d..fa581fdea4fa234740ad8bdd9e5fe2bee2fa5900 100644 --- a/src/Repository/CapsuleRepository.php +++ b/src/Repository/CapsuleRepository.php @@ -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> */ diff --git a/tests/functional/LoginTest.php b/tests/functional/LoginTest.php index b6e06d897505e30998a7cd57de9bdc95c2889970..c9ff2767141491d462794cdd8969aaeb074e6b89 100644 --- a/tests/functional/LoginTest.php +++ b/tests/functional/LoginTest.php @@ -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(); diff --git a/tests/functional/ProjectControllerTest.php b/tests/functional/ProjectControllerTest.php index 403db19a64290d8b1ccfca8a60c9d328dad28c18..fcf8dc5c1e3d50f47bfdfe1a970045be4bab2d0a 100644 --- a/tests/functional/ProjectControllerTest.php +++ b/tests/functional/ProjectControllerTest.php @@ -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); -// } -} \ No newline at end of file +} diff --git a/tests/functional/RegistrationControllerTest.php b/tests/functional/RegistrationControllerTest.php index a3cf7cc1edb2d6da54e3626ed2ef891fdbdf9367..7adec0ee52443141e672c1236a6b26cf103aef90 100644 --- a/tests/functional/RegistrationControllerTest.php +++ b/tests/functional/RegistrationControllerTest.php @@ -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); diff --git a/tests/functional/ResetPasswordControllerTest.php b/tests/functional/ResetPasswordControllerTest.php index d1970492632f5552c4c7d8bb889c4dc2d7ef51a3..f95b1489ab61ec3700c14426cc8da5076aa60991 100644 --- a/tests/functional/ResetPasswordControllerTest.php +++ b/tests/functional/ResetPasswordControllerTest.php @@ -16,6 +16,7 @@ class ResetPasswordControllerTest extends WebTestCase protected function setUp(): void { + self::ensureKernelShutdown(); $this->client = static::createClient(); $this->client->enableProfiler(); }