From 3b44f21aa8449c7656185d230cf65077b1fc02bb Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Tue, 8 Jun 2021 11:20:07 +0200 Subject: [PATCH] Documents: Add upload file test --- assets/vue/views/documents/List.vue | 1 + .../Controller/Api/BaseResourceFileAction.php | 27 ++-- .../Api/CreateDocumentFileAction.php | 11 +- tests/AbstractApiTest.php | 3 +- .../Repository/CDocumentRepositoryTest.php | 125 ++++++++++++------ 5 files changed, 106 insertions(+), 61 deletions(-) diff --git a/assets/vue/views/documents/List.vue b/assets/vue/views/documents/List.vue index 9fabfc571c..ebad8f1717 100644 --- a/assets/vue/views/documents/List.vue +++ b/assets/vue/views/documents/List.vue @@ -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, diff --git a/src/CoreBundle/Controller/Api/BaseResourceFileAction.php b/src/CoreBundle/Controller/Api/BaseResourceFileAction.php index 5f1f92989e..fbd51d576d 100644 --- a/src/CoreBundle/Controller/Api/BaseResourceFileAction.php +++ b/src/CoreBundle/Controller/Api/BaseResourceFileAction.php @@ -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) diff --git a/src/CoreBundle/Controller/Api/CreateDocumentFileAction.php b/src/CoreBundle/Controller/Api/CreateDocumentFileAction.php index da3f2123e8..7daf0d0034 100644 --- a/src/CoreBundle/Controller/Api/CreateDocumentFileAction.php +++ b/src/CoreBundle/Controller/Api/CreateDocumentFileAction.php @@ -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; } } diff --git a/tests/AbstractApiTest.php b/tests/AbstractApiTest.php index b884bbb86d..a408ef06ca 100644 --- a/tests/AbstractApiTest.php +++ b/tests/AbstractApiTest.php @@ -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]]); } diff --git a/tests/CourseBundle/Repository/CDocumentRepositoryTest.php b/tests/CourseBundle/Repository/CDocumentRepositoryTest.php index 5274c3d878..4f7f45f34e 100644 --- a/tests/CourseBundle/Repository/CDocumentRepositoryTest.php +++ b/tests/CourseBundle/Repository/CDocumentRepositoryTest.php @@ -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', ]); } }