1441 |
ariadna |
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 mod_feedback\courseformat;
|
|
|
18 |
|
|
|
19 |
use core_courseformat\local\overview\overviewfactory;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Tests for Feedback
|
|
|
23 |
*
|
|
|
24 |
* @covers \mod_feedback\courseformat\overview
|
|
|
25 |
* @package mod_feedback
|
|
|
26 |
* @category test
|
|
|
27 |
* @copyright 2025 Ferran Recio <ferran@moodle.com>
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
*/
|
|
|
30 |
final class overview_test extends \advanced_testcase {
|
|
|
31 |
#[\Override]
|
|
|
32 |
public static function setUpBeforeClass(): void {
|
|
|
33 |
global $CFG;
|
|
|
34 |
require_once($CFG->dirroot . '/mod/feedback/lib.php');
|
|
|
35 |
parent::setUpBeforeClass();
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Test get_actions_overview.
|
|
|
41 |
*
|
|
|
42 |
* @covers ::get_actions_overview
|
|
|
43 |
* @dataProvider provider_test_get_actions_overview
|
|
|
44 |
*
|
|
|
45 |
* @param string $user
|
|
|
46 |
* @param bool $expectnull
|
|
|
47 |
* @param bool $hasresponses
|
|
|
48 |
* @return void
|
|
|
49 |
*/
|
|
|
50 |
public function test_get_actions_overview(string $user, bool $expectnull, bool $hasresponses): void {
|
|
|
51 |
$this->resetAfterTest();
|
|
|
52 |
$course = $this->getDataGenerator()->create_course();
|
|
|
53 |
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
|
|
|
54 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
55 |
|
|
|
56 |
$activity = $this->getDataGenerator()->create_module(
|
|
|
57 |
'feedback',
|
|
|
58 |
['course' => $course->id],
|
|
|
59 |
);
|
|
|
60 |
$cm = get_fast_modinfo($course)->get_cm($activity->cmid);
|
|
|
61 |
|
|
|
62 |
$feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
|
|
|
63 |
$itemcreated = $feedbackgenerator->create_item_multichoice($activity, ['values' => "y\nn"]);
|
|
|
64 |
|
|
|
65 |
$expectedresonses = 0;
|
|
|
66 |
if ($hasresponses) {
|
|
|
67 |
$this->setUser($student);
|
|
|
68 |
$feedbackgenerator->create_response([
|
|
|
69 |
'userid' => $student->id,
|
|
|
70 |
'cmid' => $cm->id,
|
|
|
71 |
'anonymous' => false,
|
|
|
72 |
$itemcreated->name => 'y',
|
|
|
73 |
]);
|
|
|
74 |
$expectedresonses = 1;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
$currentuser = ($user == 'teacher') ? $teacher : $student;
|
|
|
78 |
$this->setUser($currentuser);
|
|
|
79 |
|
|
|
80 |
$item = overviewfactory::create($cm)->get_actions_overview();
|
|
|
81 |
|
|
|
82 |
// Students should not see item.
|
|
|
83 |
if ($expectnull) {
|
|
|
84 |
$this->assertNull($item);
|
|
|
85 |
return;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// Teachers should see item.
|
|
|
89 |
$this->assertEquals(get_string('responses', 'mod_feedback'), $item->get_name());
|
|
|
90 |
$this->assertEquals($expectedresonses, $item->get_value());
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Data provider for test_get_actions_overview.
|
|
|
95 |
*
|
|
|
96 |
* @return array
|
|
|
97 |
*/
|
|
|
98 |
public static function provider_test_get_actions_overview(): array {
|
|
|
99 |
return [
|
|
|
100 |
'Teacher with responses' => [
|
|
|
101 |
'user' => 'teacher',
|
|
|
102 |
'expectnull' => false,
|
|
|
103 |
'hasresponses' => true,
|
|
|
104 |
],
|
|
|
105 |
'Student with responses' => [
|
|
|
106 |
'user' => 'student',
|
|
|
107 |
'expectnull' => true,
|
|
|
108 |
'hasresponses' => true,
|
|
|
109 |
],
|
|
|
110 |
'Teacher without responses' => [
|
|
|
111 |
'user' => 'teacher',
|
|
|
112 |
'expectnull' => false,
|
|
|
113 |
'hasresponses' => false,
|
|
|
114 |
],
|
|
|
115 |
'Student without responses' => [
|
|
|
116 |
'user' => 'student',
|
|
|
117 |
'expectnull' => true,
|
|
|
118 |
'hasresponses' => false,
|
|
|
119 |
],
|
|
|
120 |
];
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Test get_due_date_overview.
|
|
|
125 |
* @covers ::get_due_date_overview
|
|
|
126 |
* @dataProvider provider_test_get_due_date_overview
|
|
|
127 |
* @param string $user
|
|
|
128 |
* @param bool $hasduedate
|
|
|
129 |
* @return void
|
|
|
130 |
*/
|
|
|
131 |
public function test_get_due_date_overview(string $user, bool $hasduedate): void {
|
|
|
132 |
$this->resetAfterTest();
|
|
|
133 |
$course = $this->getDataGenerator()->create_course();
|
|
|
134 |
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
|
|
|
135 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
136 |
|
|
|
137 |
$moddata = [
|
|
|
138 |
'course' => $course->id,
|
|
|
139 |
'timeclose' => $hasduedate ? time() + 3600 : 0,
|
|
|
140 |
];
|
|
|
141 |
|
|
|
142 |
$activity = $this->getDataGenerator()->create_module('feedback', $moddata);
|
|
|
143 |
$cm = get_fast_modinfo($course)->get_cm($activity->cmid);
|
|
|
144 |
|
|
|
145 |
$currentuser = ($user == 'teacher') ? $teacher : $student;
|
|
|
146 |
$this->setUser($currentuser);
|
|
|
147 |
|
|
|
148 |
$item = overviewfactory::create($cm)->get_due_date_overview();
|
|
|
149 |
|
|
|
150 |
// Teachers should see item.
|
|
|
151 |
$this->assertEquals(get_string('feedbackclose', 'mod_feedback'), $item->get_name());
|
|
|
152 |
$expectedvalue = $hasduedate ? $moddata['timeclose'] : null;
|
|
|
153 |
$this->assertEquals($expectedvalue, $item->get_value());
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Data provider for test_get_due_date_overview.
|
|
|
158 |
*
|
|
|
159 |
* @return array
|
|
|
160 |
*/
|
|
|
161 |
public static function provider_test_get_due_date_overview(): array {
|
|
|
162 |
return [
|
|
|
163 |
'Teacher with due date' => [
|
|
|
164 |
'user' => 'teacher',
|
|
|
165 |
'hasduedate' => true,
|
|
|
166 |
],
|
|
|
167 |
'Student with due date' => [
|
|
|
168 |
'user' => 'student',
|
|
|
169 |
'hasduedate' => true,
|
|
|
170 |
],
|
|
|
171 |
'Teacher without due date' => [
|
|
|
172 |
'user' => 'teacher',
|
|
|
173 |
'hasduedate' => false,
|
|
|
174 |
],
|
|
|
175 |
'Student without due date' => [
|
|
|
176 |
'user' => 'student',
|
|
|
177 |
'hasduedate' => false,
|
|
|
178 |
],
|
|
|
179 |
];
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* Test get_extra_submitted_overview.
|
|
|
184 |
*
|
|
|
185 |
* @covers ::get_extra_submitted_overview
|
|
|
186 |
* @dataProvider provider_test_get_extra_submitted_overview
|
|
|
187 |
*
|
|
|
188 |
* @param string $user
|
|
|
189 |
* @param bool $expectnull
|
|
|
190 |
* @param bool $hasresponses
|
|
|
191 |
* @return void
|
|
|
192 |
*/
|
|
|
193 |
public function test_get_extra_submitted_overview(string $user, bool $expectnull, bool $hasresponses): void {
|
|
|
194 |
$this->resetAfterTest();
|
|
|
195 |
$course = $this->getDataGenerator()->create_course();
|
|
|
196 |
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
|
|
|
197 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
198 |
|
|
|
199 |
$activity = $this->getDataGenerator()->create_module(
|
|
|
200 |
'feedback',
|
|
|
201 |
['course' => $course->id],
|
|
|
202 |
);
|
|
|
203 |
$cm = get_fast_modinfo($course)->get_cm($activity->cmid);
|
|
|
204 |
|
|
|
205 |
$feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
|
|
|
206 |
$itemcreated = $feedbackgenerator->create_item_multichoice($activity, ['values' => "y\nn"]);
|
|
|
207 |
|
|
|
208 |
$expectedresonses = 0;
|
|
|
209 |
if ($hasresponses) {
|
|
|
210 |
$this->setUser($student);
|
|
|
211 |
$feedbackgenerator->create_response([
|
|
|
212 |
'userid' => $student->id,
|
|
|
213 |
'cmid' => $cm->id,
|
|
|
214 |
'anonymous' => false,
|
|
|
215 |
$itemcreated->name => 'y',
|
|
|
216 |
]);
|
|
|
217 |
$expectedresonses = 1;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
$currentuser = ($user == 'teacher') ? $teacher : $student;
|
|
|
221 |
$this->setUser($currentuser);
|
|
|
222 |
|
|
|
223 |
$overview = overviewfactory::create($cm);
|
|
|
224 |
$reflection = new \ReflectionClass($overview);
|
|
|
225 |
$method = $reflection->getMethod('get_extra_submitted_overview');
|
|
|
226 |
$method->setAccessible(true);
|
|
|
227 |
$item = $method->invoke($overview);
|
|
|
228 |
|
|
|
229 |
// Students should not see item.
|
|
|
230 |
if ($expectnull) {
|
|
|
231 |
$this->assertNull($item);
|
|
|
232 |
return;
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
// Teachers should see item.
|
|
|
236 |
$this->assertEquals(get_string('responded', 'mod_feedback'), $item->get_name());
|
|
|
237 |
$this->assertEquals($hasresponses, $item->get_value());
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
/**
|
|
|
241 |
* Data provider for test_get_extra_submitted_overview.
|
|
|
242 |
*
|
|
|
243 |
* @return array
|
|
|
244 |
*/
|
|
|
245 |
public static function provider_test_get_extra_submitted_overview(): array {
|
|
|
246 |
return [
|
|
|
247 |
'Teacher with responses' => [
|
|
|
248 |
'user' => 'teacher',
|
|
|
249 |
'expectnull' => true,
|
|
|
250 |
'hasresponses' => true,
|
|
|
251 |
],
|
|
|
252 |
'Student with responses' => [
|
|
|
253 |
'user' => 'student',
|
|
|
254 |
'expectnull' => false,
|
|
|
255 |
'hasresponses' => true,
|
|
|
256 |
],
|
|
|
257 |
'Teacher without responses' => [
|
|
|
258 |
'user' => 'teacher',
|
|
|
259 |
'expectnull' => true,
|
|
|
260 |
'hasresponses' => false,
|
|
|
261 |
],
|
|
|
262 |
'Student without responses' => [
|
|
|
263 |
'user' => 'student',
|
|
|
264 |
'expectnull' => false,
|
|
|
265 |
'hasresponses' => false,
|
|
|
266 |
],
|
|
|
267 |
];
|
|
|
268 |
}
|
|
|
269 |
}
|