Files
local_webhooks/tests/autonomy_test.php
T

92 lines
4.0 KiB
PHP
Raw Normal View History

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Runtime autonomy tests.
*
* @package local_webhooks
* @copyright 2026 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_webhooks;
/**
* Runtime autonomy tests.
*/
#[\PHPUnit\Framework\Attributes\CoversFunction('local_webhooks_get_event_choices')]
final class autonomy_test extends \advanced_testcase {
/**
* Event choices keep their storage format and custom event names are strings.
*/
public function test_event_contracts(): void {
global $CFG;
require_once($CFG->dirroot . '/local/webhooks/lib.php');
require_once($CFG->dirroot . '/local/webhooks/classes/service_edit_form.php');
$debugsettings = [$CFG->debug, $CFG->debugdisplay, $CFG->debugdeveloper];
$choices = \local_webhooks_get_event_choices();
$this->assertNotEmpty($choices);
$this->assertSame($debugsettings, [$CFG->debug, $CFG->debugdisplay, $CFG->debugdeveloper]);
foreach ($choices as $component => $events) {
foreach ($events as $key => $eventname) {
$classname = ltrim($eventname, '\\');
$this->assertSame(base64_encode($eventname), $key);
$this->assertSame($component, \core_component::get_component_from_classname($classname));
$this->assertTrue(is_subclass_of($classname, \core\event\base::class));
$this->assertFalse((new \ReflectionClass($classname))->isAbstract());
$this->assertFalse($classname::is_deprecated());
}
}
$coreevent = '\\' . \core\event\user_created::class;
$localevent = '\\' . \local_webhooks\event\service_added::class;
$unknown = '\\' . \core\event\unknown_logged::class;
$deprecated = '\\' . \core\event\role_capabilities_updated::class;
$this->assertSame($coreevent, $choices['core'][base64_encode($coreevent)]);
$this->assertSame($localevent, $choices['local_webhooks'][base64_encode($localevent)]);
$this->assertArrayNotHasKey(base64_encode($unknown), $choices['core']);
$this->assertArrayNotHasKey(base64_encode($deprecated), $choices['core']);
$events = [
\local_webhooks\event\backup_performed::class => 'create',
\local_webhooks\event\backup_restored::class => 'update',
\local_webhooks\event\response_answer::class => 'answer',
\local_webhooks\event\service_added::class => 'create',
\local_webhooks\event\service_deleted::class => 'deleted',
\local_webhooks\event\service_updated::class => 'update',
];
foreach ($events as $event => $stringkey) {
$name = $event::get_name();
$this->assertIsString($name);
$this->assertNotSame('', trim($name));
$this->assertSame(get_string($stringkey, 'local_webhooks'), $name);
}
$this->assertFalse(class_exists('report_eventlist_list_generator', false));
$form = new \service_edit_form(new \moodle_url('/local/webhooks/editservice.php'));
$html = $form->render();
$this->assertStringContainsString($coreevent, $html);
$this->assertStringContainsString($localevent, $html);
$this->assertFalse(class_exists('report_eventlist_list_generator', false));
}
}