MDL-79059 core: Use full name as alt text for user picture links
[moodle.git] / lib / tests / context_test.php
blob3fffc43cb011f8b2df8c81e9707e88e45aacf079
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.
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/>.
17 namespace core;
19 /**
20 * Unit tests for base context class.
22 * NOTE: more tests are in lib/tests/accesslib_test.php
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
29 class context_test extends \advanced_testcase {
30 /**
31 * Tests legacy class name.
32 * @coversNothing
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);
42 /**
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));
51 /**
52 * Tests covered methods.
53 * @covers ::__set
54 * @covers ::__unset
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!');
67 $context->id = -10;
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!');
78 /**
79 * Tests covered method.
80 * @covers ::__get
82 public function test_incorrect_property() {
83 $context = context\system::instance();
85 $a = $context->whatever;
86 $this->assertDebuggingCalled('Invalid context property accessed! whatever');
89 /**
90 * Tests covered method.
91 * @covers ::getIterator
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,
104 $this->assertSame($expected, $array);