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; 18 19 /** 20 * Unit tests for base context class. 21 * 22 * NOTE: more tests are in lib/tests/accesslib_test.php 23 * 24 * @package core 25 * @copyright Petr Skoda 26 * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 * @coversDefaultClass \core\context 28 */ 29 class context_test extends \advanced_testcase { 30 /** 31 * Tests legacy class name. 32 * @coversNothing 33 */ 34 public function test_legacy_classname() { 35 $this->assertSame('core\context', context::class); 36 37 $context = \context_system::instance(); 38 $this->assertInstanceOf(context::class, $context); 39 $this->assertInstanceOf('context', $context); 40 } 41 42 /** 43 * Tests covered method. 44 * @covers ::instance_by_id 45 */ 46 public function test_factory_methods() { 47 $context = context::instance_by_id(SYSCONTEXTID); 48 $this->assertSame('core\\context\\system', get_class($context)); 49 } 50 51 /** 52 * Tests covered methods. 53 * @covers ::__set 54 * @covers ::__unset 55 */ 56 public function test_propery_change_protection() { 57 $context = context\system::instance(); 58 59 $context->contextlevel = -10; 60 $this->assertEquals($context::LEVEL, $context->contextlevel); 61 $this->assertDebuggingCalled('Can not change context instance properties!'); 62 63 $context->instanceid = -10; 64 $this->assertEquals(0, $context->instanceid); 65 $this->assertDebuggingCalled('Can not change context instance properties!'); 66 67 $context->id = -10; 68 $this->assertDebuggingCalled('Can not change context instance properties!'); 69 70 $context->locked = -10; 71 $this->assertDebuggingCalled('Can not change context instance properties!'); 72 73 unset($context->contextlevel); 74 $this->assertEquals($context::LEVEL, $context->contextlevel); 75 $this->assertDebuggingCalled('Can not unset context instance properties!'); 76 } 77 78 /** 79 * Tests covered method. 80 * @covers ::__get 81 */ 82 public function test_incorrect_property() { 83 $context = context\system::instance(); 84 85 $a = $context->whatever; 86 $this->assertDebuggingCalled('Invalid context property accessed! whatever'); 87 } 88 89 /** 90 * Tests covered method. 91 * @covers ::getIterator 92 */ 93 public function test_iterator() { 94 $context = context\system::instance(); 95 $array = iterator_to_array($context->getIterator()); 96 $expected = [ 97 'id' => $context->id, 98 'contextlevel' => $context->contextlevel, 99 'instanceid' => $context->instanceid, 100 'path' => $context->path, 101 'depth' => $context->depth, 102 'locked' => false, 103 ]; 104 $this->assertSame($expected, $array); 105 } 106 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body