From 08a54b0e9fda7ae9a744fb99c59cd8c73b1725a0 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 4 May 2019 20:51:54 +0400 Subject: Primary test function for add, update and delete Signed-off-by: Valentin Popov --- tests/lib_test.php | 226 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 tests/lib_test.php (limited to 'tests/lib_test.php') 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 @@ +. + +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 -- cgit v1.2.3