2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit Tests for the request helper.
20 * @package core_completion
22 * @copyright 2018 Adrian Greeve <adriangreeve.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
30 require_once($CFG->dirroot
. '/completion/tests/fixtures/completion_creation.php');
33 * Tests for the \core_completion API's provider functionality.
35 * @copyright 2018 Adrian Greeve <adriangreeve.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class core_completion_privacy_test
extends \core_privacy\tests\provider_testcase
{
40 use completion_creation
;
43 * Test joining course completion data to an sql statement.
45 public function test_get_course_completion_join_sql() {
47 $this->resetAfterTest();
48 $user = $this->getDataGenerator()->create_user();
49 $this->create_course_completion();
50 $this->complete_course($user, false);
52 list($join, $where, $params) = \core_completion\privacy\provider
::get_course_completion_join_sql($user->id
, 'comp', 'c.id');
53 $sql = "SELECT DISTINCT c.id
57 $records = $DB->get_records_sql($sql, $params);
58 $data = array_shift($records);
59 $this->assertEquals($this->course
->id
, $data->id
);
63 * Test fetching users' course completion by context and adding to a userlist.
65 public function test_add_course_completion_users_to_userlist() {
66 $this->resetAfterTest();
68 $user1 = $this->getDataGenerator()->create_user();
69 $user2 = $this->getDataGenerator()->create_user();
70 $user3 = $this->getDataGenerator()->create_user();
72 // User1 and user2 complete course.
73 $this->create_course_completion();
74 $this->complete_course($user1);
75 $this->complete_course($user2);
77 // User3 is enrolled but has not completed course.
78 $this->getDataGenerator()->enrol_user($user3->id
, $this->course
->id
, 'student');
80 $userlist = new \core_privacy\local\request\
userlist($this->coursecontext
, 'test');
81 \core_completion\privacy\provider
::add_course_completion_users_to_userlist($userlist);
83 // Ensure only users that have course completion are returned.
84 $expected = [$user1->id
, $user2->id
];
85 $actual = $userlist->get_userids();
88 $this->assertCount(2, $actual);
89 $this->assertEquals($expected, $actual);
93 * Test getting course completion information.
95 public function test_get_course_completion_info() {
96 $this->resetAfterTest();
97 $user = $this->getDataGenerator()->create_user();
98 $this->create_course_completion();
99 $this->complete_course($user);
100 $coursecompletion = \core_completion\privacy\provider
::get_course_completion_info($user, $this->course
);
101 $this->assertEquals('In progress', $coursecompletion['status']);
102 $this->assertCount(2, $coursecompletion['criteria']);
106 * Test getting activity completion information.
108 public function test_get_activity_completion_info() {
109 $this->resetAfterTest();
110 $user = $this->getDataGenerator()->create_user();
111 $this->create_course_completion();
112 $this->complete_course($user);
113 $activitycompletion = \core_completion\privacy\provider
::get_activity_completion_info($user, $this->course
,
115 $this->assertEquals($user->id
, $activitycompletion->userid
);
116 $this->assertEquals($this->cm
->id
, $activitycompletion->coursemoduleid
);
117 $this->assertEquals(1, $activitycompletion->completionstate
);
121 * Test deleting activity completion information for a user.
123 public function test_delete_completion_activity_user() {
124 $this->resetAfterTest();
125 $user = $this->getDataGenerator()->create_user();
126 $this->create_course_completion();
127 $this->complete_course($user);
128 \core_completion\privacy\provider
::delete_completion($user, null, $this->cm
->id
);
129 $activitycompletion = \core_completion\privacy\provider
::get_activity_completion_info($user, $this->course
,
131 $this->assertEquals(0, $activitycompletion->completionstate
);
135 * Test deleting course completion information.
137 public function test_delete_completion_course() {
138 $this->resetAfterTest();
139 $user = $this->getDataGenerator()->create_user();
140 $this->create_course_completion();
141 $this->complete_course($user);
142 \core_completion\privacy\provider
::delete_completion(null, $this->course
->id
);
143 $coursecompletion = \core_completion\privacy\provider
::get_course_completion_info($user, $this->course
);
144 foreach ($coursecompletion['criteria'] as $criterion) {
145 $this->assertEquals('No', $criterion['completed']);
150 * Test deleting course completion information by approved userlist.
152 public function test_delete_completion_by_approved_userlist() {
153 $this->resetAfterTest();
154 $user1 = $this->getDataGenerator()->create_user();
155 $user2 = $this->getDataGenerator()->create_user();
156 $user3 = $this->getDataGenerator()->create_user();
157 $user4 = $this->getDataGenerator()->create_user();
159 $this->create_course_completion();
160 $this->complete_course($user1);
161 $this->complete_course($user2);
162 $this->complete_course($user3);
163 $this->complete_course($user4);
165 // Prepare approved userlist (context/component are irrelevant for this test).
166 $approveduserids = [$user1->id
, $user3->id
];
167 $userlist = new \core_privacy\local\request\approved_userlist
($this->coursecontext
, 'completion', $approveduserids);
169 // Test deleting activity completion information only affects approved userlist.
170 \core_completion\privacy\provider
::delete_completion_by_approved_userlist(
171 $userlist, null, $this->cm
->id
);
172 $activitycompletion1 = \core_completion\privacy\provider
::get_activity_completion_info($user1, $this->course
,
174 $this->assertEquals(0, $activitycompletion1->completionstate
);
175 $activitycompletion2 = \core_completion\privacy\provider
::get_activity_completion_info($user2, $this->course
,
177 $this->assertNotEquals(0, $activitycompletion2->completionstate
);
178 $activitycompletion3 = \core_completion\privacy\provider
::get_activity_completion_info($user3, $this->course
,
180 $this->assertEquals(0, $activitycompletion3->completionstate
);
181 $activitycompletion4 = \core_completion\privacy\provider
::get_activity_completion_info($user4, $this->course
,
183 $this->assertNotEquals(0, $activitycompletion4->completionstate
);
185 // Prepare different approved userlist (context/component are irrelevant for this test).
186 $approveduserids = [$user2->id
, $user4->id
];
187 $userlist = new \core_privacy\local\request\approved_userlist
($this->coursecontext
, 'completion', $approveduserids);
189 // Test deleting course completion information only affects approved userlist.
190 \core_completion\privacy\provider
::delete_completion_by_approved_userlist($userlist, $this->course
->id
);
192 $coursecompletion1 = \core_completion\privacy\provider
::get_course_completion_info($user1, $this->course
);
193 $hasno = array_search('No', $coursecompletion1['criteria'], true);
194 $this->assertFalse($hasno);
195 $coursecompletion2 = \core_completion\privacy\provider
::get_course_completion_info($user2, $this->course
);
196 $hasyes = array_search('Yes', $coursecompletion2['criteria'], true);
197 $this->assertFalse($hasyes);
198 $coursecompletion3 = \core_completion\privacy\provider
::get_course_completion_info($user3, $this->course
);
199 $hasno = array_search('No', $coursecompletion3['criteria'], true);
200 $this->assertFalse($hasno);
201 $coursecompletion4 = \core_completion\privacy\provider
::get_course_completion_info($user4, $this->course
);
202 $hasyes = array_search('Yes', $coursecompletion4['criteria'], true);
203 $this->assertFalse($hasyes);
207 * Test getting course completion information with completion disabled.
209 public function test_get_course_completion_info_completion_disabled() {
210 $this->resetAfterTest();
212 $user = $this->getDataGenerator()->create_user();
214 $course = $this->getDataGenerator()->create_course(['enablecompletion' => 0]);
216 $this->getDataGenerator()->enrol_user($user->id
, $course->id
, 'student');
218 $coursecompletion = \core_completion\privacy\provider
::get_course_completion_info($user, $course);
220 $this->assertTrue(is_array($coursecompletion));
221 $this->assertEmpty($coursecompletion);