2 // This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
20 * Tests for \core\deprecated and \core\deprecation.
24 * @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * @covers \core\deprecated
27 * @covers \core\deprecation
29 class deprecation_test
extends \advanced_testcase
{
31 * @dataProvider emit_provider
33 public function test_emit(
35 bool $expectdebugging,
36 bool $expectexception,
38 if ($expectexception) {
39 $this->expectException(\coding_exception
::class);
42 $attribute = new deprecated(
47 deprecation
::emit_deprecation_notice($attribute);
49 if ($expectdebugging) {
50 $this->assertdebuggingcalledcount(1);
54 public static function emit_provider(): array {
92 * @dataProvider get_deprecation_string_provider
94 public function test_get_deprecation_string(
102 $attribute = new deprecated(
103 descriptor
: $descriptor,
106 replacement
: $replacement,
112 deprecation
::get_deprecation_string($attribute),
115 deprecation
::emit_deprecation_notice($attribute);
116 $this->assertDebuggingCalled($expected);
119 public static function get_deprecation_string_provider(): array {
127 'Deprecation: Test description has been deprecated.',
135 'Deprecation: Test description has been deprecated since 4.1.',
143 'Deprecation: Test description has been deprecated. Test reason.',
151 'Deprecation: Test description has been deprecated. Use Test replacement instead.',
158 'https://docs.moodle.org/311/en/Deprecated',
159 'Deprecation: Test description has been deprecated. See https://docs.moodle.org/311/en/Deprecated for more information.',
166 'https://docs.moodle.org/311/en/Deprecated',
167 'Deprecation: Test description has been deprecated since 4.1. Test reason. Use Test replacement instead. See https://docs.moodle.org/311/en/Deprecated for more information.',
173 * @dataProvider from_provider
175 public function test_from($reference, bool $isdeprecated): void
{
176 $attribute = deprecation
::from($reference);
178 $this->assertInstanceOf(deprecated
::class, $attribute);
179 $this->assertTrue(deprecation
::is_deprecated($reference));
180 $this->assertDebuggingNotCalled();
182 deprecation
::emit_deprecation_if_present($reference);
183 $this->assertDebuggingCalled(deprecation
::get_deprecation_string($attribute));
185 $this->assertNull($attribute);
186 $this->assertFalse(deprecation
::is_deprecated($reference));
187 deprecation
::emit_deprecation_if_present($reference);
188 $this->assertDebuggingNotCalled();
192 public function test_from_object(): void
{
193 require_once(dirname(__FILE__
) . '/fixtures/deprecated_fixtures.php');
195 $this->assertNull(deprecation
::from(new \core\fixtures\not_deprecated_class
()));
196 $this->assertInstanceOf(deprecated
::class, deprecation
::from([new \core\fixtures\
deprecated_class()]));
199 public static function from_provider(): array {
200 require_once(dirname(__FILE__
) . '/fixtures/deprecated_fixtures.php');
203 [\core\fixtures\deprecated_class
::class, true],
204 [[\core\fixtures\deprecated_class
::class], true],
205 [\core\fixtures\not_deprecated_class
::class, false],
206 [[\core\fixtures\not_deprecated_class
::class], false],
209 [\core\fixtures\deprecated_class
::class . '::deprecatedproperty', true],
210 [[\core\fixtures\deprecated_class
::class, 'deprecatedproperty'], true],
212 [\core\fixtures\deprecated_class
::class . '::notdeprecatedproperty', false],
213 [[\core\fixtures\deprecated_class
::class, 'notdeprecatedproperty'], false],
216 [\core\fixtures\deprecated_class
::class . '::DEPRECATED_CONST', true],
217 [[\core\fixtures\deprecated_class
::class, 'DEPRECATED_CONST'], true],
219 [\core\fixtures\deprecated_class
::class . '::NOT_DEPRECATED_CONST', false],
220 [[\core\fixtures\deprecated_class
::class, 'NOT_DEPRECATED_CONST'], false],
223 [\core\fixtures\deprecated_class
::class . '::deprecated_method', true],
224 [[\core\fixtures\deprecated_class
::class, 'deprecated_method'], true],
226 [\core\fixtures\deprecated_class
::class . '::not_deprecated_method', false],
227 [[\core\fixtures\deprecated_class
::class, 'not_deprecated_method'], false],
229 // Non-existent class.
230 ['non_existent_class', false],
231 [['non_existent_class'], false],
233 // Not-deprecated class.
234 [\core\fixtures\not_deprecated_class
::class, false],
236 // Deprecated global function.
237 ['core\fixtures\deprecated_function', true],
238 ['core\fixtures\not_deprecated_function', false],