Tests: Add IllustrationRepositoryTest.php, refactor upload file code

pull/3924/head
Julio Montoya 5 years ago
parent 6190860d71
commit 97be25ba58
  1. 6
      src/CoreBundle/Repository/Node/IllustrationRepository.php
  2. 7
      tests/ChamiloTestTrait.php
  3. 46
      tests/CoreBundle/Repository/Node/IllustrationRepositoryTest.php
  4. 15
      tests/CoreBundle/Repository/Node/PersonalFileRepositoryTest.php
  5. 31
      tests/CourseBundle/Repository/CDocumentRepositoryTest.php

@ -82,8 +82,10 @@ final class IllustrationRepository extends ResourceRepository implements GridInt
$em = $this->getEntityManager();
if (null === $illustrationNode) {
$illustration = new Illustration();
$this->addResourceNode($illustration, $creator, $resource);
$illustration = (new Illustration())
->setCreator($creator)
->setParent($resource)
;
$em->persist($illustration);
} else {
$illustration = $this->findOneBy([

@ -110,7 +110,7 @@ trait ChamiloTestTrait
$this->assertEquals(0, $errors->count(), implode(', ', $message));
}
public function getUploadableFile(): UploadedFile
public function getUploadedFile(): UploadedFile
{
$path = $this->getContainer()->get('kernel')->getProjectDir();
$filePath = $path.'/public/img/logo.png';
@ -127,9 +127,8 @@ trait ChamiloTestTrait
{
/** @var ValidatorInterface $validator */
$validator = static::$kernel->getContainer()->get('validator');
/** @var ConstraintViolationList $errors */
$errors = $validator->validate($entity);
return $errors;
/** @var ConstraintViolationList $errors */
return $validator->validate($entity);
}
}

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CoreBundle\Repository\Node;
use Chamilo\CoreBundle\Entity\ResourceFile;
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @covers \IllustrationRepository
*/
class IllustrationRepositoryTest extends KernelTestCase
{
use ChamiloTestTrait;
public function testCreateCourseIllustration(): void
{
self::bootKernel();
/** @var IllustrationRepository $repo */
$repo = self::getContainer()->get(IllustrationRepository::class);
$course = $this->createCourse('course');
$file = $repo->addIllustration($course, $this->getUser('admin'), $this->getUploadedFile());
$this->assertHasNoEntityViolations($file);
$this->assertNotNull($file);
}
public function testCreateUserIllustration(): void
{
self::bootKernel();
/** @var IllustrationRepository $repo */
$repo = self::getContainer()->get(IllustrationRepository::class);
$user = $this->createUser('test');
$file = $repo->addIllustration($user, $user, $this->getUploadedFile());
$this->assertHasNoEntityViolations($file);
$this->assertNotNull($file);
}
}

@ -8,7 +8,6 @@ namespace Chamilo\Tests\CoreBundle\Repository\Node;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @covers \PersonalFileRepository
@ -29,16 +28,12 @@ class PersonalFileRepositoryTest extends AbstractApiTest
'password' => $password,
]);
$path = $this->getContainer()->get('kernel')->getProjectDir();
$filePath = $path.'/public/img/logo.png';
$fileName = basename($filePath);
//$path = $this->getContainer()->get('kernel')->getProjectDir();
//$filePath = $path.'/public/img/logo.png';
$resourceNodeId = $user->getResourceNode()->getId();
$file = new UploadedFile(
$filePath,
$fileName,
'image/png',
);
$file = $this->getUploadedFile();
$fileName = $file->getFilename();
$response = $this->createClientWithCredentials($token)->request(
'POST',
@ -54,7 +49,7 @@ class PersonalFileRepositoryTest extends AbstractApiTest
],
'json' => [
'filetype' => 'file',
'size' => filesize($filePath),
'size' => $file->getSize(),
'parentResourceNodeId' => $resourceNodeId,
],
]

@ -9,7 +9,6 @@ namespace Chamilo\Tests\CourseBundle\Repository;
use Chamilo\CourseBundle\Entity\CDocument;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @covers \Chamilo\CourseBundle\Repository\CDocumentRepository
@ -83,16 +82,7 @@ class CDocumentRepositoryTest extends AbstractApiTest
'visibility' => 2,
];
$path = $this->getContainer()->get('kernel')->getProjectDir();
$filePath = $path.'/public/img/logo.png';
$fileName = basename($filePath);
$file = new UploadedFile(
$filePath,
$fileName,
'image/png',
);
$file = $this->getUploadedFile();
$token = $this->getUserToken([]);
$this->createClientWithCredentials($token)->request(
@ -109,7 +99,7 @@ class CDocumentRepositoryTest extends AbstractApiTest
],
'json' => [
'filetype' => 'file',
'size' => filesize($filePath),
'size' => $file->getSize(),
'parentResourceNodeId' => $course->getResourceNode()->getId(),
'resourceLinkList' => json_encode($resourceLinkList),
],
@ -122,7 +112,7 @@ class CDocumentRepositoryTest extends AbstractApiTest
$this->assertJsonContains([
'@context' => '/api/contexts/Documents',
'@type' => 'Documents',
'title' => $fileName,
'title' => $file->getFilename(),
'filetype' => 'file',
]);
}
@ -157,16 +147,7 @@ class CDocumentRepositoryTest extends AbstractApiTest
$data = json_decode($response->getContent());
$resourceNodeId = $data->resourceNode->id;
$path = $this->getContainer()->get('kernel')->getProjectDir();
$filePath = $path.'/public/img/logo.png';
$fileName = basename($filePath);
$file = new UploadedFile(
$filePath,
$fileName,
'image/png',
);
$file = $this->getUploadedFile();
$token = $this->getUserToken([]);
$this->createClientWithCredentials($token)->request(
@ -183,7 +164,7 @@ class CDocumentRepositoryTest extends AbstractApiTest
],
'json' => [
'filetype' => 'file',
'size' => filesize($filePath),
'size' => $file->getSize(),
'parentResourceNodeId' => $resourceNodeId,
'resourceLinkList' => json_encode($resourceLinkList),
],
@ -196,7 +177,7 @@ class CDocumentRepositoryTest extends AbstractApiTest
$this->assertJsonContains([
'@context' => '/api/contexts/Documents',
'@type' => 'Documents',
'title' => $fileName,
'title' => $file->getFilename(),
'filetype' => 'file',
]);

Loading…
Cancel
Save