Sessions: Add UniqueEntity constraints, add phpunit tests

pull/3924/head
Julio Montoya 3 years ago
parent 5e107954f7
commit 7551f32499
  1. 27
      config/packages/api_platform.yaml
  2. 5
      src/CoreBundle/Entity/SessionRelCourse.php
  3. 5
      src/CoreBundle/Entity/SessionRelUser.php
  4. 1
      src/CoreBundle/Migrations/Schema/V200/Version20190210182615.php
  5. 2
      tests/ChamiloTestTrait.php
  6. 8
      tests/CoreBundle/Repository/Node/CourseRepositoryTest.php
  7. 54
      tests/CoreBundle/Repository/Node/SessionRepositoryTest.php

@ -12,14 +12,16 @@ api_platform:
name: Authorization
type: header
formats:
jsonld:
mime_types: ['application/ld+json']
json:
mime_types: ['application/json']
html:
mime_types: ['text/html']
graphql:
mime_types: ['application/graphql']
jsonld: ['application/ld+json']
jsonhal: ['application/hal+json']
jsonapi: ['application/vnd.api+json']
json: ['application/json']
xml: ['application/xml', 'text/xml']
yaml: ['application/x-yaml']
csv: ['text/csv']
html: ['text/html']
graphql: ['application/graphql']
collection:
pagination:
items_per_page_parameter_name: itemsPerPage # Default value
@ -36,5 +38,14 @@ api_platform:
etag: true
# Default value for the response max age.
max_age: 0
exception_to_status:
# default api platform https://api-platform.com/docs/core/errors/#converting-php-exceptions-to-http-errors
Symfony\Component\Serializer\Exception\ExceptionInterface: 400
# Or with a constant defined in the 'Symfony\Component\HttpFoundation\Response' class.
ApiPlatform\Core\Exception\InvalidArgumentException: !php/const Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST
ApiPlatform\Core\Exception\FilterValidationException: 400
Doctrine\ORM\OptimisticLockException: 409
Doctrine\DBAL\Exception\UniqueConstraintViolationException: 409
# mercure:
# hub_url: '%env(MERCURE_SUBSCRIBE_URL)%'

@ -8,6 +8,7 @@ namespace Chamilo\CoreBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
@ -24,6 +25,10 @@ use Symfony\Component\Validator\Constraints as Assert;
* }
* )
* @ORM\Entity
* @UniqueEntity(
* fields={"course", "session"},
* message="The course is already registered in this session."
* )
*/
#[ApiResource(
collectionOperations: [

@ -14,6 +14,7 @@ use DateTime;
use DateTimeZone;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
/**
@ -30,6 +31,10 @@ use Symfony\Component\Serializer\Annotation\Groups;
* }
* )
* @ORM\Entity
* @UniqueEntity(
* fields={"session", "user", "relationType"},
* message="The user-course-relationType is already registered in this session."
* )
*/
#[ApiResource(
collectionOperations: [

@ -59,7 +59,6 @@ class Version20190210182615 extends AbstractMigrationChamilo
$this->addSql(' CREATE UNIQUE INDEX course_session_unique ON session_rel_course_rel_user (session_id, c_id, user_id, status);');
}
$table = $schema->getTable('session_rel_course');
if (!$table->hasIndex('UNIQ_12D110D391D79BD3')) {
$this->addSql('CREATE UNIQUE INDEX course_session_unique ON session_rel_course (session_id, c_id)');

@ -80,7 +80,7 @@ trait ChamiloTestTrait
->setGeneralCoach($this->getUser('admin'))
->addAccessUrl($this->getAccessUrl())
;
$repo->create($session);
$repo->update($session);
return $session;
}

@ -86,7 +86,13 @@ class CourseRepositoryTest extends WebTestCase
$this->assertSame(1, $course->getUsers()->count());
// retrieve the admin
// Add the same user again:
$course->addUser($student, 0, null, 5);
$courseRepo->update($course);
$this->assertSame(1, $course->getUsers()->count());
// Retrieve the admin
$user = $this->getUser('student');
$client->loginUser($user);

@ -55,7 +55,7 @@ class SessionRepositoryTest extends AbstractApiTest
$this->assertCount(1, $errors);
$this->expectException(UniqueConstraintViolationException::class);
$repo->create($session);
$repo->update($session);
}
public function testCreateWithApi(): void
@ -142,23 +142,49 @@ class SessionRepositoryTest extends AbstractApiTest
'course' => '/api/courses/'.$course->getId(),
]
);
// Add the same course again, it should fail.
$this->createClientWithCredentials($token)->request(
'POST',
'/api/session_rel_courses',
[
'json' => [
'session' => '/api/sessions/'.$session->getId(),
'course' => '/api/courses/'.$course->getId(),
],
]
);
$this->assertResponseStatusCodeSame(422);
/** @var SessionRepository $courseRepo */
$sessionRepo = self::getContainer()->get(SessionRepository::class);
$session = $sessionRepo->find($session->getId());
$this->assertSame(1, $session->getCourses()->count());
}
public function testAddUserToSessionWithApi(): void
{
$token = $this->getUserToken();
$testUser = $this->createUser('test');
$user = $this->createUser('test');
$course = $this->createCourse('course title');
$session = $this->createSession('test session');
/** @var SessionRepository $courseRepo */
$sessionRepo = self::getContainer()->get(SessionRepository::class);
$session->addCourse($course);
$sessionRepo->update($session);
$this->createClientWithCredentials($token)->request(
'POST',
'/api/session_rel_users',
[
'json' => [
'session' => '/api/sessions/'.$session->getId(),
'user' => '/api/users/'.$testUser->getId(),
'user' => '/api/users/'.$user->getId(),
],
]
);
@ -174,9 +200,27 @@ class SessionRepositoryTest extends AbstractApiTest
'@id' => '/api/sessions/'.$session->getId(),
],
'user' => [
'@id' => '/api/users/'.$testUser->getId(),
'@id' => '/api/users/'.$user->getId(),
],
]
);
/** @var Session $session */
$session = $sessionRepo->find($session->getId());
$this->assertSame(1, $session->getUsers()->count());
// Add the user again!
$this->createClientWithCredentials($token)->request(
'POST',
'/api/session_rel_users',
[
'json' => [
'session' => '/api/sessions/'.$session->getId(),
'user' => '/api/users/'.$user->getId(),
],
]
);
$this->assertResponseStatusCodeSame(422);
}
}

Loading…
Cancel
Save