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) { | |
86 | $this->assertContains($exportedtag->rawname, $dummytags); | |
87 | } | |
88 | } | |
89 | } |