MDL-40543 convert from simulated responses
[moodle.git] / mod / quiz / tests / attempt_walkthrough_test.php
CommitLineData
2baf8e8a
JP
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/>.
16
17/**
18 * Quiz attempt walk through tests.
19 *
20 * @package mod_quiz
21 * @category phpunit
22 * @copyright 2013 The Open University
23 * @author Jamie Pratt <me@jamiep.org>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/mod/quiz/editlib.php');
31require_once($CFG->dirroot . '/mod/quiz/locallib.php');
32
33/**
34 * Quiz attempt walk through.
35 *
36 * @package mod_quiz
37 * @category phpunit
38 * @copyright 2013 The Open University
39 * @author Jamie Pratt <me@jamiep.org>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class mod_quiz_attempt_walkthrough_testcase extends advanced_testcase {
43
44 /**
45 * Create a quiz with questions and walk through a quiz attempt.
46 */
47 public function test_quiz_attempt_walkthrough() {
48 global $SITE;
49
50 $this->resetAfterTest(true);
51
52 // Make a quiz.
53 $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
54
55 $quiz = $quizgenerator->create_instance(array('course'=>$SITE->id, 'questionsperpage' => 0, 'grade' => 100.0,
56 'sumgrades' => 2));
57
58 // Create a couple of questions.
59 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
60
61 $cat = $questiongenerator->create_question_category();
62 $saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
63 $numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
64
65 // Add them to the quiz.
66 quiz_add_quiz_question($saq->id, $quiz);
67 quiz_add_quiz_question($numq->id, $quiz);
68
69 // Make a user to do the quiz.
70 $user1 = $this->getDataGenerator()->create_user();
71 $this->setUser($user1);
72
73 $quizobj = quiz::create($quiz->id, $user1->id);
74
75 // Start the attempt.
76 $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
77 $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
78
79 $timenow = time();
80 $attempt = quiz_create_attempt($quizobj, 1, false, $timenow);
81
82 quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
83
84 quiz_attempt_save_started($quizobj, $quba, $attempt);
85
86 // Process some responses from the student.
87 $attemptobj = quiz_attempt::create($attempt->id);
88 $prefix1 = $quba->get_field_prefix(1);
89 $prefix2 = $quba->get_field_prefix(2);
90
2baf8e8a 91
388f0473
JP
92 $tosubmit = array(1 => array('answer' => 'frog'),
93 2 => array('answer' => '3.14'));
2baf8e8a
JP
94
95 $attemptobj->process_submitted_actions($timenow, false, $tosubmit);
96
97 // Finish the attempt.
98 $attemptobj = quiz_attempt::create($attempt->id);
99 $attemptobj->process_finish($timenow, false);
100
101 // Re-load quiz attempt data.
102 $attemptobj = quiz_attempt::create($attempt->id);
103
104 // Check that results are stored as expected.
105 $this->assertEquals(1, $attemptobj->get_attempt_number());
106 $this->assertEquals(2, $attemptobj->get_sum_marks());
107 $this->assertEquals(true, $attemptobj->is_finished());
108 $this->assertEquals($timenow, $attemptobj->get_submitted_date());
109 $this->assertEquals($user1->id, $attemptobj->get_userid());
110
111 // Check quiz grades.
112 $grades = quiz_get_user_grades($quiz, $user1->id);
113 $grade = array_shift($grades);
114 $this->assertEquals(100.0, $grade->rawgrade);
115
116 // Check grade book.
117 $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id);
118 $gradebookitem = array_shift($gradebookgrades->items);
119 $gradebookgrade = array_shift($gradebookitem->grades);
120 $this->assertEquals(100, $gradebookgrade->grade);
121 }
388f0473
JP
122
123 /**
124 * Create a second quiz with questions and walk through a quiz attempt this time with a random question.
125 */
126 public function test_quiz_with_random_question_attempt_walkthrough() {
127 global $SITE;
128
129 $this->resetAfterTest(true);
130 question_bank::get_qtype('random')->clear_caches_before_testing();
131
132 $this->setAdminUser();
133
134 // Make a quiz.
135 $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
136
137 $quiz = $quizgenerator->create_instance(array('course'=>$SITE->id, 'questionsperpage' => 0, 'grade' => 100.0,
138 'sumgrades' => 4));
139
140 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
141
142 // Add two questions to question category.
143 $cat = $questiongenerator->create_question_category();
144 $saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
145 $numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
146
147 // Add random question to the quiz.
148 quiz_add_random_questions($quiz, 0, $cat->id, 1, false);
149
150 // Make another category.
151 $cat2 = $questiongenerator->create_question_category();
152 $match = $questiongenerator->create_question('match', null, array('category' => $cat->id));
153
154 quiz_add_quiz_question($match->id, $quiz, 0);
155
156 $multichoicemulti = $questiongenerator->create_question('multichoice', 'two_of_four', array('category' => $cat->id));
157
158 quiz_add_quiz_question($multichoicemulti->id, $quiz, 0);
159
160 $multichoicesingle = $questiongenerator->create_question('multichoice', 'one_of_four', array('category' => $cat->id));
161
162 quiz_add_quiz_question($multichoicesingle->id, $quiz, 0);
163
164 // Make a user to do the quiz.
165 $user1 = $this->getDataGenerator()->create_user();
166 $this->setUser($user1);
167
168 $quizobj = quiz::create($quiz->id, $user1->id);
169
170 // Start the attempt.
171 $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
172 $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
173
174 $timenow = time();
175 $attempt = quiz_create_attempt($quizobj, 1, false, $timenow);
176
177 quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
178
179 quiz_attempt_save_started($quizobj, $quba, $attempt);
180
181 // Process some responses from the student.
182 $attemptobj = quiz_attempt::create($attempt->id);
183
184 $tosubmit = array();
185 $selectedquestionid = $quba->get_question_attempt(1)->get_question()->id;
186 if ($selectedquestionid == $numq->id) {
187 $tosubmit[1] = array('answer' => '3.14');
188 } else {
189 $tosubmit[1] = array('answer' => 'frog');
190 }
191 $tosubmit[2] = array(
192 0 => 'amphibian',
193 1 => 'mammal',
194 2 => 'amphibian');
195 $tosubmit[3] = array('1', '0', '1', '0'); // First and third choice.
196 $tosubmit[4] = array('answer' => 0); // The first choice.
197
198 $attemptobj->process_submitted_actions($timenow, false, $tosubmit);
199
200 // Finish the attempt.
201 $attemptobj = quiz_attempt::create($attempt->id);
202 $attemptobj->process_finish($timenow, false);
203
204 // Re-load quiz attempt data.
205 $attemptobj = quiz_attempt::create($attempt->id);
206
207 // Check that results are stored as expected.
208 $this->assertEquals(1, $attemptobj->get_attempt_number());
209 $this->assertEquals(4, $attemptobj->get_sum_marks());
210 $this->assertEquals(true, $attemptobj->is_finished());
211 $this->assertEquals($timenow, $attemptobj->get_submitted_date());
212 $this->assertEquals($user1->id, $attemptobj->get_userid());
213
214 // Check quiz grades.
215 $grades = quiz_get_user_grades($quiz, $user1->id);
216 $grade = array_shift($grades);
217 $this->assertEquals(100.0, $grade->rawgrade);
218
219 // Check grade book.
220 $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id);
221 $gradebookitem = array_shift($gradebookgrades->items);
222 $gradebookgrade = array_shift($gradebookitem->grades);
223 $this->assertEquals(100, $gradebookgrade->grade);
224 }
2baf8e8a 225}