1 |
efrain |
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 |
namespace tool_usertours\privacy;
|
|
|
18 |
|
|
|
19 |
use core_privacy\local\metadata\collection;
|
|
|
20 |
use core_privacy\local\request\writer;
|
|
|
21 |
use tool_usertours\tour;
|
|
|
22 |
use tool_usertours\privacy\provider;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Unit tests for the tool_usertours implementation of the privacy API.
|
|
|
26 |
*
|
|
|
27 |
* @package tool_usertours
|
|
|
28 |
* @category test
|
|
|
29 |
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
* @covers \tool_usertours\privacy\provider
|
|
|
32 |
*/
|
|
|
33 |
class provider_test extends \core_privacy\tests\provider_testcase {
|
|
|
34 |
/**
|
|
|
35 |
* Helper method for creating a tour
|
|
|
36 |
*
|
|
|
37 |
* @return tour
|
|
|
38 |
*/
|
|
|
39 |
protected function create_test_tour(): tour {
|
|
|
40 |
return (new tour())
|
|
|
41 |
->set_name('test_tour')
|
|
|
42 |
->set_description('Test tour')
|
|
|
43 |
->set_enabled(true)
|
|
|
44 |
->set_pathmatch('/')
|
|
|
45 |
->persist();
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Ensure that get_metadata exports valid content.
|
|
|
50 |
*/
|
|
|
51 |
public function test_get_metadata(): void {
|
|
|
52 |
$items = new collection('tool_usertours');
|
|
|
53 |
$result = provider::get_metadata($items);
|
|
|
54 |
$this->assertSame($items, $result);
|
|
|
55 |
$this->assertInstanceOf(collection::class, $result);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Ensure that export_user_preferences returns no data if the user has completed no tours.
|
|
|
60 |
*/
|
|
|
61 |
public function test_export_user_preferences_no_pref(): void {
|
|
|
62 |
$user = \core_user::get_user_by_username('admin');
|
|
|
63 |
provider::export_user_preferences($user->id);
|
|
|
64 |
|
|
|
65 |
$writer = writer::with_context(\context_system::instance());
|
|
|
66 |
|
|
|
67 |
$this->assertFalse($writer->has_any_data());
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Ensure that export_user_preferences returns request completion data.
|
|
|
72 |
*/
|
|
|
73 |
public function test_export_user_preferences_completed(): void {
|
|
|
74 |
global $DB;
|
|
|
75 |
|
|
|
76 |
$this->resetAfterTest();
|
|
|
77 |
$this->setAdminUser();
|
|
|
78 |
|
|
|
79 |
$tour = $this->create_test_tour();
|
|
|
80 |
|
|
|
81 |
$user = \core_user::get_user_by_username('admin');
|
|
|
82 |
$tour->mark_user_completed();
|
|
|
83 |
provider::export_user_preferences($user->id);
|
|
|
84 |
|
|
|
85 |
$writer = writer::with_context(\context_system::instance());
|
|
|
86 |
|
|
|
87 |
$this->assertTrue($writer->has_any_data());
|
|
|
88 |
$prefs = $writer->get_user_preferences('tool_usertours');
|
|
|
89 |
|
|
|
90 |
$this->assertCount(1, (array) $prefs);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Ensure that export_user_preferences returns request completion data.
|
|
|
95 |
*/
|
|
|
96 |
public function test_export_user_preferences_requested(): void {
|
|
|
97 |
global $DB;
|
|
|
98 |
|
|
|
99 |
$this->resetAfterTest();
|
|
|
100 |
$this->setAdminUser();
|
|
|
101 |
|
|
|
102 |
$tour = $this->create_test_tour();
|
|
|
103 |
|
|
|
104 |
$user = \core_user::get_user_by_username('admin');
|
|
|
105 |
$tour->mark_user_completed();
|
|
|
106 |
$tour->request_user_reset();
|
|
|
107 |
provider::export_user_preferences($user->id);
|
|
|
108 |
|
|
|
109 |
$writer = writer::with_context(\context_system::instance());
|
|
|
110 |
|
|
|
111 |
$this->assertTrue($writer->has_any_data());
|
|
|
112 |
$prefs = $writer->get_user_preferences('tool_usertours');
|
|
|
113 |
|
|
|
114 |
$this->assertCount(2, (array) $prefs);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Make sure we are exporting preferences for the correct user
|
|
|
119 |
*/
|
|
|
120 |
public function test_export_user_preferences_correct_user(): void {
|
|
|
121 |
$this->resetAfterTest();
|
|
|
122 |
|
|
|
123 |
$tour = $this->create_test_tour();
|
|
|
124 |
|
|
|
125 |
// Create test user, mark them as having completed the tour.
|
|
|
126 |
$user = $this->getDataGenerator()->create_user();
|
|
|
127 |
$this->setUser($user);
|
|
|
128 |
$tour->mark_user_completed();
|
|
|
129 |
|
|
|
130 |
// Switch to admin user, mark them as having reset the tour.
|
|
|
131 |
$this->setAdminUser();
|
|
|
132 |
$tour->request_user_reset();
|
|
|
133 |
|
|
|
134 |
// Export test users preferences.
|
|
|
135 |
provider::export_user_preferences($user->id);
|
|
|
136 |
|
|
|
137 |
$writer = writer::with_context(\context_system::instance());
|
|
|
138 |
$this->assertTrue($writer->has_any_data());
|
|
|
139 |
|
|
|
140 |
$prefs = (array)$writer->get_user_preferences('tool_usertours');
|
|
|
141 |
$this->assertCount(1, $prefs);
|
|
|
142 |
|
|
|
143 |
// We should have received back the "completed tour" preference of the test user.
|
|
|
144 |
$this->assertStringStartsWith(
|
|
|
145 |
'You last marked the "' . $tour->get_name() . '" user tour as completed on',
|
|
|
146 |
reset($prefs)->description
|
|
|
147 |
);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Ensure that export_user_preferences excludes deleted tours.
|
|
|
152 |
*/
|
|
|
153 |
public function test_export_user_preferences_deleted_tour(): void {
|
|
|
154 |
global $DB;
|
|
|
155 |
|
|
|
156 |
$this->resetAfterTest();
|
|
|
157 |
$this->setAdminUser();
|
|
|
158 |
|
|
|
159 |
$tour1 = $this->create_test_tour();
|
|
|
160 |
$tour2 = $this->create_test_tour();
|
|
|
161 |
|
|
|
162 |
$user = \core_user::get_user_by_username('admin');
|
|
|
163 |
|
|
|
164 |
$alltours = $DB->get_records('tool_usertours_tours');
|
|
|
165 |
|
|
|
166 |
$tour1->mark_user_completed();
|
|
|
167 |
|
|
|
168 |
$tour2->mark_user_completed();
|
|
|
169 |
$tour2->remove();
|
|
|
170 |
|
|
|
171 |
$writer = writer::with_context(\context_system::instance());
|
|
|
172 |
|
|
|
173 |
provider::export_user_preferences($user->id);
|
|
|
174 |
$this->assertTrue($writer->has_any_data());
|
|
|
175 |
|
|
|
176 |
// We should have one preference.
|
|
|
177 |
$prefs = (array)$writer->get_user_preferences('tool_usertours');
|
|
|
178 |
$this->assertCount(1, $prefs);
|
|
|
179 |
|
|
|
180 |
// The preference should be related to the first tour.
|
|
|
181 |
$this->assertStringContainsString($tour1->get_name(), reset($prefs)->description);
|
|
|
182 |
}
|
|
|
183 |
}
|