From 6868ea65f441ebd187bba23a568ff9b8eea56b79 Mon Sep 17 00:00:00 2001 From: Julio Date: Tue, 2 Nov 2021 14:17:42 +0100 Subject: [PATCH] Add tests --- src/CourseBundle/Entity/CDocument.php | 2 + .../Repository/CDocumentRepositoryTest.php | 53 ++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/CourseBundle/Entity/CDocument.php b/src/CourseBundle/Entity/CDocument.php index 7f520be570..9cb2c0bd76 100644 --- a/src/CourseBundle/Entity/CDocument.php +++ b/src/CourseBundle/Entity/CDocument.php @@ -36,6 +36,8 @@ use Symfony\Component\Validator\Constraints as Assert; * }, * "put_toggle_visibility" = { * "method" = "PUT", + * "deserialize"=false, + * "security" = "is_granted('EDIT', object.resourceNode)", * "path"="/documents/{iid}/toggle_visibility", * "controller"=UpdateVisibilityDocument::class, * }, diff --git a/tests/CourseBundle/Repository/CDocumentRepositoryTest.php b/tests/CourseBundle/Repository/CDocumentRepositoryTest.php index 091fa02920..5d6372f0d5 100644 --- a/tests/CourseBundle/Repository/CDocumentRepositoryTest.php +++ b/tests/CourseBundle/Repository/CDocumentRepositoryTest.php @@ -74,7 +74,7 @@ class CDocumentRepositoryTest extends AbstractApiTest $folderName = 'folder1'; $token = $this->getUserToken([]); - $response = $this->createClientWithCredentials($token)->request( + $this->createClientWithCredentials($token)->request( 'POST', '/api/documents', [ @@ -1032,7 +1032,6 @@ class CDocumentRepositoryTest extends AbstractApiTest public function testGetTotalSpaceByCourse(): void { - self::bootKernel(); $course = $this->createCourse('Test'); $admin = $this->getUser('admin'); $em = $this->getEntityManager(); @@ -1059,4 +1058,54 @@ class CDocumentRepositoryTest extends AbstractApiTest $this->assertSame(0, $documentRepo->count([])); } + + public function testToggleVisibility(): void + { + $client = static::createClient(); + $admin = $this->getUser('admin'); + $course = $this->createCourse('Test'); + $documentRepo = self::getContainer()->get(CDocumentRepository::class); + + $document = (new CDocument()) + ->setFiletype('file') + ->setTitle('title123') + ->setParent($course) + ->setCreator($admin) + ->addCourseLink($course) + ; + + $documentRepo->create($document); + + $link = $document->getFirstResourceLink(); + $this->assertSame(ResourceLink::VISIBILITY_PUBLISHED, $link->getVisibility()); + + $documentId = $document->getIid(); + $url = '/api/documents/'.$documentId.'/toggle_visibility'; + + // Not logged in. + $client->request('PUT', $url); + $this->assertResponseStatusCodeSame(401); + + // Another user. + $this->createUser('another'); + $client = $this->getClientWithGuiCredentials('another', 'another'); + $client->request('PUT', $url); + + // Admin. + $token = $this->getUserToken([]); + $this->createClientWithCredentials($token)->request( + 'PUT', + $url, + [ + 'headers' => [ + 'Content-Type' => 'application/json', + ], + ] + ); + $this->assertResponseIsSuccessful(); + + $document = $documentRepo->find($document->getIid()); + $link = $document->getFirstResourceLink(); + $this->assertSame(ResourceLink::VISIBILITY_DRAFT, $link->getVisibility()); + } }