Merge branch 'MDL-63625-master' of git://github.com/marinaglancy/moodle
[moodle.git] / course / tests / privacy_test.php
blob3f4eceedbe80a91b1f60c048fd0ef195da03bf50
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/>.
16 /**
17 * Privacy tests for core_course.
19 * @package core_course
20 * @category test
21 * @copyright 2018 Adrian Greeve <adrian@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/completion/tests/fixtures/completion_creation.php');
30 /**
31 * Unit tests for course/classes/privacy/policy
33 * @copyright 2018 Adrian Greeve <adrian@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_course_privacy_testcase extends \core_privacy\tests\provider_testcase {
38 use completion_creation;
40 /**
41 * Test getting the appropriate context for the userid. This should only ever
42 * return the user context for the user id supplied.
44 public function test_get_contexts_for_userid() {
45 $this->resetAfterTest();
46 $user = $this->getDataGenerator()->create_user();
47 $this->create_course_completion();
48 $this->complete_course($user);
49 $contextlist = \core_course\privacy\provider::get_contexts_for_userid($user->id);
50 $this->assertEquals($this->coursecontext->id, $contextlist->current()->id);
53 /**
54 * Test that user data is exported.
56 public function test_export_user_data() {
57 $this->resetAfterTest();
58 $user = $this->getDataGenerator()->create_user();
59 $this->create_course_completion();
60 $this->complete_course($user);
61 $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'core_course',
62 [$this->coursecontext->id]);
63 $writer = \core_privacy\local\request\writer::with_context($this->coursecontext);
64 \core_course\privacy\provider::export_user_data($approvedlist);
65 $completiondata = $writer->get_data([get_string('privacy:completionpath', 'course')]);
66 $this->assertEquals('In progress', $completiondata->status);
67 $this->assertCount(2, $completiondata->criteria);
70 /**
71 * Verify that if a module context is included in the contextlist_collection and its parent course is not, the
72 * export_context_data() call picks this up, and that the contextual course information is included.
74 public function test_export_context_data_module_context_only() {
75 $this->resetAfterTest();
77 // Create a course and a single module.
78 $course1 = $this->getDataGenerator()->create_course(['fullname' => 'Course 1', 'shortname' => 'C1']);
79 $context1 = context_course::instance($course1->id);
80 $modassign = $this->getDataGenerator()->create_module('assign', ['course' => $course1->id, 'name' => 'assign test 1']);
81 $assigncontext = context_module::instance($modassign->cmid);
83 // Now, let's assume during user info export, only the coursemodule context is returned in the contextlist_collection.
84 $user = $this->getDataGenerator()->create_user();
85 $collection = new \core_privacy\local\request\contextlist_collection($user->id);
86 $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'mod_assign', [$assigncontext->id]);
87 $collection->add_contextlist($approvedlist);
89 // Now, verify that core_course will detect this, and add relevant contextual information.
90 \core_course\privacy\provider::export_context_data($collection);
91 $writer = \core_privacy\local\request\writer::with_context($context1);
92 $this->assertTrue($writer->has_any_data());
93 $writerdata = $writer->get_data();
94 $this->assertObjectHasAttribute('fullname', $writerdata);
95 $this->assertObjectHasAttribute('shortname', $writerdata);
96 $this->assertObjectHasAttribute('idnumber', $writerdata);
97 $this->assertObjectHasAttribute('summary', $writerdata);
101 * Verify that if a module context and its parent course context are both included in the contextlist_collection, that course
102 * contextual information is present in the export.
104 public function test_export_context_data_course_and_module_contexts() {
105 $this->resetAfterTest();
107 // Create a course and a single module.
108 $course1 = $this->getDataGenerator()->create_course(['fullname' => 'Course 1', 'shortname' => 'C1', 'format' => 'site']);
109 $context1 = context_course::instance($course1->id);
110 $modassign = $this->getDataGenerator()->create_module('assign', ['course' => $course1->id, 'name' => 'assign test 1']);
111 $assigncontext = context_module::instance($modassign->cmid);
113 // Now, assume during user info export, that both module and course contexts are returned in the contextlist_collection.
114 $user = $this->getDataGenerator()->create_user();
115 $collection = new \core_privacy\local\request\contextlist_collection($user->id);
116 $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'mod_assign', [$assigncontext->id]);
117 $approvedlist2 = new \core_privacy\local\request\approved_contextlist($user, 'core_course', [$context1->id]);
118 $collection->add_contextlist($approvedlist);
119 $collection->add_contextlist($approvedlist2);
121 // Now, verify that core_course still adds relevant contextual information, even for courses which are explicitly listed in
122 // the contextlist_collection.
123 \core_course\privacy\provider::export_context_data($collection);
124 $writer = \core_privacy\local\request\writer::with_context($context1);
125 $this->assertTrue($writer->has_any_data());
126 $writerdata = $writer->get_data();
127 $this->assertObjectHasAttribute('fullname', $writerdata);
128 $this->assertObjectHasAttribute('shortname', $writerdata);
129 $this->assertObjectHasAttribute('idnumber', $writerdata);
130 $this->assertObjectHasAttribute('summary', $writerdata);
134 * Test deleting all user data for one context.
136 public function test_delete_data_for_all_users_in_context() {
137 global $DB;
138 $this->resetAfterTest();
139 $user1 = $this->getDataGenerator()->create_user();
140 $user2 = $this->getDataGenerator()->create_user();
141 $this->create_course_completion();
142 $this->complete_course($user1);
143 $this->complete_course($user2);
144 $records = $DB->get_records('course_modules_completion');
145 $this->assertCount(2, $records);
146 $records = $DB->get_records('course_completion_crit_compl');
147 $this->assertCount(2, $records);
148 \core_course\privacy\provider::delete_data_for_all_users_in_context($this->coursecontext);
149 $records = $DB->get_records('course_modules_completion');
150 $this->assertCount(0, $records);
151 $records = $DB->get_records('course_completion_crit_compl');
152 $this->assertCount(0, $records);
156 * Test deleting data for only one user.
158 public function test_delete_data_for_user() {
159 global $DB;
160 $this->resetAfterTest();
161 $user1 = $this->getDataGenerator()->create_user();
162 $user2 = $this->getDataGenerator()->create_user();
163 $this->create_course_completion();
164 $this->complete_course($user1);
165 $this->complete_course($user2);
166 $records = $DB->get_records('course_modules_completion');
167 $this->assertCount(2, $records);
168 $records = $DB->get_records('course_completion_crit_compl');
169 $this->assertCount(2, $records);
170 $approvedlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_course',
171 [$this->coursecontext->id]);
172 \core_course\privacy\provider::delete_data_for_user($approvedlist);
173 $records = $DB->get_records('course_modules_completion');
174 $this->assertCount(1, $records);
175 $records = $DB->get_records('course_completion_crit_compl');
176 $this->assertCount(1, $records);