Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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  /**
  18   * This file contains tests for the question_usage_by_activity class.
  19   *
  20   * @package   core_question
  21   * @copyright 2009 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once (__DIR__ . '/../lib.php');
  30  require_once (__DIR__ . '/helpers.php');
  31  
  32  
  33  /**
  34   * Unit tests for the question_usage_by_activity class.
  35   *
  36   * @copyright  2009 The Open University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class question_usage_by_activity_test extends advanced_testcase {
  40  
  41      public function test_set_get_preferred_model() {
  42          // Set up
  43          $quba = question_engine::make_questions_usage_by_activity('unit_test',
  44                  context_system::instance());
  45  
  46          // Exercise SUT and verify.
  47          $quba->set_preferred_behaviour('deferredfeedback');
  48          $this->assertEquals('deferredfeedback', $quba->get_preferred_behaviour());
  49      }
  50  
  51      public function test_set_get_id() {
  52          // Set up
  53          $quba = question_engine::make_questions_usage_by_activity('unit_test',
  54                  context_system::instance());
  55  
  56          // Exercise SUT and verify
  57          $quba->set_id_from_database(123);
  58          $this->assertEquals(123, $quba->get_id());
  59      }
  60  
  61      public function test_fake_id() {
  62          // Set up
  63          $quba = question_engine::make_questions_usage_by_activity('unit_test',
  64                  context_system::instance());
  65  
  66          // Exercise SUT and verify
  67          $this->assertNotEmpty($quba->get_id());
  68      }
  69  
  70      public function test_create_usage_and_add_question() {
  71          // Exercise SUT
  72          $context = context_system::instance();
  73          $quba = question_engine::make_questions_usage_by_activity('unit_test', $context);
  74          $quba->set_preferred_behaviour('deferredfeedback');
  75          $tf = test_question_maker::make_question('truefalse', 'true');
  76          $slot = $quba->add_question($tf);
  77  
  78          // Verify.
  79          $this->assertEquals($slot, 1);
  80          $this->assertEquals('unit_test', $quba->get_owning_component());
  81          $this->assertSame($context, $quba->get_owning_context());
  82          $this->assertEquals($quba->question_count(), 1);
  83          $this->assertEquals($quba->get_question_state($slot), question_state::$notstarted);
  84      }
  85  
  86      public function test_get_question() {
  87          // Set up.
  88          $quba = question_engine::make_questions_usage_by_activity('unit_test',
  89                  context_system::instance());
  90          $quba->set_preferred_behaviour('deferredfeedback');
  91          $tf = test_question_maker::make_question('truefalse', 'true');
  92          $slot = $quba->add_question($tf);
  93  
  94          // Exercise SUT and verify.
  95          $this->assertSame($tf, $quba->get_question($slot, false));
  96  
  97          $this->expectException('moodle_exception');
  98          $quba->get_question($slot + 1, false);
  99      }
 100  
 101      public function test_extract_responses() {
 102          // Start a deferred feedback attempt with CBM and add the question to it.
 103          $tf = test_question_maker::make_question('truefalse', 'true');
 104          $quba = question_engine::make_questions_usage_by_activity('unit_test',
 105                  context_system::instance());
 106          $quba->set_preferred_behaviour('deferredcbm');
 107          $slot = $quba->add_question($tf);
 108          $quba->start_all_questions();
 109  
 110          // Prepare data to be submitted
 111          $prefix = $quba->get_field_prefix($slot);
 112          $answername = $prefix . 'answer';
 113          $certaintyname = $prefix . '-certainty';
 114          $getdata = array(
 115              $answername => 1,
 116              $certaintyname => 3,
 117              'irrelevant' => 'should be ignored',
 118          );
 119  
 120          // Exercise SUT
 121          $submitteddata = $quba->extract_responses($slot, $getdata);
 122  
 123          // Verify.
 124          $this->assertEquals(array('answer' => 1, '-certainty' => 3), $submitteddata);
 125      }
 126  
 127      public function test_access_out_of_sequence_throws_exception() {
 128          // Start a deferred feedback attempt with CBM and add the question to it.
 129          $tf = test_question_maker::make_question('truefalse', 'true');
 130          $quba = question_engine::make_questions_usage_by_activity('unit_test',
 131                  context_system::instance());
 132          $quba->set_preferred_behaviour('deferredcbm');
 133          $slot = $quba->add_question($tf);
 134          $quba->start_all_questions();
 135  
 136          // Prepare data to be submitted
 137          $prefix = $quba->get_field_prefix($slot);
 138          $answername = $prefix . 'answer';
 139          $certaintyname = $prefix . '-certainty';
 140          $postdata = array(
 141              $answername => 1,
 142              $certaintyname => 3,
 143              $prefix . ':sequencecheck' => 1,
 144              'irrelevant' => 'should be ignored',
 145          );
 146  
 147          // Exercise SUT - no exception yet.
 148          $quba->process_all_actions($slot, $postdata);
 149  
 150          $postdata = array(
 151              $answername => 1,
 152              $certaintyname => 3,
 153              $prefix . ':sequencecheck' => 3,
 154              'irrelevant' => 'should be ignored',
 155          );
 156  
 157          // Exercise SUT - now it should fail.
 158          $this->expectException('question_out_of_sequence_exception');
 159          $quba->process_all_actions($slot, $postdata);
 160      }
 161  }