diff --git a/tests/AbstractApiTest.php b/tests/AbstractApiTest.php index 037f171360..35d92fd7d9 100644 --- a/tests/AbstractApiTest.php +++ b/tests/AbstractApiTest.php @@ -3,6 +3,7 @@ namespace Chamilo\Tests; use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase; use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Client; +use Chamilo\CoreBundle\Entity\User; use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait; abstract class AbstractApiTest extends ApiTestCase @@ -49,6 +50,15 @@ abstract class AbstractApiTest extends ApiTestCase return static::createClient([], ['headers' => ['authorization' => 'Bearer '.$token]]); } + public function getUserTokenFromUser(User $user) + { + return $this->getUserToken([ + 'username' => $user->getUsername(), + 'password' => $user->getUsername(), + ], true + ); + } + /** * Use credentials with token, by default it returns the admin token. */ diff --git a/tests/CoreBundle/Tool/ToolChainTest.php b/tests/CoreBundle/Tool/ToolChainTest.php index 6cc2bfe848..94505d6757 100644 --- a/tests/CoreBundle/Tool/ToolChainTest.php +++ b/tests/CoreBundle/Tool/ToolChainTest.php @@ -14,8 +14,10 @@ use Chamilo\CoreBundle\Entity\Tool; use Chamilo\CoreBundle\Tool\AbstractTool; use Chamilo\CoreBundle\Tool\GlobalTool; use Chamilo\CoreBundle\Tool\ToolChain; +use Chamilo\CourseBundle\Entity\CTool; use Chamilo\Tests\AbstractApiTest; use Chamilo\Tests\ChamiloTestTrait; +use Doctrine\Common\Collections\ArrayCollection; class ToolChainTest extends AbstractApiTest { @@ -125,7 +127,7 @@ class ToolChainTest extends AbstractApiTest $tools = $toolChain->getTools(); - $this->assertSame($countBefore, \count($tools)); + $this->assertCount($countBefore, $tools); $em = $this->getEntityManager(); @@ -161,5 +163,21 @@ class ToolChainTest extends AbstractApiTest $items = $resourceTypeRepo->findAll(); $this->assertNotEmpty($items); + + $resourceType = (new ResourceType()) + ->setName('test') + ; + $this->assertHasNoEntityViolations($resourceType); + $em->persist($resourceType); + $collection = new ArrayCollection(); + $collection->add($resourceType); + + $tool = (new Tool()) + ->setName('lasagna') + ->setResourceTypes($collection) + ; + $this->assertHasNoEntityViolations($tool); + $em->persist($tool); + $em->flush(); } } diff --git a/tests/CourseBundle/Repository/CToolRepositoryTest.php b/tests/CourseBundle/Repository/CToolRepositoryTest.php new file mode 100644 index 0000000000..13d61e3af4 --- /dev/null +++ b/tests/CourseBundle/Repository/CToolRepositoryTest.php @@ -0,0 +1,81 @@ +get(CToolRepository::class); + $this->assertSame(0, $repo->count([])); + + $course = $this->createCourse('new'); + $defaultCount = $repo->count([]); + + /** @var CTool $courseTool */ + $courseTool = $course->getTools()->first(); + $repo->delete($courseTool); + + $this->assertSame($defaultCount - 1, $repo->count([])); + } + + public function testGetTools(): void + { + $token = $this->getUserToken([]); + $response = $this->createClientWithCredentials($token)->request('GET', '/api/c_tools'); + $this->assertResponseIsSuccessful(); + + // Asserts that the returned content type is JSON-LD (the default) + $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8'); + + // Asserts that the returned JSON is a superset of this one + $this->assertJsonContains([ + '@context' => '/api/contexts/CTool', + '@id' => '/api/c_tools', + '@type' => 'hydra:Collection', + 'hydra:totalItems' => 0, + ]); + + $this->assertCount(0, $response->toArray()['hydra:member']); + $this->assertMatchesResourceCollectionJsonSchema(CTool::class); + + $this->createCourse('new'); + $response = $this->createClientWithCredentials($token)->request('GET', '/api/c_tools'); + $this->assertResponseIsSuccessful(); + + $repo = self::getContainer()->get(CToolRepository::class); + $defaultCount = $repo->count([]); + $this->assertCount($defaultCount, $response->toArray()['hydra:member']); + + $test = $this->createUser('student'); + $studentToken = $this->getUserTokenFromUser($test); + $this->createClientWithCredentials($studentToken)->request('GET', '/api/c_tools'); + $this->assertResponseStatusCodeSame(403); + } +}