MDL-63770 core: Fix a false-positive in `reverseproxyabused` check
[moodle.git] / question / tests / generator_test.php
blob945c9839aa8193e7afb4a1c8faf252ef6e2a6182
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.
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 * Data generators tests
20 * @package moodlecore
21 * @subpackage questionengine
22 * @copyright 2013 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Test data generator
32 * @copyright 2013 The Open University
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class core_question_generator_testcase extends advanced_testcase {
36 public function test_create() {
37 global $DB;
39 $this->resetAfterTest();
40 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
42 $count = $DB->count_records('question_categories');
44 $cat = $generator->create_question_category();
45 $count += $count ? 1 : 2; // Calling $generator->create_question_category() for the first time
46 // creates a Top category as well.
47 $this->assertEquals($count, $DB->count_records('question_categories'));
49 $cat = $generator->create_question_category(array(
50 'name' => 'My category', 'sortorder' => 1));
51 $this->assertSame('My category', $cat->name);
52 $this->assertSame(1, $cat->sortorder);
55 public function test_idnumbers_in_categories_and_questions() {
56 $this->resetAfterTest();
57 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
58 list($category, $course, $qcat, $questions) = $generator->setup_course_and_questions();
59 // Check default idnumbers in question_category and questions.
60 $this->assertNull($qcat->idnumber);
61 $this->assertNull($questions[0]->idnumber);
62 $this->assertNull($questions[1]->idnumber);
63 // Check created idnumbers.
64 $qcat1 = $generator->create_question_category(array(
65 'name' => 'My category', 'sortorder' => 1, 'idnumber' => 'myqcat'));
66 $this->assertSame('myqcat', $qcat1->idnumber);
67 $quest1 = $generator->update_question($questions[0], null, ['idnumber' => 'myquest']);
68 $this->assertSame('myquest', $quest1->idnumber);
69 $quest3 = $generator->create_question('shortanswer', null,
70 ['name' => 'sa1', 'category' => $qcat1->id, 'idnumber' => 'myquest_3']);
71 $this->assertSame('myquest_3', $quest3->idnumber);
72 // Check idnumbers of questions moved. Note have to use load_question_data or we only get to see old cached data.
73 question_move_questions_to_category([$quest1->id], $qcat1->id);
74 $this->assertSame('myquest', question_bank::load_question_data($quest1->id)->idnumber);
75 // Can only change idnumber of quest2 once quest1 has been moved to another category.
76 $quest2 = $generator->update_question($questions[1], null, ['idnumber' => 'myquest']);
77 question_move_questions_to_category([$quest2->id], $qcat1->id);
78 $this->assertSame('myquest_4', question_bank::load_question_data($quest2->id)->idnumber);
79 // Check can add an idnumber of 0.
80 $quest4 = $generator->create_question('shortanswer', null, ['name' => 'sa1', 'category' => $qcat1->id, 'idnumber' => '0']);
81 $this->assertSame('0', $quest4->idnumber);