MDL-52339 question: Fix question attempt removal for MySQL
[moodle.git] / question / tests / previewlib_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 events tests.
19  *
20  * @package    mod_quiz
21  * @category   phpunit
22  * @copyright  2013 Adrian Greeve
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/question/previewlib.php');
31 /**
32  * Unit tests for question preview.
33  *
34  * @package    question
35  * @category   phpunit
36  * @copyright  2016 Andrew Nicols
37  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class question_previewlib_testcase extends advanced_testcase {
41     /**
42      * Setup some convenience test data with a single attempt.
43      *
44      * @return question_usage_by_activity
45      */
46     protected function prepare_question_data() {
47         $this->resetAfterTest(true);
49         $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
51         // Create a questions and start the preview.
52         $cat = $questiongenerator->create_question_category();
54         $quba = question_engine::make_questions_usage_by_activity('core_question_preview', context_system::instance());
55         $quba->set_preferred_behaviour('deferredfeedback');
56         $questiondata = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
57         $question = question_bank::load_question($questiondata->id);
58         $quba->add_question($question);
59         $quba->start_all_questions();
60         question_engine::save_questions_usage_by_activity($quba);
62         return $quba;
63     }
65     /**
66      * Test the attempt deleted event.
67      */
68     public function test_question_preview_cron() {
69         global $DB;
71         // Create some quiz data.
72         // This will create two questions.
73         $quba1 = $this->prepare_question_data();
75         // Run the cron.
76         ob_start();
77         question_preview_cron();
78         $output = ob_get_clean();
79         $this->assertEquals("\n  Cleaning up old question previews...done.\n", $output);
81         // The attempt should not have been removed.
82         // There should be one question usage with two question attempts.
83         $this->assertEquals(1, $DB->count_records('question_usages', array('id' => $quba1->get_id())));
84         $this->assertEquals(1, $DB->count_records('question_attempts', array('questionusageid' => $quba1->get_id())));
85         $this->assertEquals(1, $DB->count_records('question_attempt_steps'));
86         $this->assertEquals(1, $DB->count_records('question_attempt_step_data'));
88         // Update the timemodified and timecreated to be in the past.
89         $DB->set_field('question_attempts', 'timemodified', time() - WEEKSECS);
90         $DB->set_field('question_attempt_steps', 'timecreated', time() - WEEKSECS);
92         // Create some quiz data.
93         // This will create two questions.
94         $quba2 = $this->prepare_question_data();
96         // There will now be 2 usages, etc.
97         $this->assertEquals(2, $DB->count_records('question_usages'));
98         $this->assertEquals(2, $DB->count_records('question_attempts'));
99         $this->assertEquals(2, $DB->count_records('question_attempt_steps'));
100         $this->assertEquals(2, $DB->count_records('question_attempt_step_data'));
102         // Run the cron again.
103         // $quba1 will be removed, but $quba2 should still be present.
104         ob_start();
105         question_preview_cron();
106         $output = ob_get_clean();
107         $this->assertEquals("\n  Cleaning up old question previews...done.\n", $output);
109         $this->assertEquals(0, $DB->count_records('question_usages', array('id' => $quba1->get_id())));
110         $this->assertEquals(0, $DB->count_records('question_attempts', array('questionusageid' => $quba1->get_id())));
111         $this->assertEquals(1, $DB->count_records('question_usages', array('id' => $quba2->get_id())));
112         $this->assertEquals(1, $DB->count_records('question_attempts', array('questionusageid' => $quba2->get_id())));
113         $this->assertEquals(1, $DB->count_records('question_attempt_steps'));
114         $this->assertEquals(1, $DB->count_records('question_attempt_step_data'));
115     }