MDL-61475 mod_choice: Add implementation of Privacy API
[moodle.git] / mod / choice / tests / privacy_provider_test.php
CommitLineData
138816b1
JP
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/**
18 * Privacy provider tests.
19 *
20 * @package mod_choice
21 * @copyright 2018 Jun Pataleta
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25use core_privacy\metadata\item_collection;
26use core_privacy\request\deletion_criteria;
27use mod_choice\privacy\provider;
28
29defined('MOODLE_INTERNAL') || die();
30
31/**
32 * Privacy provider tests class.
33 *
34 * @package mod_choice
35 * @copyright 2018 Jun Pataleta
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class mod_choice_privacy_provider_testcase extends advanced_testcase {
39 /** @var stdClass The student object. */
40 protected $student;
41
42 /** @var stdClass The choice object. */
43 protected $choice;
44
45 /** @var stdClass The course object. */
46 protected $course;
47
48 /**
49 * @inheritdoc
50 */
51 protected function setUp() {
52 $this->resetAfterTest();
53
54 global $DB;
55 $generator = $this->getDataGenerator();
56 $course = $generator->create_course();
57 $options = ['fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza'];
58 $params = [
59 'course' => $course->id,
60 'option' => $options,
61 'name' => 'First Choice Activity',
62 'showpreview' => 0
63 ];
64
65 $plugingenerator = $generator->get_plugin_generator('mod_choice');
66 // The choice activity the user will answer.
67 $choice = $plugingenerator->create_instance($params);
68 // Create another choice activity.
69 $plugingenerator->create_instance($params);
70 $cm = get_coursemodule_from_instance('choice', $choice->id);
71
72 // Create a student which will make a choice.
73 $student = $generator->create_user();
74 $studentrole = $DB->get_record('role', ['shortname' => 'student']);
75 $generator->enrol_user($student->id, $course->id, $studentrole->id);
76
77 $choicewithoptions = choice_get_choice($choice->id);
78 $optionids = array_keys($choicewithoptions->option);
79
80 choice_user_submit_response($optionids[2], $choice, $student->id, $course, $cm);
81 $this->student = $student;
82 $this->choice = $choice;
83 $this->course = $course;
84 }
85
86 /**
87 * Test for provider::get_metadata().
88 */
89 public function test_get_metadata() {
90 $collection = new item_collection('mod_choice');
91 $newcollection = provider::get_metadata($collection);
92 $itemcollection = $newcollection->get_item_collection();
93 $this->assertCount(1, $itemcollection);
94
95 $table = reset($itemcollection);
96 $this->assertEquals('choice_answers', $table->get_name());
97
98 $privacyfields = $table->get_privacy_fields();
99 $this->assertArrayHasKey('choiceid', $privacyfields);
100 $this->assertArrayHasKey('optionid', $privacyfields);
101 $this->assertArrayHasKey('userid', $privacyfields);
102 $this->assertArrayHasKey('timemodified', $privacyfields);
103
104 $this->assertEquals('privacy:metadata:choice_answers', $table->get_summary());
105 }
106
107 /**
108 * Test for provider::get_contexts_for_userid().
109 */
110 public function test_get_contexts_for_userid() {
111 $cm = get_coursemodule_from_instance('choice', $this->choice->id);
112
113 $contextlist = provider::get_contexts_for_userid($this->student->id);
114 $this->assertCount(1, $contextlist);
115 $contextforuser = $contextlist->current();
116 $cmcontext = context_module::instance($cm->id);
117 $this->assertEquals($cmcontext->id, $contextforuser->id);
118 }
119
120 /**
121 * Test for provider::delete_for_context().
122 */
123 public function test_delete_for_context() {
124 global $DB;
125
126 $choice = $this->choice;
127 $generator = $this->getDataGenerator();
128 $cm = get_coursemodule_from_instance('choice', $this->choice->id);
129
130 // Create another student who will answer the choice activity.
131 $student = $generator->create_user();
132 $studentrole = $DB->get_record('role', ['shortname' => 'student']);
133 $generator->enrol_user($student->id, $this->course->id, $studentrole->id);
134
135 $choicewithoptions = choice_get_choice($choice->id);
136 $optionids = array_keys($choicewithoptions->option);
137
138 choice_user_submit_response($optionids[1], $choice, $student->id, $this->course, $cm);
139
140 // Before deletion, we should have 2 responses.
141 $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id]);
142 $this->assertEquals(2, $count);
143
144 // Delete data based on context.
145 $cmcontext = context_module::instance($cm->id);
146 $criteria = new deletion_criteria($cmcontext);
147 provider::delete_for_context($criteria);
148
149 // After deletion, the choice answers for that choice activity should have been deleted.
150 $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id]);
151 $this->assertEquals(0, $count);
152 }
153
154 /**
155 * Test for provider::delete_user_data().
156 */
157 public function test_delete_user_data() {
158 global $DB;
159
160 $choice = $this->choice;
161 $generator = $this->getDataGenerator();
162 $cm1 = get_coursemodule_from_instance('choice', $this->choice->id);
163
164 // Create a second choice activity.
165 $options = ['Boracay', 'Camiguin', 'Bohol', 'Cebu', 'Coron'];
166 $params = [
167 'course' => $this->course->id,
168 'option' => $options,
169 'name' => 'Which do you think is the best island in the Philippines?',
170 'showpreview' => 0
171 ];
172 $plugingenerator = $generator->get_plugin_generator('mod_choice');
173 $choice2 = $plugingenerator->create_instance($params);
174 $plugingenerator->create_instance($params);
175 $cm2 = get_coursemodule_from_instance('choice', $choice2->id);
176
177 // Make a selection for the first student for the 2nd choice activity.
178 $choicewithoptions = choice_get_choice($choice2->id);
179 $optionids = array_keys($choicewithoptions->option);
180 choice_user_submit_response($optionids[2], $choice2, $this->student->id, $this->course, $cm2);
181
182 // Create another student who will answer the first choice activity.
183 $otherstudent = $generator->create_user();
184 $studentrole = $DB->get_record('role', ['shortname' => 'student']);
185 $generator->enrol_user($otherstudent->id, $this->course->id, $studentrole->id);
186
187 $choicewithoptions = choice_get_choice($choice->id);
188 $optionids = array_keys($choicewithoptions->option);
189
190 choice_user_submit_response($optionids[1], $choice, $otherstudent->id, $this->course, $cm1);
191
192 // Before deletion, we should have 2 responses.
193 $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id]);
194 $this->assertEquals(2, $count);
195
196 $context1 = context_module::instance($cm1->id);
197 $context2 = context_module::instance($cm2->id);
198 $contextlist = new \core_privacy\request\approved_contextlist($this->student, [$context1->id, $context2->id]);
199 provider::delete_user_data($contextlist);
200
201 // After deletion, the choice answers for the first student should have been deleted.
202 $count = $DB->count_records('choice_answers', ['choiceid' => $choice->id, 'userid' => $this->student->id]);
203 $this->assertEquals(0, $count);
204
205 // Confirm that we only have one choice answer available.
206 $choiceanswers = $DB->get_records('choice_answers');
207 $this->assertCount(1, $choiceanswers);
208 $lastresponse = reset($choiceanswers);
209 // And that it's the other student's response.
210 $this->assertEquals($otherstudent->id, $lastresponse->userid);
211 }
212}