Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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 qbank_previewquestion;
  18  
  19  use context_course;
  20  use moodle_url;
  21  use core\plugininfo\qbank;
  22  use question_bank;
  23  use question_engine;
  24  use stdClass;
  25  
  26  /**
  27   * Helper tests for question preview.
  28   *
  29   * @package    qbank_previewquestion
  30   * @copyright  2021 Catalyst IT Australia Pty Ltd
  31   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   * @coversDefaultClass \qbank_previewquestion\helper
  34   */
  35  class qbank_preview_helper_test extends \advanced_testcase {
  36  
  37      /**
  38       * @var bool|\context|\context_course $context
  39       */
  40      public $context;
  41  
  42      /**
  43       * @var object $questiondata;
  44       */
  45      public $questiondata;
  46  
  47      /**
  48       * @var \question_usage_by_activity $quba
  49       */
  50      public $quba;
  51  
  52      /**
  53       * @var question_preview_options $options
  54       */
  55      public $options;
  56  
  57      /**
  58       * @var \moodle_url $returnurl
  59       */
  60      public $returnurl;
  61  
  62      /**
  63       * Test set up.
  64       *
  65       * This is executed before running any test in this file.
  66       */
  67      public function setUp(): void {
  68          global $USER;
  69          $this->resetAfterTest();
  70          $this->setAdminUser();
  71          $generator = $this->getDataGenerator();
  72          $questiongenerator = $generator->get_plugin_generator('core_question');
  73          // Create a course.
  74          $course = $generator->create_course();
  75          $this->context = context_course::instance($course->id);
  76          // Create a question in the default category.
  77          $contexts = new \core_question\local\bank\question_edit_contexts($this->context);
  78          $cat = question_make_default_categories($contexts->all());
  79          $this->questiondata = $questiongenerator->create_question('numerical', null,
  80                  ['name' => 'Example question', 'category' => $cat->id]);
  81          $this->quba = question_engine::make_questions_usage_by_activity('core_question_preview',
  82              \context_user::instance($USER->id));
  83          $this->options = new question_preview_options($this->questiondata);
  84          $this->options->load_user_defaults();
  85          $this->options->set_from_request();
  86          $this->returnurl = new moodle_url('/question/edit.php');
  87      }
  88  
  89      /**
  90       * Test the preview action url from the helper class.
  91       *
  92       * @covers ::question_preview_action_url
  93       */
  94      public function test_question_preview_action_url() {
  95          $actionurl = helper::question_preview_action_url($this->questiondata->id, $this->quba->get_id(), $this->options,
  96                  $this->context, $this->returnurl, question_preview_options::ALWAYS_LATEST);
  97          $params = [
  98             'id' => $this->questiondata->id,
  99             'previewid' => $this->quba->get_id(),
 100             'returnurl' => $this->returnurl,
 101             'courseid' => $this->context->instanceid,
 102             'restartversion' => question_preview_options::ALWAYS_LATEST,
 103          ];
 104          $params = array_merge($params, $this->options->get_url_params());
 105          $expectedurl = new moodle_url('/question/bank/previewquestion/preview.php', $params);
 106          $this->assertEquals($expectedurl, $actionurl);
 107      }
 108  
 109      /**
 110       * Test the preview action url from the helper class when no restartversion is passed.
 111       *
 112       * @covers ::question_preview_action_url
 113       */
 114      public function test_question_preview_action_url_no_restartversion() {
 115          $actionurl = helper::question_preview_action_url($this->questiondata->id, $this->quba->get_id(), $this->options,
 116                  $this->context, $this->returnurl);
 117          $params = [
 118              'id' => $this->questiondata->id,
 119              'previewid' => $this->quba->get_id(),
 120              'returnurl' => $this->returnurl,
 121              'courseid' => $this->context->instanceid
 122          ];
 123          $params = array_merge($params, $this->options->get_url_params());
 124          $expectedurl = new moodle_url('/question/bank/previewquestion/preview.php', $params);
 125          $this->assertEquals($expectedurl, $actionurl);
 126      }
 127  
 128      /**
 129       * Test the preview form url from the helper class.
 130       *
 131       * @covers ::question_preview_form_url
 132       */
 133      public function test_question_preview_form_url() {
 134          $formurl = helper::question_preview_form_url(
 135                  $this->questiondata->id, $this->context, $this->quba->get_id(), $this->returnurl);
 136          $params = [
 137              'id' => $this->questiondata->id,
 138              'previewid' => $this->quba->get_id(),
 139              'returnurl' => $this->returnurl,
 140              'courseid' => $this->context->instanceid
 141          ];
 142          $expectedurl = new moodle_url('/question/bank/previewquestion/preview.php', $params);
 143          $this->assertEquals($expectedurl, $formurl);
 144      }
 145  
 146      /**
 147       * Test the preview url from the helper class.
 148       *
 149       * @covers ::question_preview_url
 150       */
 151      public function test_question_preview_url() {
 152          $previewurl = helper::question_preview_url($this->questiondata->id, $this->options->behaviour, $this->options->maxmark,
 153                  $this->options, $this->options->variant, $this->context, null, question_preview_options::ALWAYS_LATEST);
 154          $params = [
 155              'id' => $this->questiondata->id,
 156              'behaviour' => $this->options->behaviour,
 157              'maxmark' => $this->options->maxmark,
 158              'courseid' => $this->context->instanceid,
 159              'restartversion' => question_preview_options::ALWAYS_LATEST,
 160          ];
 161          // Extra params for options.
 162          $params['correctness']     = $this->options->correctness;
 163          $params['marks']           = $this->options->marks;
 164          $params['markdp']          = $this->options->markdp;
 165          $params['feedback']        = (bool) $this->options->feedback;
 166          $params['generalfeedback'] = (bool) $this->options->generalfeedback;
 167          $params['rightanswer']     = (bool) $this->options->rightanswer;
 168          $params['history']         = (bool) $this->options->history;
 169          $expectedurl = new moodle_url('/question/bank/previewquestion/preview.php', $params);
 170          $this->assertEquals($expectedurl, $previewurl);
 171      }
 172  
 173  
 174      /**
 175       * Test the preview url from the helper class.
 176       *
 177       * @covers ::question_preview_url
 178       */
 179      public function test_question_preview_url_no_restartversion() {
 180          $previewurl = helper::question_preview_url($this->questiondata->id, $this->options->behaviour, $this->options->maxmark,
 181                  $this->options, $this->options->variant, $this->context, null);
 182          $params = [
 183              'id' => $this->questiondata->id,
 184              'behaviour' => $this->options->behaviour,
 185              'maxmark' => $this->options->maxmark,
 186              'courseid' => $this->context->instanceid,
 187          ];
 188          // Extra params for options.
 189          $params['correctness']     = $this->options->correctness;
 190          $params['marks']           = $this->options->marks;
 191          $params['markdp']          = $this->options->markdp;
 192          $params['feedback']        = (bool) $this->options->feedback;
 193          $params['generalfeedback'] = (bool) $this->options->generalfeedback;
 194          $params['rightanswer']     = (bool) $this->options->rightanswer;
 195          $params['history']         = (bool) $this->options->history;
 196          $expectedurl = new moodle_url('/question/bank/previewquestion/preview.php', $params);
 197          $this->assertEquals($expectedurl, $previewurl);
 198      }
 199  
 200      /**
 201       * Test the preview comment callback if available.
 202       *
 203       * @covers ::get_preview_extra_elements
 204       */
 205      public function test_get_preview_extra_elements() {
 206          global $PAGE;
 207          $PAGE->set_url('/');
 208  
 209          $question = \question_bank::load_question($this->questiondata->id);
 210          list($comment, $extraelements) = helper::get_preview_extra_elements($question, $this->context->instanceid);
 211          if (qbank::is_plugin_enabled('qbank_comment')) {
 212              $this->assertStringContainsString("comment-area", $comment);
 213          } else {
 214              $this->assertEquals('', $comment);
 215          }
 216      }
 217  
 218      /**
 219       * Test method load_versions().
 220       *
 221       * @covers ::load_versions
 222       */
 223      public function test_load_versions() {
 224          $this->resetAfterTest();
 225  
 226          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 227          $qcat1 = $generator->create_question_category(['name' => 'My category', 'sortorder' => 1, 'idnumber' => 'myqcat']);
 228          $questiongenerated = $generator->create_question('description', null, ['name' => 'q1', 'category' => $qcat1->id]);
 229  
 230          $qtypeobj = question_bank::get_qtype($questiongenerated->qtype);
 231          $question = question_bank::load_question($questiongenerated->id);
 232          $versionids = helper::load_versions($question->questionbankentryid);
 233          $this->assertEquals([
 234              $question->id => 1,
 235          ], $versionids);
 236  
 237          $fromform = new stdClass();
 238          $fromform->name = 'Name edited';
 239          $fromform->category = $qcat1->id;
 240          $questiontwo = $qtypeobj->save_question($questiongenerated, $fromform);
 241          $versionids = helper::load_versions($question->questionbankentryid);
 242          $this->assertSame([
 243              $question->id => 1,
 244              $questiontwo->id => 2,
 245          ], $versionids);
 246      }
 247  
 248      /**
 249       * Test method get_restart_id().
 250       *
 251       * This should return the value of the specified version number, or the latest version if ALWAYS_LATEST is passed.
 252       *
 253       * @covers ::get_restart_id
 254       * @return void
 255       */
 256      public function test_get_restart_id(): void {
 257          $versions = [
 258              100 => 1,
 259              200 => 2,
 260              300 => 3
 261          ];
 262  
 263          $this->assertEquals(100, helper::get_restart_id($versions, 1));
 264          $this->assertEquals(200, helper::get_restart_id($versions, 2));
 265          $this->assertEquals(300, helper::get_restart_id($versions, 3));
 266          $this->assertEquals(300, helper::get_restart_id($versions, question_preview_options::ALWAYS_LATEST));
 267          $this->assertNull(helper::get_restart_id($versions, 4));
 268          $this->assertNull(helper::get_restart_id([], 1));
 269          $this->assertNull(helper::get_restart_id([], question_preview_options::ALWAYS_LATEST));
 270      }
 271  }