Skip to content

Commit

Permalink
Merge branch 'MDL-61859-master' of https://github.com/snake/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Apr 11, 2018
2 parents 0f0d000 + fdae6f7 commit 74e48d1
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions completion/tests/externallib_test.php
Expand Up @@ -244,13 +244,12 @@ public function test_override_activity_completion_status() {
$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]);
Expand Down Expand Up @@ -291,22 +290,38 @@ public function test_override_activity_completion_status() {
$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);
}

/**
Expand Down

0 comments on commit 74e48d1

Please sign in to comment.