From 4806fad728116cbd6b440eb285fcbcc867a62b84 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 6 May 2019 00:35:18 +0400 Subject: Did a deep refactoring main class in the plugin Signed-off-by: Valentin Popov --- classes/local/api.php | 198 +++++++++++++++++++++++++++ classes/local/record.php | 80 +++++++++++ classes/record.php | 80 ----------- lib.php | 216 ------------------------------ tests/api_test.php | 342 +++++++++++++++++++++++++++++++++++++++++++++++ tests/lib_test.php | 341 ---------------------------------------------- 6 files changed, 620 insertions(+), 637 deletions(-) create mode 100644 classes/local/api.php create mode 100644 classes/local/record.php delete mode 100644 classes/record.php delete mode 100644 lib.php create mode 100644 tests/api_test.php delete mode 100644 tests/lib_test.php diff --git a/classes/local/api.php b/classes/local/api.php new file mode 100644 index 0000000..2ccec4b --- /dev/null +++ b/classes/local/api.php @@ -0,0 +1,198 @@ +. + +namespace local_webhooks\local; + +defined('MOODLE_INTERNAL') || die(); + +define('LW_TABLE_EVENTS', 'local_webhooks_events'); +define('LW_TABLE_SERVICES', 'local_webhooks_service'); + +global $CFG; + +require_once($CFG->dirroot . '/local/webhooks/classes/local/record.php'); + +use coding_exception; +use function define; +use function defined; +use function is_array; +use function is_int; +use function is_object; + +/** + * The main class for the plugin. + * + * @copyright 2019 'Valentin Popov' + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package local_webhooks\local + */ +final class api { + /** + * Create a new record in the database. + * + * @param \local_webhooks\local\record $record + * + * @return int + * @throws \coding_exception + * @throws \dml_exception + */ + public static function create_service(record $record): int { + global $DB; + + $id = $DB->insert_record(LW_TABLE_SERVICES, $record); + + if (is_int($id) && is_array($record->events)) { + $DB->insert_records(LW_TABLE_EVENTS, array_map(static function (string $name) use ($id) { + return ['name' => $name, 'serviceid' => $id]; + }, $record->events)); + } + + if (!is_int($id)) { + throw new coding_exception('Variable \'id\' must be type \'Integer\''); + } + + return $id; + } + + /** + * Delete an existing record in the database. + * + * @param int $id + * + * @return bool + * @throws \dml_exception + */ + public static function delete_service(int $id): bool { + global $DB; + + $DB->delete_records(LW_TABLE_EVENTS, ['serviceid' => $id]); + + return $DB->delete_records(LW_TABLE_SERVICES, ['id' => $id]); + } + + /** + * Get an existing record from the database. + * + * @param int $id + * + * @return \local_webhooks\local\record + * @throws \dml_exception + */ + public static function get_service(int $id): record { + global $DB; + + $record = $DB->get_record(LW_TABLE_SERVICES, ['id' => $id], '*', MUST_EXIST); + $events = $DB->get_records(LW_TABLE_EVENTS, ['serviceid' => $id]); + + $service = new record(); + $service->events = array_column($events, 'name'); + $service->header = $record->header; + $service->id = $record->id; + $service->name = $record->name; + $service->point = $record->point; + $service->status = $record->status; + $service->token = $record->token; + + return $service; + } + + /** + * Get list records from the database. + * + * @param array|null $conditions + * @param int|null $limitfrom + * @param int|null $limitnum + * + * @return \local_webhooks\local\record[] + * @throws \dml_exception + */ + public static function get_services(array $conditions = null, int $limitfrom = null, int $limitnum = null): array { + global $DB; + + $records = $DB->get_records(LW_TABLE_SERVICES, $conditions ?? [], '', '*', $limitfrom ?? 0, $limitnum ?? 0); + + $services = []; + foreach ($records as $record) { + if (!is_object($record)) { + continue; + } + + $events = $DB->get_records(LW_TABLE_EVENTS, ['serviceid' => $record->id]); + + $service = new record(); + $service->events = array_column($events, 'name'); + $service->header = $record->header; + $service->id = $record->id; + $service->name = $record->name; + $service->point = $record->point; + $service->status = $record->status; + $service->token = $record->token; + + $services[] = $service; + } + + return $services; + } + + /** + * Get list records from the database by the event's name. + * + * @param string $name + * + * @return \local_webhooks\local\record[] + * @throws \dml_exception + */ + public static function get_services_by_event(string $name): array { + global $DB; + + $events = $DB->get_records(LW_TABLE_EVENTS, ['name' => $name]); + + $services = []; + foreach ($events as $event) { + if (!is_object($event)) { + continue; + } + + $services[] = self::get_service($event->serviceid); + } + + return $services; + } + + /** + * Update an existing record in the database. + * + * @param \local_webhooks\local\record $service + * + * @return bool + * @throws \coding_exception + * @throws \dml_exception + */ + public static function update_service(record $service): bool { + global $DB; + + $result = $DB->update_record(LW_TABLE_SERVICES, $service); + $DB->delete_records(LW_TABLE_EVENTS, ['serviceid' => $service->id]); + + if ($result && is_array($service->events)) { + $DB->insert_records(LW_TABLE_EVENTS, array_map(static function (string $name) use ($service) { + return ['name' => $name, 'serviceid' => $service->id]; + }, $service->events)); + } + + return $result; + } +} \ No newline at end of file diff --git a/classes/local/record.php b/classes/local/record.php new file mode 100644 index 0000000..29dadce --- /dev/null +++ b/classes/local/record.php @@ -0,0 +1,80 @@ +. + +namespace local_webhooks\local; + +defined('MOODLE_INTERNAL') || die(); + +use stdClass; +use function defined; + +/** + * It's a class description record. + * + * @copyright 2019 'Valentin Popov' + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package local_webhooks\local + */ +final class record extends stdClass { + /** + * List of some events. + * + * @var array|null + */ + public $events; + + /** + * Type of the package. + * + * @var string + */ + public $header; + + /** + * A unique identifier of the service. + * + * @var int|null + */ + public $id; + + /** + * Name of the service. + * + * @var string + */ + public $name; + + /** + * Url's an endpoint to send notifications. + * + * @var string + */ + public $point; + + /** + * Status activity of the service. + * + * @var bool + */ + public $status; + + /** + * Secret key of the service. + * + * @var string + */ + public $token; +} \ No newline at end of file diff --git a/classes/record.php b/classes/record.php deleted file mode 100644 index 238fd2c..0000000 --- a/classes/record.php +++ /dev/null @@ -1,80 +0,0 @@ -. - -namespace local_webhooks\local; - -defined('MOODLE_INTERNAL') || die(); - -use stdClass; -use function defined; - -/** - * It's a class description record. - * - * @copyright 2019 'Valentin Popov' - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package local_webhooks\local\local_webhooks_record - */ -final class local_webhooks_record extends stdClass { - /** - * List of some events. - * - * @var array|null - */ - public $events; - - /** - * Type of the package. - * - * @var string - */ - public $header; - - /** - * A unique identifier of the service. - * - * @var int|null - */ - public $id; - - /** - * Name of the service. - * - * @var string - */ - public $name; - - /** - * Url's an endpoint to send notifications. - * - * @var string - */ - public $point; - - /** - * Status activity of the service. - * - * @var bool - */ - public $status; - - /** - * Secret key of the service. - * - * @var string - */ - public $token; -} \ No newline at end of file diff --git a/lib.php b/lib.php deleted file mode 100644 index aefb783..0000000 --- a/lib.php +++ /dev/null @@ -1,216 +0,0 @@ -. - -/** - * This file contains the functions used by the plugin. - * - * @copyright 2018 'Valentin Popov' - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package local_webhooks - */ - -defined('MOODLE_INTERNAL') || die(); - -define('LW_TABLE_SERVICES', 'local_webhooks_service'); -define('LW_TABLE_EVENTS', 'local_webhooks_events'); - -/** - * Class local_webhooks_api - * - * @copyright 2018 'Valentin Popov' - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package local_webhooks - */ -class local_webhooks_api { - /** - * Get information about the service. - * - * @param int $serviceid - * - * @return object - * @throws \dml_exception - * @throws \moodle_exception - */ - public static function get_service($serviceid = 0) { - global $DB; - - if (!is_numeric($serviceid) || $serviceid === 0) { - print_error('unknowparamtype', 'error', null, 'serviceid'); - } - - $service = $DB->get_record(LW_TABLE_SERVICES, array('id' => $serviceid), '*', MUST_EXIST); - $events = $DB->get_records(LW_TABLE_EVENTS, array('serviceid' => $serviceid), '', '*', 0, 0); - - $service->events = array(); - foreach ($events as $event) { - $service->events[] = $event->name; - } - - return $service; - } - - /** - * Get a list of services. - * By default, the entire list of services is given. - * - * @param array $conditions - * @param int $limitfrom - * @param int $limitnum - * - * @return array - * @throws \dml_exception - */ - public static function get_services(array $conditions = array(), $limitfrom = 0, $limitnum = 0) { - global $DB; - - $services = $DB->get_records(LW_TABLE_SERVICES, $conditions, '', '*', $limitfrom, $limitnum); - - foreach ($services as $service) { - $events = $DB->get_records(LW_TABLE_EVENTS, array('serviceid' => $service->id), '', '*', 0, 0); - - $service->events = array(); - foreach ($events as $event) { - $service->events[] = $event->name; - } - } - - return $services; - } - - /** - * Get the list of services subscribed to the event. - * - * @param string $eventname - * - * @return array - * @throws \dml_exception - * @throws \moodle_exception - */ - public static function get_services_by_event($eventname = '') { - global $DB; - - if (!is_string($eventname) || $eventname === '') { - print_error('unknowparamtype', 'error', null, 'eventname'); - } - - $events = $DB->get_records(LW_TABLE_EVENTS, array('name' => $eventname), '', '*', 0, 0); - - $services = array(); - foreach ($events as $event) { - $services[] = self::get_service($event->serviceid); - } - - return $services; - } - - /** - * Create service data in the database. - * - * @param array $service - * - * @return int - * @throws \dml_exception - * @throws \moodle_exception - */ - public static function create_service(array $service = array()) { - global $DB; - - if (!is_array($service) || count($service) === 0) { - print_error('unknowparamtype', 'error', null, 'service'); - } - - $serviceid = $DB->insert_record(LW_TABLE_SERVICES, (object) $service, true, false); - if ($serviceid && !empty($service['events']) && is_array($service['events'])) { - self::insert_events($service['events'], $serviceid); - } - - // TODO: Mark the log. - - return (int) $serviceid; - } - - /** - * Delete the service data from the database. - * - * @param int $serviceid - * - * @return bool - * @throws \dml_exception - * @throws \moodle_exception - */ - public static function delete_service($serviceid = 0) { - global $DB; - - if (!is_numeric($serviceid) || $serviceid === 0) { - print_error('unknowparamtype', 'error', null, 'serviceid'); - } - - // TODO: Mark the log. - - $DB->delete_records(LW_TABLE_EVENTS, array('serviceid' => $serviceid)); - - return $DB->delete_records(LW_TABLE_SERVICES, array('id' => $serviceid)); - } - - /** - * Update the service data in the database. - * - * @param array $service - * - * @return bool - * @throws \dml_exception - * @throws \moodle_exception - */ - public static function update_service(array $service = array()) { - global $DB; - - if (!is_array($service) || count($service) === 0 || !isset($service['id'])) { - print_error('unknowparamtype', 'error', null, 'service'); - } - - // TODO: Add transactions for operations. - $result = $DB->update_record(LW_TABLE_SERVICES, (object) $service, false); - $DB->delete_records(LW_TABLE_EVENTS, array('serviceid' => $service['id'])); - - if ($result && is_array($service['events']) && count($service) !== 0) { - self::insert_events($service['events'], $service['id']); - } - - // TODO: Mark the log. - - return $result; - } - - /** - * Save the list of events to the database. - * - * @param array $events - * @param int $serviceid - * - * @throws \coding_exception - * @throws \dml_exception - */ - protected static function insert_events(array $events = array(), $serviceid = 0) { - global $DB; - - $conditions = array(); - foreach ($events as $eventname) { - $conditions[] = array('name' => $eventname, 'serviceid' => $serviceid); - } - - $DB->insert_records(LW_TABLE_EVENTS, $conditions); - } -} \ No newline at end of file diff --git a/tests/api_test.php b/tests/api_test.php new file mode 100644 index 0000000..998db9e --- /dev/null +++ b/tests/api_test.php @@ -0,0 +1,342 @@ +. + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php'); + +use local_webhooks\local\api; +use local_webhooks\local\record; + +/** + * Class local_webhooks_api_testcase. + * + * @copyright 2019 'Valentin Popov' + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +final class local_webhooks_api_testcase extends advanced_testcase { + /** + * Testing creation of the service. + * + * @group local_webhooks + * + * @throws \dml_exception + * @throws \moodle_exception + */ + public function test_adding() { + global $DB; + + $this->resetAfterTest(); + + $record = new record(); + $record->header = 'application/json'; + $record->name = 'Example name'; + $record->point = 'http://example.org/'; + $record->status = true; + $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; + $record->events = [ + '\core\event\course_created', + '\core\event\course_deleted', + '\core\event\course_updated', + '\core\event\course_viewed', + ]; + + $record->id = api::create_service($record); + + $events = $DB->get_records(LW_TABLE_EVENTS); + $services = $DB->get_records(LW_TABLE_SERVICES); + + self::assertCount(1, $services); + $service = array_shift($services); + + self::assertEquals($record->header, $service->header); + self::assertEquals($record->id, $service->id); + self::assertEquals($record->name, $service->name); + self::assertEquals($record->point, $service->point); + self::assertEquals($record->status, (bool) $service->status); + self::assertEquals($record->token, $service->token); + + self::assertCount(count($record->events), $events); + foreach ($events as $event) { + self::assertContains($event->name, $record->events); + self::assertEquals($record->id, $event->serviceid); + } + } + + /** + * Test deletion of the service. + * + * @group local_webhooks + * + * @throws \dml_exception + * @throws \moodle_exception + */ + public function test_deleting() { + global $DB; + + $this->resetAfterTest(); + + $record = new record(); + $record->header = 'application/json'; + $record->name = 'Example name'; + $record->point = 'http://example.org/'; + $record->status = true; + $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; + $record->events = [ + '\core\event\course_created', + '\core\event\course_deleted', + '\core\event\course_updated', + '\core\event\course_viewed', + ]; + + $record->id = api::create_service($record); + self::assertTrue(api::delete_service($record->id)); + self::assertCount(0, $DB->get_records(LW_TABLE_EVENTS)); + self::assertCount(0, $DB->get_records(LW_TABLE_SERVICES)); + } + + /** + * Testing get to a service. + * + * @throws \dml_exception + * @throws \moodle_exception + */ + public function test_get_service() { + $this->resetAfterTest(); + + $record = new record(); + $record->header = 'application/json'; + $record->name = 'Example name'; + $record->point = 'http://example.org/'; + $record->status = true; + $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; + $record->events = [ + '\core\event\course_created', + '\core\event\course_deleted', + '\core\event\course_updated', + '\core\event\course_viewed', + ]; + + $record->id = api::create_service($record); + $service = api::get_service($record->id); + + self::assertEquals($record->header, $service->header); + self::assertEquals($record->id, $service->id); + self::assertEquals($record->name, $service->name); + self::assertEquals($record->point, $service->point); + self::assertEquals($record->status, (bool) $service->status); + self::assertEquals($record->token, $service->token); + + self::assertInternalType('array', $service->events); + self::assertCount(count($record->events), $service->events); + foreach ($service->events as $event) { + self::assertContains($event, $record->events); + } + } + + /** + * Testing get to the list services. + * + * @throws \dml_exception + * @throws \moodle_exception + */ + public function test_get_services() { + $this->resetAfterTest(); + + $record = new record(); + $record->header = 'application/json'; + $record->name = 'Example name'; + $record->point = 'http://example.org/'; + $record->status = true; + $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; + $record->events = [ + '\core\event\course_created', + '\core\event\course_deleted', + '\core\event\course_updated', + '\core\event\course_viewed', + ]; + + $ids = []; + $total = random_int(5, 20); + for ($i = 0; $i < $total; $i++) { + $ids[] = api::create_service($record); + } + + $services = api::get_services(); + self::assertCount(count($ids), $services); + + foreach ($services as $service) { + self::assertContains($service->id, $ids); + self::assertEquals($record->header, $service->header); + self::assertEquals($record->name, $service->name); + self::assertEquals($record->point, $service->point); + self::assertEquals($record->status, $service->status); + self::assertEquals($record->token, $service->token); + + self::assertInternalType('array', $service->events); + self::assertCount(count($record->events), $service->events); + foreach ($service->events as $event) { + self::assertContains($event, $record->events); + } + } + } + + /** + * Testing get to the list services by event name. + * + * @throws \dml_exception + * @throws \moodle_exception + */ + public function test_get_services_by_event() { + $this->resetAfterTest(); + + $record = new record(); + $record->header = 'application/json'; + $record->name = 'Example name'; + $record->point = 'http://example.org/'; + $record->status = true; + $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; + $record->events = [ + '\core\event\course_created', + '\core\event\course_deleted', + '\core\event\course_updated', + '\core\event\course_viewed', + ]; + + $ids = []; + $total = random_int(5, 20); + for ($i = 0; $i < $total; $i++) { + $ids[] = api::create_service($record); + } + + $eventname = $record->events[random_int(1, count($record->events) - 1)]; + $services = api::get_services_by_event($eventname); + self::assertCount(count($ids), $services); + + foreach ($services as $service) { + self::assertContains($service->id, $ids); + self::assertEquals($record->header, $service->header); + self::assertEquals($record->name, $service->name); + self::assertEquals($record->point, $service->point); + self::assertEquals($record->status, $service->status); + self::assertEquals($record->token, $service->token); + + self::assertInternalType('array', $service->events); + self::assertCount(count($record->events), $service->events); + foreach ($service->events as $event) { + self::assertContains($event, $record->events); + } + } + } + + /** + * Testing get to the list services with conditions. + * + * @throws \dml_exception + * @throws \moodle_exception + */ + public function test_get_services_with_conditions() { + $this->resetAfterTest(); + + $record = new record(); + $record->header = 'application/json'; + $record->status = true; + $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; + $record->events = [ + '\core\event\course_created', + '\core\event\course_deleted', + '\core\event\course_updated', + '\core\event\course_viewed', + ]; + + $total = random_int(5, 20); + for ($i = 0; $i < $total; $i++) { + $record->name = 'Example name #' . $i; + $record->point = 'http://example.org/test_' . $i; + api::create_service($record); + } + + self::assertCount(1, api::get_services([ + 'name' => 'Example name #' . random_int(1, $total), + ])); + + $limit = intdiv($total, 2); + self::assertCount($limit, api::get_services([], 1, $limit)); + } + + /** + * Testing of the service update. + * + * @group local_webhooks + * + * @throws \dml_exception + * @throws \moodle_exception + */ + public function test_updating() { + global $DB; + + $this->resetAfterTest(); + + $record1 = new record(); + $record1->header = 'application/json'; + $record1->name = 'Example name'; + $record1->point = 'http://example.org/'; + $record1->status = true; + $record1->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; + $record1->events = [ + '\core\event\course_created', + '\core\event\course_deleted', + '\core\event\course_updated', + '\core\event\course_viewed', + ]; + + $record2 = new record(); + $record2->header = 'application/x-www-form-urlencoded'; + $record2->name = 'New name'; + $record2->point = 'http://domain.local/example'; + $record2->status = false; + $record2->token = 'add62250-2f03-49a9-97c4-6cd73a79e83b'; + $record2->events = [ + '\core\event\calendar_event_created', + '\core\event\calendar_event_deleted', + '\core\event\calendar_event_updated', + ]; + + $record2->id = api::create_service($record1); + self::assertTrue(api::update_service($record2)); + + $events = $DB->get_records(LW_TABLE_EVENTS); + $services = $DB->get_records(LW_TABLE_SERVICES); + + self::assertCount(1, $services); + $service = array_shift($services); + + self::assertEquals($record2->header, $service->header); + self::assertEquals($record2->id, $service->id); + self::assertEquals($record2->name, $service->name); + self::assertEquals($record2->point, $service->point); + self::assertEquals($record2->status, (bool) $service->status); + self::assertEquals($record2->token, $service->token); + + self::assertCount(count($record2->events), $events); + foreach ($events as $event) { + self::assertContains($event->name, $record2->events); + self::assertEquals($record2->id, $event->serviceid); + } + } +} \ No newline at end of file diff --git a/tests/lib_test.php b/tests/lib_test.php deleted file mode 100644 index b813658..0000000 --- a/tests/lib_test.php +++ /dev/null @@ -1,341 +0,0 @@ -. - -defined('MOODLE_INTERNAL') || die(); - -global $CFG; - -require_once($CFG->dirroot . '/local/webhooks/lib.php'); - -use local_webhooks\local\local_webhooks_record as stdRecord; - -/** - * Class local_webhooks_lib_testcase - * - * @copyright 2019 'Valentin Popov' - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -final class local_webhooks_lib_testcase extends advanced_testcase { - /** - * Testing creation of the service. - * - * @group local_webhooks - * - * @throws \dml_exception - * @throws \moodle_exception - */ - public function test_adding() { - global $DB; - - $this->resetAfterTest(); - - $record = new stdRecord(); - $record->header = 'application/json'; - $record->name = 'Example name'; - $record->point = 'http://example.org/'; - $record->status = true; - $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; - $record->events = [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ]; - - $record->id = local_webhooks_api::create_service($record); - - $events = $DB->get_records(LW_TABLE_EVENTS); - $services = $DB->get_records(LW_TABLE_SERVICES); - - self::assertCount(1, $services); - $service = array_shift($services); - - self::assertEquals($record->header, $service->header); - self::assertEquals($record->id, $service->id); - self::assertEquals($record->name, $service->name); - self::assertEquals($record->point, $service->point); - self::assertEquals($record->status, (bool) $service->status); - self::assertEquals($record->token, $service->token); - - self::assertCount(count($record->events), $events); - foreach ($events as $event) { - self::assertContains($event->name, $record->events); - self::assertEquals($record->id, $event->serviceid); - } - } - - /** - * Test deletion of the service. - * - * @group local_webhooks - * - * @throws \dml_exception - * @throws \moodle_exception - */ - public function test_deleting() { - global $DB; - - $this->resetAfterTest(); - - $record = new stdRecord(); - $record->header = 'application/json'; - $record->name = 'Example name'; - $record->point = 'http://example.org/'; - $record->status = true; - $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; - $record->events = [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ]; - - $record->id = local_webhooks_api::create_service($record); - self::assertTrue(local_webhooks_api::delete_service($record->id)); - self::assertCount(0, $DB->get_records(LW_TABLE_EVENTS)); - self::assertCount(0, $DB->get_records(LW_TABLE_SERVICES)); - } - - /** - * Testing get to a service. - * - * @throws \dml_exception - * @throws \moodle_exception - */ - public function test_get_service() { - $this->resetAfterTest(); - - $record = new stdRecord(); - $record->header = 'application/json'; - $record->name = 'Example name'; - $record->point = 'http://example.org/'; - $record->status = true; - $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; - $record->events = [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ]; - - $record->id = local_webhooks_api::create_service($record); - $service = local_webhooks_api::get_service($record->id); - - self::assertEquals($record->header, $service->header); - self::assertEquals($record->id, $service->id); - self::assertEquals($record->name, $service->name); - self::assertEquals($record->point, $service->point); - self::assertEquals($record->status, (bool) $service->status); - self::assertEquals($record->token, $service->token); - - self::assertInternalType('array', $service->events); - self::assertCount(count($record->events), $service->events); - foreach ($service->events as $event) { - self::assertContains($event, $record->events); - } - } - - /** - * Testing get to the list services. - * - * @throws \dml_exception - * @throws \moodle_exception - */ - public function test_get_services() { - $this->resetAfterTest(); - - $record = new stdRecord(); - $record->header = 'application/json'; - $record->name = 'Example name'; - $record->point = 'http://example.org/'; - $record->status = true; - $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; - $record->events = [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ]; - - $ids = []; - $total = random_int(5, 20); - for ($i = 0; $i < $total; $i++) { - $ids[] = local_webhooks_api::create_service($record); - } - - $services = local_webhooks_api::get_services(); - self::assertCount(count($ids), $services); - - foreach ($services as $service) { - self::assertContains($service->id, $ids); - self::assertEquals($record->header, $service->header); - self::assertEquals($record->name, $service->name); - self::assertEquals($record->point, $service->point); - self::assertEquals($record->status, $service->status); - self::assertEquals($record->token, $service->token); - - self::assertInternalType('array', $service->events); - self::assertCount(count($record->events), $service->events); - foreach ($service->events as $event) { - self::assertContains($event, $record->events); - } - } - } - - /** - * Testing get to the list services by event name. - * - * @throws \dml_exception - * @throws \moodle_exception - */ - public function test_get_services_by_event() { - $this->resetAfterTest(); - - $record = new stdRecord(); - $record->header = 'application/json'; - $record->name = 'Example name'; - $record->point = 'http://example.org/'; - $record->status = true; - $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; - $record->events = [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ]; - - $ids = []; - $total = random_int(5, 20); - for ($i = 0; $i < $total; $i++) { - $ids[] = local_webhooks_api::create_service($record); - } - - $eventname = $record->events[random_int(1, count($record->events) - 1)]; - $services = local_webhooks_api::get_services_by_event($eventname); - self::assertCount(count($ids), $services); - - foreach ($services as $service) { - self::assertContains($service->id, $ids); - self::assertEquals($record->header, $service->header); - self::assertEquals($record->name, $service->name); - self::assertEquals($record->point, $service->point); - self::assertEquals($record->status, $service->status); - self::assertEquals($record->token, $service->token); - - self::assertInternalType('array', $service->events); - self::assertCount(count($record->events), $service->events); - foreach ($service->events as $event) { - self::assertContains($event, $record->events); - } - } - } - - /** - * Testing get to the list services with conditions. - * - * @throws \dml_exception - * @throws \moodle_exception - */ - public function test_get_services_with_conditions() { - $this->resetAfterTest(); - - $record = new stdRecord(); - $record->header = 'application/json'; - $record->status = true; - $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; - $record->events = [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ]; - - $total = random_int(5, 20); - for ($i = 0; $i < $total; $i++) { - $record->name = 'Example name #' . $i; - $record->point = 'http://example.org/test_' . $i; - local_webhooks_api::create_service($record); - } - - self::assertCount(1, local_webhooks_api::get_services([ - 'name' => 'Example name #' . random_int(1, $total), - ])); - - $limit = random_int(1, $total); - self::assertCount($limit, local_webhooks_api::get_services([], 1, $limit)); - } - - /** - * Testing of the service update. - * - * @group local_webhooks - * - * @throws \dml_exception - * @throws \moodle_exception - */ - public function test_updating() { - global $DB; - - $this->resetAfterTest(); - - $record1 = new stdRecord(); - $record1->header = 'application/json'; - $record1->name = 'Example name'; - $record1->point = 'http://example.org/'; - $record1->status = true; - $record1->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; - $record1->events = [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ]; - - $record2 = new stdRecord(); - $record2->header = 'application/x-www-form-urlencoded'; - $record2->name = 'New name'; - $record2->point = 'http://domain.local/example'; - $record2->status = false; - $record2->token = 'add62250-2f03-49a9-97c4-6cd73a79e83b'; - $record2->events = [ - '\core\event\calendar_event_created', - '\core\event\calendar_event_deleted', - '\core\event\calendar_event_updated', - ]; - - $record2->id = local_webhooks_api::create_service($record1); - self::assertTrue(local_webhooks_api::update_service($record2)); - - $events = $DB->get_records(LW_TABLE_EVENTS); - $services = $DB->get_records(LW_TABLE_SERVICES); - - self::assertCount(1, $services); - $service = array_shift($services); - - self::assertEquals($record2->header, $service->header); - self::assertEquals($record2->id, $service->id); - self::assertEquals($record2->name, $service->name); - self::assertEquals($record2->point, $service->point); - self::assertEquals($record2->status, (bool) $service->status); - self::assertEquals($record2->token, $service->token); - - self::assertCount(count($record2->events), $events); - foreach ($events as $event) { - self::assertContains($event->name, $record2->events); - self::assertEquals($record2->id, $event->serviceid); - } - } -} \ No newline at end of file -- cgit v1.2.3