From c7bb1acf8cdf13bf413953cf9ea0bb3aef7b9fbb Mon Sep 17 00:00:00 2001 From: Julio Date: Fri, 13 Aug 2021 12:37:21 +0200 Subject: [PATCH] Calendar Event: Add tests --- .../Repository/CCalendarEventRepository.php | 2 +- tests/ChamiloTestTrait.php | 6 +++ .../CCalendarEventRepositoryTest.php | 47 ++++++++++++++++--- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/CourseBundle/Repository/CCalendarEventRepository.php b/src/CourseBundle/Repository/CCalendarEventRepository.php index 1d54efbe1b..560e62631d 100644 --- a/src/CourseBundle/Repository/CCalendarEventRepository.php +++ b/src/CourseBundle/Repository/CCalendarEventRepository.php @@ -58,7 +58,7 @@ final class CCalendarEventRepository extends ResourceRepository $event->addResourceToGroupList($sendTo['groups'], $course, $session); } - // Storing the selected users + // Storing the selected users. if (\is_array($sendTo['users'])) { $sendTo['users'] = array_map( fn ($userId) => $em->find(User::class, $userId), diff --git a/tests/ChamiloTestTrait.php b/tests/ChamiloTestTrait.php index cd9ed7dba9..6d52977b46 100644 --- a/tests/ChamiloTestTrait.php +++ b/tests/ChamiloTestTrait.php @@ -10,6 +10,7 @@ use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository; use Chamilo\CoreBundle\Repository\Node\CourseRepository; use Chamilo\CoreBundle\Repository\Node\UserRepository; use Chamilo\CoreBundle\Repository\SessionRepository; +use Doctrine\ORM\EntityManager; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Validator\ValidatorInterface; @@ -136,4 +137,9 @@ trait ChamiloTestTrait { return $localTime->setTimezone(new \DateTimeZone('UTC'))->format('c'); } + + public function getManager(): EntityManager + { + return self::getContainer()->get('doctrine')->getManager(); + } } diff --git a/tests/CourseBundle/Repository/CCalendarEventRepositoryTest.php b/tests/CourseBundle/Repository/CCalendarEventRepositoryTest.php index a8a7a2642c..b69c83757e 100644 --- a/tests/CourseBundle/Repository/CCalendarEventRepositoryTest.php +++ b/tests/CourseBundle/Repository/CCalendarEventRepositoryTest.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace Chamilo\Tests\CourseBundle\Repository; use Chamilo\CoreBundle\Entity\ResourceLink; +use Chamilo\CoreBundle\Repository\Node\UserRepository; use Chamilo\CourseBundle\Entity\CCalendarEvent; use Chamilo\CourseBundle\Repository\CCalendarEventRepository; use Chamilo\Tests\AbstractApiTest; @@ -118,6 +119,8 @@ class CCalendarEventRepositoryTest extends AbstractApiTest public function testCreatePersonalEventAsAnotherUser(): void { + self::bootKernel(); + // 1. Create user 'test'. $user = $this->createUser('test'); $resourceNodeId = $user->getResourceNode()->getId(); @@ -167,6 +170,8 @@ class CCalendarEventRepositoryTest extends AbstractApiTest public function testAccessPersonalEvent(): void { + self::bootKernel(); + // 1. Create user 'test'. $user = $this->createUser('test'); $token = $this->getUserToken( @@ -233,20 +238,50 @@ class CCalendarEventRepositoryTest extends AbstractApiTest $event->setCollective(true); $calendarRepo->update($event); - // view + // View. $this->createClientWithCredentials($anotherToken)->request('GET', $eventIri); $this->assertResponseStatusCodeSame(403); - // edit - $this->createClientWithCredentials($anotherToken)->request('PUT', $eventIri, ['json' => ['title' => 'hehe']]); + // Edit. + $this->createClientWithCredentials($anotherToken)->request( + 'PUT', + $eventIri, + ['json' => ['title' => 'hehe', 'content' => 'modified']] + ); $this->assertResponseStatusCodeSame(403); - // Now also add "another" as shared. @todo - /*$event->addUserLink($another); + // Delete + $this->createClientWithCredentials($anotherToken)->request('DELETE', $eventIri); + $this->assertResponseStatusCodeSame(403); + + // Share event with "another" user. + $userRepo = self::getContainer()->get(UserRepository::class); + $calendarRepo = self::getContainer()->get(CCalendarEventRepository::class); + + /** @var CCalendarEvent $event */ + $event = $calendarRepo->find($eventId); + $another = $userRepo->find($another->getId()); + + // Add "another" as to the user. + $event + ->addUserLink($another) + ; $calendarRepo->update($event); $this->createClientWithCredentials($anotherToken)->request('GET', $eventIri); - $this->assertResponseStatusCodeSame(403);*/ + $this->assertResponseStatusCodeSame(200); + + // Edit. + $this->createClientWithCredentials($anotherToken)->request( + 'PUT', + $eventIri, + ['json' => ['title' => 'hehe', 'content' => 'modified']] + ); + $this->assertResponseStatusCodeSame(200); + + // Delete + $this->createClientWithCredentials($anotherToken)->request('DELETE', $eventIri); + $this->assertResponseStatusCodeSame(403); } public function testCreateCourseEvent(): void