Sessions: Add course to session + add user to session #3811

pull/3924/head
Julio Montoya 4 years ago
parent 564741165d
commit 89c9939250
  1. 31
      src/CoreBundle/Entity/SessionRelCourse.php
  2. 1
      src/CoreBundle/Entity/SessionRelUser.php
  3. 65
      tests/CoreBundle/Repository/Node/SessionRepositoryTest.php

@ -6,7 +6,9 @@ declare(strict_types=1);
namespace Chamilo\CoreBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* Course subscriptions to a session.
@ -16,6 +18,33 @@ use Doctrine\ORM\Mapping as ORM;
* })
* @ORM\Entity
*/
#[ApiResource(
attributes: [
'security' => "is_granted('ROLE_ADMIN')",
],
collectionOperations: [
'get' => [
'security' => "is_granted('ROLE_ADMIN')",
],
'post' => [
'security' => "is_granted('ROLE_ADMIN')",
],
],
itemOperations: [
'get' => [
'security' => "is_granted('ROLE_ADMIN')",
],
'put' => [
'security' => "is_granted('ROLE_ADMIN')",
],
],
normalizationContext: [
'groups' => ['session_rel_course:read'],
],
denormalizationContext: [
'groups' => ['session_rel_course:write'],
],
)]
class SessionRelCourse
{
/**
@ -26,12 +55,14 @@ class SessionRelCourse
protected int $id;
/**
* @Groups({"session_rel_course:read", "session_rel_course:write"})
* @ORM\ManyToOne(targetEntity="Session", inversedBy="courses", cascade={"persist"})
* @ORM\JoinColumn(name="session_id", referencedColumnName="id", nullable=false)
*/
protected ?Session $session = null;
/**
* @Groups({"session_rel_course:read", "session_rel_course:write"})
* @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", inversedBy="sessions", cascade={"persist"})
* @ORM\JoinColumn(name="c_id", referencedColumnName="id", nullable=false)
*/

@ -122,6 +122,7 @@ class SessionRelUser
public function __construct()
{
$this->relationType = 0;
$this->duration = 0;
$this->movedTo = null;
$this->movedStatus = null;

@ -109,4 +109,69 @@ class SessionRepositoryTest extends AbstractApiTest
'name' => $newSessionName,
]);
}
public function testAddCourseToSessionWithApi(): void
{
$token = $this->getUserToken();
$session = $this->createSession('test session');
$course = $this->createCourse('test course');
$this->createClientWithCredentials($token)->request(
'POST',
'/api/session_rel_courses',
[
'json' => [
'session' => '/api/sessions/'.$session->getId(),
'course' => '/api/courses/'.$course->getId(),
],
]
);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(201);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/api/contexts/SessionRelCourse',
'@type' => 'SessionRelCourse',
'session' => '/api/sessions/'.$session->getId(),
'course' => '/api/courses/'.$course->getId(),
]);
}
public function testAddUserToSessionWithApi(): void
{
$token = $this->getUserToken();
$testUser = $this->createUser('test');
$session = $this->createSession('test session');
$this->createClientWithCredentials($token)->request(
'POST',
'/api/session_rel_users',
[
'json' => [
'session' => '/api/sessions/'.$session->getId(),
'user' => '/api/users/'.$testUser->getId(),
],
]
);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(201);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains(
[
'@context' => '/api/contexts/SessionRelUser',
'@type' => 'SessionRelUser',
'session' => [
'@id' => '/api/sessions/'.$session->getId(),
],
[
'@id' => '/api/users/'.$testUser->getId(),
],
]
);
}
}

Loading…
Cancel
Save