From 2afa4488cda1002df52fdaa2ae835c06d8a0e868 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Fri, 22 Jul 2022 10:23:14 +0800 Subject: [PATCH] MDL-73076 completion: split criteria duration test into two tests --- completion/tests/completion_criteria_test.php | 60 +++++++++++++------ 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/completion/tests/completion_criteria_test.php b/completion/tests/completion_criteria_test.php index d7ffaacef57..7061d7e86e7 100644 --- a/completion/tests/completion_criteria_test.php +++ b/completion/tests/completion_criteria_test.php @@ -79,26 +79,16 @@ class completion_criteria_test extends \advanced_testcase { } /** - * Test that enrolment timestart/timecreated are used when duration criteria is marked as completed. + * Test that enrolment timestart are used when duration criteria is marked as completed. */ - public function test_completion_criteria_duration(): void { + public function test_completion_criteria_duration_timestart(): void { global $DB; $timestarted = 1610000000; - $timecreated = 1620000000; $durationperiod = DAYSECS; // Create a course and users. $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); - $user1 = $this->getDataGenerator()->create_user(); - $user2 = $this->getDataGenerator()->create_user(); - $studentrole = $DB->get_record('role', ['shortname' => 'student']); - - // Enrol User 1 with time start = $timestarted. - $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id, 'manual', $timestarted); - - // Enrol User 2 with an empty time start, but update the record like it was created at $timecreated. - $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id); - $DB->set_field('user_enrolments', 'timecreated', $timecreated, ['userid' => $user2->id]); + $user = $this->getDataGenerator()->create_and_enrol($course, 'student', null, 'manual', $timestarted); // Set completion criteria. $criteriadata = (object) [ @@ -118,13 +108,49 @@ class completion_criteria_test extends \advanced_testcase { sleep(1); $task->execute(); - // The course for User 1 is supposed to be marked as completed at $timestarted + $durationperiod. - $ccompletion = new \completion_completion(['userid' => $user1->id, 'course' => $course->id]); + // The course for User is supposed to be marked as completed at $timestarted + $durationperiod. + $ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]); $this->assertEquals($timestarted + $durationperiod, $ccompletion->timecompleted); $this->assertTrue($ccompletion->is_complete()); + } - // The course for User 2 is supposed to be marked as completed at $timecreated + $durationperiod. - $ccompletion = new \completion_completion(['userid' => $user2->id, 'course' => $course->id]); + /** + * Test that enrolment timecreated are used when duration criteria is marked as completed. + */ + public function test_completion_criteria_duration_timecreated(): void { + global $DB; + + $timecreated = 1620000000; + $durationperiod = DAYSECS; + + // Create a course and users. + $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + + // Create and enrol user with an empty time start, but update the record like it was created at $timecreated. + $user = $this->getDataGenerator()->create_and_enrol($course); + $DB->set_field('user_enrolments', 'timecreated', $timecreated, ['userid' => $user->id]); + + // Set completion criteria. + $criteriadata = (object) [ + 'id' => $course->id, + 'criteria_duration' => 1, + 'criteria_duration_days' => $durationperiod, + ]; + $criterion = new \completion_criteria_duration(); + $criterion->update_config($criteriadata); + + // Run completion scheduled task. + $task = new \core\task\completion_regular_task(); + $this->expectOutputRegex("/Marking complete/"); + $task->execute(); + + // Hopefully, some day MDL-33320 will be fixed and all these sleeps + // and double cron calls in behat and unit tests will be removed. + sleep(1); + $task->execute(); + + // The course for user is supposed to be marked as completed at $timecreated + $durationperiod. + $ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]); $this->assertEquals($timecreated + $durationperiod, $ccompletion->timecompleted); $this->assertTrue($ccompletion->is_complete()); }