Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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 core_question;
  18  
  19  use question_bank;
  20  use question_engine;
  21  use question_state;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  require_once (__DIR__ . '/../lib.php');
  27  require_once (__DIR__ . '/helpers.php');
  28  
  29  /**
  30   * Unit tests for the question_usage_by_activity class.
  31   *
  32   * @package    core_question
  33   * @copyright  2009 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class questionusagebyactivity_test extends \advanced_testcase {
  37  
  38      public function test_set_get_preferred_model() {
  39          // Set up
  40          $quba = question_engine::make_questions_usage_by_activity('unit_test',
  41                  \context_system::instance());
  42  
  43          // Exercise SUT and verify.
  44          $quba->set_preferred_behaviour('deferredfeedback');
  45          $this->assertEquals('deferredfeedback', $quba->get_preferred_behaviour());
  46      }
  47  
  48      public function test_set_get_id() {
  49          // Set up
  50          $quba = question_engine::make_questions_usage_by_activity('unit_test',
  51                  \context_system::instance());
  52  
  53          // Exercise SUT and verify
  54          $quba->set_id_from_database(123);
  55          $this->assertEquals(123, $quba->get_id());
  56      }
  57  
  58      public function test_fake_id() {
  59          // Set up
  60          $quba = question_engine::make_questions_usage_by_activity('unit_test',
  61                  \context_system::instance());
  62  
  63          // Exercise SUT and verify
  64          $this->assertNotEmpty($quba->get_id());
  65      }
  66  
  67      public function test_create_usage_and_add_question() {
  68          // Exercise SUT
  69          $context = \context_system::instance();
  70          $quba = question_engine::make_questions_usage_by_activity('unit_test', $context);
  71          $quba->set_preferred_behaviour('deferredfeedback');
  72          $tf = \test_question_maker::make_question('truefalse', 'true');
  73          $slot = $quba->add_question($tf);
  74  
  75          // Verify.
  76          $this->assertEquals($slot, 1);
  77          $this->assertEquals('unit_test', $quba->get_owning_component());
  78          $this->assertSame($context, $quba->get_owning_context());
  79          $this->assertEquals($quba->question_count(), 1);
  80          $this->assertEquals($quba->get_question_state($slot), question_state::$notstarted);
  81      }
  82  
  83      public function test_get_question() {
  84          // Set up.
  85          $quba = question_engine::make_questions_usage_by_activity('unit_test',
  86                  \context_system::instance());
  87          $quba->set_preferred_behaviour('deferredfeedback');
  88          $tf = \test_question_maker::make_question('truefalse', 'true');
  89          $slot = $quba->add_question($tf);
  90  
  91          // Exercise SUT and verify.
  92          $this->assertSame($tf, $quba->get_question($slot, false));
  93  
  94          $this->expectException('moodle_exception');
  95          $quba->get_question($slot + 1, false);
  96      }
  97  
  98      public function test_extract_responses() {
  99          // Start a deferred feedback attempt with CBM and add the question to it.
 100          $tf = \test_question_maker::make_question('truefalse', 'true');
 101          $quba = question_engine::make_questions_usage_by_activity('unit_test',
 102                  \context_system::instance());
 103          $quba->set_preferred_behaviour('deferredcbm');
 104          $slot = $quba->add_question($tf);
 105          $quba->start_all_questions();
 106  
 107          // Prepare data to be submitted
 108          $prefix = $quba->get_field_prefix($slot);
 109          $answername = $prefix . 'answer';
 110          $certaintyname = $prefix . '-certainty';
 111          $getdata = array(
 112              $answername => 1,
 113              $certaintyname => 3,
 114              'irrelevant' => 'should be ignored',
 115          );
 116  
 117          // Exercise SUT
 118          $submitteddata = $quba->extract_responses($slot, $getdata);
 119  
 120          // Verify.
 121          $this->assertEquals(array('answer' => 1, '-certainty' => 3), $submitteddata);
 122      }
 123  
 124      public function test_access_out_of_sequence_throws_exception() {
 125          // Start a deferred feedback attempt with CBM and add the question to it.
 126          $tf = \test_question_maker::make_question('truefalse', 'true');
 127          $quba = question_engine::make_questions_usage_by_activity('unit_test',
 128                  \context_system::instance());
 129          $quba->set_preferred_behaviour('deferredcbm');
 130          $slot = $quba->add_question($tf);
 131          $quba->start_all_questions();
 132  
 133          // Prepare data to be submitted
 134          $prefix = $quba->get_field_prefix($slot);
 135          $answername = $prefix . 'answer';
 136          $certaintyname = $prefix . '-certainty';
 137          $postdata = array(
 138              $answername => 1,
 139              $certaintyname => 3,
 140              $prefix . ':sequencecheck' => 1,
 141              'irrelevant' => 'should be ignored',
 142          );
 143  
 144          // Exercise SUT - no exception yet.
 145          $quba->process_all_actions($slot, $postdata);
 146  
 147          $postdata = array(
 148              $answername => 1,
 149              $certaintyname => 3,
 150              $prefix . ':sequencecheck' => 3,
 151              'irrelevant' => 'should be ignored',
 152          );
 153  
 154          // Exercise SUT - now it should fail.
 155          $this->expectException('question_out_of_sequence_exception');
 156          $quba->process_all_actions($slot, $postdata);
 157      }
 158  
 159      /**
 160       * Test function preload all step users.
 161       */
 162      public function test_preload_all_step_users() {
 163          $this->resetAfterTest();
 164          $this->setAdminUser();
 165          // Set up.
 166          $quba = question_engine::make_questions_usage_by_activity('unit_test',
 167                  \context_system::instance());
 168  
 169          // Create an essay question in the DB.
 170          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 171          $cat = $generator->create_question_category();
 172          $essay = $generator->create_question('essay', 'editorfilepicker', ['category' => $cat->id]);
 173  
 174          // Start attempt at the question.
 175          $q = question_bank::load_question($essay->id);
 176          $quba->set_preferred_behaviour('deferredfeedback');
 177          $slot = $quba->add_question($q, 10);
 178          $quba->start_question($slot, 1);
 179  
 180          // Finish the attempt.
 181          $quba->finish_all_questions();
 182          question_engine::save_questions_usage_by_activity($quba);
 183  
 184          // The user information of question attempt step should be loaded.
 185          $quba->preload_all_step_users();
 186          $qa = $quba->get_attempt_iterator()->current();
 187          $steps = $qa->get_full_step_iterator();
 188          $this->assertEquals('Admin User', $steps[0]->get_user_fullname());
 189      }
 190  }