Merge branch 'MDL-66230' of https://github.com/paulholden/moodle
[moodle.git] / admin / tool / usertours / tests / privacy_provider_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  * 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  */
26 defined('MOODLE_INTERNAL') || die();
28 use \core_privacy\local\metadata\collection;
29 use \core_privacy\local\request\writer;
30 use \core_privacy\local\request\approved_contextlist;
31 use \core_privacy\local\request\deletion_criteria;
32 use \tool_usertours\tour;
33 use \tool_usertours\privacy\provider;
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  */
41 class tool_usertours_privacy_testcase extends \core_privacy\tests\provider_testcase {
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     }
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);
60         $writer = writer::with_context(\context_system::instance());
62         $this->assertFalse($writer->has_any_data());
63     }
65     /**
66      * Ensure that export_user_preferences returns request completion data.
67      */
68     public function test_export_user_preferences_completed() {
69         global $DB;
71         $this->resetAfterTest();
72         $this->setAdminUser();
74         $alltours = $DB->get_records('tool_usertours_tours');
75         $tourdata = reset($alltours);
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);
82         $writer = writer::with_context(\context_system::instance());
84         $this->assertTrue($writer->has_any_data());
85         $prefs = $writer->get_user_preferences('tool_usertours');
87         $this->assertCount(1, (array) $prefs);
88     }
90     /**
91      * Ensure that export_user_preferences returns request completion data.
92      */
93     public function test_export_user_preferences_requested() {
94         global $DB;
96         $this->resetAfterTest();
97         $this->setAdminUser();
99         $alltours = $DB->get_records('tool_usertours_tours');
100         $tourdata = reset($alltours);
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);
108         $writer = writer::with_context(\context_system::instance());
110         $this->assertTrue($writer->has_any_data());
111         $prefs = $writer->get_user_preferences('tool_usertours');
113         $this->assertCount(2, (array) $prefs);
114     }
116     /**
117      * Ensure that export_user_preferences excludes deleted tours.
118      */
119     public function test_export_user_preferences_deleted_tour() {
120         global $DB;
122         $this->resetAfterTest();
123         $this->setAdminUser();
125         $user = \core_user::get_user_by_username('admin');
127         $alltours = $DB->get_records('tool_usertours_tours');
129         $tour1 = tour::instance(array_shift($alltours)->id);
130         $tour1->mark_user_completed();
132         $tour2 = tour::instance(array_shift($alltours)->id);
133         $tour2->mark_user_completed();
134         $tour2->remove();
136         $writer = writer::with_context(\context_system::instance());
138         provider::export_user_preferences($user->id);
139         $this->assertTrue($writer->has_any_data());
141         // We should have one preference.
142         $prefs = $writer->get_user_preferences('tool_usertours');
143         $this->assertCount(1, (array) $prefs);
145         // The preference should be related to the first tour.
146         $this->assertContains($tour1->get_name(), reset($prefs)->description);
147     }