MDL-77046 availability: validate profile field in condition.
[moodle.git] / mod / h5pactivity / tests / lib_test.php
blob9bfa5ab774c9a4dad6f52bf4ae3e406d3375cc7e
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.
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/>.
17 /**
18 * Unit tests for (some of) mod/h5pactivity/lib.php.
20 * @package mod_h5pactivity
21 * @copyright 2021 Ilya Tregubov <ilya@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 use mod_h5pactivity\local\manager;
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot . '/mod/h5pactivity/lib.php');
32 /**
33 * Unit tests for (some of) mod/h5pactivity/lib.php.
35 * @copyright 2021 Ilya Tregubov <ilya@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class lib_test extends advanced_testcase {
40 /**
41 * Test that assign_print_recent_activity shows ungraded submitted assignments.
43 public function test_print_recent_activity() {
44 $this->resetAfterTest();
45 $this->setAdminUser();
47 $course = $this->getDataGenerator()->create_course();
48 $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
49 $activity = $this->getDataGenerator()->create_module('h5pactivity',
50 ['course' => $course, 'enabletracking' => 1, 'grademethod' => manager::GRADEHIGHESTATTEMPT]);
52 $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
54 $manager = manager::create_from_instance($activity);
55 $cm = $manager->get_coursemodule();
57 $user = $student;
58 $params = ['cmid' => $cm->id, 'userid' => $user->id];
59 $generator->create_content($activity, $params);
60 $this->setUser($student);
61 $this->expectOutputRegex('/submitted:/');
62 h5pactivity_print_recent_activity($course, true, time() - 3600);
65 /**
66 * Test that h5pactivity_print_recent_activity does not display any warnings when a custom fullname has been configured.
68 public function test_print_recent_activity_fullname() {
69 $this->resetAfterTest();
70 $this->setAdminUser();
72 $course = $this->getDataGenerator()->create_course();
73 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
74 $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
75 $activity = $this->getDataGenerator()->create_module('h5pactivity',
76 ['course' => $course, 'enabletracking' => 1, 'grademethod' => manager::GRADEHIGHESTATTEMPT]);
78 $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
80 $manager = manager::create_from_instance($activity);
81 $cm = $manager->get_coursemodule();
83 $user = $student;
84 $params = ['cmid' => $cm->id, 'userid' => $user->id];
85 $generator->create_content($activity, $params);
87 $this->setUser($teacher);
89 $this->expectOutputRegex('/submitted:/');
90 set_config('fullnamedisplay', 'firstname, lastnamephonetic');
91 h5pactivity_print_recent_activity($course, false, time() - 3600);
94 /**
95 * Test that h5pactivity_get_recent_mod_activity fetches the h5pactivity correctly.
97 public function test_h5pactivity_get_recent_mod_activity() {
98 $this->resetAfterTest();
99 $this->setAdminUser();
101 $course = $this->getDataGenerator()->create_course();
102 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
103 $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
104 $activity = $this->getDataGenerator()->create_module('h5pactivity',
105 ['course' => $course, 'enabletracking' => 1, 'grademethod' => manager::GRADEHIGHESTATTEMPT]);
107 $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
109 $manager = manager::create_from_instance($activity);
110 $cm = $manager->get_coursemodule();
112 $user = $student;
113 $params = ['cmid' => $cm->id, 'userid' => $user->id];
114 $generator->create_content($activity, $params);
116 $index = 1;
117 $activities = [
118 $index => (object) [
119 'type' => 'h5pactivity',
120 'cmid' => $cm->id,
124 $this->setUser($teacher);
125 h5pactivity_get_recent_mod_activity($activities, $index, time() - HOURSECS, $course->id, $cm->id);
127 $activity = $activities[1];
128 $this->assertEquals("h5pactivity", $activity->type);
129 $this->assertEquals($student->id, $activity->user->id);
133 * Test that h5pactivity_get_recent_mod_activity fetches activity correctly.
135 public function test_h5pactivity_fetch_recent_activity() {
136 global $DB;
138 $this->resetAfterTest();
139 $this->setAdminUser();
141 $course = $this->getDataGenerator()->create_course();
143 // Create users and groups.
144 $students = array();
145 $groups = array();
146 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
148 for ($i = 1; $i < 4; $i++) {
149 $students[$i] = $this->getDataGenerator()->create_and_enrol($course, 'student');
150 $groups[$i] = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
153 // Update the course set the groupmode SEPARATEGROUPS and forced.
154 update_course((object)array('id' => $course->id, 'groupmode' => SEPARATEGROUPS, 'groupmodeforce' => true));
156 // Student 1 is in groups 1 and 3.
157 groups_add_member($groups[1], $students[1]);
158 groups_add_member($groups[3], $students[1]);
160 // Student 2 is in groups 1 and 2.
161 groups_add_member($groups[1], $students[2]);
162 groups_add_member($groups[2], $students[2]);
164 // Student 3 is only in group 3.
165 groups_add_member($groups[3], $students[3]);
167 // Grader is only in group 3.
168 groups_add_member($groups[3], $teacher);
170 $timestart = time() - 1;
171 // Create h5pactivity.
172 $activity = $this->getDataGenerator()->create_module('h5pactivity',
173 ['course' => $course->id, 'enabletracking' => 1, 'grademethod' => manager::GRADEHIGHESTATTEMPT,
174 'groupmode' => SEPARATEGROUPS]);
176 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
177 $cmcontext = \context_module::instance($activity->cmid);
178 assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, $cmcontext, true);
180 $manager = manager::create_from_instance($activity);
181 $cm = $manager->get_coursemodule();
183 // Create attempts.
184 $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
185 foreach ($students as $student) {
186 $params = ['cmid' => $cm->id, 'userid' => $student->id];
187 $generator->create_content($activity, $params);
190 // Get all attempts.
191 $dbparams = [$timestart, $course->id, 'h5pactivity'];
192 $userfieldsapi = \core_user\fields::for_userpic();
193 $namefields = $userfieldsapi->get_sql('u', false, '', 'userid', false)->selects;;
195 $sql = "SELECT h5pa.id, h5pa.timemodified, cm.id as cmid, $namefields
196 FROM {h5pactivity_attempts} h5pa
197 JOIN {h5pactivity} h5p ON h5p.id = h5pa.h5pactivityid
198 JOIN {course_modules} cm ON cm.instance = h5p.id
199 JOIN {modules} md ON md.id = cm.module
200 JOIN {user} u ON u.id = h5pa.userid
201 WHERE h5pa.timemodified > ?
202 AND h5p.course = ?
203 AND md.name = ?
204 ORDER BY h5pa.timemodified ASC";
206 $submissions = $DB->get_records_sql($sql, $dbparams);
207 $this->assertCount(count($students), $submissions);
209 // Fetch activity for student (only his own).
210 $this->setUser($students[1]);
211 $recentactivity = h5pactivity_fetch_recent_activity($submissions, $course->id);
212 $this->assertCount(1, $recentactivity);
213 $this->assertEquals($students[1]->id, $recentactivity[$students[1]->id]->userid);
215 // Fetch users group info for grader.
216 $this->setUser($teacher);
217 $recentactivity = h5pactivity_fetch_recent_activity($submissions, $course->id);
218 $this->assertCount(2, $recentactivity);
219 // Grader, Student 1 and 3 are in Group 3.
220 $this->assertEquals($students[1]->id, $recentactivity[$students[1]->id]->userid);
221 $this->assertEquals($students[3]->id, $recentactivity[$students[3]->id]->userid);
223 // Grader is in Group 2.
224 groups_remove_member($groups[3], $teacher);
225 groups_add_member($groups[2], $teacher);
226 get_fast_modinfo($course->id, 0, true);
227 $recentactivity = h5pactivity_fetch_recent_activity($submissions, $course->id);
228 $this->assertCount(1, $recentactivity);
229 // Grader, Student 2 are in Group 2.
230 $this->assertEquals($students[2]->id, $recentactivity[$students[2]->id]->userid);
232 // Grader is in Group 1.
233 groups_remove_member($groups[2], $teacher);
234 groups_add_member($groups[1], $teacher);
235 get_fast_modinfo($course->id, 0, true);
236 $recentactivity = h5pactivity_fetch_recent_activity($submissions, $course->id);
237 $this->assertCount(2, $recentactivity);
238 // Grader, Student 1 and 2 are in Group 1.
239 $this->assertEquals($students[1]->id, $recentactivity[$students[1]->id]->userid);
240 $this->assertEquals($students[2]->id, $recentactivity[$students[2]->id]->userid);