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 | ||
124 | /** | |
125 | * Test method delete_item_tags_select(). | |
126 | */ | |
127 | public function test_delete_item_tags_select() { | |
128 | global $DB; | |
129 | ||
130 | $this->resetAfterTest(true); | |
131 | ||
132 | // Create a course to tag. | |
133 | $course1 = $this->getDataGenerator()->create_course(); | |
134 | $context1 = context_course::instance($course1->id); | |
135 | $course2 = $this->getDataGenerator()->create_course(); | |
136 | $context2 = context_course::instance($course2->id); | |
137 | ||
138 | // Tag courses. | |
139 | core_tag_tag::set_item_tags('core_course', 'course', $course1->id, $context1, ['Tag 1', 'Tag 2', 'Tag 3']); | |
140 | core_tag_tag::set_item_tags('core_course', 'course', $course2->id, $context2, ['Tag 1', 'Tag 2']); | |
141 | ||
142 | $expectedtagcount = $DB->count_records('tag_instance'); | |
143 | // Delete tags for course1. | |
144 | list($sql, $params) = $DB->get_in_or_equal([$course1->id, $course2->id], SQL_PARAMS_NAMED); | |
145 | core_tag\privacy\provider::delete_item_tags_select($context1, 'core_course', 'course', $sql, $params); | |
146 | $expectedtagcount -= 3; | |
147 | $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); | |
148 | ||
149 | // Delete tags for course2. | |
150 | core_tag\privacy\provider::delete_item_tags_select($context2, 'core_course', 'course', $sql, $params); | |
151 | $expectedtagcount -= 2; | |
152 | $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); | |
153 | } | |
2207b0fa MG |
154 | |
155 | protected function set_up_tags() { | |
156 | global $CFG; | |
157 | require_once($CFG->dirroot.'/user/editlib.php'); | |
158 | ||
159 | $this->resetAfterTest(true); | |
160 | ||
161 | $user1 = $this->getDataGenerator()->create_user(); | |
162 | $user2 = $this->getDataGenerator()->create_user(); | |
163 | ||
164 | $this->setUser($user1); | |
165 | useredit_update_interests($user1, ['Birdwatching', 'Computers']); | |
166 | ||
167 | $this->setUser($user2); | |
168 | useredit_update_interests($user2, ['computers']); | |
169 | ||
170 | $this->setAdminUser(); | |
171 | ||
172 | $tag = core_tag_tag::get_by_name(0, 'computers', '*'); | |
173 | $tag->update(['description' => '<img src="@@PLUGINFILE@@/computer.jpg">']); | |
174 | get_file_storage()->create_file_from_string([ | |
175 | 'contextid' => context_system::instance()->id, | |
176 | 'component' => 'tag', | |
177 | 'filearea' => 'description', | |
178 | 'itemid' => $tag->id, | |
179 | 'filepath' => '/', | |
180 | 'filename' => 'computer.jpg' | |
181 | ], "jpg:image"); | |
182 | ||
183 | return [$user1, $user2]; | |
184 | } | |
185 | ||
186 | public function test_export_item_tags() { | |
187 | list($user1, $user2) = $this->set_up_tags(); | |
188 | $this->assertEquals([context_system::instance()->id], | |
189 | provider::get_contexts_for_userid($user1->id)->get_contextids()); | |
190 | $this->assertEmpty(provider::get_contexts_for_userid($user2->id)->get_contextids()); | |
191 | } | |
192 | ||
193 | public function test_delete_data_for_user() { | |
194 | global $DB; | |
195 | list($user1, $user2) = $this->set_up_tags(); | |
196 | $context = context_system::instance(); | |
197 | $this->assertEquals(2, $DB->count_records('tag', [])); | |
198 | $this->assertEquals(0, $DB->count_records('tag', ['userid' => 0])); | |
199 | provider::delete_data_for_user(new \core_privacy\local\request\approved_contextlist($user2, 'core_tag', [$context->id])); | |
200 | $this->assertEquals(2, $DB->count_records('tag', [])); | |
201 | $this->assertEquals(0, $DB->count_records('tag', ['userid' => 0])); | |
202 | provider::delete_data_for_user(new \core_privacy\local\request\approved_contextlist($user1, 'core_tag', [$context->id])); | |
203 | $this->assertEquals(2, $DB->count_records('tag', [])); | |
204 | $this->assertEquals(2, $DB->count_records('tag', ['userid' => 0])); | |
205 | } | |
206 | ||
207 | public function test_delete_data_for_all_users_in_context() { | |
208 | global $DB; | |
209 | $course = $this->getDataGenerator()->create_course(); | |
210 | list($user1, $user2) = $this->set_up_tags(); | |
211 | $this->assertEquals(2, $DB->count_records('tag', [])); | |
212 | $this->assertEquals(3, $DB->count_records('tag_instance', [])); | |
213 | provider::delete_data_for_all_users_in_context(context_course::instance($course->id)); | |
214 | $this->assertEquals(2, $DB->count_records('tag', [])); | |
215 | $this->assertEquals(3, $DB->count_records('tag_instance', [])); | |
216 | provider::delete_data_for_all_users_in_context(context_system::instance()); | |
217 | $this->assertEquals(0, $DB->count_records('tag', [])); | |
218 | $this->assertEquals(0, $DB->count_records('tag_instance', [])); | |
219 | } | |
220 | ||
221 | public function test_export_data_for_user() { | |
222 | global $DB; | |
223 | list($user1, $user2) = $this->set_up_tags(); | |
224 | $context = context_system::instance(); | |
225 | provider::export_user_data(new \core_privacy\local\request\approved_contextlist($user2, 'core_tag', [$context->id])); | |
226 | $this->assertFalse(writer::with_context($context)->has_any_data()); | |
227 | ||
228 | $tagids = array_values(array_map(function($tag) { | |
229 | return $tag->id; | |
230 | }, core_tag_tag::get_by_name_bulk(core_tag_collection::get_default(), ['Birdwatching', 'Computers']))); | |
231 | ||
232 | provider::export_user_data(new \core_privacy\local\request\approved_contextlist($user1, 'core_tag', [$context->id])); | |
233 | $writer = writer::with_context($context); | |
234 | ||
235 | $data = $writer->get_data(['Tags', $tagids[0]]); | |
236 | $files = $writer->get_files(['Tags', $tagids[0]]); | |
237 | $this->assertEquals('Birdwatching', $data->rawname); | |
238 | $this->assertEmpty($files); | |
239 | ||
240 | $data = $writer->get_data(['Tags', $tagids[1]]); | |
241 | $files = $writer->get_files(['Tags', $tagids[1]]); | |
242 | $this->assertEquals('Computers', $data->rawname); | |
243 | $this->assertEquals(['computer.jpg'], array_keys($files)); | |
244 | } | |
e4f6c0c2 | 245 | } |