ccbda47ae9524c11482c2966b7b5b5cb08945e89
[moodle.git] / tag / 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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
29 require_once($CFG->dirroot . '/tag/lib.php');
31 use \core_privacy\tests\provider_testcase;
32 use \core_privacy\local\request\writer;
33 use \core_tag\privacy\provider;
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 {
43     /**
44      * Check the exporting of tags for a user id in a context.
45      */
46     public function test_export_tags() {
47         global $DB;
49         $this->resetAfterTest(true);
51         // Create a user to perform tagging.
52         $user = $this->getDataGenerator()->create_user();
53         $this->setUser($user);
55         // Create a course to tag.
56         $course = $this->getDataGenerator()->create_course();
57         $context = context_course::instance($course->id);
58         $subcontext = [];
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);
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);
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         }
75         // Retrieve tags only for this user.
76         provider::export_item_tags($user->id, $context, $subcontext, 'core_course', 'course', $course->id, true);
78         $writer = writer::with_context($context);
79         $this->assertTrue($writer->has_any_data());
81         $exportedtags = $writer->get_related_data($subcontext, 'tags');
82         $this->assertCount(count($dummytags), $exportedtags);
84         // Check the exported tag's rawname is found in the initial dummy tags.
85         foreach ($exportedtags as $exportedtag) {
86             $this->assertContains($exportedtag, $dummytags);
87         }
88     }
90     /**
91      * Test method delete_item_tags().
92      */
93     public function test_delete_item_tags() {
94         global $DB;
96         $this->resetAfterTest(true);
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);
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']);
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'));
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'));
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     }
124     /**
125      * Test method delete_item_tags_select().
126      */
127     public function test_delete_item_tags_select() {
128         global $DB;
130         $this->resetAfterTest(true);
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);
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']);
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'));
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     }
155     protected function set_up_tags() {
156         global $CFG;
157         require_once($CFG->dirroot.'/user/editlib.php');
159         $this->resetAfterTest(true);
161         $user1 = $this->getDataGenerator()->create_user();
162         $user2 = $this->getDataGenerator()->create_user();
164         $this->setUser($user1);
165         useredit_update_interests($user1, ['Birdwatching', 'Computers']);
167         $this->setUser($user2);
168         useredit_update_interests($user2, ['computers']);
170         $this->setAdminUser();
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");
183         return [$user1, $user2];
184     }
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     }
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     }
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     }
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());
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'])));
232         provider::export_user_data(new \core_privacy\local\request\approved_contextlist($user1, 'core_tag', [$context->id]));
233         $writer = writer::with_context($context);
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);
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     }