Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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  namespace qbank_history;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/question/editlib.php');
  23  
  24  /**
  25   * Custom history view - qbank api test.
  26   *
  27   * @package    qbank_history
  28   * @copyright  2022 Catalyst IT Australia Pty Ltd
  29   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   * @coversDefaultClass \qbank_history\question_history_view
  32   */
  33  class question_history_view_test extends \advanced_testcase {
  34  
  35      /**
  36       * Test that the history page shows all the versions of a question.
  37       *
  38       * @covers ::display
  39       */
  40      public function test_question_history_shows_all_versions() {
  41          $this->resetAfterTest();
  42          $this->setAdminUser();
  43          $generator = $this->getDataGenerator();
  44          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  45  
  46          // Create a course.
  47          $course = $generator->create_course();
  48          $context = \context_course::instance($course->id);
  49  
  50          // Create a question in the default category.
  51          $contexts = new \core_question\local\bank\question_edit_contexts($context);
  52          $cat = $questiongenerator->create_question_category();
  53          $questiondata1 = $questiongenerator->create_question('numerical', null,
  54              ['name' => 'Example question', 'category' => $cat->id]);
  55  
  56          // Create a new version.
  57          $questiondata2 = $questiongenerator->update_question($questiondata1, null,
  58              ['name' => 'Example question second version']);
  59  
  60          $entry = get_question_bank_entry($questiondata1->id);
  61  
  62          $pagevars = [
  63              'qpage' => 0,
  64              'qperpage' => DEFAULT_QUESTIONS_PER_PAGE,
  65              'cat' => $cat->id . ',' . $cat->contextid,
  66              'tabname' => 'questions'
  67          ];
  68          // Generate the view.
  69          $viewclass = \qbank_history\question_history_view::class;
  70          $extraparams = [
  71              'view' => $viewclass,
  72              'entryid' => $entry->id,
  73              'returnurl' => "/",
  74          ];
  75          $view = new $viewclass($contexts, new \moodle_url('/'), $course, null, $pagevars, $extraparams);
  76          ob_start();
  77          $view->display();
  78          $html = ob_get_clean();
  79  
  80          // Verify the output includes the first version.
  81          $this->assertStringContainsString($questiondata1->name, $html);
  82  
  83          // Verify the output includes the second version.
  84          $this->assertStringContainsString($questiondata2->name, $html);
  85      }
  86  
  87      /**
  88       * Test that the question bank header in the history page shows the latest question.
  89       *
  90       * @covers ::display_question_bank_header
  91       */
  92      public function test_display_question_bank_header() {
  93          $this->resetAfterTest();
  94          $this->setAdminUser();
  95          $generator = $this->getDataGenerator();
  96          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  97  
  98          // Create a course.
  99          $course = $generator->create_course();
 100          $context = \context_course::instance($course->id);
 101  
 102          // Create a question in the default category.
 103          $contexts = new \core_question\local\bank\question_edit_contexts($context);
 104          $cat = $questiongenerator->create_question_category();
 105          $questiondata1 = $questiongenerator->create_question('numerical', null,
 106              ['name' => 'First version', 'category' => $cat->id]);
 107  
 108          $entry = get_question_bank_entry($questiondata1->id);
 109          $pagevars = [
 110              'qpage' => 0,
 111              'qperpage' => DEFAULT_QUESTIONS_PER_PAGE,
 112              'cat' => $cat->id . ',' . $cat->contextid,
 113              'tabname' => 'questions'
 114          ];
 115          // Generate the view.
 116          $viewclass = \qbank_history\question_history_view::class;
 117          $extraparams = [
 118              'view' => $viewclass,
 119              'entryid' => $entry->id,
 120              'returnurl' => "/",
 121          ];
 122          $view = new $viewclass($contexts, new \moodle_url('/'), $course,  null, $pagevars, $extraparams);
 123          ob_start();
 124          $view->display_question_bank_header();
 125          $headerhtml = ob_get_clean();
 126          // Verify the output includes the latest version.
 127          $this->assertStringContainsString($questiondata1->name, $headerhtml);
 128  
 129          $questiondata2 = $questiongenerator->update_question($questiondata1, null,
 130              ['name' => 'Second version']);
 131          $view = new $viewclass($contexts, new \moodle_url('/'), $course,  null, $pagevars, $extraparams);
 132          ob_start();
 133          $view->display_question_bank_header();
 134          $headerhtml = ob_get_clean();
 135  
 136          $this->assertStringContainsString($questiondata2->name, $headerhtml);
 137      }
 138  }