MDL-57572 redis: Prevent installation to fail on sites without redis
[moodle.git] / mod / assign / tests / locallib_participants_test.php
blob7121383bd7a6a1f9322b29ab642b2314cacbc4f7
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Unit tests for (some of) mod/assign/locallib.php.
20 * @package mod_assign
21 * @category phpunit
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once(__DIR__ . '/../locallib.php');
32 class mod_assign_locallib_participants extends advanced_testcase {
33 public function test_list_participants_blind_marking() {
34 global $DB;
35 $this->resetAfterTest(true);
37 $course = $this->getDataGenerator()->create_course();
39 $roles = $DB->get_records('role', null, '', 'shortname, id');
40 $teacher = $this->getDataGenerator()->create_user();
42 $this->getDataGenerator()->enrol_user($teacher->id,
43 $course->id,
44 $roles['teacher']->id);
46 $this->setUser($teacher);
48 // Enrol two students.
49 $students = [];
50 for ($i = 0; $i < 2; $i++) {
51 $student = $this->getDataGenerator()->create_user();
52 $this->getDataGenerator()->enrol_user($student->id,
53 $course->id,
54 $roles['student']->id);
55 $students[$student->id] = $student;
58 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
59 $instance = $generator->create_instance(['course' => $course->id, 'blindmarking' => 1]);
60 $cm = get_coursemodule_from_instance('assign', $instance->id);
61 $context = context_module::instance($cm->id);
62 $assign = new assign($context, $cm, $course);
64 // Allocate IDs now.
65 // We're testing whether the IDs are correct after allocation.
66 assign::allocate_unique_ids($assign->get_instance()->id);
68 $participants = $assign->list_participants(null, false);
70 // There should be exactly two participants and they should be the students.
71 $this->assertCount(2, $participants);
72 foreach ($participants as $participant) {
73 $this->assertArrayHasKey($participant->id, $students);
76 $keys = array_keys($participants);
78 // Create a grading table, and query the DB This should have the same order.
79 $table = new assign_grading_table($assign, 10, '', 0, false);
80 $table->setup();
81 $table->query_db(10);
82 $this->assertEquals($keys, array_keys($table->rawdata));
84 // Submit a file for the second student.
85 $data = new stdClass();
86 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
87 'text'=>'Submission text',
88 'format'=>FORMAT_MOODLE);
89 static::helper_add_submission($assign, $participants[$keys[1]], $data, 'onlinetext');
91 // Assign has a private cache. The easiest way to clear this is to create a new instance.
92 $assign = new assign($context, $cm, $course);
94 $newparticipants = $assign->list_participants(null, false);
96 // There should be exactly two participants and they should be the students.
97 $this->assertCount(2, $newparticipants);
98 foreach ($newparticipants as $participant) {
99 $this->assertArrayHasKey($participant->id, $students);
101 $newkeys = array_keys($newparticipants);
103 // The user who submitted first should now be listed first.
104 $this->assertEquals($participants[$keys[1]]->id, $newparticipants[$newkeys[0]]->id);
105 $this->assertEquals($participants[$keys[0]]->id, $newparticipants[$newkeys[1]]->id);
107 // Submit for the other student.
108 static::helper_add_submission($assign, $participants[$keys[0]], $data, 'onlinetext');
109 $assign = new assign($context, $cm, $course);
110 $newparticipants = $assign->list_participants(null, false);
112 // The users should still be listed in order of the first submission
113 $this->assertEquals($participants[$keys[1]]->id, $newparticipants[$newkeys[0]]->id);
114 $this->assertEquals($participants[$keys[0]]->id, $newparticipants[$newkeys[1]]->id);
116 // The updated grading table should have the same order as the updated participant list.
117 $table->query_db(10);
118 $this->assertEquals($newkeys, array_keys($table->rawdata));
121 public function helper_add_submission($assign, $user, $data, $type) {
122 global $USER;
124 $previoususer = $USER;
126 $this->setUser($user);
127 $submission = $assign->get_user_submission($user->id, true);
128 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
130 $rc = new ReflectionClass('assign');
131 $rcm = $rc->getMethod('update_submission');
132 $rcm->setAccessible(true);
133 $rcm->invokeArgs($assign, [$submission, $user->id, true, false]);
135 $plugin = $assign->get_submission_plugin_by_type($type);
136 $plugin->save($submission, $data);
138 $this->setUser($previoususer);