Select Git revision
CapsuleEditorController.php
ProjectControllerTest.php 10.50 KiB
<?php
namespace App\Tests\functional;
use App\Entity\Capsule;
use App\Entity\User;
use Doctrine\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DomCrawler\Form;
use Symfony\Component\Filesystem\Filesystem;
class ProjectControllerTest extends WebTestCase
{
private KernelBrowser $client;
private ObjectManager $object_manager;
private Form $form;
private const TEST_DIR_PATH = __DIR__ . '/../../legacy/';
private const CAPSULE_NAME = 'TestCapsuleName';
private const TEST_FILES_DIRECTORY = "../tests/files_for_tests/";
protected function setUp(): void
{
chdir(self::TEST_DIR_PATH);
self::ensureKernelShutdown();
$this->client = static::createClient();
$this->object_manager = $this->client->getContainer()
->get('doctrine')
->getManager();
$verified_user = $this->object_manager
->getRepository(User::class)
->findOneBy(['email' => 'defaultUser@localhost.com'])
;
if (! $verified_user instanceof User) {
throw new \Exception("User does not exist.");
}
$this->client->loginUser($verified_user);
}
protected function tearDown(): void
{
parent::tearDown();
$capsule_repository = $this->object_manager->getRepository(Capsule::class);
$capsule = $capsule_repository->findOneBy(['name' => self::CAPSULE_NAME]);
if (! $capsule instanceof Capsule) {
throw new \Exception("Capsule does not exist.");
}
$this->deleteCapsuleDirectoryAndInDatabase($capsule);
}
/** @phpstan-ignore-next-line */
private function getDOMDocument(string $capsule_directory): \DOMDocument
{
$dom_xml = new \DOMDocument();
$dom_xml->preserveWhiteSpace = false;
$dom_xml->load($this->getXmlFilePath($capsule_directory));
return $dom_xml;
}
private function getCapsuleDirPath(string $capsule_directory): string
{
return self::TEST_DIR_PATH . $capsule_directory;
}
private function getXmlFilePath(string $capsule_directory): string
{
return $this->getCapsuleDirPath($capsule_directory) . '/file/project.xml';
}
private function getPasswordFilePath(string $capsule_directory): string
{
return $this->getCapsuleDirPath($capsule_directory) . '/file/projectPassword.txt';
}
public function testProjectDirectoryWithCorrespondingXMLFileIsCreatedWhenCapsuleCreationIsSuccessful(): void
{
$crawler = $this->client->request('GET', '/create');
$this->assertResponseIsSuccessful();
$this->client->enableProfiler();
$submit_button = $crawler->selectButton('Create a capsule');
$this->form = $submit_button->form();
$video_url = "https://TestUrl";
$this->form['create_capsule_form[name]'] = self::CAPSULE_NAME;
$this->form['create_capsule_form[video_url]'] = $video_url;
$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->assertResponseIsSuccessful('/my_capsules');
$capsule_repository = $this->object_manager->getRepository(Capsule::class);
$capsule_in_db = $capsule_repository->findOneBy(['name' => self::CAPSULE_NAME]);
if (! $capsule_in_db instanceof Capsule) {
throw new \Exception("Capsule does not exist.");
}
$capsule_name_in_db = $capsule_in_db->getName();
$capsule_directory = $capsule_in_db->getLinkPath();
$this->assertDirectoryExists($capsule_directory);
$this->assertDirectoryIsReadable($capsule_directory);
$dom_xml = self::getDomDocument($capsule_directory);
$video_node = $dom_xml->getElementsByTagName('video')->item(0);
if ($video_node === null) {
throw new \Exception("Video node could not be found in XML project file");
}
$video_url_in_xml_file = $video_node->getAttribute('url');
$this->assertEquals($video_url, $video_url_in_xml_file);
$this->assertSame(self::CAPSULE_NAME, $capsule_name_in_db);
}
public function testProjectDirectoryWithCorrespondingPasswordFileIsCreatedWhenCapsuleCreationIsSuccessful(): void
{
$this->createCapsule();
$this->assertResponseRedirects(
'/my_capsules',
302,
'Once the capsule is created, the user should be redirected to its capsules lists'
);
$this->client->followRedirect();
$this->assertResponseIsSuccessful('/my_capsules');
$capsule_repository = $this->object_manager->getRepository(Capsule::class);
$capsule_in_db = $capsule_repository->findOneBy(['name' => self::CAPSULE_NAME]);
if (! $capsule_in_db instanceof Capsule) {
throw new \Exception("Capsule does not exist.");
}
$capsule_directory = $capsule_in_db->getLinkPath();
$this->assertDirectoryExists($capsule_directory);
$this->assertDirectoryIsReadable($capsule_directory);
$password_file_path = $this->getPasswordFilePath($capsule_directory);
$password = file_get_contents($password_file_path, true);
$this->assertTrue(false !== $password, "The projectPassword.txt should be readable");
$this->assertSame(
$capsule_in_db->getPassword(),
$password,
"The password should be saved in projectPassword.txt file"
);
}
public function testDuplicatedDirectoryDuplicatedFilesFromParentWhenCapsuleDuplicationIsSuccessful(): void
{
$duplicated_capsule_name = 'duplicated ' . self::CAPSULE_NAME;
$this->createCapsule();
$capsule_repository = $this->object_manager->getRepository(Capsule::class);
$parent_capsule = $capsule_repository->findOneBy(['name' => self::CAPSULE_NAME]);
if (! $parent_capsule instanceof Capsule) {
throw new \Exception("Capsule does not exist.");
}
$parent_capsule_directory = $parent_capsule->getLinkPath();
$crawler = $this->client->request('GET', '/capsule/duplicate/' . $parent_capsule->getId());
$this->assertResponseIsSuccessful();
$this->client->enableProfiler();
$submit_button = $crawler->selectButton('Validate');
$this->form = $submit_button->form();
$this->form['duplicate_capsule_form[name]'] = $duplicated_capsule_name;
$this->client->submit($this->form);
$this->assertResponseRedirects(
'/my_capsules',
302,
'Once the capsule is duplicated, the user should be redirected to its capsules lists'
);
$this->client->followRedirect();
$this->assertResponseIsSuccessful('/my_capsules');
$capsule_repository = $this->object_manager->getRepository(Capsule::class);
$duplicated_capsule_in_db = $capsule_repository->findOneBy(['name' => $duplicated_capsule_name]);
if (! $duplicated_capsule_in_db instanceof Capsule) {
throw new \Exception("Capsule does not exist.");
}
$duplicated_capsule_directory = $duplicated_capsule_in_db->getLinkPath();
$this->assertDirectoryExists($duplicated_capsule_directory);
$this->assertDirectoryIsReadable($duplicated_capsule_directory);
$this->assertXmlFileEqualsXmlFile(
$this->getXmlFilePath($duplicated_capsule_directory),
$this->getXmlFilePath($duplicated_capsule_directory)
);
$this->assertAllFilesOfFileDirectoryAreSameExceptPasswordOne(
$parent_capsule_directory,
$duplicated_capsule_directory
);
$password_file_path = $this->getPasswordFilePath($duplicated_capsule_directory);
$password = file_get_contents($password_file_path, true);
$this->assertTrue(false !== $password, "The projectPassword.txt should be readable");
$this->assertSame(
$duplicated_capsule_in_db->getPassword(),
$password,
"The password should be saved in projectPassword.txt file"
);
$this->deleteCapsuleDirectoryAndInDatabase($duplicated_capsule_in_db);
}
private function assertAllFilesOfFileDirectoryAreSameExceptPasswordOne(
string $parent_capsule_directory,
string $duplicated_capsule_directory
): void {
$parent_capsule_files = scandir($parent_capsule_directory . "/file/");
if (! is_array($parent_capsule_files)) {
return;
}
foreach ($parent_capsule_files as $key => $file) {
if ($file === "projectPassword.txt" || is_dir($file)) {
return;
}
$this->assertFileExists($duplicated_capsule_directory . "/file/" . $file);
$this->assertFileEquals($file, $duplicated_capsule_directory . "/file/" . $file);
}
}
private function createCapsule(): void
{
$crawler = $this->client->request('GET', '/create');
$this->assertResponseIsSuccessful();
$this->client->enableProfiler();
$submit_button = $crawler->selectButton('Create a capsule');
$this->form = $submit_button->form();
$video_url = "https://TestUrl";
$this->form['create_capsule_form[name]'] = self::CAPSULE_NAME;
$this->form['create_capsule_form[video_url]'] = $video_url;
$this->client->submit($this->form);
$capsule_repository = $this->object_manager->getRepository(Capsule::class);
$capsule = $capsule_repository->findOneBy(['name' => self::CAPSULE_NAME]);
if (! $capsule instanceof Capsule) {
throw new \Exception("Capsule does not exist.");
}
$capsule_directory_path = $capsule->getLinkPath();
$file_system = new Filesystem();
$file_system->mirror(
self::TEST_FILES_DIRECTORY,
$capsule_directory_path . '/file/',
null,
['override' => true]
);
}
private function deleteCapsuleDirectoryAndInDatabase(Capsule $capsule): void
{
$file_system = new Filesystem();
$capsule_directory_path = $capsule->getLinkPath();
$file_system->remove(self::TEST_DIR_PATH . $capsule_directory_path);
$capsule_to_delete = $this->object_manager->merge($capsule);
$this->object_manager->remove($capsule_to_delete);
$this->object_manager->flush();
}
}