Skip to content

Commit 496e660

Browse files
committed
Minor - add tests
1 parent 31d3851 commit 496e660

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/* For licensing terms, see /license.txt */
6+
7+
namespace Chamilo\Tests\CoreBundle\Security\Authorization\Voter;
8+
9+
use Chamilo\CoreBundle\Entity\Course;
10+
use Chamilo\CoreBundle\Entity\CourseRelUser;
11+
use Chamilo\CoreBundle\Security\Authorization\Voter\CourseVoter;
12+
use Chamilo\Tests\ChamiloTestTrait;
13+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
14+
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
15+
16+
class CourseVoterTest extends WebTestCase
17+
{
18+
use ChamiloTestTrait;
19+
20+
// @dataProvider provideVoteTests not working
21+
public function testVote(): void
22+
{
23+
$client = static::createClient();
24+
$tests = $this->provideVoteTests();
25+
$voter = $this->getContainer()->get(CourseVoter::class);
26+
foreach ($tests as $message => $test) {
27+
[$expected, $user, $course] = $test;
28+
$client->loginUser($user);
29+
$token = $this->getContainer()->get('security.untracked_token_storage')->getToken();
30+
$this->assertSame($expected, $voter->vote($token, $course, ['VIEW']), $message);
31+
}
32+
}
33+
34+
public function provideVoteTests()
35+
{
36+
$em = $this->getEntityManager();
37+
$admin = $this->getAdmin();
38+
$student = $this->createUser('student');
39+
$studentWithAccess = $this->createUser('student_access');
40+
41+
$teacher = $this->createUser('teacher', '', '', 'ROLE_TEACHER');
42+
$teacherWithAccess = $this->createUser('teacher_with_access', '', '', 'ROLE_TEACHER');
43+
44+
// Group in public course.
45+
$publicCourse = $this->createCourse('public');
46+
$publicCourse->addUser($studentWithAccess, 0, null, CourseRelUser::STUDENT);
47+
$publicCourse->addUser($teacherWithAccess, 0, null, CourseRelUser::TEACHER);
48+
$em->persist($publicCourse);
49+
$em->flush();
50+
51+
$denied = VoterInterface::ACCESS_DENIED;
52+
$granted = VoterInterface::ACCESS_GRANTED;
53+
54+
yield 'admin access to course' => [$granted, $admin, $publicCourse];
55+
yield 'student access to course' => [$granted, $student, $publicCourse];
56+
yield 'student access to course' => [$granted, $studentWithAccess, $publicCourse];
57+
yield 'teacher no access to course' => [$granted, $teacher, $publicCourse];
58+
yield 'teacher with access to course' => [$granted, $teacherWithAccess, $publicCourse];
59+
60+
// REGISTERED course.
61+
$registeredCourse = $this->createCourse('registered');
62+
$registeredCourse->setVisibility(Course::REGISTERED);
63+
$registeredCourse->addUser($studentWithAccess, 0, null, CourseRelUser::STUDENT);
64+
$registeredCourse->addUser($teacherWithAccess, 0, null, CourseRelUser::TEACHER);
65+
$em->persist($registeredCourse);
66+
$em->flush();
67+
68+
$admin = $this->getAdmin();
69+
70+
yield 'admin access to reg course' => [$granted, $admin, $registeredCourse];
71+
yield 'teacher access to reg course' => [$granted, $teacherWithAccess, $registeredCourse];
72+
yield 'student access to reg course ' => [$granted, $studentWithAccess, $registeredCourse];
73+
yield 'teacher no access to reg course' => [$denied, $teacher, $registeredCourse];
74+
yield 'student no access to reg course' => [$denied, $student, $registeredCourse];
75+
76+
// Hidden
77+
$registeredCourse->setVisibility(Course::HIDDEN);
78+
$em->persist($registeredCourse);
79+
$em->flush();
80+
81+
yield 'admin access to reg course' => [$granted, $admin, $registeredCourse];
82+
yield 'teacher access to reg course' => [$denied, $teacherWithAccess, $registeredCourse];
83+
yield 'student access to reg course ' => [$denied, $studentWithAccess, $registeredCourse];
84+
yield 'teacher no access to reg course' => [$denied, $teacher, $registeredCourse];
85+
yield 'student no access to reg course' => [$denied, $student, $registeredCourse];
86+
}
87+
}

tests/CoreBundle/Security/Authorization/Voter/GroupVoterTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,15 @@ public function provideVoteTests()
138138
yield 'student no access to reg course status=true' => [$denied, $student, $group2];
139139
yield 'student no access to group 2' => [$granted, $studentWithAccess, $group2];
140140
yield 'student access to reg course group status=true' => [$granted, $studentInGroup2IsMember, $group2];
141+
142+
$registeredCourse->setVisibility(Course::HIDDEN);
143+
$em->persist($registeredCourse);
144+
$em->flush();
145+
146+
yield 'admin access to reg course hidden' => [$granted, $admin, $group2];
147+
yield 'teacher access to reg course hidden' => [$denied, $teacherWithAccess, $group2];
148+
yield 'teacher no access to reg course hidden' => [$denied, $teacher, $group2];
149+
yield 'studentWithAccess no access reg course hidden' => [$denied, $studentWithAccess, $group2];
150+
yield 'studentInGroup2IsMember to reg course hidden' => [$denied, $studentInGroup2IsMember, $group2];
141151
}
142152
}

0 commit comments

Comments
 (0)