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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [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 mod_quiz;
  18  
  19  use core_question\local\bank\question_edit_contexts;
  20  use mod_quiz\question\bank\custom_view;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once($CFG->dirroot . '/question/editlib.php');
  26  
  27  /**
  28   * Unit tests for the quiz's own question bank view class.
  29   *
  30   * @package    mod_quiz
  31   * @category   test
  32   * @copyright  2018 the Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class quiz_question_bank_view_test extends \advanced_testcase {
  36  
  37      public function test_viewing_question_bank_should_not_load_individual_questions() {
  38          $this->resetAfterTest();
  39          $this->setAdminUser();
  40          $generator = $this->getDataGenerator();
  41          /** @var core_question_generator $questiongenerator */
  42          $questiongenerator = $generator->get_plugin_generator('core_question');
  43  
  44          // Create a course and a quiz.
  45          $course = $generator->create_course();
  46          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
  47          $context = \context_module::instance($quiz->cmid);
  48          $cm = get_coursemodule_from_instance('quiz', $quiz->id);
  49  
  50          // Create a question in the default category.
  51          $contexts = new question_edit_contexts($context);
  52          $cat = question_make_default_categories($contexts->all());
  53          $questiondata = $questiongenerator->create_question('numerical', null,
  54                  ['name' => 'Example question', 'category' => $cat->id]);
  55  
  56          // Ensure the question is not in the cache.
  57          $cache = \cache::make('core', 'questiondata');
  58          $cache->delete($questiondata->id);
  59  
  60          // Generate the view.
  61          $view = new custom_view($contexts, new \moodle_url('/'), $course, $cm, $quiz);
  62          ob_start();
  63          $pagevars = [
  64              'qpage' => 0,
  65              'qperpage' => 20,
  66              'cat' => $cat->id . ',' . $context->id,
  67              'recurse' => false,
  68              'showhidden' => false,
  69              'qbshowtext' => false
  70          ];
  71          $view->display($pagevars, 'editq');
  72          $html = ob_get_clean();
  73  
  74          // Verify the output includes the expected question.
  75          $this->assertStringContainsString('Example question', $html);
  76  
  77          // Verify the question has not been loaded into the cache.
  78          $this->assertFalse($cache->has($questiondata->id));
  79      }
  80  }