aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValentin Popov <info@valentineus.link>2019-05-11 02:45:50 +0300
committerValentin Popov <info@valentineus.link>2019-05-11 02:45:50 +0300
commit7dc023b05a9292cb9e5ea4fa65e86668c857cd82 (patch)
tree8c882e9baff3a11f951f59c45c8f725a7a473c88
parent3a865453a46a2c0f7fcb0b35bd31948620e687d7 (diff)
downloadlocal_webhooks-7dc023b05a9292cb9e5ea4fa65e86668c857cd82.tar.xz
local_webhooks-7dc023b05a9292cb9e5ea4fa65e86668c857cd82.zip
Added external function update existing service
Signed-off-by: Valentin Popov <info@valentineus.link>
-rw-r--r--externallib.php91
-rw-r--r--tests/external_test.php72
2 files changed, 149 insertions, 14 deletions
diff --git a/externallib.php b/externallib.php
index d6e3604..1e4884a 100644
--- a/externallib.php
+++ b/externallib.php
@@ -45,17 +45,13 @@ final class local_webhooks_external extends external_api {
*/
public static function add_service(array $conditions): int {
$parameters = self::validate_parameters(self::add_service_parameters(), [
- 'events' => $conditions['events'],
- 'header' => $conditions['header'],
- 'name' => $conditions['name'],
- 'point' => $conditions['point'],
- 'status' => $conditions['status'],
- 'token' => $conditions['token'],
+ 'record' => array_filter($conditions),
]);
$context = context_system::instance();
self::validate_context($context);
+ $parameters = array_filter($parameters['record']);
$record = new record($parameters);
return api::add_service($record);
@@ -68,14 +64,16 @@ final class local_webhooks_external extends external_api {
*/
public static function add_service_parameters(): external_function_parameters {
return new external_function_parameters([
- 'events' => new external_multiple_structure(
- new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.'
- ),
- 'header' => new external_value(PARAM_RAW, 'The request\'s header or type'),
- '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.'),
+ 'record' => new external_single_structure([
+ 'events' => new external_multiple_structure(
+ new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.', false
+ ),
+ 'header' => new external_value(PARAM_RAW, 'The request\'s header or type', false, 'application/json'),
+ '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.', false, true),
+ 'token' => new external_value(PARAM_RAW, 'The service\'s secret key.'),
+ ], ''),
], '');
}
@@ -300,4 +298,69 @@ final class local_webhooks_external extends external_api {
], ''), ''
);
}
+
+ /**
+ * Update the existing service.
+ *
+ * @param array $conditions
+ *
+ * @return bool
+ *
+ * @throws \coding_exception
+ * @throws \dml_exception
+ * @throws \invalid_parameter_exception
+ * @throws \restricted_context_exception
+ */
+ public static function set_service(array $conditions): bool {
+ $conditions = array_filter($conditions);
+ $conditions['events'] = $conditions['events'] ?? [];
+
+ $parameters = self::validate_parameters(self::set_service_parameters(), [
+ 'record' => $conditions,
+ ]);
+
+ $context = context_system::instance();
+ self::validate_context($context);
+
+ $parameters = array_filter($parameters['record']);
+ $record = api::get_service($parameters['id']);
+
+ foreach ($parameters as $index => $value) {
+ if (property_exists($record, $index)) {
+ $record->$index = $value;
+ }
+ }
+
+ return api::set_service($record);
+ }
+
+ /**
+ * Returns description of the method parameters.
+ *
+ * @return \external_function_parameters
+ */
+ public static function set_service_parameters(): external_function_parameters {
+ return new external_function_parameters([
+ 'record' => new external_single_structure([
+ 'events' => new external_multiple_structure(
+ new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.', false
+ ),
+ 'header' => new external_value(PARAM_RAW, 'The request\'s header or type', false),
+ 'id' => new external_value(PARAM_INT, 'The service\'s ID.'),
+ 'name' => new external_value(PARAM_RAW, 'The service\'s name.', false),
+ 'point' => new external_value(PARAM_URL, 'The service\'s endpoint.', false),
+ 'status' => new external_value(PARAM_BOOL, 'The service\'s status.', false),
+ 'token' => new external_value(PARAM_RAW, 'The service\'s secret key.', false),
+ ], ''),
+ ], '');
+ }
+
+ /**
+ * Returns description of the method result value.
+ *
+ * @return \external_value
+ */
+ public static function set_service_returns(): external_value {
+ return new external_value(PARAM_BOOL, '');
+ }
} \ No newline at end of file
diff --git a/tests/external_test.php b/tests/external_test.php
index 387db86..e5374f6 100644
--- a/tests/external_test.php
+++ b/tests/external_test.php
@@ -311,4 +311,76 @@ final class local_webhooks_external_testcase extends externallib_advanced_testca
self::assertNotEquals($service1['id'], $service2['id']);
}
+
+ /**
+ * Testing full update parameters of the service.
+ *
+ * @throws \ReflectionException
+ * @throws \coding_exception
+ * @throws \dml_exception
+ * @throws \invalid_parameter_exception
+ * @throws \restricted_context_exception
+ */
+ public function test_updating_full() {
+ $this->resetAfterTest();
+ self::setAdminUser();
+
+ $record = self::get_random_record();
+ $record2 = self::get_random_record();
+ $record2->id = api::add_service($record);
+
+ $return = local_webhooks_external::set_service((array) $record2);
+ $return = external_api::validate_parameters(local_webhooks_external::set_service_returns(), $return);
+ self::assertInternalType('bool', $return);
+
+ $service = api::get_service($record2->id);
+ self::assertEquals($record2->id, $service->id);
+ self::assertEquals($record2->header, $service->header);
+ self::assertEquals($record2->name, $service->name);
+ self::assertEquals($record2->point, $service->point);
+ self::assertEquals($record2->status, $service->status);
+ self::assertEquals($record2->token, $service->token);
+
+ self::assertInternalType('array', $service->events);
+ self::assertCount(count($record2->events), $service->events);
+ foreach ($service->events as $event) {
+ self::assertContains($event, $record2->events);
+ }
+ }
+
+ /**
+ * Testing selective update parameters of the service.
+ *
+ * @throws \ReflectionException
+ * @throws \coding_exception
+ * @throws \dml_exception
+ * @throws \invalid_parameter_exception
+ * @throws \restricted_context_exception
+ */
+ public function test_updating_short() {
+ $this->resetAfterTest();
+ self::setAdminUser();
+
+ $record = self::get_random_record();
+ $record2 = self::get_random_record();
+ $record->id = api::add_service($record);
+
+ $return = local_webhooks_external::set_service(['id' => $record->id, 'name' => $record2->name, 'point' => $record2->point]);
+ $return = external_api::validate_parameters(local_webhooks_external::set_service_returns(), $return);
+ self::assertInternalType('bool', $return);
+
+ $service = api::get_service($record->id);
+ self::assertEquals($record->header, $service->header);
+ self::assertEquals($record->id, $service->id);
+ self::assertEquals($record->status, $service->status);
+ self::assertEquals($record->token, $service->token);
+ self::assertEquals($record2->name, $service->name);
+ self::assertEquals($record2->point, $service->point);
+
+ self::assertInternalType('array', $service->events);
+ self::assertCount(count($record->events), $service->events);
+ foreach ($service->events as $event) {
+ self::assertContains($event, $record->events);
+ }
+ }
} \ No newline at end of file