Merge branch 'MDL-66230' of https://github.com/paulholden/moodle
[moodle.git] / admin / tool / usertours / tests / privacy_provider_test.php
CommitLineData
d3c60717
AN
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 * Unit tests for the block_html implementation of the privacy API.
19 *
20 * @package block_html
21 * @category test
22 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28use \core_privacy\local\metadata\collection;
29use \core_privacy\local\request\writer;
30use \core_privacy\local\request\approved_contextlist;
31use \core_privacy\local\request\deletion_criteria;
32use \tool_usertours\tour;
33use \tool_usertours\privacy\provider;
34
35/**
36 * Unit tests for the block_html implementation of the privacy API.
37 *
38 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class tool_usertours_privacy_testcase extends \core_privacy\tests\provider_testcase {
42
43 /**
44 * Ensure that get_metadata exports valid content.
45 */
46 public function test_get_metadata() {
47 $items = new collection('tool_usertours');
48 $result = provider::get_metadata($items);
49 $this->assertSame($items, $result);
50 $this->assertInstanceOf(collection::class, $result);
51 }
52
53 /**
54 * Ensure that export_user_preferences returns no data if the user has completed no tours.
55 */
56 public function test_export_user_preferences_no_pref() {
57 $user = \core_user::get_user_by_username('admin');
58 provider::export_user_preferences($user->id);
59
60 $writer = writer::with_context(\context_system::instance());
61
62 $this->assertFalse($writer->has_any_data());
63 }
64
65 /**
66 * Ensure that export_user_preferences returns request completion data.
67 */
68 public function test_export_user_preferences_completed() {
69 global $DB;
70
71 $this->resetAfterTest();
72 $this->setAdminUser();
73
74 $alltours = $DB->get_records('tool_usertours_tours');
75 $tourdata = reset($alltours);
76
77 $user = \core_user::get_user_by_username('admin');
78 $tour = tour::instance($tourdata->id);
79 $tour->mark_user_completed();
80 provider::export_user_preferences($user->id);
81
82 $writer = writer::with_context(\context_system::instance());
83
84 $this->assertTrue($writer->has_any_data());
85 $prefs = $writer->get_user_preferences('tool_usertours');
86
87 $this->assertCount(1, (array) $prefs);
88 }
89
90 /**
91 * Ensure that export_user_preferences returns request completion data.
92 */
93 public function test_export_user_preferences_requested() {
94 global $DB;
95
96 $this->resetAfterTest();
97 $this->setAdminUser();
98
99 $alltours = $DB->get_records('tool_usertours_tours');
100 $tourdata = reset($alltours);
101
102 $user = \core_user::get_user_by_username('admin');
103 $tour = tour::instance($tourdata->id);
104 $tour->mark_user_completed();
105 $tour->request_user_reset();
106 provider::export_user_preferences($user->id);
107
108 $writer = writer::with_context(\context_system::instance());
109
110 $this->assertTrue($writer->has_any_data());
111 $prefs = $writer->get_user_preferences('tool_usertours');
112
113 $this->assertCount(2, (array) $prefs);
114 }
a1ab8ec2
PH
115
116 /**
117 * Ensure that export_user_preferences excludes deleted tours.
118 */
119 public function test_export_user_preferences_deleted_tour() {
120 global $DB;
121
122 $this->resetAfterTest();
123 $this->setAdminUser();
124
125 $user = \core_user::get_user_by_username('admin');
126
127 $alltours = $DB->get_records('tool_usertours_tours');
128
129 $tour1 = tour::instance(array_shift($alltours)->id);
130 $tour1->mark_user_completed();
131
132 $tour2 = tour::instance(array_shift($alltours)->id);
133 $tour2->mark_user_completed();
134 $tour2->remove();
135
136 $writer = writer::with_context(\context_system::instance());
137
138 provider::export_user_preferences($user->id);
139 $this->assertTrue($writer->has_any_data());
140
141 // We should have one preference.
142 $prefs = $writer->get_user_preferences('tool_usertours');
143 $this->assertCount(1, (array) $prefs);
144
145 // The preference should be related to the first tour.
146 $this->assertContains($tour1->get_name(), reset($prefs)->description);
147 }
d3c60717 148}