2 // This file is part of Moodle - https://moodle.org/
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.
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.
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/>.
20 * Unit tests for base context class.
22 * NOTE: more tests are in lib/tests/accesslib_test.php
25 * @copyright Petr Skoda
26 * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 * @coversDefaultClass \core\context
29 class context_test
extends \advanced_testcase
{
31 * Tests legacy class name.
34 public function test_legacy_classname() {
35 $this->assertSame('core\context', context
::class);
37 $context = \context_system
::instance();
38 $this->assertInstanceOf(context
::class, $context);
39 $this->assertInstanceOf('context', $context);
43 * Tests covered method.
44 * @covers ::instance_by_id
46 public function test_factory_methods() {
47 $context = context
::instance_by_id(SYSCONTEXTID
);
48 $this->assertSame('core\\context\\system', get_class($context));
52 * Tests covered methods.
56 public function test_propery_change_protection() {
57 $context = context\system
::instance();
59 $context->contextlevel
= -10;
60 $this->assertEquals($context::LEVEL
, $context->contextlevel
);
61 $this->assertDebuggingCalled('Can not change context instance properties!');
63 $context->instanceid
= -10;
64 $this->assertEquals(0, $context->instanceid
);
65 $this->assertDebuggingCalled('Can not change context instance properties!');
68 $this->assertDebuggingCalled('Can not change context instance properties!');
70 $context->locked
= -10;
71 $this->assertDebuggingCalled('Can not change context instance properties!');
73 unset($context->contextlevel
);
74 $this->assertEquals($context::LEVEL
, $context->contextlevel
);
75 $this->assertDebuggingCalled('Can not unset context instance properties!');
79 * Tests covered method.
82 public function test_incorrect_property() {
83 $context = context\system
::instance();
85 $a = $context->whatever
;
86 $this->assertDebuggingCalled('Invalid context property accessed! whatever');
90 * Tests covered method.
91 * @covers ::getIterator
93 public function test_iterator() {
94 $context = context\system
::instance();
95 $array = iterator_to_array($context->getIterator());
98 'contextlevel' => $context->contextlevel
,
99 'instanceid' => $context->instanceid
,
100 'path' => $context->path
,
101 'depth' => $context->depth
,
104 $this->assertSame($expected, $array);