1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace tool_courserating;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Tests for permission class
|
|
|
21 |
*
|
|
|
22 |
* @package tool_courserating
|
|
|
23 |
* @covers \tool_courserating\permission
|
|
|
24 |
* @copyright 2022 Marina Glancy <marina.glancy@gmail.com>
|
|
|
25 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
final class permission_test extends \advanced_testcase {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* setUp
|
|
|
31 |
*/
|
|
|
32 |
protected function setUp(): void {
|
|
|
33 |
$this->resetAfterTest();
|
|
|
34 |
set_config(\tool_courserating\constants::SETTING_RATINGMODE,
|
|
|
35 |
\tool_courserating\constants::RATEBY_ANYTIME, 'tool_courserating');
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Generator
|
|
|
40 |
*
|
|
|
41 |
* @return \tool_courserating_generator
|
|
|
42 |
*/
|
|
|
43 |
protected function get_generator(): \tool_courserating_generator {
|
|
|
44 |
/** @var \tool_courserating_generator $generator */
|
|
|
45 |
$generator = self::getDataGenerator()->get_plugin_generator('tool_courserating');
|
|
|
46 |
return $generator;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public function test_can_view(): void {
|
|
|
50 |
global $DB;
|
|
|
51 |
|
|
|
52 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
53 |
$course2 = $this->getDataGenerator()->create_course();
|
|
|
54 |
$course3 = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
|
|
|
55 |
$cat = $this->getDataGenerator()->create_category();
|
|
|
56 |
$userrole = $DB->get_field('role', 'id', ['shortname' => 'user'], MUST_EXIST);
|
|
|
57 |
assign_capability('moodle/category:viewcourselist', CAP_PROHIBIT,
|
|
|
58 |
$userrole, \context_coursecat::instance($cat->id)->id, true);
|
|
|
59 |
$course4 = $this->getDataGenerator()->create_course(['category' => $cat->id]);
|
|
|
60 |
$user = $this->getDataGenerator()->create_user();
|
|
|
61 |
|
|
|
62 |
// User can normally view ratings because they can see courses in the course list.
|
|
|
63 |
// Course4 is in a category that prevents viewing. However if user is enrolled in this course they can view ratings.
|
|
|
64 |
$this->setUser($user);
|
|
|
65 |
$this->assertTrue(permission::can_view_ratings($course1->id));
|
|
|
66 |
$this->assertFalse(permission::can_view_ratings($course4->id));
|
|
|
67 |
$this->getDataGenerator()->enrol_user($user->id, $course4->id, 'student');
|
|
|
68 |
$this->assertTrue(permission::can_view_ratings($course4->id));
|
|
|
69 |
|
|
|
70 |
// Disable ratings everywhere.
|
|
|
71 |
$this->get_generator()->set_config(constants::SETTING_RATINGMODE, constants::RATEBY_NOONE);
|
|
|
72 |
$this->assertFalse(permission::can_view_ratings($course1->id));
|
|
|
73 |
|
|
|
74 |
// Allow to override ratings per course.
|
|
|
75 |
$this->get_generator()->set_config(constants::SETTING_PERCOURSE, 1);
|
|
|
76 |
$this->get_generator()->set_course_rating_mode($course2->id, constants::RATEBY_ANYTIME);
|
|
|
77 |
$this->assertTrue(permission::can_view_ratings($course2->id));
|
|
|
78 |
|
|
|
79 |
$this->get_generator()->set_course_rating_mode($course3->id, constants::RATEBY_COMPLETED);
|
|
|
80 |
$this->assertTrue(permission::can_view_ratings($course3->id));
|
|
|
81 |
|
|
|
82 |
// Assert exception in require- method.
|
|
|
83 |
try {
|
|
|
84 |
permission::require_can_view_ratings($course4->id);
|
|
|
85 |
$this->fail('Exception expected');
|
|
|
86 |
} catch (\moodle_exception $e) {
|
|
|
87 |
$this->assertNotEmpty($e->getMessage());
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public function test_can_rate(): void {
|
|
|
92 |
$course = $this->getDataGenerator()->create_course();
|
|
|
93 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
94 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
95 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
|
|
|
96 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
97 |
$this->getDataGenerator()->enrol_user($user3->id, $course->id, 'teacher');
|
|
|
98 |
|
|
|
99 |
// Only user who is enrolled as a student can add rating.
|
|
|
100 |
$this->setUser($user1);
|
|
|
101 |
$this->assertTrue(permission::can_add_rating($course->id));
|
|
|
102 |
|
|
|
103 |
$this->setUser($user2);
|
|
|
104 |
$this->assertFalse(permission::can_add_rating($course->id));
|
|
|
105 |
|
|
|
106 |
$this->setUser($user3);
|
|
|
107 |
$this->assertFalse(permission::can_add_rating($course->id));
|
|
|
108 |
|
|
|
109 |
// Assert exception in require- method.
|
|
|
110 |
try {
|
|
|
111 |
permission::require_can_add_rating($course->id);
|
|
|
112 |
$this->fail('Exception expected');
|
|
|
113 |
} catch (\moodle_exception $e) {
|
|
|
114 |
$this->assertNotEmpty($e->getMessage());
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
public function test_can_rate_completion(): void {
|
|
|
119 |
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
|
|
|
120 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
121 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
122 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
|
|
|
123 |
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
|
|
|
124 |
$cc = ['course' => $course->id, 'userid' => $user2->id];
|
|
|
125 |
$ccompletion = new \completion_completion($cc);
|
|
|
126 |
$ccompletion->mark_complete();
|
|
|
127 |
|
|
|
128 |
// Only user who completed the course can rate it.
|
|
|
129 |
$this->get_generator()->set_config(constants::SETTING_RATINGMODE, constants::RATEBY_COMPLETED);
|
|
|
130 |
|
|
|
131 |
$this->setUser($user1);
|
|
|
132 |
$this->assertFalse(permission::can_add_rating($course->id));
|
|
|
133 |
|
|
|
134 |
$this->setUser($user2);
|
|
|
135 |
$this->assertTrue(permission::can_add_rating($course->id));
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
public function test_can_flag_rating(): void {
|
|
|
139 |
global $DB;
|
|
|
140 |
|
|
|
141 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
142 |
$course2 = $this->getDataGenerator()->create_course();
|
|
|
143 |
$course3 = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
|
|
|
144 |
$cat = $this->getDataGenerator()->create_category();
|
|
|
145 |
$userrole = $DB->get_field('role', 'id', ['shortname' => 'user'], MUST_EXIST);
|
|
|
146 |
assign_capability('moodle/category:viewcourselist', CAP_PROHIBIT,
|
|
|
147 |
$userrole, \context_coursecat::instance($cat->id)->id, true);
|
|
|
148 |
$course4 = $this->getDataGenerator()->create_course(['category' => $cat->id]);
|
|
|
149 |
$user = $this->getDataGenerator()->create_user();
|
|
|
150 |
|
|
|
151 |
// Leave ratings for all courses as another user.
|
|
|
152 |
$userother = $this->getDataGenerator()->create_user();
|
|
|
153 |
$this->setUser($userother);
|
|
|
154 |
$rating1 = api::set_rating($course1->id, (object)['rating' => 1]);
|
|
|
155 |
$rating2 = api::set_rating($course2->id, (object)['rating' => 2]);
|
|
|
156 |
$rating3 = api::set_rating($course3->id, (object)['rating' => 3]);
|
|
|
157 |
$rating4 = api::set_rating($course4->id, (object)['rating' => 4]);
|
|
|
158 |
|
|
|
159 |
// User can normally view ratings because they can see courses in the course list.
|
|
|
160 |
// Course4 is in a category that prevents viewing. However if user is enrolled in this course they can view ratings.
|
|
|
161 |
$this->setUser($user);
|
|
|
162 |
$this->assertTrue(permission::can_flag_rating($rating1->get('id')));
|
|
|
163 |
$this->assertTrue(permission::can_flag_rating($rating1->get('id'), $course1->id));
|
|
|
164 |
$this->assertFalse(permission::can_flag_rating($rating4->get('id')));
|
|
|
165 |
$this->getDataGenerator()->enrol_user($user->id, $course4->id, 'student');
|
|
|
166 |
$this->assertTrue(permission::can_flag_rating($rating4->get('id')));
|
|
|
167 |
|
|
|
168 |
// Disable ratings everywhere.
|
|
|
169 |
$this->get_generator()->set_config(constants::SETTING_RATINGMODE, constants::RATEBY_NOONE);
|
|
|
170 |
$this->assertFalse(permission::can_flag_rating($rating1->get('id')));
|
|
|
171 |
|
|
|
172 |
// Allow to override ratings per course.
|
|
|
173 |
$this->get_generator()->set_config(constants::SETTING_PERCOURSE, 1);
|
|
|
174 |
$this->get_generator()->set_course_rating_mode($course2->id, constants::RATEBY_ANYTIME);
|
|
|
175 |
$this->assertTrue(permission::can_flag_rating($rating2->get('id')));
|
|
|
176 |
|
|
|
177 |
$this->get_generator()->set_course_rating_mode($course3->id, constants::RATEBY_COMPLETED);
|
|
|
178 |
$this->assertTrue(permission::can_flag_rating($rating3->get('id')));
|
|
|
179 |
|
|
|
180 |
// Assert exception in require- method.
|
|
|
181 |
try {
|
|
|
182 |
permission::require_can_flag_rating($rating1->get('id'));
|
|
|
183 |
$this->fail('Exception expected');
|
|
|
184 |
} catch (\moodle_exception $e) {
|
|
|
185 |
$this->assertNotEmpty($e->getMessage());
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
public function test_can_delete_rating(): void {
|
|
|
190 |
global $DB;
|
|
|
191 |
$user = $this->getDataGenerator()->create_user();
|
|
|
192 |
$manager = $this->getDataGenerator()->create_user();
|
|
|
193 |
|
|
|
194 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
195 |
$cat = $this->getDataGenerator()->create_category();
|
|
|
196 |
$course2 = $this->getDataGenerator()->create_course(['category' => $cat->id]);
|
|
|
197 |
|
|
|
198 |
$managerrole = $DB->get_field('role', 'id', ['shortname' => 'manager'], MUST_EXIST);
|
|
|
199 |
role_assign($managerrole, $manager->id, \context_coursecat::instance($cat->id)->id);
|
|
|
200 |
|
|
|
201 |
$this->setUser($user);
|
|
|
202 |
$rating1 = api::set_rating($course1->id, (object)['rating' => 1]);
|
|
|
203 |
$rating2 = api::set_rating($course2->id, (object)['rating' => 2]);
|
|
|
204 |
|
|
|
205 |
$this->setUser($manager);
|
|
|
206 |
$this->assertFalse(permission::can_delete_rating($rating1->get('id')));
|
|
|
207 |
$this->assertTrue(permission::can_delete_rating($rating2->get('id')));
|
|
|
208 |
|
|
|
209 |
// Assert exception in require- method.
|
|
|
210 |
try {
|
|
|
211 |
permission::require_can_delete_rating($rating1->get('id'));
|
|
|
212 |
$this->fail('Exception expected');
|
|
|
213 |
} catch (\moodle_exception $e) {
|
|
|
214 |
$this->assertNotEmpty($e->getMessage());
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
}
|