diff options
author | Valentin Popov <info@valentineus.link> | 2019-05-05 22:27:48 +0300 |
---|---|---|
committer | Valentin Popov <info@valentineus.link> | 2019-05-05 23:10:26 +0300 |
commit | 654eade42b8a088a08f2de4111aa2abfda6c7f45 (patch) | |
tree | 8e9d80903c80ad17e9a2d38e80ffde5deeb3afd7 | |
parent | 3f50d21a7f521623254868efe49041f6139ac52b (diff) | |
download | local_webhooks-654eade42b8a088a08f2de4111aa2abfda6c7f45.tar.xz local_webhooks-654eade42b8a088a08f2de4111aa2abfda6c7f45.zip |
Added description record's class
Signed-off-by: Valentin Popov <info@valentineus.link>
-rw-r--r-- | classes/record.php | 80 | ||||
-rw-r--r-- | tests/lib_test.php | 288 |
2 files changed, 220 insertions, 148 deletions
diff --git a/classes/record.php b/classes/record.php new file mode 100644 index 0000000..238fd2c --- /dev/null +++ b/classes/record.php @@ -0,0 +1,80 @@ +<?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/>. + +namespace local_webhooks\local; + +defined('MOODLE_INTERNAL') || die(); + +use stdClass; +use function defined; + +/** + * It's a class description record. + * + * @copyright 2019 'Valentin Popov' <info@valentineus.link> + * @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/tests/lib_test.php b/tests/lib_test.php index e372a9e..b813658 100644 --- a/tests/lib_test.php +++ b/tests/lib_test.php @@ -20,12 +20,13 @@ 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' <info@valentineus.link> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @package local_webhooks */ final class local_webhooks_lib_testcase extends advanced_testcase { /** @@ -41,21 +42,20 @@ final class local_webhooks_lib_testcase extends advanced_testcase { $this->resetAfterTest(); - $data = [ - 'events' => [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ], - 'header' => 'application/json', - 'name' => 'Example name', - 'point' => 'http://example.org/', - 'status' => 1, - 'token' => '967b2286-0874-4938-b088-efdbcf8a79bc', + $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', ]; - $serviceid = local_webhooks_api::create_service($data); + $record->id = local_webhooks_api::create_service($record); $events = $DB->get_records(LW_TABLE_EVENTS); $services = $DB->get_records(LW_TABLE_SERVICES); @@ -63,17 +63,17 @@ final class local_webhooks_lib_testcase extends advanced_testcase { self::assertCount(1, $services); $service = array_shift($services); - self::assertEquals($data['header'], $service->header); - self::assertEquals($data['name'], $service->name); - self::assertEquals($data['point'], $service->point); - self::assertEquals($data['status'], (int) $service->status); - self::assertEquals($data['token'], $service->token); - self::assertEquals($serviceid, $service->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::assertCount(count($data['events']), $events); + self::assertCount(count($record->events), $events); foreach ($events as $event) { - self::assertEquals($serviceid, $event->serviceid); - self::assertContains($event->name, $data['events']); + self::assertContains($event->name, $record->events); + self::assertEquals($record->id, $event->serviceid); } } @@ -90,22 +90,21 @@ final class local_webhooks_lib_testcase extends advanced_testcase { $this->resetAfterTest(); - $data = [ - 'events' => [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ], - 'header' => 'application/json', - 'name' => 'Example name', - 'point' => 'http://example.org/', - 'status' => 1, - 'token' => '967b2286-0874-4938-b088-efdbcf8a79bc', + $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', ]; - $serviceid = local_webhooks_api::create_service($data); - self::assertTrue(local_webhooks_api::delete_service($serviceid)); + $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)); } @@ -119,34 +118,33 @@ final class local_webhooks_lib_testcase extends advanced_testcase { public function test_get_service() { $this->resetAfterTest(); - $data = [ - 'events' => [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ], - 'header' => 'application/json', - 'name' => 'Example name', - 'point' => 'http://example.org/', - 'status' => 1, - 'token' => '967b2286-0874-4938-b088-efdbcf8a79bc', + $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', ]; - $serviceid = local_webhooks_api::create_service($data); - $service = local_webhooks_api::get_service($serviceid); + $record->id = local_webhooks_api::create_service($record); + $service = local_webhooks_api::get_service($record->id); - self::assertEquals($data['header'], $service->header); - self::assertEquals($data['name'], $service->name); - self::assertEquals($data['point'], $service->point); - self::assertEquals($data['status'], (int) $service->status); - self::assertEquals($data['token'], $service->token); - self::assertEquals($serviceid, $service->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($data['events']), $service->events); + self::assertCount(count($record->events), $service->events); foreach ($service->events as $event) { - self::assertContains($event, $data['events']); + self::assertContains($event, $record->events); } } @@ -159,24 +157,23 @@ final class local_webhooks_lib_testcase extends advanced_testcase { public function test_get_services() { $this->resetAfterTest(); - $data = [ - 'events' => [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ], - 'header' => 'application/json', - 'name' => 'Example name', - 'point' => 'http://example.org/', - 'status' => 1, - 'token' => '967b2286-0874-4938-b088-efdbcf8a79bc', + $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($data); + $ids[] = local_webhooks_api::create_service($record); } $services = local_webhooks_api::get_services(); @@ -184,16 +181,16 @@ final class local_webhooks_lib_testcase extends advanced_testcase { foreach ($services as $service) { self::assertContains($service->id, $ids); - self::assertEquals($data['header'], $service->header); - self::assertEquals($data['name'], $service->name); - self::assertEquals($data['point'], $service->point); - self::assertEquals($data['status'], $service->status); - self::assertEquals($data['token'], $service->token); + 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($data['events']), $service->events); + self::assertCount(count($record->events), $service->events); foreach ($service->events as $event) { - self::assertContains($event, $data['events']); + self::assertContains($event, $record->events); } } } @@ -207,42 +204,41 @@ final class local_webhooks_lib_testcase extends advanced_testcase { public function test_get_services_by_event() { $this->resetAfterTest(); - $data = [ - 'events' => [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ], - 'header' => 'application/json', - 'name' => 'Example name', - 'point' => 'http://example.org/', - 'status' => 1, - 'token' => '967b2286-0874-4938-b088-efdbcf8a79bc', + $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($data); + $ids[] = local_webhooks_api::create_service($record); } - $eventname = $data['events'][random_int(1, count($data['events']) - 1)]; + $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($data['header'], $service->header); - self::assertEquals($data['name'], $service->name); - self::assertEquals($data['point'], $service->point); - self::assertEquals($data['status'], $service->status); - self::assertEquals($data['token'], $service->token); + 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($data['events']), $service->events); + self::assertCount(count($record->events), $service->events); foreach ($service->events as $event) { - self::assertContains($event, $data['events']); + self::assertContains($event, $record->events); } } } @@ -256,24 +252,22 @@ final class local_webhooks_lib_testcase extends advanced_testcase { public function test_get_services_with_conditions() { $this->resetAfterTest(); - $data = [ - 'events' => [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ], - 'header' => 'application/json', - 'status' => 1, - 'token' => '967b2286-0874-4938-b088-efdbcf8a79bc', + $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++) { - local_webhooks_api::create_service(array_merge($data, [ - 'name' => 'Example name #' . $i, - 'point' => 'http://example.org/test_' . $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([ @@ -297,35 +291,33 @@ final class local_webhooks_lib_testcase extends advanced_testcase { $this->resetAfterTest(); - $data1 = [ - 'events' => [ - '\core\event\course_created', - '\core\event\course_deleted', - '\core\event\course_updated', - '\core\event\course_viewed', - ], - 'header' => 'application/json', - 'name' => 'Example name', - 'point' => 'http://example.org/', - 'status' => 1, - 'token' => '967b2286-0874-4938-b088-efdbcf8a79bc', + $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', ]; - $data2 = [ - 'events' => [ - '\core\event\calendar_event_created' => '1', - '\core\event\calendar_event_deleted' => '1', - '\core\event\calendar_event_updated' => '1', - ], - 'header' => 'application/x-www-form-urlencoded', - 'name' => 'New name', - 'point' => 'http://domain.local/example', - 'status' => 0, - 'token' => 'add62250-2f03-49a9-97c4-6cd73a79e83b', + $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', ]; - $serviceid = local_webhooks_api::create_service($data1); - self::assertTrue(local_webhooks_api::update_service(array_merge($data2, ['id' => $serviceid]))); + $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); @@ -333,17 +325,17 @@ final class local_webhooks_lib_testcase extends advanced_testcase { self::assertCount(1, $services); $service = array_shift($services); - self::assertEquals($data2['header'], $service->header); - self::assertEquals($data2['name'], $service->name); - self::assertEquals($data2['point'], $service->point); - self::assertEquals($data2['status'], (int) $service->status); - self::assertEquals($data2['token'], $service->token); - self::assertEquals($serviceid, $service->id); + 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($data2['events']), $events); + self::assertCount(count($record2->events), $events); foreach ($events as $event) { - self::assertEquals($serviceid, $event->serviceid); - self::assertContains($event->name, $data2['events']); + self::assertContains($event->name, $record2->events); + self::assertEquals($record2->id, $event->serviceid); } } }
\ No newline at end of file |