18
18
19
19
use moodle_url ;
20
20
use moodle_exception ;
21
+ use tool_dynamic_cohorts \event \rule_created ;
22
+ use tool_dynamic_cohorts \event \rule_deleted ;
23
+ use tool_dynamic_cohorts \event \rule_updated ;
21
24
22
25
/**
23
26
* Tests for rule manager class.
@@ -255,7 +258,7 @@ public function test_process_rule_form_with_cohort_managed_by_another_rule() {
255
258
}
256
259
257
260
/**
258
- * Test trying to submit form data and not updating the cohort.
261
+ * Test submitting form data keeps cohort.
259
262
*/
260
263
public function test_process_rule_form_update_rule_form_keeping_cohort () {
261
264
global $ DB ;
@@ -273,6 +276,43 @@ public function test_process_rule_form_update_rule_form_keeping_cohort() {
273
276
$ formdata = ['id ' => $ rule ->get ('id ' ), 'name ' => 'Test1 ' ,
274
277
'cohortid ' => $ cohort ->id , 'description ' => 'D ' , 'conditionjson ' => '' , 'bulkprocessing ' => 1 ];
275
278
rule_manager::process_form ((object )$ formdata );
279
+
280
+ $ this ->assertEquals ('tool_dynamic_cohorts ' , $ DB ->get_field ('cohort ' , 'component ' , ['id ' => $ cohort ->id ]));
281
+ }
282
+
283
+ /**
284
+ * Test triggering events.
285
+ */
286
+ public function test_process_rule_form_triggers_events () {
287
+ $ this ->resetAfterTest ();
288
+
289
+ $ cohort = $ this ->getDataGenerator ()->create_cohort ();
290
+
291
+ $ eventsink = $ this ->redirectEvents ();
292
+ $ formdata = ['name ' => 'Test1 ' , 'cohortid ' => $ cohort ->id , 'description ' => 'D ' ,
293
+ 'conditionjson ' => '' , 'bulkprocessing ' => 1 ];
294
+ $ rule = rule_manager::process_form ((object ) $ formdata );
295
+
296
+ $ events = array_filter ($ eventsink ->get_events (), function ($ event ) {
297
+ return $ event instanceof rule_created;
298
+ });
299
+
300
+ $ this ->assertCount (1 , $ events );
301
+ $ this ->assertEquals ($ rule ->get ('id ' ), reset ($ events )->other ['ruleid ' ]);
302
+ $ eventsink ->clear ();
303
+
304
+ // Update the rule, changing the name. Should work as cohort is the same.
305
+ $ formdata = ['id ' => $ rule ->get ('id ' ), 'name ' => 'Test1 ' ,
306
+ 'cohortid ' => $ cohort ->id , 'description ' => 'D ' , 'conditionjson ' => '' , 'bulkprocessing ' => 1 ];
307
+ rule_manager::process_form ((object ) $ formdata );
308
+
309
+ $ events = array_filter ($ eventsink ->get_events (), function ($ event ) {
310
+ return $ event instanceof rule_updated;
311
+ });
312
+
313
+ $ this ->assertCount (1 , $ events );
314
+ $ this ->assertEquals ($ rule ->get ('id ' ), reset ($ events )->other ['ruleid ' ]);
315
+ $ eventsink ->clear ();
276
316
}
277
317
278
318
/**
@@ -346,4 +386,28 @@ public function test_deleting_rule_releases_cohorts() {
346
386
rule_manager::delete_rule ($ rule2 );
347
387
$ this ->assertEquals ('' , $ DB ->get_field ('cohort ' , 'component ' , ['id ' => $ cohort2 ->id ]));
348
388
}
389
+
390
+ /**
391
+ * Test deleting a rule triggers event.
392
+ */
393
+ public function test_deleting_rule_triggers_event () {
394
+ $ this ->resetAfterTest ();
395
+
396
+ $ cohort = $ this ->getDataGenerator ()->create_cohort (['component ' => 'tool_dynamic_cohorts ' ]);
397
+
398
+ $ rule = new rule (0 , (object )['name ' => 'Test rule ' , 'cohortid ' => $ cohort ->id ]);
399
+ $ rule ->save ();
400
+ $ expectedruleid = $ rule ->get ('id ' );
401
+
402
+ $ eventsink = $ this ->redirectEvents ();
403
+
404
+ rule_manager::delete_rule ($ rule );
405
+
406
+ $ events = array_filter ($ eventsink ->get_events (), function ($ event ) {
407
+ return $ event instanceof rule_deleted;
408
+ });
409
+
410
+ $ this ->assertCount (1 , $ events );
411
+ $ this ->assertEquals ($ expectedruleid , reset ($ events )->other ['ruleid ' ]);
412
+ }
349
413
}
0 commit comments