From 60a6b36cd564e1bed4e5d78a29ea6c11ca201345 Mon Sep 17 00:00:00 2001 From: Eiz Eddin Al Katrib Date: Fri, 17 Feb 2017 14:48:25 +0000 Subject: [PATCH] MDL-37361 completion: Modified and Added phpunit tests --- completion/tests/externallib_test.php | 100 ++++++++++++++++++++++++++ lib/tests/completionlib_test.php | 71 ++++++++++++++++-- 2 files changed, 166 insertions(+), 5 deletions(-) diff --git a/completion/tests/externallib_test.php b/completion/tests/externallib_test.php index 61ef5378ac9..b66c24ea9a1 100644 --- a/completion/tests/externallib_test.php +++ b/completion/tests/externallib_test.php @@ -186,6 +186,106 @@ class core_completion_externallib_testcase extends externallib_advanced_testcase $this->assertCount(3, $result['statuses']); } + /** + * Test override_activity_completion_status + */ + public function test_override_activity_completion_status() { + global $DB, $CFG; + + $this->resetAfterTest(true); + + $CFG->enablecompletion = true; + $student = $this->getDataGenerator()->create_user(); + $teacher = $this->getDataGenerator()->create_user(); + + $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); + + $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id), + array('completion' => 1)); + $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id), + array('completion' => 2)); + + $cmdata = get_coursemodule_from_id('data', $data->cmid); + $cmforum = get_coursemodule_from_id('forum', $forum->cmid); + + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); + + $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); + $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); + + $completion = new completion_info($course); + + $this->setUser($student); + + // Mark data activity complete by student. + $completion->update_state($cmdata, COMPLETION_COMPLETE); + $completiondata = $completion->get_data($cmdata); + + $this->setUser($teacher); + + // Override data completion state to incomplete. + $result = core_completion_external::override_activity_completion_status($student->id, $data->cmid, COMPLETION_INCOMPLETE); + // We need to execute the return values cleaning process to simulate the web service server. + $result = external_api::clean_returnvalue( + core_completion_external::override_activity_completion_status_returns(), $result); + $this->assertTrue($result['status']); + + // Check in DB. + $this->assertEquals(COMPLETION_INCOMPLETE, $DB->get_field('course_modules_completion', 'completionstate', + array('coursemoduleid' => $data->cmid))); + + // Check using the API. + $completiondata = $completion->get_data($cmdata, false, $student->id); + $this->assertEquals(COMPLETION_INCOMPLETE, $completiondata->completionstate); + + // Override data completion state to complete. + $result = core_completion_external::override_activity_completion_status($student->id, $data->cmid, COMPLETION_COMPLETE); + // We need to execute the return values cleaning process to simulate the web service server. + $result = external_api::clean_returnvalue( + core_completion_external::override_activity_completion_status_returns(), $result); + $this->assertTrue($result['status']); + + // Check in DB. + $this->assertEquals(COMPLETION_COMPLETE, $DB->get_field('course_modules_completion', 'completionstate', + array('coursemoduleid' => $data->cmid))); + + // Check using the API. + $completiondata = $completion->get_data($cmdata, false, $student->id); + $this->assertEquals(COMPLETION_COMPLETE, $completiondata->completionstate); + + // Override forum completion state to complete. + $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_COMPLETE); + // We need to execute the return values cleaning process to simulate the web service server. + $result = external_api::clean_returnvalue( + core_completion_external::override_activity_completion_status_returns(), $result); + $this->assertTrue($result['status']); + + // Check in DB. + $this->assertEquals(COMPLETION_COMPLETE, $DB->get_field('course_modules_completion', 'completionstate', + array('coursemoduleid' => $forum->cmid))); + + // Check using the API. + $completionforum = $completion->get_data($cmforum, false, $student->id); + $this->assertEquals(COMPLETION_COMPLETE, $completionforum->completionstate); + + // Override forum completion state to incomplete. + $result = core_completion_external::override_activity_completion_status($student->id, $forum->cmid, COMPLETION_INCOMPLETE); + // We need to execute the return values cleaning process to simulate the web service server. + $result = external_api::clean_returnvalue( + core_completion_external::override_activity_completion_status_returns(), $result); + $this->assertTrue($result['status']); + + // Check in DB. + $this->assertEquals(COMPLETION_INCOMPLETE, $DB->get_field('course_modules_completion', 'completionstate', + array('coursemoduleid' => $forum->cmid))); + + // Check using the API. + $completionforum = $completion->get_data($cmforum, false, $student->id); + $this->assertEquals(COMPLETION_INCOMPLETE, $completionforum->completionstate); + + } + /** * Test get_course_completion_status */ diff --git a/lib/tests/completionlib_test.php b/lib/tests/completionlib_test.php index da9265f1692..e74f0a07559 100644 --- a/lib/tests/completionlib_test.php +++ b/lib/tests/completionlib_test.php @@ -142,7 +142,7 @@ class core_completionlib_testcase extends advanced_testcase { $c->update_state($cm); // Enabled, but current state is same as possible result, do nothing. - $current = (object)array('completionstate'=>COMPLETION_COMPLETE); + $current = (object)array('completionstate' => COMPLETION_COMPLETE, 'overrideby' => null); $c->expects($this->at(0)) ->method('is_enabled') ->with($cm) @@ -200,7 +200,7 @@ class core_completionlib_testcase extends advanced_testcase { // Auto, change state. $cm = (object)array('id'=>13, 'course'=>42, 'completion'=>COMPLETION_TRACKING_AUTOMATIC); - $current = (object)array('completionstate'=>COMPLETION_COMPLETE); + $current = (object)array('completionstate' => COMPLETION_COMPLETE, 'overrideby' => null); $c->expects($this->at(0)) ->method('is_enabled') ->with($cm) @@ -221,6 +221,64 @@ class core_completionlib_testcase extends advanced_testcase { ->method('internal_set_data') ->with($cm, $comparewith); $c->update_state($cm, COMPLETION_COMPLETE_PASS); + + // Manual tracking, change state by overriding it manually. + $cm = (object)array('id' => 13, 'course' => 42, 'completion' => COMPLETION_TRACKING_MANUAL); + $current = (object)array('completionstate' => COMPLETION_INCOMPLETE, 'overrideby' => null); + $c->expects($this->at(0)) + ->method('is_enabled') + ->with($cm) + ->will($this->returnValue(true)); + $c->expects($this->at(1)) + ->method('get_data') + ->with($cm, false, 100) + ->will($this->returnValue($current)); + $changed = clone($current); + $changed->timemodified = time(); + $changed->completionstate = COMPLETION_COMPLETE; + $changed->overrideby = 314159; + $comparewith = new phpunit_constraint_object_is_equal_with_exceptions($changed); + $comparewith->add_exception('timemodified', 'assertGreaterThanOrEqual'); + $c->expects($this->at(2)) + ->method('internal_set_data') + ->with($cm, $comparewith); + $c->update_state($cm, COMPLETION_COMPLETE, 100, true); + + // Auto tracking, change state by overriding it manually. + $cm = (object)array('id' => 13, 'course' => 42, 'completion' => COMPLETION_TRACKING_AUTOMATIC); + $current = (object)array('completionstate' => COMPLETION_INCOMPLETE, 'overrideby' => null); + $c->expects($this->at(0)) + ->method('is_enabled') + ->with($cm) + ->will($this->returnValue(true)); + $c->expects($this->at(1)) + ->method('get_data') + ->with($cm, false, 100) + ->will($this->returnValue($current)); + $changed = clone($current); + $changed->timemodified = time(); + $changed->completionstate = COMPLETION_COMPLETE; + $changed->overrideby = 314159; + $comparewith = new phpunit_constraint_object_is_equal_with_exceptions($changed); + $comparewith->add_exception('timemodified', 'assertGreaterThanOrEqual'); + $c->expects($this->at(2)) + ->method('internal_set_data') + ->with($cm, $comparewith); + $c->update_state($cm, COMPLETION_COMPLETE, 100, true); + + // Auto tracking, but current state is set by override, so do nothing. + $cm = (object)array('id' => 13, 'course' => 42, 'completion' => COMPLETION_TRACKING_AUTOMATIC); + $current = (object)array('completionstate' => COMPLETION_COMPLETE, 'overrideby' => 2); + $c->expects($this->at(0)) + ->method('is_enabled') + ->with($cm) + ->will($this->returnValue(true)); + $c->expects($this->at(1)) + ->method('get_data') + ->with($cm, false, 0) + ->will($this->returnValue($current)); + $c->update_state($cm, COMPLETION_COMPLETE_PASS); + } public function test_internal_get_state() { @@ -416,8 +474,8 @@ class core_completionlib_testcase extends advanced_testcase { $modinfo->cms = array((object)array('id'=>13)); $result=$c->get_data($cm, true, 123, $modinfo); $this->assertEquals((object)array( - 'id'=>'0', 'coursemoduleid'=>13, 'userid'=>123, 'completionstate'=>0, - 'viewed'=>0, 'timemodified'=>0), $result); + 'id' => '0', 'coursemoduleid' => 13, 'userid' => 123, 'completionstate' => 0, + 'viewed' => 0, 'timemodified' => 0, 'overrideby' => 0), $result); $this->assertEquals(false, $cache->get('123_42')); // Not current user is not cached. // 3. Current user, single record, not from cache. @@ -455,7 +513,7 @@ class core_completionlib_testcase extends advanced_testcase { $cachevalue = $cache->get('314159_42'); $this->assertEquals($basicrecord, (object)$cachevalue[13]); $this->assertEquals(array('id' => '0', 'coursemoduleid' => 14, - 'userid'=>314159, 'completionstate'=>0, 'viewed'=>0, 'timemodified'=>0), + 'userid' => 314159, 'completionstate' => 0, 'viewed' => 0, 'overrideby' => 0, 'timemodified' => 0), $cachevalue[14]); } @@ -477,6 +535,7 @@ class core_completionlib_testcase extends advanced_testcase { $data->completionstate = COMPLETION_COMPLETE; $data->timemodified = time(); $data->viewed = COMPLETION_NOT_VIEWED; + $data->overrideby = null; $c->internal_set_data($cm, $data); $d1 = $DB->get_field('course_modules_completion', 'id', array('coursemoduleid' => $cm->id)); @@ -498,6 +557,7 @@ class core_completionlib_testcase extends advanced_testcase { $d2->completionstate = COMPLETION_COMPLETE; $d2->timemodified = time(); $d2->viewed = COMPLETION_NOT_VIEWED; + $d2->overrideby = null; $c->internal_set_data($cm2, $d2); // Cache for current user returns the data. $cachevalue = $cache->get($data->userid . '_' . $cm->course); @@ -518,6 +578,7 @@ class core_completionlib_testcase extends advanced_testcase { $d3->completionstate = COMPLETION_COMPLETE; $d3->timemodified = time(); $d3->viewed = COMPLETION_NOT_VIEWED; + $d3->overrideby = null; $DB->insert_record('course_modules_completion', $d3); $c->internal_set_data($cm, $data); }