Added external function delete service

Signed-off-by: Valentin Popov <info@valentineus.link>
This commit is contained in:
Valentin Popov 2019-05-11 00:17:20 +04:00
parent 6b18cffc97
commit ed0ce52060
Signed by: Valentin Popov
GPG Key ID: 269A00ACA90A8EA3
3 changed files with 95 additions and 13 deletions

View File

@ -31,10 +31,52 @@ use local_webhooks\local\record;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
final class local_webhooks_external extends external_api { final class local_webhooks_external extends external_api {
/**
* Delete the existing service.
*
* @param int $serviceid
*
* @return bool
*
* @throws \dml_exception
* @throws \invalid_parameter_exception
* @throws \restricted_context_exception
*/
public static function del_service(int $serviceid): bool {
$parameters = self::validate_parameters(self::del_service_parameters(), [
'serviceid' => $serviceid,
]);
$context = context_system::instance();
self::validate_context($context);
return api::del_service($parameters['serviceid']);
}
/**
* Returns description of the method parameters.
*
* @return \external_function_parameters
*/
public static function del_service_parameters(): external_function_parameters {
return new external_function_parameters([
'serviceid' => new external_value(PARAM_INT, 'The service\'s ID.'),
], '');
}
/**
* Returns description of the method result value.
*
* @return \external_value
*/
public static function del_service_returns(): external_value {
return new external_value(PARAM_BOOL, 'The result operation.');
}
/** /**
* Get data by service. * Get data by service.
* *
* @param int $serviceid Service's ID. * @param int $serviceid
* *
* @return \local_webhooks\local\record * @return \local_webhooks\local\record
* *
@ -43,7 +85,9 @@ final class local_webhooks_external extends external_api {
* @throws \restricted_context_exception * @throws \restricted_context_exception
*/ */
public static function get_service(int $serviceid): record { public static function get_service(int $serviceid): record {
$parameters = self::validate_parameters(self::get_service_parameters(), ['serviceid' => $serviceid]); $parameters = self::validate_parameters(self::get_service_parameters(), [
'serviceid' => $serviceid,
]);
$context = context_system::instance(); $context = context_system::instance();
self::validate_context($context); self::validate_context($context);
@ -52,7 +96,7 @@ final class local_webhooks_external extends external_api {
} }
/** /**
* Returns description of method parameters. * Returns description of the method parameters.
* *
* @return \external_function_parameters * @return \external_function_parameters
*/ */
@ -63,7 +107,7 @@ final class local_webhooks_external extends external_api {
} }
/** /**
* Returns description of method parameters. * Returns description of the method result value.
* *
* @return \external_single_structure * @return \external_single_structure
*/ */
@ -115,7 +159,7 @@ final class local_webhooks_external extends external_api {
} }
/** /**
* Returns description of method parameters. * Returns description of the method parameters.
* *
* @return \external_function_parameters * @return \external_function_parameters
*/ */
@ -135,7 +179,7 @@ final class local_webhooks_external extends external_api {
} }
/** /**
* Returns description of method parameters. * Returns description of the method result value.
* *
* @return \external_multiple_structure * @return \external_multiple_structure
*/ */

View File

@ -122,10 +122,10 @@ final class local_webhooks_api_testcase extends advanced_testcase {
self::assertCount(0, $DB->get_records(LW_TABLE_EVENTS)); self::assertCount(0, $DB->get_records(LW_TABLE_EVENTS));
self::assertCount(0, $DB->get_records(LW_TABLE_SERVICES)); self::assertCount(0, $DB->get_records(LW_TABLE_SERVICES));
// Testing correct delete record of the record's list.
$ids = []; $ids = [];
$total = random_int(5, 20); $total = random_int(5, 20);
// Testing correct delete record of the record's list.
for ($i = 0; $i < $total; $i++) { for ($i = 0; $i < $total; $i++) {
$record = self::get_random_record(); $record = self::get_random_record();
$ids[] = api::add_service($record); $ids[] = api::add_service($record);

View File

@ -64,6 +64,47 @@ final class local_webhooks_external_testcase extends externallib_advanced_testca
return $record; return $record;
} }
/**
* Testing the external delete service.
*
* @throws \ReflectionException
* @throws \coding_exception
* @throws \dml_exception
* @throws \invalid_parameter_exception
* @throws \invalid_response_exception
* @throws \restricted_context_exception
*/
public function test_deleting() {
$this->resetAfterTest();
self::setAdminUser();
// Testing correct delete record of the database.
$record = self::get_random_record();
$record->id = api::add_service($record);
$return = local_webhooks_external::del_service($record->id);
$return = external_api::clean_returnvalue(local_webhooks_external::del_service_returns(), $return);
self::assertEquals(0, api::get_total_count());
self::assertInternalType('bool', $return);
$ids = [];
$total = random_int(5, 20);
// Testing correct delete record of the record's list.
for ($i = 0; $i < $total; $i++) {
$record = self::get_random_record();
$ids[] = api::add_service($record);
}
self::assertEquals(count($ids), api::get_total_count());
$return = local_webhooks_external::del_service($ids[array_rand($ids, 1)]);
$return = external_api::clean_returnvalue(local_webhooks_external::del_service_returns(), $return);
self::assertEquals(count($ids) - 1, api::get_total_count());
self::assertInternalType('bool', $return);
}
/** /**
* Testing external get record's data. * Testing external get record's data.
* *
@ -75,9 +116,8 @@ final class local_webhooks_external_testcase extends externallib_advanced_testca
* @throws \restricted_context_exception * @throws \restricted_context_exception
*/ */
public function test_get_service() { public function test_get_service() {
self::setAdminUser();
$this->resetAfterTest(); $this->resetAfterTest();
self::setAdminUser();
// Creating a new record. // Creating a new record.
$record = self::get_random_record(); $record = self::get_random_record();
@ -114,9 +154,8 @@ final class local_webhooks_external_testcase extends externallib_advanced_testca
* @throws \restricted_context_exception * @throws \restricted_context_exception
*/ */
public function test_get_services() { public function test_get_services() {
self::setAdminUser();
$this->resetAfterTest(); $this->resetAfterTest();
self::setAdminUser();
$records = []; $records = [];
$total = random_int(5, 10); $total = random_int(5, 10);
@ -168,9 +207,8 @@ final class local_webhooks_external_testcase extends externallib_advanced_testca
* @throws \restricted_context_exception * @throws \restricted_context_exception
*/ */
public function test_get_services_with_conditions() { public function test_get_services_with_conditions() {
self::setAdminUser();
$this->resetAfterTest(); $this->resetAfterTest();
self::setAdminUser();
$records = []; $records = [];
$total = random_int(5, 10); $total = random_int(5, 10);