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