1 <?php 2 // This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>. 16 17 namespace core\context; 18 19 use core\context, core\context_helper; 20 21 /** 22 * Unit tests for user context class. 23 * 24 * NOTE: more tests are in lib/tests/accesslib_test.php 25 * 26 * @package core 27 * @copyright Petr Skoda 28 * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 29 * @coversDefaultClass \core\context\user 30 */ 31 class user_test extends \advanced_testcase { 32 /** 33 * Tests legacy class. 34 * @coversNothing 35 */ 36 public function test_legacy_classname() { 37 $admin = get_admin(); 38 $context = \context_user::instance($admin->id); 39 $this->assertInstanceOf(user::class, $context); 40 $this->assertInstanceOf(\context_user::class, $context); 41 } 42 43 /** 44 * Tests covered methods. 45 * @covers ::instance 46 * @covers \core\context::instance_by_id 47 */ 48 public function test_factory_methods() { 49 $admin = get_admin(); 50 $context = user::instance($admin->id); 51 $this->assertInstanceOf(user::class, $context); 52 $this->assertSame($admin->id, $context->instanceid); 53 54 $context = context::instance_by_id($context->id); 55 $this->assertInstanceOf(user::class, $context); 56 $this->assertSame($admin->id, $context->instanceid); 57 } 58 59 /** 60 * Tests covered method. 61 * @covers ::get_short_name 62 */ 63 public function test_get_short_name() { 64 $this->assertSame('user', user::get_short_name()); 65 } 66 67 /** 68 * Tests context level. 69 * @coversNothing 70 */ 71 public function test_level() { 72 $this->assertSame(30, user::LEVEL); 73 $this->assertSame(CONTEXT_USER, user::LEVEL); 74 } 75 76 /** 77 * Tests covered method. 78 * @covers ::get_level_name 79 */ 80 public function test_get_level_name() { 81 $this->assertSame('User', user::get_level_name()); 82 } 83 84 /** 85 * Tests covered method. 86 * @covers ::get_context_name 87 */ 88 public function test_get_context_name() { 89 $admin = get_admin(); 90 $context = user::instance($admin->id); 91 $this->assertSame('User: Admin User', $context->get_context_name()); 92 $this->assertSame('User: Admin User', $context->get_context_name(true)); 93 $this->assertSame('Admin User', $context->get_context_name(false)); 94 $this->assertSame('Admin User', $context->get_context_name(false, true)); 95 $this->assertSame('User: Admin User', $context->get_context_name(true, true, false)); 96 } 97 98 /** 99 * Tests covered method. 100 * @covers ::get_url 101 */ 102 public function test_get_url() { 103 $admin = get_admin(); 104 $context = user::instance($admin->id); 105 $expected = new \moodle_url('/user/profile.php', ['id' => $admin->id]); 106 $url = $context->get_url(); 107 $this->assertInstanceOf(\moodle_url::class, $url); 108 $this->assertSame($expected->out(), $url->out()); 109 } 110 111 /** 112 * Tests covered methods. 113 * @covers ::get_instance_table() 114 * @covers ::get_behat_reference_columns() 115 * @covers \core\context_helper::resolve_behat_reference 116 */ 117 public function test_resolve_behat_reference() { 118 $this->resetAfterTest(); 119 120 $instance = $this->getDataGenerator()->create_user(); 121 $context = context\user::instance($instance->id); 122 123 $result = context_helper::resolve_behat_reference('User', $instance->username); 124 $this->assertSame($context->id, $result->id); 125 126 $result = context_helper::resolve_behat_reference('user', $instance->username); 127 $this->assertSame($context->id, $result->id); 128 129 $result = context_helper::resolve_behat_reference('30', $instance->username); 130 $this->assertSame($context->id, $result->id); 131 132 $result = context_helper::resolve_behat_reference('User', 'dshjkdshjkhjsadjhdsa'); 133 $this->assertNull($result); 134 135 $result = context_helper::resolve_behat_reference('User', ''); 136 $this->assertNull($result); 137 } 138 139 /** 140 * Tests covered method. 141 * @covers ::get_compatible_role_archetypes 142 */ 143 public function test_get_compatible_role_archetypes() { 144 global $DB; 145 146 $allarchetypes = $DB->get_fieldset_select('role', 'DISTINCT archetype', 'archetype IS NOT NULL'); 147 foreach ($allarchetypes as $allarchetype) { 148 $levels = context_helper::get_compatible_levels($allarchetype); 149 $this->assertNotContains(user::LEVEL, $levels, "$allarchetype is not expected to be compatible with context"); 150 } 151 } 152 153 /** 154 * Tests covered method. 155 * @covers ::get_possible_parent_levels 156 */ 157 public function test_get_possible_parent_levels() { 158 $this->assertSame([system::LEVEL], user::get_possible_parent_levels()); 159 } 160 161 /** 162 * Tests covered method. 163 * @covers ::get_capabilities 164 */ 165 public function test_get_capabilities() { 166 $admin = get_admin(); 167 168 $context = user::instance($admin->id); 169 $capabilities = $context->get_capabilities(); 170 $capabilities = convert_to_array($capabilities); 171 $capabilities = array_column($capabilities, 'name'); 172 173 $this->assertContains('moodle/user:viewalldetails', $capabilities); 174 $this->assertContains('moodle/grade:viewall', $capabilities); 175 $this->assertNotContains('moodle/course:view', $capabilities); 176 $this->assertNotContains('moodle/site:config', $capabilities); 177 } 178 179 /** 180 * Tests covered method. 181 * @covers ::create_level_instances 182 */ 183 public function test_create_level_instances() { 184 global $DB; 185 $this->resetAfterTest(); 186 187 $user = $this->getDataGenerator()->create_user(); 188 $usercontext = user::instance($user->id); 189 190 $DB->delete_records('context', ['id' => $usercontext->id]); 191 context_helper::create_instances(user::LEVEL); 192 $record = $DB->get_record('context', ['contextlevel' => user::LEVEL, 'instanceid' => $user->id], '*', MUST_EXIST); 193 } 194 195 /** 196 * Tests covered method. 197 * @covers ::get_child_contexts 198 */ 199 public function test_get_child_contexts() { 200 $admin = get_admin(); 201 202 $context = user::instance($admin->id); 203 $children = $context->get_child_contexts(); 204 $this->assertCount(0, $children); 205 } 206 207 /** 208 * Tests covered method. 209 * @covers ::get_cleanup_sql 210 */ 211 public function test_get_cleanup_sql() { 212 global $DB; 213 $this->resetAfterTest(); 214 215 $user = $this->getDataGenerator()->create_user(); 216 $usercontext = user::instance($user->id); 217 218 $DB->set_field('user', 'deleted', 1, ['id' => $user->id]); 219 220 context_helper::cleanup_instances(); 221 $this->assertFalse($DB->record_exists('context', ['contextlevel' => user::LEVEL, 'instanceid' => $user->id])); 222 } 223 224 /** 225 * Tests covered method. 226 * @covers ::build_paths 227 */ 228 public function test_build_paths() { 229 global $DB; 230 $this->resetAfterTest(); 231 232 $user = $this->getDataGenerator()->create_user(); 233 $usercontext = user::instance($user->id); 234 $syscontext = system::instance(); 235 236 $DB->set_field('context', 'depth', 1, ['id' => $usercontext->id]); 237 $DB->set_field('context', 'path', '/0', ['id' => $usercontext->id]); 238 239 context_helper::build_all_paths(true); 240 241 $record = $DB->get_record('context', ['id' => $usercontext->id]); 242 $this->assertSame('2', $record->depth); 243 $this->assertSame('/' . $syscontext->id . '/' . $record->id, $record->path); 244 } 245 246 /** 247 * Tests covered method. 248 * @covers ::set_locked 249 */ 250 public function test_set_locked() { 251 global $DB; 252 $this->resetAfterTest(); 253 254 $admin = get_admin(); 255 $context = user::instance($admin->id); 256 $this->assertFalse($context->locked); 257 258 $context->set_locked(true); 259 $this->assertTrue($context->locked); 260 $record = $DB->get_record('context', ['id' => $context->id]); 261 $this->assertSame('1', $record->locked); 262 263 $context->set_locked(false); 264 $this->assertFalse($context->locked); 265 $record = $DB->get_record('context', ['id' => $context->id]); 266 $this->assertSame('0', $record->locked); 267 } 268 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body