From fdae6f77d6c97cbedb798ad136b14cf87fe2d39b Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Thu, 5 Apr 2018 14:27:43 +0800 Subject: [PATCH] MDL-61859 core_completion: fix unit tests expecting exceptions twice We can't expectException twice in the same test, so split this out into another test. --- completion/tests/externallib_test.php | 51 +++++++++++++++++---------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/completion/tests/externallib_test.php b/completion/tests/externallib_test.php index cd1e4fa75e9..ae06d97af1a 100644 --- a/completion/tests/externallib_test.php +++ b/completion/tests/externallib_test.php @@ -244,13 +244,12 @@ class core_completion_externallib_testcase extends externallib_advanced_testcase $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); $student = $this->getDataGenerator()->create_user(); $teacher = $this->getDataGenerator()->create_user(); - $studentrole = $DB->get_record('role', array('shortname' => 'student')); + $studentrole = $DB->get_record('role', ['shortname' => 'student']); $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); - $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); + $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']); $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); - $coursecontext = context_course::instance($course->id); - // Create 2 activities, one with manual completion (data), one with automatic completion triggered by viewiung it (forum). + // Create 2 activities, one with manual completion (data), one with automatic completion triggered by viewing it (forum). $data = $this->getDataGenerator()->create_module('data', ['course' => $course->id], ['completion' => 1]); $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id], ['completion' => 2, 'completionview' => 1]); @@ -291,22 +290,38 @@ class core_completion_externallib_testcase extends externallib_advanced_testcase $completionforum = $completion->get_data($cmforum, false, $student->id); $this->assertEquals(COMPLETION_INCOMPLETE, $completionforum->completionstate); - // Test overriding the status of the auto-completion-activity to an invalid state. It should remain incomplete. + // Test overriding the status of the auto-completion-activity to an invalid state. $this->expectException('moodle_exception'); - $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, 3); - $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result); - $this->assertEquals($result['state'], COMPLETION_INCOMPLETE); - $completionforum = $completion->get_data($cmforum, false, $student->id); - $this->assertEquals(COMPLETION_INCOMPLETE, $completionforum->completionstate); + core_completion_external::override_activity_completion_status($student->id, $forum->cmid, 3); + } - // Test overriding the status of the auto-completion-activity for a user without capabilities. It should remain incomplete. - $this->expectException('moodle_exception'); - unassign_capability('moodle/course:overridecompletion', $teacherrole->id, $coursecontext); - $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, 1); - $result = external_api::clean_returnvalue(core_completion_external::override_activity_completion_status_returns(), $result); - $this->assertEquals($result['state'], COMPLETION_INCOMPLETE); - $completionforum = $completion->get_data($cmforum, false, $student->id); - $this->assertEquals(COMPLETION_INCOMPLETE, $completionforum->completionstate); + /** + * Test overriding the activity completion status as a user without the capability to do so. + */ + public function test_override_status_user_without_capability() { + global $DB, $CFG; + $this->resetAfterTest(true); + + // Create course with teacher and student enrolled. + $CFG->enablecompletion = true; + $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + $student = $this->getDataGenerator()->create_user(); + $teacher = $this->getDataGenerator()->create_user(); + $studentrole = $DB->get_record('role', ['shortname' => 'student']); + $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); + $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']); + $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); + $coursecontext = context_course::instance($course->id); + + // Create an activity with automatic completion (a forum). + $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id], + ['completion' => 2, 'completionview' => 1]); + + // Test overriding the status of the activity for a user without the capability. + $this->setUser($teacher); + assign_capability('moodle/course:overridecompletion', CAP_PREVENT, $teacherrole->id, $coursecontext); + $this->expectException('required_capability_exception'); + core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_COMPLETE); } /** -- 2.43.0