Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace mod_quiz\backup;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
23
 
24
/**
25
 * Restore date tests.
26
 *
27
 * @package    mod_quiz
28
 * @copyright  2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
1441 ariadna 31
final class restore_date_test extends \restore_date_testcase {
1 efrain 32
 
33
    /**
34
     * Test restore dates.
35
     */
11 efrain 36
    public function test_restore_dates(): void {
1 efrain 37
        global $DB, $USER;
38
 
39
        // Create quiz data.
1441 ariadna 40
        $record = [
41
            'timeopen' => 100,
42
            'timeclose' => 100,
43
            'timemodified' => 100,
44
            'timecreated' => 100,
45
            'questionsperpage' => 0,
46
            'grade' => 100.0,
47
            'sumgrades' => 2,
48
            'precreateattempts' => 1,
49
        ];
1 efrain 50
        list($course, $quiz) = $this->create_course_and_module('quiz', $record);
51
 
52
        // Create questions.
53
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
54
        $cat = $questiongenerator->create_question_category();
55
        $saq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
56
        // Add to the quiz.
57
        quiz_add_quiz_question($saq->id, $quiz);
58
 
59
        // Create an attempt.
60
        $timestamp = 100;
61
        $quizobj = \mod_quiz\quiz_settings::create($quiz->id);
62
        $attempt = quiz_create_attempt($quizobj, 1, false, $timestamp, false);
63
        $quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
64
        $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
65
        quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timestamp);
1441 ariadna 66
        quiz_attempt_save_started($quizobj, $quba, $attempt, $timestamp);
1 efrain 67
 
68
        // Quiz grade.
69
        $grade = new \stdClass();
70
        $grade->quiz = $quiz->id;
71
        $grade->userid = $USER->id;
72
        $grade->grade = 8.9;
73
        $grade->timemodified = $timestamp;
74
        $grade->id = $DB->insert_record('quiz_grades', $grade);
75
 
76
        // User override.
77
        $override = (object)[
78
            'quiz' => $quiz->id,
79
            'groupid' => 0,
80
            'userid' => $USER->id,
81
            'sortorder' => 1,
82
            'timeopen' => 100,
83
            'timeclose' => 200
84
        ];
85
        $DB->insert_record('quiz_overrides', $override);
86
 
87
        // Set time fields to a constant for easy validation.
88
        $DB->set_field('quiz_attempts', 'timefinish', $timestamp);
89
 
90
        // Do backup and restore.
91
        $newcourseid = $this->backup_and_restore($course);
92
        $newquiz = $DB->get_record('quiz', ['course' => $newcourseid]);
93
 
94
        $this->assertFieldsNotRolledForward($quiz, $newquiz, ['timecreated', 'timemodified']);
95
        $props = ['timeclose', 'timeopen'];
96
        $this->assertFieldsRolledForward($quiz, $newquiz, $props);
1441 ariadna 97
        $this->assertEquals($quiz->precreateattempts, $newquiz->precreateattempts);
1 efrain 98
 
99
        $newattempt = $DB->get_record('quiz_attempts', ['quiz' => $newquiz->id]);
100
        $newoverride = $DB->get_record('quiz_overrides', ['quiz' => $newquiz->id]);
101
        $newgrade = $DB->get_record('quiz_grades', ['quiz' => $newquiz->id]);
102
 
103
        // Attempt time checks.
104
        $diff = $this->get_diff();
105
        $this->assertEquals($timestamp, $newattempt->timemodified);
106
        $this->assertEquals($timestamp, $newattempt->timefinish);
107
        $this->assertEquals($timestamp, $newattempt->timestart);
108
        $this->assertEquals($timestamp + $diff, $newattempt->timecheckstate); // Should this be rolled?
109
 
110
        // Quiz override time checks.
111
        $diff = $this->get_diff();
112
        $this->assertEquals($override->timeopen + $diff, $newoverride->timeopen);
113
        $this->assertEquals($override->timeclose + $diff, $newoverride->timeclose);
114
 
115
        // Quiz grade time checks.
116
        $this->assertEquals($grade->timemodified, $newgrade->timemodified);
117
    }
118
}