Merge branch 'MDL-53226-master' of git://github.com/andrewnicols/moodle
[moodle.git] / question / tests / least_used_variant_strategy_test.php
blobf1b626781ef98af185dd9a2f925e741f6d249f3a
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 * Tests for the {@link core_question\engine\variants\least_used_strategy} class.
20 * @package core_question
21 * @copyright 2015 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
31 /**
32 * Tests for the {@link core_question\engine\variants\least_used_strategy} class.
34 * @copyright 2015 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class least_used_variant_strategy_testcase extends advanced_testcase {
39 public function test_question_with_one_variant_always_picks_that() {
40 $question = test_question_maker::make_question('shortanswer');
41 $quba = question_engine::make_questions_usage_by_activity('test', context_system::instance());
42 $quba->set_preferred_behaviour('deferredfeedback');
43 $slot = $quba->add_question($question);
44 $quba->start_all_questions(new core_question\engine\variants\least_used_strategy(
45 $quba, new qubaid_list(array())));
46 $this->assertEquals(1, $quba->get_variant($slot));
49 public function test_synchronised_question_should_use_the_same_dataset() {
50 // Actually, we cheat here. We use the same question twice, not two different synchronised questions.
51 $question = test_question_maker::make_question('calculated');
52 $quba = question_engine::make_questions_usage_by_activity('test', context_system::instance());
53 $quba->set_preferred_behaviour('deferredfeedback');
54 $slot1 = $quba->add_question($question);
55 $slot2 = $quba->add_question($question);
56 $quba->start_all_questions(new core_question\engine\variants\least_used_strategy(
57 $quba, new qubaid_list(array())));
58 $this->assertEquals($quba->get_variant($slot1), $quba->get_variant($slot2));
61 public function test_second_attempt_uses_other_dataset() {
62 global $DB;
63 $this->resetAfterTest();
64 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
66 $cat = $generator->create_question_category();
67 $questiondata = $generator->create_question('calculated', null, array('category' => $cat->id));
69 // Create two dataset items.
70 $adefinitionid = $DB->get_field_sql("
71 SELECT qdd.id
72 FROM {question_dataset_definitions} qdd
73 JOIN {question_datasets} qd ON qd.datasetdefinition = qdd.id
74 WHERE qd.question = ?
75 AND qdd.name = ?", array($questiondata->id, 'a'));
76 $bdefinitionid = $DB->get_field_sql("
77 SELECT qdd.id
78 FROM {question_dataset_definitions} qdd
79 JOIN {question_datasets} qd ON qd.datasetdefinition = qdd.id
80 WHERE qd.question = ?
81 AND qdd.name = ?", array($questiondata->id, 'b'));
82 $DB->set_field('question_dataset_definitions', 'itemcount', 2, array('id' => $adefinitionid));
83 $DB->set_field('question_dataset_definitions', 'itemcount', 2, array('id' => $bdefinitionid));
84 $DB->insert_record('question_dataset_items', array('definition' => $adefinitionid,
85 'itemnumber' => 1, 'value' => 3));
86 $DB->insert_record('question_dataset_items', array('definition' => $bdefinitionid,
87 'itemnumber' => 1, 'value' => 7));
88 $DB->insert_record('question_dataset_items', array('definition' => $adefinitionid,
89 'itemnumber' => 2, 'value' => 6));
90 $DB->insert_record('question_dataset_items', array('definition' => $bdefinitionid,
91 'itemnumber' => 2, 'value' => 4));
93 $question = question_bank::load_question($questiondata->id);
95 $quba1 = question_engine::make_questions_usage_by_activity('test', context_system::instance());
96 $quba1->set_preferred_behaviour('deferredfeedback');
97 $slot1 = $quba1->add_question($question);
98 $quba1->start_all_questions(new core_question\engine\variants\least_used_strategy(
99 $quba1, new qubaid_list(array())));
100 question_engine::save_questions_usage_by_activity($quba1);
101 $variant1 = $quba1->get_variant($slot1);
103 // Second attempt should use the other variant.
104 $quba2 = question_engine::make_questions_usage_by_activity('test', context_system::instance());
105 $quba2->set_preferred_behaviour('deferredfeedback');
106 $slot2 = $quba2->add_question($question);
107 $quba2->start_all_questions(new core_question\engine\variants\least_used_strategy(
108 $quba1, new qubaid_list(array($quba1->get_id()))));
109 question_engine::save_questions_usage_by_activity($quba2);
110 $variant2 = $quba2->get_variant($slot2);
112 $this->assertNotEquals($variant1, $variant2);
114 // Third attempt uses either variant at random.
115 $quba3 = question_engine::make_questions_usage_by_activity('test', context_system::instance());
116 $quba3->set_preferred_behaviour('deferredfeedback');
117 $slot3 = $quba3->add_question($question);
118 $quba3->start_all_questions(new core_question\engine\variants\least_used_strategy(
119 $quba1, new qubaid_list(array($quba1->get_id(), $quba2->get_id()))));
120 $variant3 = $quba3->get_variant($slot3);
122 $this->assertTrue($variant3 == $variant1 || $variant3 == $variant2);