aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--classes/event/observer.php (renamed from classes/event_observer.php)34
-rw-r--r--classes/task/notify.php2
-rw-r--r--db/events.php13
-rw-r--r--tests/task_test.php89
4 files changed, 110 insertions, 28 deletions
diff --git a/classes/event_observer.php b/classes/event/observer.php
index 983552a..ff6a89e 100644
--- a/classes/event_observer.php
+++ b/classes/event/observer.php
@@ -14,35 +14,31 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
-/**
- * Handlers of observers for events.
- *
- * @copyright 2018 'Valentin Popov' <info@valentineus.link>
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- * @package local_webhooks
- */
-
-namespace local_webhooks;
+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;
+
/**
- * Defines event handlers.
+ * Class observer.
*
- * @copyright 2018 'Valentin Popov' <info@valentineus.link>
+ * @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- * @package local_webhooks
+ * @package local_webhooks\event
*/
-class event_observer {
+final class observer {
/**
- * Handler of all the events.
- * Each event is put into the job queue.
+ * Main handler for events.
*
- * @param object $event
+ * @param \core\event\base $event
*/
- public static function observe_all($event) {
- $task = new \local_webhooks\task\process_events_task();
+ public static function handler(event $event) {
+ $task = new notify();
$task->set_custom_data($event->get_data());
- \core\task\manager::queue_adhoc_task($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' <info@valentineus.link>
+ * @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @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