This commit is contained in:
+2
-67
@@ -24,18 +24,6 @@
|
|||||||
|
|
||||||
namespace local_webhooks;
|
namespace local_webhooks;
|
||||||
|
|
||||||
defined('MOODLE_INTERNAL') || die();
|
|
||||||
|
|
||||||
global $CFG;
|
|
||||||
|
|
||||||
require_once(__DIR__ . '/../lib.php');
|
|
||||||
require_once(__DIR__ . '/../locallib.php');
|
|
||||||
|
|
||||||
require_once($CFG->libdir . '/filelib.php');
|
|
||||||
|
|
||||||
use curl;
|
|
||||||
use local_webhooks_events;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines how to work with events.
|
* Defines how to work with events.
|
||||||
*
|
*
|
||||||
@@ -47,61 +35,8 @@ class handler {
|
|||||||
* External handler.
|
* External handler.
|
||||||
*
|
*
|
||||||
* @param object $event
|
* @param object $event
|
||||||
*
|
|
||||||
* @throws \dml_exception
|
|
||||||
* @throws \coding_exception
|
|
||||||
*/
|
*/
|
||||||
public static function events($event) {
|
public static function events($event): void {
|
||||||
$data = $event->get_data();
|
// Delivery remains disabled until events can be persisted before any network request.
|
||||||
|
|
||||||
if (!empty($callbacks = local_webhooks_get_list_records())) {
|
|
||||||
foreach ($callbacks as $callback) {
|
|
||||||
self::handler_callback($data, $callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Processes each callback.
|
|
||||||
*
|
|
||||||
* @param array $data
|
|
||||||
* @param object $callback
|
|
||||||
*
|
|
||||||
* @throws \coding_exception
|
|
||||||
* @throws \dml_exception
|
|
||||||
*/
|
|
||||||
private static function handler_callback($data, $callback) {
|
|
||||||
global $CFG;
|
|
||||||
|
|
||||||
if ((bool) $callback->enable && !empty($callback->events[$data['eventname']])) {
|
|
||||||
$urlparse = parse_url($CFG->wwwroot);
|
|
||||||
|
|
||||||
$data['host'] = $urlparse['host'];
|
|
||||||
$data['token'] = $callback->token;
|
|
||||||
$data['extra'] = $callback->other;
|
|
||||||
|
|
||||||
self::send($data, $callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sending data to the node.
|
|
||||||
*
|
|
||||||
* @param array $data
|
|
||||||
* @param object $callback
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
* @throws \coding_exception
|
|
||||||
* @throws \dml_exception
|
|
||||||
*/
|
|
||||||
private static function send($data, $callback) {
|
|
||||||
$curl = new curl();
|
|
||||||
$curl->setHeader(['Content-Type: application/' . $callback->type]);
|
|
||||||
$curl->post($callback->url, json_encode($data));
|
|
||||||
|
|
||||||
$response = $curl->getResponse();
|
|
||||||
local_webhooks_events::response_answer($callback->id, $response);
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
<?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/>.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event handler 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event handler tests.
|
||||||
|
*/
|
||||||
|
#[\PHPUnit\Framework\Attributes\CoversClass(handler::class)]
|
||||||
|
final class handler_test extends \advanced_testcase {
|
||||||
|
/**
|
||||||
|
* The legacy observer does not inspect or deliver events.
|
||||||
|
*/
|
||||||
|
public function test_legacy_delivery_is_disabled(): void {
|
||||||
|
global $CFG;
|
||||||
|
|
||||||
|
$this->resetAfterTest();
|
||||||
|
|
||||||
|
$event = new class {
|
||||||
|
/** @var bool Whether event data was requested. */
|
||||||
|
public bool $accessed = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fails if the disabled handler reads the event.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function get_data(): array {
|
||||||
|
$this->accessed = true;
|
||||||
|
throw new \LogicException('The disabled handler must not read event data.');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
handler::events($event);
|
||||||
|
$this->assertFalse($event->accessed);
|
||||||
|
|
||||||
|
$user = $this->getDataGenerator()->create_user();
|
||||||
|
$coreevent = \core\event\user_created::create([
|
||||||
|
'objectid' => $user->id,
|
||||||
|
'context' => \context_user::instance($user->id),
|
||||||
|
]);
|
||||||
|
$sink = $this->redirectEvents();
|
||||||
|
$debug = $CFG->debug;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$CFG->debug = DEBUG_DEVELOPER;
|
||||||
|
handler::events($coreevent);
|
||||||
|
} finally {
|
||||||
|
$CFG->debug = $debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
$events = $sink->get_events();
|
||||||
|
$sink->close();
|
||||||
|
$this->assertEmpty($events);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user