From eb4b9b69f862df8ba23cafc8cd5e4a887baa02f0 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 6 May 2019 05:45:35 +0400 Subject: Added worked a class observer Signed-off-by: Valentin Popov --- classes/event/observer.php | 44 +++++++++++++++++++++++ classes/event_observer.php | 48 ------------------------- classes/task/notify.php | 2 +- db/events.php | 13 ++++--- tests/task_test.php | 89 +++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 139 insertions(+), 57 deletions(-) create mode 100644 classes/event/observer.php delete mode 100644 classes/event_observer.php diff --git a/classes/event/observer.php b/classes/event/observer.php new file mode 100644 index 0000000..ff6a89e --- /dev/null +++ b/classes/event/observer.php @@ -0,0 +1,44 @@ +. + +namespace local_webhooks\event; + +defined('MOODLE_INTERNAL') || die(); + +use core\event\base as event; +use core\task\manager; +use local_webhooks\task\notify; +use function defined; + +/** + * Class observer. + * + * @copyright 2019 'Valentin Popov' + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package local_webhooks\event + */ +final class observer { + /** + * Main handler for events. + * + * @param \core\event\base $event + */ + public static function handler(event $event) { + $task = new notify(); + $task->set_custom_data($event->get_data()); + manager::queue_adhoc_task($task); + } +} \ No newline at end of file diff --git a/classes/event_observer.php b/classes/event_observer.php deleted file mode 100644 index 983552a..0000000 --- a/classes/event_observer.php +++ /dev/null @@ -1,48 +0,0 @@ -. - -/** - * Handlers of observers for events. - * - * @copyright 2018 'Valentin Popov' - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package local_webhooks - */ - -namespace local_webhooks; - -defined('MOODLE_INTERNAL') || die(); - -/** - * Defines event handlers. - * - * @copyright 2018 'Valentin Popov' - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package local_webhooks - */ -class event_observer { - /** - * Handler of all the events. - * Each event is put into the job queue. - * - * @param object $event - */ - public static function observe_all($event) { - $task = new \local_webhooks\task\process_events_task(); - $task->set_custom_data($event->get_data()); - \core\task\manager::queue_adhoc_task($task); - } -} \ No newline at end of file diff --git a/classes/task/notify.php b/classes/task/notify.php index faa68a9..bd11f24 100644 --- a/classes/task/notify.php +++ b/classes/task/notify.php @@ -79,7 +79,7 @@ final class notify extends adhoc_task { public function post(record $service, array $data) { $curl = new curl(); $curl->setHeader(['Content-Type: ' . $service->header]); - $curl->post($service->point, $data); + $curl->post($service->point, json_encode($data)); if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) { $this->debug = array_merge($this->debug ?? [], [ diff --git a/db/events.php b/db/events.php index 0fc9abb..5960797 100644 --- a/db/events.php +++ b/db/events.php @@ -17,16 +17,15 @@ /** * Registration of observers for events. * - * @copyright 2018 'Valentin Popov' + * @copyright 2019 'Valentin Popov' * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package local_webhooks */ defined('MOODLE_INTERNAL') || die(); -$observers = array( - array( - 'callback' => '\local_webhooks\event_observer::observe_all', +$observers = [ + [ + 'callback' => '\local_webhooks\event\observer::handler', 'eventname' => '*', - ), -); \ No newline at end of file + ], +]; \ No newline at end of file diff --git a/tests/task_test.php b/tests/task_test.php index dc9d42f..1fe9bc2 100644 --- a/tests/task_test.php +++ b/tests/task_test.php @@ -64,7 +64,7 @@ final class local_webhooks_task_testcase extends advanced_testcase { $record->status = false; $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; - $record->id = api::create_service($record); + api::create_service($record); $task = new notify(); $task->set_custom_data(['eventname' => '\core\event\course_viewed']); @@ -108,4 +108,91 @@ final class local_webhooks_task_testcase extends advanced_testcase { self::assertEquals($record->id, $element['service']->id); self::assertEquals($record->token, $element['data']['token']); } + + /** + * Testing count creating tasks. + * + * @throws \coding_exception + * @throws \dml_exception + */ + public function test_observer_multiple() { + $generator = self::getDataGenerator(); + curl::mock_response('{}'); + $this->resetAfterTest(); + + $record = new record(); + $record->events = ['\core\event\course_created']; + $record->header = 'application/json'; + $record->name = 'Example name'; + $record->point = 'http://example.org/'; + $record->status = true; + $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; + + api::create_service($record); + + $total = random_int(5, 20); + for ($i = 0; $i < $total; $i++) { + $generator->create_course(); + } + + $debug = []; + foreach (manager::get_adhoc_tasks(notify::class) as $event) { + if (!is_object($event)) { + continue; + } + + /** @var \local_webhooks\task\notify $event */ + $event->execute(); + + if (isset($event->debug) && is_array($event->debug)) { + $debug[] = array_merge(...$event->debug); + } + } + + self::assertCount($total, $debug); + } + + /** + * Testing structure a creating task. + * + * @throws \coding_exception + * @throws \dml_exception + */ + public function test_observer_single() { + $generator = self::getDataGenerator(); + curl::mock_response('{}'); + $this->resetAfterTest(); + + $record = new record(); + $record->events = ['\core\event\course_created']; + $record->header = 'application/json'; + $record->name = 'Example name'; + $record->point = 'http://example.org/'; + $record->status = true; + $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; + + api::create_service($record); + + $course = $generator->create_course(); + + $debug = []; + foreach (manager::get_adhoc_tasks(notify::class) as $event) { + if (!is_object($event)) { + continue; + } + + /** @var \local_webhooks\task\notify $event */ + $event->execute(); + + if (isset($event->debug) && is_array($event->debug)) { + $debug[] = array_merge(...$event->debug); + } + } + + self::assertCount(1, $debug); + $element = array_shift($debug); + + self::assertInternalType('array', $element['data']); + self::assertEquals($course->id, $element['data']['courseid']); + } } \ No newline at end of file -- cgit v1.2.3