Merge branch 'MDL-73883-master' of https://github.com/andrewnicols/moodle
[moodle.git] / lib / tests / customcontext_test.php
blob8a3077b3098e4d7e717b3cab6aada4364bb73a78
1 <?php
2 // This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
17 /**
18 * Code quality unit tests that are fast enough to run each time.
20 * @package core
21 * @category phpunit
22 * @copyright (C) 2013 onwards Remote Learner.net Inc (http://www.remote-learner.net)
23 * @author Brent Boghosian (brent.boghosian@remote-learner.net)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Bogus custom context class for testing
32 class context_bogus1 extends context {
33 /**
34 * Returns the most relevant URL for this context.
36 * @return moodle_url
38 public function get_url() {
39 global $ME;
40 return $ME;
43 /**
44 * Returns array of relevant context capability records.
46 * @return array
48 public function get_capabilities(string $sort = self::DEFAULT_CAPABILITY_SORT) {
49 return array();
53 /**
54 * Bogus custom context class for testing
56 class context_bogus2 extends context {
57 /**
58 * Returns the most relevant URL for this context.
60 * @return moodle_url
62 public function get_url() {
63 global $ME;
64 return $ME;
67 /**
68 * Returns array of relevant context capability records.
70 * @return array
72 public function get_capabilities(string $sort = self::DEFAULT_CAPABILITY_SORT) {
73 return array();
77 /**
78 * Bogus custom context class for testing
80 class context_bogus3 extends context {
81 /**
82 * Returns the most relevant URL for this context.
84 * @return moodle_url
86 public function get_url() {
87 global $ME;
88 return $ME;
91 /**
92 * Returns array of relevant context capability records.
94 * @return array
96 public function get_capabilities(string $sort = self::DEFAULT_CAPABILITY_SORT) {
97 return array();
101 class customcontext_testcase extends advanced_testcase {
104 * Perform setup before every test. This tells Moodle's phpunit to reset the database after every test.
106 protected function setUp(): void {
107 parent::setUp();
108 $this->resetAfterTest(true);
112 * Test case for custom context classes
114 public function test_customcontexts() {
115 global $CFG;
116 static $customcontexts = array(
117 11 => 'context_bogus1',
118 12 => 'context_bogus2',
119 13 => 'context_bogus3'
122 // save any existing custom contexts
123 $existingcustomcontexts = get_config(null, 'custom_context_classes');
125 set_config('custom_context_classes', serialize($customcontexts));
126 initialise_cfg();
127 context_helper::reset_levels();
128 $alllevels = context_helper::get_all_levels();
129 $this->assertEquals($alllevels[11], 'context_bogus1');
130 $this->assertEquals($alllevels[12], 'context_bogus2');
131 $this->assertEquals($alllevels[13], 'context_bogus3');
133 // clean-up & restore any custom contexts
134 set_config('custom_context_classes', ($existingcustomcontexts === false) ? null : $existingcustomcontexts);
135 initialise_cfg();
136 context_helper::reset_levels();