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 system 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\system 30 */ 31 class system_test extends \advanced_testcase { 32 /** 33 * Tests legacy class. 34 * @coversNothing 35 */ 36 public function test_legacy_classname() { 37 $context = \context_system::instance(); 38 $this->assertInstanceOf(system::class, $context); 39 $this->assertInstanceOf(\context_system::class, $context); 40 } 41 42 /** 43 * Tests covered methods. 44 * @covers ::instance 45 * @covers \core\context::instance_by_id 46 */ 47 public function test_factory_methods() { 48 $context = system::instance(); 49 $this->assertInstanceOf(system::class, $context); 50 $this->assertEquals(SYSCONTEXTID, $context->id); 51 52 $context = context::instance_by_id($context->id); 53 $this->assertInstanceOf(system::class, $context); 54 $this->assertEquals(SYSCONTEXTID, $context->id); 55 } 56 57 /** 58 * Tests covered method. 59 * @covers ::get_short_name 60 */ 61 public function test_get_short_name() { 62 $this->assertSame('system', system::get_short_name()); 63 } 64 65 /** 66 * Tests context level. 67 * @coversNothing 68 */ 69 public function test_level() { 70 $this->assertSame(10, system::LEVEL); 71 $this->assertSame(CONTEXT_SYSTEM, system::LEVEL); 72 } 73 74 /** 75 * Tests covered method. 76 * @covers ::get_level_name 77 */ 78 public function test_get_level_name() { 79 $this->assertSame('System', system::get_level_name()); 80 } 81 82 /** 83 * Tests covered method. 84 * @covers ::get_context_name 85 */ 86 public function test_get_context_name() { 87 $context = system::instance(); 88 $this->assertSame('System', $context->get_context_name()); 89 $this->assertSame('System', $context->get_context_name(true)); 90 $this->assertSame('System', $context->get_context_name(false)); 91 $this->assertSame('System', $context->get_context_name(false, true)); 92 $this->assertSame('System', $context->get_context_name(true, true, false)); 93 } 94 95 /** 96 * Tests covered method. 97 * @covers ::get_url 98 */ 99 public function test_get_url() { 100 $context = system::instance(); 101 $expected = new \moodle_url('/'); 102 $url = $context->get_url(); 103 $this->assertInstanceOf(\moodle_url::class, $url); 104 $this->assertSame($expected->out(), $url->out()); 105 } 106 107 /** 108 * Tests covered method. 109 * @covers \core\context_helper::resolve_behat_reference 110 */ 111 public function test_resolve_behat_reference() { 112 $syscontext = context\system::instance(); 113 114 $result = context_helper::resolve_behat_reference('System', ''); 115 $this->assertSame($syscontext->id, $result->id); 116 117 $result = context_helper::resolve_behat_reference('System', '44'); 118 $this->assertSame($syscontext->id, $result->id); 119 120 $result = context_helper::resolve_behat_reference('system', ''); 121 $this->assertSame($syscontext->id, $result->id); 122 123 $result = context_helper::resolve_behat_reference('10', ''); 124 $this->assertSame($syscontext->id, $result->id); 125 } 126 127 /** 128 * Tests covered method. 129 * @covers ::get_compatible_role_archetypes 130 */ 131 public function test_get_compatible_role_archetypes() { 132 global $DB; 133 134 $allarchetypes = $DB->get_fieldset_select('role', 'DISTINCT archetype', 'archetype IS NOT NULL'); 135 foreach ($allarchetypes as $allarchetype) { 136 $levels = context_helper::get_compatible_levels($allarchetype); 137 if ($allarchetype === 'manager' || $allarchetype === 'coursecreator') { 138 $this->assertContains(system::LEVEL, $levels, "$allarchetype is expected to be compatible with context"); 139 } else { 140 $this->assertNotContains(system::LEVEL, $levels, "$allarchetype is not expected to be compatible with context"); 141 } 142 } 143 } 144 145 /** 146 * Tests covered method. 147 * @covers ::get_possible_parent_levels 148 */ 149 public function test_get_possible_parent_levels() { 150 $this->assertSame([], system::get_possible_parent_levels()); 151 } 152 153 /** 154 * Tests covered method. 155 * @covers ::get_capabilities 156 */ 157 public function test_get_capabilities() { 158 global $DB; 159 160 $context = system::instance(); 161 $capabilities = $context->get_capabilities(); 162 $expected = $DB->count_records('capabilities', []); 163 $this->assertCount($expected, $capabilities); 164 } 165 166 /** 167 * Tests covered method. 168 * @covers ::create_level_instances 169 */ 170 public function test_create_level_instances() { 171 context_helper::create_instances(system::LEVEL); 172 } 173 174 /** 175 * Tests covered method. 176 * @covers ::get_child_contexts 177 */ 178 public function test_get_child_contexts() { 179 global $DB; 180 181 $context = system::instance(); 182 $children = $context->get_child_contexts(); 183 $expected = $DB->count_records('context', []) - 1; 184 $this->assertCount($expected, $children); 185 $this->assertDebuggingCalled('Fetching of system context child courses is strongly ' 186 . 'discouraged on production servers (it may eat all available memory)!'); 187 } 188 189 /** 190 * Tests covered method. 191 * @covers ::get_cleanup_sql 192 */ 193 public function test_get_cleanup_sql() { 194 // Nothing to clean up actually. 195 context_helper::cleanup_instances(); 196 } 197 198 /** 199 * Tests covered method. 200 * @covers ::build_paths 201 */ 202 public function test_build_paths() { 203 global $DB; 204 $this->resetAfterTest(); 205 206 $DB->set_field('context', 'depth', 2, ['id' => SYSCONTEXTID]); 207 $DB->set_field('context', 'path', '/0', ['id' => SYSCONTEXTID]); 208 209 context_helper::build_all_paths(true); 210 211 $record = $DB->get_record('context', ['id' => SYSCONTEXTID]); 212 $this->assertSame('1', $record->depth); 213 $this->assertSame('/' . $record->id, $record->path); 214 } 215 216 /** 217 * Tests covered method. 218 * @covers ::set_locked 219 */ 220 public function test_set_locked() { 221 $context = system::instance(); 222 223 $context->set_locked(false); 224 225 try { 226 $context->set_locked(true); 227 } catch (\moodle_exception $e) { 228 $this->assertInstanceOf(\coding_exception::class, $e); 229 $this->assertSame('Coding error detected, it must be fixed by a programmer: ' 230 . 'It is not possible to lock the system context', $e->getMessage()); 231 } 232 } 233 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body