Documents: Add upload file test

pull/3904/head
Julio Montoya 4 years ago
parent c66daf3c2e
commit 3b44f21aa8
  1. 1
      assets/vue/views/documents/List.vue
  2. 27
      src/CoreBundle/Controller/Api/BaseResourceFileAction.php
  3. 11
      src/CoreBundle/Controller/Api/CreateDocumentFileAction.php
  4. 3
      tests/AbstractApiTest.php
  5. 125
      tests/CourseBundle/Repository/CDocumentRepositoryTest.php

@ -469,6 +469,7 @@ export default {
if (this.item.id) {
} else {
//this.products.push(this.product);
this.item.filetype = 'folder';
this.item.parentResourceNodeId = this.$route.params.node;
this.item.resourceLinkList = JSON.stringify([{
gid: this.$route.query.gid,

@ -17,24 +17,19 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class BaseResourceFileAction
{
protected function handleCreateRequest(AbstractResource $resource, Request $request): void
protected function handleCreateRequest(AbstractResource $resource, Request $request): array
{
//error_log('handleCreateRequest');
$contentData = $request->getContent();
if (!empty($contentData)) {
$contentData = json_decode($contentData, true);
var_dump($contentData);
$title = $contentData['title'] ?? '';
$comment = $contentData['comment'] ?? '';
$nodeId = $contentData['parentResourceNodeId'] ?? 0;
$parentResourceNodeId = $contentData['parentResourceNodeId'] ?? 0;
$fileType = $contentData['filetype'] ?? '';
} else {
$title = $request->get('title');
$comment = $request->get('comment');
$nodeId = (int) $request->get('parentResourceNodeId');
}
$fileType = 'folder';
if ($request->request->has('filetype')) {
$parentResourceNodeId = (int) $request->get('parentResourceNodeId');
$fileType = $request->get('filetype');
}
@ -42,13 +37,12 @@ class BaseResourceFileAction
throw new Exception('filetype needed: folder or file');
}
if (0 === $nodeId) {
if (0 === $parentResourceNodeId) {
throw new Exception('parentResourceNodeId int value needed');
}
$resource->setParentResourceNode($nodeId);
$resource->setParentResourceNode($parentResourceNodeId);
//error_log("fileType: $fileType");
switch ($fileType) {
case 'file':
$content = '';
@ -80,7 +74,7 @@ class BaseResourceFileAction
}
if (!$fileParsed) {
throw new InvalidArgumentException('filetype was set to "file" but not upload found');
throw new InvalidArgumentException('filetype was set to "file" but not upload file found');
}
break;
@ -93,6 +87,13 @@ class BaseResourceFileAction
}
$resource->setResourceName($title);
return [
'title' => $title,
'comment' => $comment,
'parentResourceNodeId' => $parentResourceNodeId,
'filetype' => $fileType,
];
}
protected function handleUpdateRequest(AbstractResource $resource, $repo, Request $request)

@ -17,11 +17,12 @@ class CreateDocumentFileAction extends BaseResourceFileAction
error_log('CreateDocumentFileAction __invoke');
$document = new CDocument();
$this->handleCreateRequest($document, $request);
if ($request->request->has('filetype')) {
$document->setFiletype($request->get('filetype'));
}
$result = $this->handleCreateRequest($document, $request);
$document->setFiletype($result['filetype']);
$document->setComment($result['comment']);
// Specific for the CDocument because it needs to be registered in a course.
if ($request->request->has('resourceLinkList')) {
$links = $request->get('resourceLinkList');
$links = false === strpos($links, '[') ? json_decode('['.$links.']', true) : json_decode($links, true);
@ -33,8 +34,6 @@ class CreateDocumentFileAction extends BaseResourceFileAction
$document->setResourceLinkArray($links);
}
//$document->setComment($comment);
return $document;
}
}

@ -35,7 +35,6 @@ abstract class AbstractApiTest extends ApiTestCase
$this->assertResponseIsSuccessful();
$data = json_decode($response->getContent());
//$this->token = $data->access_token;
$this->assertEquals('admin', $data->username);
@ -44,7 +43,7 @@ abstract class AbstractApiTest extends ApiTestCase
protected function createClientWithCredentials($token = null): Client
{
$token = $token ?: $this->getToken();
$token = $token ?: $this->getUserToken();
return static::createClient([], ['headers' => ['authorization' => 'Bearer '.$token]]);
}

@ -6,8 +6,6 @@ declare(strict_types=1);
namespace Chamilo\Tests\CourseBundle\Repository;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
use Chamilo\CourseBundle\Entity\CDocument;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
@ -20,7 +18,7 @@ class CDocumentRepositoryTest extends AbstractApiTest
{
use ChamiloTestTrait;
public function testGetDocuments(): void
public function testGetDocumentsAsAdmin(): void
{
$token = $this->getUserToken([]);
$response = $this->createClientWithCredentials($token)->request('GET', '/api/documents');
@ -35,10 +33,6 @@ class CDocumentRepositoryTest extends AbstractApiTest
'@id' => '/api/documents',
'@type' => 'hydra:Collection',
'hydra:totalItems' => 0,
/*'hydra:view' => [
'@id' => '/api/documents?page=1',
'@type' => 'hydra:PartialCollectionView',
],*/
]);
$this->assertCount(0, $response->toArray()['hydra:member']);
@ -47,18 +41,7 @@ class CDocumentRepositoryTest extends AbstractApiTest
public function testCreateFolder(): void
{
$courseRepo = self::getContainer()->get(CourseRepository::class);
$admin = $this->getUser('admin');
$accessUrl = $this->getAccessUrl();
$course = (new Course())
->setTitle('Test course')
->setCode('test_course')
->addAccessUrl($accessUrl)
->setCreator($admin)
;
$courseRepo->create($course);
$course = $this->createCourse('Test');
// Create folder.
$resourceLinkList = [
@ -66,13 +49,15 @@ class CDocumentRepositoryTest extends AbstractApiTest
'visibility' => 2,
];
$folderName = 'folder1';
$token = $this->getUserToken([]);
$this->createClientWithCredentials($token)->request(
'POST',
'/api/documents',
[
'json' => [
'title' => 'folder1',
'title' => $folderName,
'filetype' => 'folder',
'parentResourceNodeId' => $course->getResourceNode()->getId(),
'resourceLinkList' => json_encode($resourceLinkList),
],
@ -85,39 +70,101 @@ class CDocumentRepositoryTest extends AbstractApiTest
$this->assertJsonContains([
'@context' => '/api/contexts/Documents',
'@type' => 'Documents',
'title' => 'folder1',
'title' => $folderName,
]);
}
public function testCreateFile(): void
public function testUploadFile(): void
{
$courseRepo = self::getContainer()->get(CourseRepository::class);
$course = $this->createCourse('Test');
$resourceLinkList = [
'cid' => $course->getId(),
'visibility' => 2,
];
$path = $this->getContainer()->get('kernel')->getProjectDir();
$filePath = $path.'/public/img/logo.png';
$fileName = basename($filePath);
$file = new UploadedFile(
$filePath,
$fileName,
'image/png',
);
$token = $this->getUserToken([]);
$this->createClientWithCredentials($token)->request(
'POST',
'/api/documents',
[
'headers' => [
'Content-Type' => 'multipart/form-data',
],
'extra' => [
'files' => [
'uploadFile' => $file,
],
],
'json' => [
'filetype' => 'file',
'size' => filesize($filePath),
'parentResourceNodeId' => $course->getResourceNode()->getId(),
'resourceLinkList' => json_encode($resourceLinkList),
],
]
);
$admin = $this->getUser('admin');
// Get access url.
$accessUrl = $this->getAccessUrl();
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(201);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/api/contexts/Documents',
'@type' => 'Documents',
'title' => $fileName,
'filetype' => 'file',
]);
}
$course = (new Course())
->setTitle('Test course')
->setCode('test_course')
->addAccessUrl($accessUrl)
->setCreator($admin)
;
$courseRepo->create($course);
public function testUploadFileInSideASubFolder(): void
{
$course = $this->createCourse('Test');
// Create file.
// Create folder.
$resourceLinkList = [
'cid' => $course->getId(),
'visibility' => 2,
];
$token = $this->getUserToken([]);
// Creates a folder.
$folderName = 'test';
$response = $this->createClientWithCredentials($token)->request(
'POST',
'/api/documents',
[
'json' => [
'title' => $folderName,
'filetype' => 'folder',
'parentResourceNodeId' => $course->getResourceNode()->getId(),
'resourceLinkList' => json_encode($resourceLinkList),
],
]
);
$this->assertResponseIsSuccessful();
$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,
'logo.png',
$fileName,
'image/png',
);
@ -135,13 +182,10 @@ class CDocumentRepositoryTest extends AbstractApiTest
],
],
'json' => [
'title' => 'my image',
'type' => 'image/jpeg',
'filetype' => 'file',
'size' => filesize($filePath),
'parentResourceNodeId' => $course->getResourceNode()->getId(),
'parentResourceNodeId' => $resourceNodeId,
'resourceLinkList' => json_encode($resourceLinkList),
//'uploadFile' => new F
],
]
);
@ -152,7 +196,8 @@ class CDocumentRepositoryTest extends AbstractApiTest
$this->assertJsonContains([
'@context' => '/api/contexts/Documents',
'@type' => 'Documents',
'title' => 'my image',
'title' => $fileName,
'filetype' => 'file',
]);
}
}

Loading…
Cancel
Save