diff options
-rw-r--r-- | externallib.php | 204 | ||||
-rw-r--r-- | tests/external_test.php | 78 |
2 files changed, 109 insertions, 173 deletions
diff --git a/externallib.php b/externallib.php index 1ccfd7c..1f1dfeb 100644 --- a/externallib.php +++ b/externallib.php @@ -14,211 +14,69 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. -/** - * This file defines the plugin's external functions. - * - * @copyright 2018 '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(); -require_once($CFG->dirroot . '/local/webhooks/lib.php'); +global $CFG; + +require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php'); require_once($CFG->libdir . '/externallib.php'); +use local_webhooks\local\api; + /** - * External functions. + * WebHooks external functions. * - * @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 */ -class local_webhooks_external extends external_api { - /** - * Returns description of method parameters. - * - * @return \external_function_parameters - * - * @since Moodle 2.2 - * @since Moodle 2.9 Options available - */ - public static function get_service_parameters() { - return new external_function_parameters( - array( - 'serviceid' => new external_value(PARAM_INT, 'Service ID.'), - ) - ); - } - +final class local_webhooks_external extends external_api { /** - * Get information about the service. + * Testing get to a service. * - * @param $serviceid + * @param int $serviceid Service's ID. * - * @return object - * - * @since Moodle 2.2 - * @since Moodle 2.9 Options available + * @return \local_webhooks\local\record * * @throws \dml_exception * @throws \invalid_parameter_exception - * @throws \moodle_exception * @throws \restricted_context_exception */ - public static function get_service($serviceid) { - $parameters = self::validate_parameters(self::get_service_parameters(), array('serviceid' => $serviceid)); + public static function get_service(int $serviceid) { + $parameters = self::validate_parameters(self::get_service_parameters(), ['serviceid' => $serviceid]); $context = context_system::instance(); self::validate_context($context); - return local_webhooks_api::get_service($parameters['serviceid']); - } - - /** - * Returns description of method result value. - * - * @return \external_single_structure - * - * @since Moodle 2.2 - * @since Moodle 2.9 Options available - */ - public static function get_service_returns() { - return new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Service ID.'), - 'header' => new external_value(PARAM_RAW, 'Type of outgoing header.'), - 'name' => new external_value(PARAM_RAW, 'Name of the service.'), - 'point' => new external_value(PARAM_URL, 'Point of delivery of notifications.'), - 'status' => new external_value(PARAM_BOOL, 'Current status of the service.'), - 'token' => new external_value(PARAM_RAW, 'Token for verification of requests.'), - 'events' => new external_multiple_structure( - new external_value(PARAM_RAW, 'Event name.'), 'List of events.' - ), - ) - ); + return api::get_service($parameters['serviceid']); } /** * Returns description of method parameters. * * @return \external_function_parameters - * - * @since Moodle 2.2 - * @since Moodle 2.9 Options available - */ - public static function get_services_parameters() { - return new external_function_parameters(array()); - } - - /** - * Get a list of services. - * - * @return array - * - * @since Moodle 2.2 - * @since Moodle 2.9 Options available - * - * @throws \dml_exception - * @throws \invalid_parameter_exception - * @throws \restricted_context_exception */ - public static function get_services() { - $context = context_system::instance(); - self::validate_context($context); - - return local_webhooks_api::get_services(); - } - - /** - * Returns description of method result value. - * - * @return \external_multiple_structure - * - * @since Moodle 2.2 - * @since Moodle 2.9 Options available - */ - public static function get_services_returns() { - return new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Service ID.'), - 'header' => new external_value(PARAM_RAW, 'Type of outgoing header.'), - 'name' => new external_value(PARAM_RAW, 'Name of the service.'), - 'point' => new external_value(PARAM_URL, 'Point of delivery of notifications.'), - 'status' => new external_value(PARAM_BOOL, 'Current status of the service.'), - 'token' => new external_value(PARAM_RAW, 'Token for verification of requests.'), - 'events' => new external_multiple_structure( - new external_value(PARAM_RAW, 'Event name.'), 'List of events.' - ), - ) - ) - ); + public static function get_service_parameters() { + return new external_function_parameters([ + 'serviceid' => new external_value(PARAM_INT, 'The service\'s ID.'), + ]); } /** * Returns description of method parameters. * - * @return \external_function_parameters - * - * @since Moodle 2.2 - * @since Moodle 2.9 Options available - */ - public static function get_services_by_event_parameters() { - return new external_function_parameters( - array( - 'eventname' => new external_value(PARAM_RAW, 'Event name.'), - ) - ); - } - - /** - * Get the list of services subscribed to the event. - * - * @param $eventname - * - * @return array - * - * @since Moodle 2.2 - * @since Moodle 2.9 Options available - * - * @throws \dml_exception - * @throws \invalid_parameter_exception - * @throws \moodle_exception - * @throws \restricted_context_exception - */ - public static function get_services_by_event($eventname) { - $parameters = self::validate_parameters(self::get_services_by_event_parameters(), array('eventname' => $eventname)); - - $context = context_system::instance(); - self::validate_context($context); - - return local_webhooks_api::get_services_by_event($parameters['eventname']); - } - - /** - * Returns description of method result value. - * - * @return \external_multiple_structure - * - * @since Moodle 2.2 - * @since Moodle 2.9 Options available + * @return \external_single_structure */ - public static function get_services_by_event_returns() { - return new external_multiple_structure( - new external_single_structure( - array( - 'id' => new external_value(PARAM_INT, 'Service ID.'), - 'header' => new external_value(PARAM_RAW, 'Type of outgoing header.'), - 'name' => new external_value(PARAM_RAW, 'Name of the service.'), - 'point' => new external_value(PARAM_URL, 'Point of delivery of notifications.'), - 'status' => new external_value(PARAM_BOOL, 'Current status of the service.'), - 'token' => new external_value(PARAM_RAW, 'Token for verification of requests.'), - 'events' => new external_multiple_structure( - new external_value(PARAM_RAW, 'Event name.'), 'List of events.' - ), - ) - ) - ); + public static function get_service_returns() { + return new external_single_structure([ + 'header' => new external_value(PARAM_RAW, 'The request\'s header or type'), + 'id' => new external_value(PARAM_INT, 'The service\'s ID.'), + 'name' => new external_value(PARAM_RAW, 'The service\'s name.'), + 'point' => new external_value(PARAM_URL, 'The service\'s endpoint.'), + 'status' => new external_value(PARAM_BOOL, 'The service\'s status.'), + 'token' => new external_value(PARAM_RAW, 'The service\'s secret key.'), + 'events' => new external_multiple_structure( + new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.' + ), + ]); } }
\ No newline at end of file diff --git a/tests/external_test.php b/tests/external_test.php new file mode 100644 index 0000000..2a9be98 --- /dev/null +++ b/tests/external_test.php @@ -0,0 +1,78 @@ +<?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/externallib.php'); +require_once($CFG->dirroot . '/webservice/tests/helpers.php'); + +use local_webhooks\local\api; +use local_webhooks\local\record; + +/** + * Testing external functions. + * + * @copyright 2019 'Valentin Popov' <info@valentineus.link> + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +final class local_webhooks_external_testcase extends externallib_advanced_testcase { + /** + * Testing external get record's data. + * + * @throws \ReflectionException + * @throws \coding_exception + * @throws \dml_exception + * @throws \invalid_response_exception + */ + public function test_get_service() { + self::setAdminUser(); + + $this->resetAfterTest(); + + $record = new record(); + $record->events = array_rand(api::get_events(), random_int(2, 10)); + $record->header = 'application/json'; + $record->name = 'Example name'; + $record->point = 'http://example.org/'; + $record->status = true; + $record->token = generate_uuid(); + + // Creating a new record. + $record->id = api::create_service($record); + + $return = local_webhooks_external::get_service($record->id); + $return = external_api::clean_returnvalue(local_webhooks_external::get_service_returns(), $return); + self::assertInternalType('array', $return); + + // Testing the main fields. + self::assertEquals($record->header, $return['header']); + self::assertEquals($record->id, $return['id']); + self::assertEquals($record->name, $return['name']); + self::assertEquals($record->point, $return['point']); + self::assertEquals($record->status, (int) $return['status']); + self::assertEquals($record->token, $return['token']); + + // Testing an event's list. + self::assertInternalType('array', $return['events']); + self::assertNotCount(0, $return['events']); + + foreach ($return['events'] as $event) { + self::assertContains($event, $record->events); + } + } +}
\ No newline at end of file |