diff options
author | Valentin Popov <info@valentineus.link> | 2019-05-04 19:51:54 +0300 |
---|---|---|
committer | Valentin Popov <info@valentineus.link> | 2019-05-05 15:15:38 +0300 |
commit | 08a54b0e9fda7ae9a744fb99c59cd8c73b1725a0 (patch) | |
tree | dc30836ff4c0c0413f0d20fa55c7d93c830cd1c1 /tests/lib_test.php | |
parent | 6f6d50776c83b3af3a81f2d74e3a5ca3a5eef31c (diff) | |
download | local_webhooks-08a54b0e9fda7ae9a744fb99c59cd8c73b1725a0.tar.xz local_webhooks-08a54b0e9fda7ae9a744fb99c59cd8c73b1725a0.zip |
Primary test function for add, update and delete
Signed-off-by: Valentin Popov <info@valentineus.link>
Diffstat (limited to 'tests/lib_test.php')
-rw-r--r-- | tests/lib_test.php | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/tests/lib_test.php b/tests/lib_test.php new file mode 100644 index 0000000..85830de --- /dev/null +++ b/tests/lib_test.php @@ -0,0 +1,226 @@ +<?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/>. + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/local/webhooks/lib.php'); + +/** + * Class local_webhooks_lib_testcase + */ +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(); + + $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', + ]; + + $serviceid = local_webhooks_api::create_service($data); + + $events = $DB->get_records(LW_TABLE_EVENTS); + $services = $DB->get_records(LW_TABLE_SERVICES); + + // Check the table of services. + 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); + + // Check the table of events. + self::assertCount(count($data['events']), $events); + + foreach ($events as $event) { + self::assertEquals($serviceid, $event->serviceid); + self::assertContains($event->name, $data['events']); + } + } + + /** + * Test deletion of the service. + * + * @group local_webhooks + * + * @throws \dml_exception + * @throws \moodle_exception + */ + public function test_deleting() { + global $DB; + + $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', + ]; + + // Creating a service. + $serviceid = local_webhooks_api::create_service($data); + + $events = $DB->get_records(LW_TABLE_EVENTS); + $services = $DB->get_records(LW_TABLE_SERVICES); + + // Check the table of services. + 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); + + // Check the table of events. + self::assertCount(count($data['events']), $events); + + foreach ($events as $event) { + self::assertEquals($serviceid, $event->serviceid); + self::assertContains($event->name, $data['events']); + } + + // Deleting a service. + self::assertTrue(local_webhooks_api::delete_service($serviceid)); + self::assertCount(0, $DB->get_records(LW_TABLE_EVENTS)); + self::assertCount(0, $DB->get_records(LW_TABLE_SERVICES)); + } + + /** + * Testing of the service update. + * + * @group local_webhooks + * + * @throws \dml_exception + * @throws \moodle_exception + */ + public function test_updating() { + global $DB; + + $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', + ]; + + $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', + ]; + + // Creating a service. + $serviceid = local_webhooks_api::create_service($data1); + $events = $DB->get_records(LW_TABLE_EVENTS); + $services = $DB->get_records(LW_TABLE_SERVICES); + + // Check the table of services. + self::assertCount(1, $services); + $service = array_shift($services); + + self::assertEquals($data1['header'], $service->header); + self::assertEquals($data1['name'], $service->name); + self::assertEquals($data1['point'], $service->point); + self::assertEquals($data1['status'], (int) $service->status); + self::assertEquals($data1['token'], $service->token); + self::assertEquals($serviceid, $service->id); + + // Check the table of events. + self::assertCount(count($data1['events']), $events); + + foreach ($events as $event) { + self::assertEquals($serviceid, $event->serviceid); + self::assertContains($event->name, $data1['events']); + } + + // Updating a service. + self::assertTrue(local_webhooks_api::update_service(array_merge($data2, ['id' => $serviceid]))); + + $events = $DB->get_records(LW_TABLE_EVENTS); + $services = $DB->get_records(LW_TABLE_SERVICES); + + // Check the table of services. + 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); + + // Check the table of events. + self::assertCount(count($data2['events']), $events); + + foreach ($events as $event) { + self::assertEquals($serviceid, $event->serviceid); + self::assertContains($event->name, $data2['events']); + } + } +}
\ No newline at end of file |