MDL-62029 core_course: Create privacy files for core_course.
[moodle.git] / course / tests / privacy_test.php
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  * Privacy tests for core_course.
18  *
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
23  */
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
32  *
33  * @copyright  2018 Adrian Greeve <adrian@moodle.com>
34  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
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.
43      */
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);
51     }
53     /**
54      * Test that user data is exported.
55      */
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);
68     }
70     public function test_export_complete_context_data() {
71         $this->resetAfterTest();
72         $user = $this->getDataGenerator()->create_user();
73         $course1 = $this->getDataGenerator()->create_course(['fullname' => 'Course 1', 'shortname' => 'C1']);
74         $context1 = context_course::instance($course1->id);
75         $course2 = $this->getDataGenerator()->create_course(['fullname' => 'Course 2', 'shortname' => 'C2']);
76         $context2 = context_course::instance($course2->id);
77         $course3 = $this->getDataGenerator()->create_course(['fullname' => 'Course 3', 'shortname' => 'C3']);
79         $this->setUser($user);
80         $modforum = $this->getDataGenerator()->create_module('forum', ['course' => $course1->id]);
81         $modresource = $this->getDataGenerator()->create_module('resource', ['course' => $course2->id]);
82         $modpage = $this->getDataGenerator()->create_module('page', ['course' => $course3->id]);
83         $forumcontext = context_module::instance($modforum->cmid);
84         $resourcecontext = context_module::instance($modresource->cmid);
86         $collection = new \core_privacy\local\request\contextlist_collection($user->id);
87         $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'mod_forum', [$forumcontext->id]);
88         $collection->add_contextlist($approvedlist);
89         $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'mod_resource', [$resourcecontext->id]);
90         $collection->add_contextlist($approvedlist);
92         $writer = \core_privacy\local\request\writer::with_context(context_system::instance());
93         \core_course\privacy\provider::export_complete_context_data($collection);
94         $courses = $writer->get_data();
95         print_object($courses);
96         // print_object($writer);
97     }
99     /**
100      * Test deleting all user data for one context.
101      */
102     public function test_delete_data_for_all_users_in_context() {
103         global $DB;
104         $this->resetAfterTest();
105         $user1 = $this->getDataGenerator()->create_user();
106         $user2 = $this->getDataGenerator()->create_user();
107         $this->create_course_completion();
108         $this->complete_course($user1);
109         $this->complete_course($user2);
110         $records = $DB->get_records('course_modules_completion');
111         $this->assertCount(2, $records);
112         $records = $DB->get_records('course_completion_crit_compl');
113         $this->assertCount(2, $records);
114         \core_course\privacy\provider::delete_data_for_all_users_in_context($this->coursecontext);
115         $records = $DB->get_records('course_modules_completion');
116         $this->assertCount(0, $records);
117         $records = $DB->get_records('course_completion_crit_compl');
118         $this->assertCount(0, $records);
119     }
121     /**
122      * Test deleting data for only one user.
123      */
124     public function test_delete_data_for_user() {
125         global $DB;
126         $this->resetAfterTest();
127         $user1 = $this->getDataGenerator()->create_user();
128         $user2 = $this->getDataGenerator()->create_user();
129         $this->create_course_completion();
130         $this->complete_course($user1);
131         $this->complete_course($user2);
132         $records = $DB->get_records('course_modules_completion');
133         $this->assertCount(2, $records);
134         $records = $DB->get_records('course_completion_crit_compl');
135         $this->assertCount(2, $records);
136         $approvedlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_course',
137                 [$this->coursecontext->id]);
138         \core_course\privacy\provider::delete_data_for_user($approvedlist);
139         $records = $DB->get_records('course_modules_completion');
140         $this->assertCount(1, $records);
141         $records = $DB->get_records('course_completion_crit_compl');
142         $this->assertCount(1, $records);
143     }