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 401 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          // Generate the view.
  63          $view = new question_history_view($contexts, new \moodle_url('/'), $course,  $entry->id, '/');
  64          ob_start();
  65          $pagevars = [
  66              'qpage' => 0,
  67              'qperpage' => 20,
  68              'cat' => $cat->id . ',' . $cat->contextid,
  69              'recurse' => false,
  70              'showhidden' => false,
  71              'qbshowtext' => false
  72          ];
  73          $view->display($pagevars, 'questions');
  74          $html = ob_get_clean();
  75  
  76          // Verify the output includes the first version.
  77          $this->assertStringContainsString($questiondata1->name, $html);
  78  
  79          // Verify the output includes the second version.
  80          $this->assertStringContainsString($questiondata2->name, $html);
  81      }
  82  
  83      /**
  84       * Test that the question bank header in the history page shows the latest question.
  85       *
  86       * @covers ::display_question_bank_header
  87       */
  88      public function test_display_question_bank_header() {
  89          $this->resetAfterTest();
  90          $this->setAdminUser();
  91          $generator = $this->getDataGenerator();
  92          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  93  
  94          // Create a course.
  95          $course = $generator->create_course();
  96          $context = \context_course::instance($course->id);
  97  
  98          // Create a question in the default category.
  99          $contexts = new \core_question\local\bank\question_edit_contexts($context);
 100          $cat = $questiongenerator->create_question_category();
 101          $questiondata1 = $questiongenerator->create_question('numerical', null,
 102              ['name' => 'First version', 'category' => $cat->id]);
 103  
 104          $entry = get_question_bank_entry($questiondata1->id);
 105  
 106          // Generate the view.
 107          $view = new question_history_view($contexts, new \moodle_url('/'), $course,  $entry->id, '/');
 108          ob_start();
 109          $view->display_question_bank_header();
 110          $headerhtml = ob_get_clean();
 111          // Verify the output includes the latest version.
 112          $this->assertStringContainsString($questiondata1->name, $headerhtml);
 113  
 114          $questiondata2 = $questiongenerator->update_question($questiondata1, null,
 115              ['name' => 'Second version']);
 116          $view = new question_history_view($contexts, new \moodle_url('/'), $course,  $entry->id, new \moodle_url('/'));
 117          ob_start();
 118          $view->display_question_bank_header();
 119          $headerhtml = ob_get_clean();
 120  
 121          $this->assertStringContainsString($questiondata2->name, $headerhtml);
 122      }
 123  }