-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
StudentQuiz: fix code review #732619
- Loading branch information
Khoa Nguyen Dang
committed
Oct 31, 2023
1 parent
7b1dd5d
commit 43e361d
Showing
6 changed files
with
52 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,9 @@ | |
|
||
use advanced_testcase; | ||
use mod_studentquiz\completion\custom_completion; | ||
use mod_studentquiz\local\studentquiz_question; | ||
use cm_info; | ||
use mod_studentquiz\local\studentquiz_helper; | ||
|
||
/** | ||
* Class for unit testing mod_studentquiz/custom_completion. | ||
|
@@ -70,57 +72,61 @@ public function test_get_custom_rule_descriptions() { | |
} | ||
|
||
/** | ||
* Test update state function in custom_completion. | ||
* Test trigger completion state update function in custom_completion. | ||
* | ||
* @dataProvider test_update_provider | ||
* @covers ::update_state | ||
* @param int $completion The completion type: 0 - Do not indicate activity completion, | ||
* 1 - Students can manually mark the activity as completed, 2 - Show activity as complete when conditions are met. | ||
* @param bool $expected Expected result when run update. | ||
* @covers ::trigger_completion_state_update | ||
*/ | ||
public function test_update_state(int $completion, bool $expected): void { | ||
public function test_trigger_completion_state_update(): void { | ||
$this->resetAfterTest(); | ||
global $DB; | ||
|
||
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); | ||
$generator = $this->getDataGenerator(); | ||
$questiongenerator = $generator->get_plugin_generator('core_question'); | ||
|
||
$user = $this->getDataGenerator()->create_user(); | ||
// Prepare course. | ||
$course = $generator->create_course([ | ||
'enablecompletion' => COMPLETION_ENABLED | ||
]); | ||
|
||
// Prepare users. | ||
$student = $generator->create_user(['firstname' => 'Student', 'lastname' => '1', | ||
'email' => '[email protected]']); | ||
|
||
// Users enrolments. | ||
$studentrole = $DB->get_record('role', ['shortname' => 'student']); | ||
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); | ||
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual'); | ||
|
||
$studentquiz = $this->getDataGenerator()->create_module('studentquiz', [ | ||
// Prepare studentquiz. | ||
$studentquizdata = [ | ||
'course' => $course->id, | ||
'completion' => $completion, | ||
]); | ||
'completionquestionapproved' => 1, | ||
'completion' => COMPLETION_TRACKING_AUTOMATIC, | ||
]; | ||
|
||
$cm = cm_info::create(get_coursemodule_from_id('studentquiz', $studentquiz->cmid)); | ||
$completioninfo = new \completion_info($course); | ||
$customcompletion = new custom_completion($cm, $user->id, $completioninfo->get_core_completion_state($cm, $user->id)); | ||
$status = $customcompletion::update_state($course, $cm, $user->id); | ||
$cmid = $generator->create_module('studentquiz', $studentquizdata)->cmid; | ||
$studentquiz = mod_studentquiz_load_studentquiz($cmid, \context_module::instance($cmid)->id); | ||
|
||
$this->assertEquals($expected, $status); | ||
} | ||
// Prepare question. | ||
$this->setUser($student); | ||
$cm = cm_info::create(get_coursemodule_from_id('studentquiz', $cmid)); | ||
$questions = $questiongenerator->create_question('truefalse', null, | ||
['name' => 'Student 1 Question', 'category' => $studentquiz->categoryid]); | ||
$questions = \question_bank::load_question($questions->id); | ||
$studentquizquestions = studentquiz_question::get_studentquiz_question_from_question($questions); | ||
|
||
/** | ||
* Data provider for test_update_state() test cases. | ||
* | ||
* @coversNothing | ||
* @return array List of data sets (test cases) | ||
*/ | ||
public static function test_update_provider(): array { | ||
return [ | ||
'Do not indicate activity completion' => [ | ||
0, | ||
false, | ||
], | ||
'Students can manually mark the activity as completed' => [ | ||
1, | ||
false, | ||
], | ||
'Show activity as complete when conditions are met' => [ | ||
2, | ||
true, | ||
], | ||
]; | ||
// The completion data should be empty. | ||
$count = $DB->count_records('course_modules_completion'); | ||
$this->assertEquals(0, $count); | ||
|
||
// Approve question. | ||
$this->setAdminUser(); | ||
$studentquizquestions->change_state_visibility(studentquiz_helper::STATE_APPROVED); | ||
\mod_studentquiz\completion\custom_completion::trigger_completion_state_update( | ||
$course, $cm, $student->id | ||
); | ||
|
||
// The completion data should exist. | ||
$count = $DB->count_records('course_modules_completion'); | ||
$this->assertEquals(1, $count); | ||
} | ||
} |