Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 39 and 401]

   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 test helper code for testing the upgrade to the new
  19   * question engine. The actual tests are organised by question type in files
  20   * like question/type/truefalse/tests/upgradelibnewqe_test.php.
  21   *
  22   * @package    moodlecore
  23   * @subpackage questionengine
  24   * @copyright  2009 The Open University
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  global $CFG;
  32  require_once (__DIR__ . '/../upgradelib.php');
  33  
  34  
  35  /**
  36   * Subclass of question_engine_attempt_upgrader to help with testing.
  37   *
  38   * @copyright  2009 The Open University
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class test_question_engine_attempt_upgrader extends question_engine_attempt_upgrader {
  42      public function prevent_timeout() {
  43      }
  44  
  45      public function __construct($loader, $logger) {
  46          $this->questionloader = $loader;
  47          $this->logger = $logger;
  48      }
  49  }
  50  
  51  
  52  /**
  53   * Subclass of question_engine_upgrade_question_loader for unit testing.
  54   *
  55   * @copyright  2009 The Open University
  56   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  57   */
  58  class test_question_engine_upgrade_question_loader extends question_engine_upgrade_question_loader {
  59      public function put_question_in_cache($question) {
  60          $this->cache[$question->id] = $question;
  61      }
  62  
  63      public function load_question($questionid, $quizid) {
  64          global $CFG;
  65  
  66          if (isset($this->cache[$questionid])) {
  67              return $this->cache[$questionid];
  68          }
  69  
  70          return null;
  71      }
  72  
  73      public function put_dataset_in_cache($questionid, $selecteditem, $dataset) {
  74          $this->datasetcache[$questionid][$selecteditem] = $dataset;
  75      }
  76  
  77      public function load_dataset($questionid, $selecteditem) {
  78          global $DB;
  79  
  80          if (isset($this->datasetcache[$questionid][$selecteditem])) {
  81              return $this->datasetcache[$questionid][$selecteditem];
  82          }
  83  
  84          throw new coding_exception('Test dataset not loaded.');
  85      }
  86  }
  87  
  88  
  89  /**
  90   * Base class for tests that thest the upgrade of one particular attempt and
  91   * one question.
  92   *
  93   * @copyright  2009 The Open University
  94   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  95   */
  96  abstract class question_attempt_upgrader_test_base extends advanced_testcase {
  97      protected $updater;
  98      protected $loader;
  99  
 100      protected function setUp(): void {
 101          parent::setUp();
 102          $logger = new dummy_question_engine_assumption_logger();
 103          $this->loader = new test_question_engine_upgrade_question_loader($logger);
 104          $this->updater = new test_question_engine_attempt_upgrader($this->loader, $logger);
 105      }
 106  
 107      protected function tearDown(): void {
 108          $this->updater = null;
 109          parent::tearDown();
 110      }
 111  
 112      /**
 113       * Clear text, bringing independence of html2text results
 114       *
 115       * Some tests performing text comparisons of converted text are too much
 116       * dependent of the behavior of the html2text library. This function is
 117       * aimed to reduce such dependencies that should not affect the results
 118       * of these question attempt upgrade tests.
 119       */
 120      protected function clear_html2text_dependencies($qa) {
 121          // Cleaning all whitespace should be enough to ignore any html2text dependency
 122          if (property_exists($qa, 'responsesummary')) {
 123              $qa->responsesummary = preg_replace('/\s/', '', $qa->responsesummary);
 124          }
 125          if (property_exists($qa, 'questionsummary')) {
 126              $qa->questionsummary = preg_replace('/\s/', '', $qa->questionsummary);
 127          }
 128      }
 129  
 130      /**
 131       * Compare two qas, ignoring inessential differences.
 132       * @param object $expectedqa the expected qa.
 133       * @param object $qa the actual qa.
 134       */
 135      protected function compare_qas($expectedqa, $qa) {
 136          $this->clear_html2text_dependencies($expectedqa);
 137          $this->clear_html2text_dependencies($qa);
 138  
 139          $this->assertEquals($expectedqa, $qa);
 140      }
 141  }