Commit | Line | Data |
---|---|---|
e4f6c0c2 ZT |
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 tests for core_tag. | |
19 | * | |
20 | * @package core_comment | |
21 | * @category test | |
22 | * @copyright 2018 Zig Tan <zig@moodle.com> | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | global $CFG; | |
28 | ||
29 | require_once($CFG->dirroot . '/tag/lib.php'); | |
30 | ||
31 | use \core_privacy\tests\provider_testcase; | |
32 | use \core_privacy\local\request\writer; | |
33 | use \core_tag\privacy\provider; | |
34 | ||
35 | /** | |
36 | * Unit tests for tag/classes/privacy/policy | |
37 | * | |
38 | * @copyright 2018 Zig Tan <zig@moodle.com> | |
39 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
40 | */ | |
41 | class core_tag_privacy_testcase extends provider_testcase { | |
42 | ||
43 | /** | |
44 | * Check the exporting of tags for a user id in a context. | |
45 | */ | |
46 | public function test_export_tags() { | |
47 | global $DB; | |
48 | ||
49 | $this->resetAfterTest(true); | |
50 | ||
51 | // Create a user to perform tagging. | |
52 | $user = $this->getDataGenerator()->create_user(); | |
53 | $this->setUser($user); | |
54 | ||
55 | // Create a course to tag. | |
56 | $course = $this->getDataGenerator()->create_course(); | |
57 | $context = context_course::instance($course->id); | |
58 | $subcontext = []; | |
59 | ||
60 | // Create three dummy tags and tag instances. | |
61 | $dummytags = [ 'Tag 1', 'Tag 2', 'Tag 3' ]; | |
62 | core_tag_tag::set_item_tags('core_course', 'course', $course->id, context_course::instance($course->id), | |
63 | $dummytags, $user->id); | |
64 | ||
65 | // Get the tag instances that should have been created. | |
66 | $taginstances = $DB->get_records('tag_instance', array('itemtype' => 'course', 'itemid' => $course->id)); | |
67 | $this->assertCount(count($dummytags), $taginstances); | |
68 | ||
69 | // Check tag instances match the component and context. | |
70 | foreach ($taginstances as $taginstance) { | |
71 | $this->assertEquals('core_course', $taginstance->component); | |
72 | $this->assertEquals(context_course::instance($course->id)->id, $taginstance->contextid); | |
73 | } | |
74 | ||
75 | // Retrieve tags only for this user. | |
76 | provider::export_item_tags($user->id, $context, $subcontext, 'core_course', 'course', $course->id, true); | |
77 | ||
78 | $writer = writer::with_context($context); | |
79 | $this->assertTrue($writer->has_any_data()); | |
80 | ||
81 | $exportedtags = $writer->get_related_data($subcontext, 'tags'); | |
82 | $this->assertCount(count($dummytags), $exportedtags); | |
83 | ||
84 | // Check the exported tag's rawname is found in the initial dummy tags. | |
85 | foreach ($exportedtags as $exportedtag) { | |
2207b0fa | 86 | $this->assertContains($exportedtag, $dummytags); |
e4f6c0c2 ZT |
87 | } |
88 | } | |
1c4b87cb MG |
89 | |
90 | /** | |
91 | * Test method delete_item_tags(). | |
92 | */ | |
93 | public function test_delete_item_tags() { | |
94 | global $DB; | |
95 | ||
96 | $this->resetAfterTest(true); | |
97 | ||
98 | // Create a course to tag. | |
99 | $course1 = $this->getDataGenerator()->create_course(); | |
100 | $context1 = context_course::instance($course1->id); | |
101 | $course2 = $this->getDataGenerator()->create_course(); | |
102 | $context2 = context_course::instance($course2->id); | |
103 | ||
104 | // Tag courses. | |
105 | core_tag_tag::set_item_tags('core_course', 'course', $course1->id, $context1, ['Tag 1', 'Tag 2', 'Tag 3']); | |
106 | core_tag_tag::set_item_tags('core_course', 'course', $course2->id, $context2, ['Tag 1', 'Tag 2']); | |
107 | ||
108 | $expectedtagcount = $DB->count_records('tag_instance'); | |
109 | // Delete tags for course1. | |
110 | core_tag\privacy\provider::delete_item_tags($context1, 'core_course', 'course'); | |
111 | $expectedtagcount -= 3; | |
112 | $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); | |
113 | ||
114 | // Delete tags for course2. Use wrong itemid. | |
115 | core_tag\privacy\provider::delete_item_tags($context2, 'core_course', 'course', $course1->id); | |
116 | $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); | |
117 | ||
118 | // Use correct itemid. | |
119 | core_tag\privacy\provider::delete_item_tags($context2, 'core_course', 'course', $course2->id); | |
120 | $expectedtagcount -= 2; | |
121 | $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); | |
122 | } | |
123 | ||
af2e8ed9 HN |
124 | /** |
125 | * Test method delete_item_tags() with userid. | |
126 | */ | |
127 | public function test_delete_item_tags_with_userid() { | |
128 | global $DB; | |
129 | ||
130 | $this->resetAfterTest(true); | |
131 | // Create a course to tag. | |
132 | $course = $this->getDataGenerator()->create_course(); | |
133 | $context = context_course::instance($course->id); | |
134 | ||
135 | // Create a user to perform tagging. | |
136 | $user = $this->getDataGenerator()->create_user(); | |
137 | $this->setUser($user); | |
138 | ||
139 | // Tag courses. | |
140 | core_tag_tag::set_item_tags('core_course', 'course', $course->id, $context, ['Tag 1', 'Tag 2'], $user->id); | |
141 | $expectedtagcount = $DB->count_records('tag_instance'); | |
142 | ||
143 | // Delete tags for course. Use wrong userid. | |
144 | core_tag\privacy\provider::delete_item_tags($context, 'core_course', 'course', null, 1); | |
145 | $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); | |
146 | ||
147 | $expectedtagcount -= 2; | |
148 | // Delete tags for course. Use correct userid. | |
149 | core_tag\privacy\provider::delete_item_tags($context, 'core_course', 'course', null, $user->id); | |
150 | $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); | |
151 | } | |
152 | ||
1c4b87cb MG |
153 | /** |
154 | * Test method delete_item_tags_select(). | |
155 | */ | |
156 | public function test_delete_item_tags_select() { | |
157 | global $DB; | |
158 | ||
159 | $this->resetAfterTest(true); | |
160 | ||
161 | // Create a course to tag. | |
162 | $course1 = $this->getDataGenerator()->create_course(); | |
163 | $context1 = context_course::instance($course1->id); | |
164 | $course2 = $this->getDataGenerator()->create_course(); | |
165 | $context2 = context_course::instance($course2->id); | |
166 | ||
167 | // Tag courses. | |
168 | core_tag_tag::set_item_tags('core_course', 'course', $course1->id, $context1, ['Tag 1', 'Tag 2', 'Tag 3']); | |
169 | core_tag_tag::set_item_tags('core_course', 'course', $course2->id, $context2, ['Tag 1', 'Tag 2']); | |
170 | ||
171 | $expectedtagcount = $DB->count_records('tag_instance'); | |
172 | // Delete tags for course1. | |
173 | list($sql, $params) = $DB->get_in_or_equal([$course1->id, $course2->id], SQL_PARAMS_NAMED); | |
174 | core_tag\privacy\provider::delete_item_tags_select($context1, 'core_course', 'course', $sql, $params); | |
175 | $expectedtagcount -= 3; | |
176 | $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); | |
177 | ||
178 | // Delete tags for course2. | |
179 | core_tag\privacy\provider::delete_item_tags_select($context2, 'core_course', 'course', $sql, $params); | |
180 | $expectedtagcount -= 2; | |
181 | $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); | |
182 | } | |
2207b0fa MG |
183 | |
184 | protected function set_up_tags() { | |
185 | global $CFG; | |
186 | require_once($CFG->dirroot.'/user/editlib.php'); | |
187 | ||
188 | $this->resetAfterTest(true); | |
189 | ||
190 | $user1 = $this->getDataGenerator()->create_user(); | |
191 | $user2 = $this->getDataGenerator()->create_user(); | |
192 | ||
193 | $this->setUser($user1); | |
194 | useredit_update_interests($user1, ['Birdwatching', 'Computers']); | |
195 | ||
196 | $this->setUser($user2); | |
197 | useredit_update_interests($user2, ['computers']); | |
198 | ||
199 | $this->setAdminUser(); | |
200 | ||
201 | $tag = core_tag_tag::get_by_name(0, 'computers', '*'); | |
202 | $tag->update(['description' => '<img src="@@PLUGINFILE@@/computer.jpg">']); | |
203 | get_file_storage()->create_file_from_string([ | |
204 | 'contextid' => context_system::instance()->id, | |
205 | 'component' => 'tag', | |
206 | 'filearea' => 'description', | |
207 | 'itemid' => $tag->id, | |
208 | 'filepath' => '/', | |
209 | 'filename' => 'computer.jpg' | |
210 | ], "jpg:image"); | |
211 | ||
212 | return [$user1, $user2]; | |
213 | } | |
214 | ||
215 | public function test_export_item_tags() { | |
216 | list($user1, $user2) = $this->set_up_tags(); | |
217 | $this->assertEquals([context_system::instance()->id], | |
218 | provider::get_contexts_for_userid($user1->id)->get_contextids()); | |
219 | $this->assertEmpty(provider::get_contexts_for_userid($user2->id)->get_contextids()); | |
220 | } | |
221 | ||
222 | public function test_delete_data_for_user() { | |
223 | global $DB; | |
224 | list($user1, $user2) = $this->set_up_tags(); | |
225 | $context = context_system::instance(); | |
226 | $this->assertEquals(2, $DB->count_records('tag', [])); | |
227 | $this->assertEquals(0, $DB->count_records('tag', ['userid' => 0])); | |
228 | provider::delete_data_for_user(new \core_privacy\local\request\approved_contextlist($user2, 'core_tag', [$context->id])); | |
229 | $this->assertEquals(2, $DB->count_records('tag', [])); | |
230 | $this->assertEquals(0, $DB->count_records('tag', ['userid' => 0])); | |
231 | provider::delete_data_for_user(new \core_privacy\local\request\approved_contextlist($user1, 'core_tag', [$context->id])); | |
232 | $this->assertEquals(2, $DB->count_records('tag', [])); | |
233 | $this->assertEquals(2, $DB->count_records('tag', ['userid' => 0])); | |
234 | } | |
235 | ||
236 | public function test_delete_data_for_all_users_in_context() { | |
237 | global $DB; | |
238 | $course = $this->getDataGenerator()->create_course(); | |
239 | list($user1, $user2) = $this->set_up_tags(); | |
240 | $this->assertEquals(2, $DB->count_records('tag', [])); | |
241 | $this->assertEquals(3, $DB->count_records('tag_instance', [])); | |
242 | provider::delete_data_for_all_users_in_context(context_course::instance($course->id)); | |
243 | $this->assertEquals(2, $DB->count_records('tag', [])); | |
244 | $this->assertEquals(3, $DB->count_records('tag_instance', [])); | |
245 | provider::delete_data_for_all_users_in_context(context_system::instance()); | |
246 | $this->assertEquals(0, $DB->count_records('tag', [])); | |
247 | $this->assertEquals(0, $DB->count_records('tag_instance', [])); | |
248 | } | |
249 | ||
250 | public function test_export_data_for_user() { | |
251 | global $DB; | |
252 | list($user1, $user2) = $this->set_up_tags(); | |
253 | $context = context_system::instance(); | |
254 | provider::export_user_data(new \core_privacy\local\request\approved_contextlist($user2, 'core_tag', [$context->id])); | |
255 | $this->assertFalse(writer::with_context($context)->has_any_data()); | |
256 | ||
257 | $tagids = array_values(array_map(function($tag) { | |
258 | return $tag->id; | |
259 | }, core_tag_tag::get_by_name_bulk(core_tag_collection::get_default(), ['Birdwatching', 'Computers']))); | |
260 | ||
261 | provider::export_user_data(new \core_privacy\local\request\approved_contextlist($user1, 'core_tag', [$context->id])); | |
262 | $writer = writer::with_context($context); | |
263 | ||
264 | $data = $writer->get_data(['Tags', $tagids[0]]); | |
265 | $files = $writer->get_files(['Tags', $tagids[0]]); | |
266 | $this->assertEquals('Birdwatching', $data->rawname); | |
267 | $this->assertEmpty($files); | |
268 | ||
269 | $data = $writer->get_data(['Tags', $tagids[1]]); | |
270 | $files = $writer->get_files(['Tags', $tagids[1]]); | |
271 | $this->assertEquals('Computers', $data->rawname); | |
272 | $this->assertEquals(['computer.jpg'], array_keys($files)); | |
273 | } | |
e4f6c0c2 | 274 | } |