Merge branch 'MDL-59811-master' of git://github.com/junpataleta/moodle
[moodle.git] / mod / quiz / report / overview / tests / report_test.php
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.
13 //
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  * Tests for the quiz overview report.
19  *
20  * @package   quiz_overview
21  * @copyright 2014 The Open University
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
29 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
30 require_once($CFG->dirroot . '/mod/quiz/report/default.php');
31 require_once($CFG->dirroot . '/mod/quiz/report/overview/report.php');
34 /**
35  * Tests for the quiz overview report.
36  *
37  * @copyright  2014 The Open University
38  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class quiz_overview_report_testcase extends advanced_testcase {
42     public function test_report_sql() {
43         global $DB;
44         $this->resetAfterTest(true);
46         $generator = $this->getDataGenerator();
47         $course = $generator->create_course();
48         $quizgenerator = $generator->get_plugin_generator('mod_quiz');
49         $quiz = $quizgenerator->create_instance(array('course' => $course->id,
50                 'grademethod' => QUIZ_GRADEHIGHEST, 'grade' => 100.0, 'sumgrades' => 10.0,
51                 'attempts' => 10));
53         $student1 = $generator->create_user();
54         $student2 = $generator->create_user();
55         $student3 = $generator->create_user();
56         $generator->enrol_user($student1->id, $course->id);
57         $generator->enrol_user($student2->id, $course->id);
58         $generator->enrol_user($student3->id, $course->id);
60         $timestamp = 1234567890;
62         // The test data.
63         $fields = array('quiz', 'userid', 'attempt', 'sumgrades', 'state');
64         $attempts = array(
65             array($quiz->id, $student1->id, 1, 0.0,  quiz_attempt::FINISHED),
66             array($quiz->id, $student1->id, 2, 5.0,  quiz_attempt::FINISHED),
67             array($quiz->id, $student1->id, 3, 8.0,  quiz_attempt::FINISHED),
68             array($quiz->id, $student1->id, 4, null, quiz_attempt::ABANDONED),
69             array($quiz->id, $student1->id, 5, null, quiz_attempt::IN_PROGRESS),
70             array($quiz->id, $student2->id, 1, null, quiz_attempt::ABANDONED),
71             array($quiz->id, $student2->id, 2, null, quiz_attempt::ABANDONED),
72             array($quiz->id, $student2->id, 3, 7.0,  quiz_attempt::FINISHED),
73             array($quiz->id, $student2->id, 4, null, quiz_attempt::ABANDONED),
74             array($quiz->id, $student2->id, 5, null, quiz_attempt::ABANDONED),
75         );
77         // Load it in to quiz attempts table.
78         $uniqueid = 1;
79         foreach ($attempts as $attempt) {
80             $data = array_combine($fields, $attempt);
81             $data['timestart'] = $timestamp + 3600 * $data['attempt'];
82             $data['timemodifed'] = $data['timestart'];
83             if ($data['state'] == quiz_attempt::FINISHED) {
84                 $data['timefinish'] = $data['timestart'] + 600;
85                 $data['timemodifed'] = $data['timefinish'];
86             }
87             $data['layout'] = ''; // Not used, but cannot be null.
88             $data['uniqueid'] = $uniqueid++;
89             $data['preview'] = 0;
90             $DB->insert_record('quiz_attempts', $data);
91         }
93         // This line is not really necessary for the test asserts below,
94         // but what it does is add an extra user row returned by
95         // get_enrolled_with_capabilities_join because of a second enrolment.
96         // The extra row returned used to make $table->query_db complain
97         // about duplicate records. So this is really a test that an extra
98         // student enrolment does not cause duplicate records in this query.
99         $generator->enrol_user($student2->id, $course->id, null, 'self');
101         // Actually getting the SQL to run is quite hard. Do a minimal set up of
102         // some objects.
103         $context = context_module::instance($quiz->cmid);
104         $cm = get_coursemodule_from_id('quiz', $quiz->cmid);
105         $qmsubselect = quiz_report_qm_filter_select($quiz);
106         $studentsjoins = get_enrolled_with_capabilities_join($context);
107         $empty = new \core\dml\sql_join();
109         // Set the options.
110         $reportoptions = new quiz_overview_options('overview', $quiz, $cm, null);
111         $reportoptions->attempts = quiz_attempts_report::ENROLLED_ALL;
112         $reportoptions->onlygraded = true;
113         $reportoptions->states = array(quiz_attempt::IN_PROGRESS, quiz_attempt::OVERDUE, quiz_attempt::FINISHED);
115         // Now do a minimal set-up of the table class.
116         $table = new quiz_overview_table($quiz, $context, $qmsubselect, $reportoptions,
117                 $empty, $studentsjoins, array(1), null);
118         $table->define_columns(array('fullname'));
119         $table->sortable(true, 'uniqueid');
120         $table->define_baseurl(new moodle_url('/mod/quiz/report.php'));
121         $table->setup();
123         // Run the query.
124         list($fields, $from, $where, $params) = $table->base_sql($studentsjoins);
125         $table->set_sql($fields, $from, $where, $params);
126         $table->set_count_sql("SELECT COUNT(1) FROM (SELECT $fields FROM $from WHERE $where) temp WHERE 1 = 1", $params);
127         $table->query_db(30, false);
129         // Verify what was returned: Student 1's best and in progress attempts.
130         // Student 2's finshed attempt, and Student 3 with no attempt.
131         // The array key is {student id}#{attempt number}.
132         $this->assertEquals(4, count($table->rawdata));
133         $this->assertArrayHasKey($student1->id . '#3', $table->rawdata);
134         $this->assertEquals(1, $table->rawdata[$student1->id . '#3']->gradedattempt);
135         $this->assertArrayHasKey($student1->id . '#3', $table->rawdata);
136         $this->assertEquals(0, $table->rawdata[$student1->id . '#5']->gradedattempt);
137         $this->assertArrayHasKey($student2->id . '#3', $table->rawdata);
138         $this->assertEquals(1, $table->rawdata[$student2->id . '#3']->gradedattempt);
139         $this->assertArrayHasKey($student3->id . '#0', $table->rawdata);
140         $this->assertEquals(0, $table->rawdata[$student3->id . '#0']->gradedattempt);
142         // Ensure that filtering by inital does not break it.
143         // This involves setting a private properly of the base class, which is
144         // only really possible using reflection :-(.
145         $reflectionobject = new ReflectionObject($table);
146         while ($parent = $reflectionobject->getParentClass()) {
147             $reflectionobject = $parent;
148         }
149         $prefsproperty = $reflectionobject->getProperty('prefs');
150         $prefsproperty->setAccessible(true);
151         $prefs = $prefsproperty->getValue($table);
152         $prefs['i_first'] = 'A';
153         $prefsproperty->setValue($table, $prefs);
155         list($fields, $from, $where, $params) = $table->base_sql($studentsjoins);
156         $table->set_count_sql("SELECT COUNT(1) FROM (SELECT $fields FROM $from WHERE $where) temp WHERE 1 = 1", $params);
157         $table->set_sql($fields, $from, $where, $params);
158         $table->query_db(30, false);
159         // Just verify that this does not cause a fatal error.
160     }
162     /**
163      * Bands provider.
164      * @return array
165      */
166     public function get_bands_count_and_width_provider() {
167         return [
168             [10, [20, .5]],
169             [20, [20, 1]],
170             [30, [15, 2]],
171             // TODO MDL-55068 Handle bands better when grade is 50.
172             // [50, [10, 5]],
173             [100, [20, 5]],
174             [200, [20, 10]],
175         ];
176     }
178     /**
179      * Test bands.
180      *
181      * @dataProvider get_bands_count_and_width_provider
182      * @param int $grade grade
183      * @param array $expected
184      */
185     public function test_get_bands_count_and_width($grade, $expected) {
186         $this->resetAfterTest(true);
187         $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
188         $quiz = $quizgenerator->create_instance(['course' => SITEID, 'grade' => $grade]);
189         $this->assertEquals($expected, quiz_overview_report::get_bands_count_and_width($quiz));
190     }