Tests: Move test files inside fixtures + add tests

pull/3984/head
Julio 4 years ago
parent 258428e1d0
commit efb2f5b4b5
  1. 15
      tests/ChamiloTestTrait.php
  2. 85
      tests/CoreBundle/Controller/AssetControllerTest.php
  3. 83
      tests/CoreBundle/Repository/AssetRepositoryTest.php
  4. 2
      tests/behat/features/toolExercise.feature
  5. 0
      tests/fixtures/exercise.xls
  6. BIN
      tests/fixtures/logo.png
  7. BIN
      tests/fixtures/logo.zip

@ -121,7 +121,20 @@ trait ChamiloTestTrait
public function getUploadedFile(): UploadedFile
{
$path = $this->getContainer()->get('kernel')->getProjectDir();
$filePath = $path.'/public/img/logo.png';
$filePath = $path.'/tests/fixtures/logo.png';
$fileName = basename($filePath);
return new UploadedFile(
$filePath,
$fileName,
'image/png',
);
}
public function getUploadedZipFile(): UploadedFile
{
$path = $this->getContainer()->get('kernel')->getProjectDir();
$filePath = $path.'/tests/fixtures/logo.zip';
$fileName = basename($filePath);
return new UploadedFile(

@ -11,96 +11,11 @@ use Chamilo\CoreBundle\Repository\AssetRepository;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
use ZipArchive;
class AssetControllerTest extends WebTestCase
{
use ChamiloTestTrait;
public function testCreateWatermark(): void
{
$client = static::createClient();
$file = $this->getUploadedFile();
$assetRepo = self::getContainer()->get(AssetRepository::class);
// Create asset.
$asset = (new Asset())
->setTitle('test')
->setCategory(Asset::WATERMARK)
->setCrop('100,100,100,100')
->setFile($file)
;
$this->assertHasNoEntityViolations($asset);
$assetRepo->update($asset);
$folder = $assetRepo->getFolder($asset);
$this->assertSame('/watermark/'.$asset->getFile()->getFilename().'/', $folder);
$url = $assetRepo->getAssetUrl($asset);
$client->request('GET', $url);
$this->assertResponseIsSuccessful();
$asset = (new Asset())
->setTitle('test2')
->setCategory(Asset::WATERMARK)
->setCrop('100,100,100,100')
;
$assetRepo->createFromString($asset, 'text/html', 'hello');
$this->assertHasNoEntityViolations($asset);
$asset = (new Asset())
->setTitle('test3')
->setCategory(Asset::WATERMARK)
;
$file = [
'tmp_name' => $this->getUploadedFile()->getRealPath(),
'name' => $this->getUploadedFile()->getFilename(),
'type' => $this->getUploadedFile()->getMimeType(),
'size' => $this->getUploadedFile()->getSize(),
'error' => UPLOAD_ERR_OK,
];
$assetRepo->createFromRequest($asset, $file);
$this->assertHasNoEntityViolations($asset);
}
public function testCreateScormAsset(): void
{
$client = static::createClient();
$file = $this->getUploadedFile();
$assetRepo = self::getContainer()->get(AssetRepository::class);
$this->assertSame(0, $assetRepo->count([]));
/*$zipPath = '/tmp/example.zip';
$zip = new ZipArchive();
$zip->open($zipPath, ZipArchive::CREATE);
$zip->addFile($file->getRealPath());
$zip->close();
$asset = (new Asset())
->setTitle('test')
->setCategory(Asset::SCORM)
;
$file = [
'tmp_name' => $zipPath,
'name' => 'example.zip',
'type' => 'zip',
'size' => 100,
'error' => UPLOAD_ERR_OK,
];
$assetRepo->createFromRequest($asset, $file);
$this->assertHasNoEntityViolations($asset);*/
//$assetRepo->update($asset);
//$url = $assetRepo->getAssetUrl($asset);
//$client->request('GET', $url);
//$this->assertResponseIsSuccessful();
}
public function testShowFile(): void
{
$client = static::createClient();

@ -10,7 +10,6 @@ use Chamilo\CoreBundle\Entity\Asset;
use Chamilo\CoreBundle\Repository\AssetRepository;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Component\HttpFoundation\Response;
class AssetRepositoryTest extends AbstractApiTest
{
@ -38,41 +37,84 @@ class AssetRepositoryTest extends AbstractApiTest
$this->assertSame(1, $assetRepo->count([]));
}
public function testAssetWatermark(): void
public function testCreateWatermark(): void
{
self::bootKernel();
$client = static::createClient();
$file = $this->getUploadedFile();
$em = $this->getEntityManager();
/** @var AssetRepository $assetRepo */
$assetRepo = self::getContainer()->get(AssetRepository::class);
$file = $this->getUploadedFile();
// Create asset.
$asset = (new Asset())
->setTitle('test')
->setCategory(Asset::WATERMARK)
->setCrop('100,100,100,100')
->setFile($file)
;
$em->persist($asset);
$em->flush();
$this->assertHasNoEntityViolations($asset);
$assetRepo->update($asset);
$folder = $assetRepo->getFolder($asset);
$this->assertSame('/watermark/'.$asset->getFile()->getFilename().'/', $folder);
$url = $assetRepo->getAssetUrl($asset);
$client->request('GET', $url);
$this->assertResponseIsSuccessful();
// Check Asset URL.
$this->assertNotEmpty($url);
$asset = (new Asset())
->setTitle('test2')
->setCategory(Asset::WATERMARK)
->setCrop('100,100,100,100')
;
$assetRepo->createFromString($asset, 'text/html', 'hello');
$this->assertHasNoEntityViolations($asset);
$asset = (new Asset())
->setTitle('test3')
->setCategory(Asset::WATERMARK)
;
$file = [
'tmp_name' => $this->getUploadedFile()->getRealPath(),
'name' => $this->getUploadedFile()->getFilename(),
'type' => $this->getUploadedFile()->getMimeType(),
'size' => $this->getUploadedFile()->getSize(),
'error' => UPLOAD_ERR_OK,
];
$assetRepo->createFromRequest($asset, $file);
$this->assertHasNoEntityViolations($asset);
}
public function testUnZipFile(): void
{
$client = static::createClient();
$client->request('GET', $url);
$assetRepo = self::getContainer()->get(AssetRepository::class);
$this->assertSame(0, $assetRepo->count([]));
$file = $this->getUploadedZipFile();
$asset = (new Asset())
->setTitle('test')
->setCategory(Asset::SCORM)
->setFile($file)
->setCompressed(true)
;
$assetRepo->update($asset);
$this->assertHasNoEntityViolations($asset);
$assetRepo->unZipFile($asset);
$url = $assetRepo->getAssetUrl($asset);
$client->request('GET', $url.'/logo.png');
$this->assertResponseIsSuccessful();
}
public function testAssetDelete(): void
public function testDelete(): void
{
self::bootKernel();
$em = $this->getEntityManager();
/** @var AssetRepository $assetRepo */
$assetRepo = self::getContainer()->get(AssetRepository::class);
$file = $this->getUploadedFile();
@ -83,20 +125,11 @@ class AssetRepositoryTest extends AbstractApiTest
->setCategory(Asset::WATERMARK)
->setFile($file)
;
$em->persist($asset);
$em->flush();
$url = $assetRepo->getAssetUrl($asset);
$this->assertNotEmpty($url);
$client = static::createClient();
$client->request('GET', $url);
$this->assertResponseIsSuccessful();
$assetRepo->update($asset);
$asset = $assetRepo->find($asset->getId());
$assetRepo->delete($asset);
$response = $client->request('GET', $url);
$this->assertSame(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode());
$this->assertSame(0, $assetRepo->count([]));
}
}

@ -257,7 +257,7 @@ Feature: Exercise tool
# Scenario: Import exercise to test questions categories
# Given I am on "/main/exercise/upload_exercise.php?cid=1"
# And I should see "Import quiz from Excel"
# And I attach the file "/tests/behat/uploadable_files/exercise.xls" to "user_upload_quiz"
# And I attach the file "/tests/fixtures/exercise.xls" to "user_upload_quiz"
# When I press "Upload"
# And wait for the page to be loaded
# Then I should see "Exercise for Behat test"

Binary file not shown.

After

Width:  |  Height:  |  Size: 969 B

Binary file not shown.
Loading…
Cancel
Save