Skip to content

Commit 70e04a5

Browse files
committed
issue #116: add WS to delete and toggle rules
1 parent ba32823 commit 70e04a5

File tree

2 files changed

+82
-22
lines changed

2 files changed

+82
-22
lines changed

classes/external/rules.php

+15-17
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,26 @@ public static function delete_rules_parameters(): external_function_parameters {
5858
* @return array
5959
*/
6060
public static function delete_rules(array $ruleids): array {
61-
global $DB;
62-
6361
self::validate_parameters(self::delete_rules_parameters(), ['ruleids' => $ruleids]);
6462

6563
$context = context_system::instance();
6664
self::validate_context($context);
6765
require_capability('tool/dynamic_cohorts:manage', $context);
6866

6967
// We would like to treat deletion for multiple rules as one operation.
70-
// If one failed we would like to fail whole call and roll back.
71-
$transaction = $DB->start_delegated_transaction();
72-
try {
73-
foreach ($ruleids as $ruleid) {
74-
$rule = rule::get_record(['id' => (int) $ruleid]);
75-
if (empty($rule)) {
76-
throw new invalid_parameter_exception('Rule does not exist. ID: ' . $ruleid);
77-
}
78-
rule_manager::delete_rule($rule);
79-
//$transaction->allow_commit();
68+
// So let's check that all rules exist and then delete them.
69+
// Otherwise throw an exception and fail whole WS call.
70+
$rulestodelete = [];
71+
foreach ($ruleids as $ruleid) {
72+
$rule = rule::get_record(['id' => (int) $ruleid]);
73+
if (empty($rule)) {
74+
throw new invalid_parameter_exception('Rule does not exist. ID: ' . $ruleid);
8075
}
81-
} catch (throwable $ex) {
82-
$transaction->rollback($ex);
76+
$rulestodelete[] = $rule;
77+
}
78+
79+
foreach ($rulestodelete as $rule) {
80+
rule_manager::delete_rule($rule);
8381
}
8482

8583
return [];
@@ -112,14 +110,14 @@ public static function toggle_status_parameters(): external_function_parameters
112110
* @return array
113111
*/
114112
public static function toggle_status(int $ruleid): array {
115-
self::validate_parameters(self::delete_rules_parameters(), ['ruleid' => $ruleid]);
113+
self::validate_parameters(self::toggle_status_parameters(), ['ruleid' => $ruleid]);
116114

117115
self::validate_context(context_system::instance());
118116
require_capability('tool/dynamic_cohorts:manage', context_system::instance());
119117

120118
$rule = rule::get_record(['id' => $ruleid]);
121119
if (empty($rule)) {
122-
throw new invalid_parameter_exception('Rule does not exist.');
120+
throw new invalid_parameter_exception('Rule does not exist. ID: ' . $ruleid);
123121
}
124122

125123
if ($rule->is_broken()) {
@@ -128,7 +126,7 @@ public static function toggle_status(int $ruleid): array {
128126
$rule->save();
129127
rule_updated::create(['other' => ['ruleid' => $rule->get('id')]])->trigger();
130128

131-
throw new invalid_parameter_exception('A broken rule can\'t be enabled');
129+
throw new invalid_parameter_exception('A broken rule can\'t be enabled ID: ' . $ruleid);
132130
}
133131

134132
$newvalue = (int) !$rule->is_enabled();

tests/external/rules_test.php

+67-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function test_delete_rules_permissions() {
6363
}
6464

6565
/**
66-
* Test can get total.
66+
* Test exception thrown on deleting invalid rule.
6767
*/
6868
public function test_exception_delete_rules_when_one_is_invalid() {
6969
$this->resetAfterTest();
@@ -79,7 +79,7 @@ public function test_exception_delete_rules_when_one_is_invalid() {
7979
}
8080

8181
/**
82-
* Test can get total.
82+
* Test rules are not deleted if one is invalid.
8383
*/
8484
public function test_delete_rules_keep_rules_when_one_is_invalid() {
8585
$this->resetAfterTest();
@@ -93,9 +93,6 @@ public function test_delete_rules_keep_rules_when_one_is_invalid() {
9393

9494
$this->assertCount(2, rule::get_records());
9595

96-
$this->expectException(\required_capability_exception::class);
97-
$this->expectExceptionMessage('Rule does not exist. ID: 777');
98-
9996
try {
10097
rules::delete_rules([$rule1->get('id'), $rule2->get('id'), 777]);
10198
} catch (\invalid_parameter_exception $exception) {
@@ -106,4 +103,69 @@ public function test_delete_rules_keep_rules_when_one_is_invalid() {
106103
$this->assertCount(2, rule::get_records());
107104
}
108105
}
106+
107+
/**
108+
* Test exception if rule is not exist.
109+
*/
110+
public function test_toggle_status_exception_on_invalid_rule() {
111+
$this->resetAfterTest();
112+
113+
$this->setAdminUser();
114+
$this->expectException(\invalid_parameter_exception::class);
115+
$this->expectExceptionMessage('Rule does not exist. ID: 777');
116+
117+
rules::toggle_status(777);
118+
}
119+
120+
/**
121+
* Test required permissions.
122+
*/
123+
public function test_toggle_status_permissions() {
124+
$this->resetAfterTest();
125+
$user = $this->getDataGenerator()->create_user();
126+
$this->setUser($user);
127+
128+
$this->expectException(\required_capability_exception::class);
129+
$this->expectExceptionMessage('Sorry, but you do not currently have permissions to do that (Manage rules).');
130+
131+
rules::toggle_status(777);
132+
}
133+
134+
/**
135+
* Test exception is thrown trying to toggle a broken rule
136+
*/
137+
public function test_exception_toggle_status_on_broken_rule() {
138+
$this->resetAfterTest();
139+
$this->setAdminUser();
140+
141+
$rule = new rule(0, (object)['name' => 'Test rule 1']);
142+
$rule->set('broken', 1);
143+
$rule->save();
144+
145+
$this->expectException(\invalid_parameter_exception::class);
146+
$this->expectExceptionMessage('A broken rule can\'t be enabled ID: ' . $rule->get('id'));
147+
148+
rules::toggle_status($rule->get('id'));
149+
}
150+
151+
/**
152+
* Test toggling status on rule.
153+
*/
154+
public function test_toggle_status_on_rule() {
155+
$this->resetAfterTest();
156+
$this->setAdminUser();
157+
158+
$rule = new rule(0, (object)['name' => 'Test rule 1']);
159+
$rule->save();
160+
161+
$this->assertFalse($rule->is_enabled());
162+
163+
rules::toggle_status($rule->get('id'));
164+
$rule = rule::get_record(['id' => $rule->get('id')]);
165+
$this->assertTrue($rule->is_enabled());
166+
167+
rules::toggle_status($rule->get('id'));
168+
$rule = rule::get_record(['id' => $rule->get('id')]);
169+
$this->assertFalse($rule->is_enabled());
170+
}
109171
}

0 commit comments

Comments
 (0)