Skip to content

Commit

Permalink
exception when no roles are defined is catched issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
NinaHerrmann committed Aug 23, 2017
1 parent b70595e commit 59bc3cd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
13 changes: 11 additions & 2 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function check_course($course) {
// Checks whether role is represented in course.
$hasuserincharge = $this->check_course_has_role($course->id);

// When an exception was thrown the course is not handled.
if ($hasuserincharge === null) {
return trigger_response::next();
}
$trigger = $this->handle_course($hasuserincharge, $course->id);
if ($trigger) {
return trigger_response::trigger();
Expand All @@ -56,11 +60,16 @@ public function check_course($course) {
* Checks whether a specific course has a responsible person.
* This check is based on roles. The responsible roles are fixed in the admin settings.
* @param $courseid
* @return bool
* @return boolean | null
*/
private function check_course_has_role($courseid) {
// Gets roles from the settings.
$roles = $this->get_roles();
try {
$roles = $this->get_roles();
} catch (\coding_exception $e) {
// Writhe in Log without writing it repeatedly.
return null;
}
$context = \context_course::instance($courseid);
// Returns all roles used in context and in parent context. Therefore be carefully with global roles!
$courseroles = get_roles_used_in_context($context);
Expand Down
11 changes: 7 additions & 4 deletions tests/cleanupcoursestrigger_byrole_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,20 @@ public function test_changedelay() {
$this->assertEquals(false, $exist);
}
/**
* Test whether an exception is thrown when no roles are defined.
* Test whether trigger::next() is thrown when no roles are defined.
*/
public function test_noroles_exception() {
global $DB;
$generator = $this->getDataGenerator()->get_plugin_generator('cleanupcoursestrigger_byrole');
$data = $generator->test_create_preparation();
set_config('roles', '', 'cleanupcoursestrigger_byrole');
$mytrigger = new byrole_test();
$mytrigger->reset_roles();
$this->expectException('coding_exception');
// Which course is checked is insignificant any course would throw an coding exception.
$mytrigger->check_course($data['teachercourse']);
// Although the course would be deleted it is triggered as next.
$nothandle = $mytrigger->check_course($data['norolefoundcourse2']);
$exist = $DB->record_exists('cleanupcoursestrigger_byrole', array('courseid' => $data['norolefoundcourse2']->id));
$this->assertEquals(trigger_response::next(), $nothandle);
$this->assertEquals(true, $exist);
}
/**
* Method recommended by moodle to assure database and dataroot is reset.
Expand Down

0 comments on commit 59bc3cd

Please sign in to comment.