9062af1b82b1ef41e1a12cda5fba8729002e4057
[moodle.git] / mod / quiz / tests / attempt_walkthrough_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  * 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  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot . '/mod/quiz/editlib.php');
31 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
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  */
42 class mod_quiz_attempt_walkthrough_testcase extends advanced_testcase {
44     /**
45      * Create a quiz with questions and walk through a quiz attempt.
46      */
47     public function test_quiz_attempt_walkthrough() {
48         global $SITE;
50         $this->resetAfterTest(true);
52         // Make a quiz.
53         $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
55         $quiz = $quizgenerator->create_instance(array('course'=>$SITE->id, 'questionsperpage' => 0, 'grade' => 100.0,
56                                                       'sumgrades' => 2));
58         // Create a couple of questions.
59         $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
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));
65         // Add them to the quiz.
66         quiz_add_quiz_question($saq->id, $quiz);
67         quiz_add_quiz_question($numq->id, $quiz);
69         // Make a user to do the quiz.
70         $user1 = $this->getDataGenerator()->create_user();
71         $this->setUser($user1);
73         $quizobj = quiz::create($quiz->id, $user1->id);
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);
79         $timenow = time();
80         $attempt = quiz_create_attempt($quizobj, 1, false, $timenow);
82         quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
84         quiz_attempt_save_started($quizobj, $quba, $attempt);
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);
91         $tosubmit = array();
92         $tosubmit['slots'] = '1,2';
93         $tosubmit[$prefix1.':sequencecheck'] = 1;
94         $tosubmit[$prefix1.'answer'] = 'frog';
96         $tosubmit[$prefix2.':sequencecheck'] = 1;
97         $tosubmit[$prefix2.'answer'] = '3.14';
99         $attemptobj->process_submitted_actions($timenow, false, $tosubmit);
101         // Finish the attempt.
102         $attemptobj = quiz_attempt::create($attempt->id);
103         $attemptobj->process_finish($timenow, false);
105         // Re-load quiz attempt data.
106         $attemptobj = quiz_attempt::create($attempt->id);
108         // Check that results are stored as expected.
109         $this->assertEquals(1, $attemptobj->get_attempt_number());
110         $this->assertEquals(2, $attemptobj->get_sum_marks());
111         $this->assertEquals(true, $attemptobj->is_finished());
112         $this->assertEquals($timenow, $attemptobj->get_submitted_date());
113         $this->assertEquals($user1->id, $attemptobj->get_userid());
115         // Check quiz grades.
116         $grades = quiz_get_user_grades($quiz, $user1->id);
117         $grade = array_shift($grades);
118         $this->assertEquals(100.0, $grade->rawgrade);
120         // Check grade book.
121         $gradebookgrades = grade_get_grades($SITE->id, 'mod', 'quiz', $quiz->id, $user1->id);
122         $gradebookitem = array_shift($gradebookgrades->items);
123         $gradebookgrade = array_shift($gradebookitem->grades);
124         $this->assertEquals(100, $gradebookgrade->grade);
125     }