Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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 mod_quiz\question\bank\custom_view;
  20  use question_edit_contexts;
  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          $view->display('editq', 0, 20, $cat->id . ',' . $cat->contextid, false, false, false);
  64          $html = ob_get_clean();
  65  
  66          // Verify the output includes the expected question.
  67          $this->assertStringContainsString('Example question', $html);
  68  
  69          // Verify the question has not been loaded into the cache.
  70          $this->assertFalse($cache->has($questiondata->id));
  71      }
  72  }