Minor - add tests

pull/4020/head^2
Julio 3 years ago
parent 31d3851807
commit 496e660d1e
  1. 87
      tests/CoreBundle/Security/Authorization/Voter/CourseVoterTest.php
  2. 10
      tests/CoreBundle/Security/Authorization/Voter/GroupVoterTest.php

@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CoreBundle\Security\Authorization\Voter;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\CourseRelUser;
use Chamilo\CoreBundle\Security\Authorization\Voter\CourseVoter;
use Chamilo\Tests\ChamiloTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class CourseVoterTest extends WebTestCase
{
use ChamiloTestTrait;
// @dataProvider provideVoteTests not working
public function testVote(): void
{
$client = static::createClient();
$tests = $this->provideVoteTests();
$voter = $this->getContainer()->get(CourseVoter::class);
foreach ($tests as $message => $test) {
[$expected, $user, $course] = $test;
$client->loginUser($user);
$token = $this->getContainer()->get('security.untracked_token_storage')->getToken();
$this->assertSame($expected, $voter->vote($token, $course, ['VIEW']), $message);
}
}
public function provideVoteTests()
{
$em = $this->getEntityManager();
$admin = $this->getAdmin();
$student = $this->createUser('student');
$studentWithAccess = $this->createUser('student_access');
$teacher = $this->createUser('teacher', '', '', 'ROLE_TEACHER');
$teacherWithAccess = $this->createUser('teacher_with_access', '', '', 'ROLE_TEACHER');
// Group in public course.
$publicCourse = $this->createCourse('public');
$publicCourse->addUser($studentWithAccess, 0, null, CourseRelUser::STUDENT);
$publicCourse->addUser($teacherWithAccess, 0, null, CourseRelUser::TEACHER);
$em->persist($publicCourse);
$em->flush();
$denied = VoterInterface::ACCESS_DENIED;
$granted = VoterInterface::ACCESS_GRANTED;
yield 'admin access to course' => [$granted, $admin, $publicCourse];
yield 'student access to course' => [$granted, $student, $publicCourse];
yield 'student access to course' => [$granted, $studentWithAccess, $publicCourse];
yield 'teacher no access to course' => [$granted, $teacher, $publicCourse];
yield 'teacher with access to course' => [$granted, $teacherWithAccess, $publicCourse];
// REGISTERED course.
$registeredCourse = $this->createCourse('registered');
$registeredCourse->setVisibility(Course::REGISTERED);
$registeredCourse->addUser($studentWithAccess, 0, null, CourseRelUser::STUDENT);
$registeredCourse->addUser($teacherWithAccess, 0, null, CourseRelUser::TEACHER);
$em->persist($registeredCourse);
$em->flush();
$admin = $this->getAdmin();
yield 'admin access to reg course' => [$granted, $admin, $registeredCourse];
yield 'teacher access to reg course' => [$granted, $teacherWithAccess, $registeredCourse];
yield 'student access to reg course ' => [$granted, $studentWithAccess, $registeredCourse];
yield 'teacher no access to reg course' => [$denied, $teacher, $registeredCourse];
yield 'student no access to reg course' => [$denied, $student, $registeredCourse];
// Hidden
$registeredCourse->setVisibility(Course::HIDDEN);
$em->persist($registeredCourse);
$em->flush();
yield 'admin access to reg course' => [$granted, $admin, $registeredCourse];
yield 'teacher access to reg course' => [$denied, $teacherWithAccess, $registeredCourse];
yield 'student access to reg course ' => [$denied, $studentWithAccess, $registeredCourse];
yield 'teacher no access to reg course' => [$denied, $teacher, $registeredCourse];
yield 'student no access to reg course' => [$denied, $student, $registeredCourse];
}
}

@ -138,5 +138,15 @@ class GroupVoterTest extends WebTestCase
yield 'student no access to reg course status=true' => [$denied, $student, $group2];
yield 'student no access to group 2' => [$granted, $studentWithAccess, $group2];
yield 'student access to reg course group status=true' => [$granted, $studentInGroup2IsMember, $group2];
$registeredCourse->setVisibility(Course::HIDDEN);
$em->persist($registeredCourse);
$em->flush();
yield 'admin access to reg course hidden' => [$granted, $admin, $group2];
yield 'teacher access to reg course hidden' => [$denied, $teacherWithAccess, $group2];
yield 'teacher no access to reg course hidden' => [$denied, $teacher, $group2];
yield 'studentWithAccess no access reg course hidden' => [$denied, $studentWithAccess, $group2];
yield 'studentInGroup2IsMember to reg course hidden' => [$denied, $studentInGroup2IsMember, $group2];
}
}

Loading…
Cancel
Save