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  /**
  18   * Unit tests for (some of) mod/quiz/locallib.php.
  19   *
  20   * @package    mod_quiz
  21   * @category   test
  22   * @copyright  2008 Tim Hunt
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace mod_quiz;
  26  
  27  use quiz_attempt;
  28  use mod_quiz_display_options;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  global $CFG;
  33  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  34  
  35  
  36  /**
  37   * Unit tests for (some of) mod/quiz/locallib.php.
  38   *
  39   * @copyright  2008 Tim Hunt
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class locallib_test extends \advanced_testcase {
  43  
  44      public function test_quiz_rescale_grade() {
  45          $quiz = new \stdClass();
  46          $quiz->decimalpoints = 2;
  47          $quiz->questiondecimalpoints = 3;
  48          $quiz->grade = 10;
  49          $quiz->sumgrades = 10;
  50          $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, false), 0.12345678);
  51          $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, true), format_float(0.12, 2));
  52          $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, 'question'),
  53              format_float(0.123, 3));
  54          $quiz->sumgrades = 5;
  55          $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, false), 0.24691356);
  56          $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, true), format_float(0.25, 2));
  57          $this->assertEquals(quiz_rescale_grade(0.12345678, $quiz, 'question'),
  58              format_float(0.247, 3));
  59      }
  60  
  61      public function quiz_attempt_state_data_provider() {
  62          return [
  63              [quiz_attempt::IN_PROGRESS, null, null, mod_quiz_display_options::DURING],
  64              [quiz_attempt::FINISHED, -90, null, mod_quiz_display_options::IMMEDIATELY_AFTER],
  65              [quiz_attempt::FINISHED, -7200, null, mod_quiz_display_options::LATER_WHILE_OPEN],
  66              [quiz_attempt::FINISHED, -7200, 3600, mod_quiz_display_options::LATER_WHILE_OPEN],
  67              [quiz_attempt::FINISHED, -30, 30, mod_quiz_display_options::IMMEDIATELY_AFTER],
  68              [quiz_attempt::FINISHED, -90, -30, mod_quiz_display_options::AFTER_CLOSE],
  69              [quiz_attempt::FINISHED, -7200, -3600, mod_quiz_display_options::AFTER_CLOSE],
  70              [quiz_attempt::FINISHED, -90, -3600, mod_quiz_display_options::AFTER_CLOSE],
  71              [quiz_attempt::ABANDONED, -10000000, null, mod_quiz_display_options::LATER_WHILE_OPEN],
  72              [quiz_attempt::ABANDONED, -7200, 3600, mod_quiz_display_options::LATER_WHILE_OPEN],
  73              [quiz_attempt::ABANDONED, -7200, -3600, mod_quiz_display_options::AFTER_CLOSE],
  74          ];
  75      }
  76  
  77      /**
  78       * @dataProvider quiz_attempt_state_data_provider
  79       *
  80       * @param unknown $attemptstate as in the quiz_attempts.state DB column.
  81       * @param unknown $relativetimefinish time relative to now when the attempt finished, or null for 0.
  82       * @param unknown $relativetimeclose time relative to now when the quiz closes, or null for 0.
  83       * @param unknown $expectedstate expected result. One of the mod_quiz_display_options constants/
  84       */
  85      public function test_quiz_attempt_state($attemptstate,
  86              $relativetimefinish, $relativetimeclose, $expectedstate) {
  87  
  88          $attempt = new \stdClass();
  89          $attempt->state = $attemptstate;
  90          if ($relativetimefinish === null) {
  91              $attempt->timefinish = 0;
  92          } else {
  93              $attempt->timefinish = time() + $relativetimefinish;
  94          }
  95  
  96          $quiz = new \stdClass();
  97          if ($relativetimeclose === null) {
  98              $quiz->timeclose = 0;
  99          } else {
 100              $quiz->timeclose = time() + $relativetimeclose;
 101          }
 102  
 103          $this->assertEquals($expectedstate, quiz_attempt_state($quiz, $attempt));
 104      }
 105  
 106      public function test_quiz_question_tostring() {
 107          $question = new \stdClass();
 108          $question->qtype = 'multichoice';
 109          $question->name = 'The question name';
 110          $question->questiontext = '<p>What sort of <b>inequality</b> is x &lt; y<img alt="?" src="..."></p>';
 111          $question->questiontextformat = FORMAT_HTML;
 112  
 113          $summary = quiz_question_tostring($question);
 114          $this->assertEquals('<span class="questionname">The question name</span> ' .
 115                  '<span class="questiontext">What sort of INEQUALITY is x &lt; y[?]' . "\n" . '</span>', $summary);
 116      }
 117  
 118      /**
 119       * Test quiz_view
 120       * @return void
 121       */
 122      public function test_quiz_view() {
 123          global $CFG;
 124  
 125          $CFG->enablecompletion = 1;
 126          $this->resetAfterTest();
 127  
 128          $this->setAdminUser();
 129          // Setup test data.
 130          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 131          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
 132                                                              array('completion' => 2, 'completionview' => 1));
 133          $context = \context_module::instance($quiz->cmid);
 134          $cm = get_coursemodule_from_instance('quiz', $quiz->id);
 135  
 136          // Trigger and capture the event.
 137          $sink = $this->redirectEvents();
 138  
 139          quiz_view($quiz, $course, $cm, $context);
 140  
 141          $events = $sink->get_events();
 142          // 2 additional events thanks to completion.
 143          $this->assertCount(3, $events);
 144          $event = array_shift($events);
 145  
 146          // Checking that the event contains the expected values.
 147          $this->assertInstanceOf('\mod_quiz\event\course_module_viewed', $event);
 148          $this->assertEquals($context, $event->get_context());
 149          $moodleurl = new \moodle_url('/mod/quiz/view.php', array('id' => $cm->id));
 150          $this->assertEquals($moodleurl, $event->get_url());
 151          $this->assertEventContextNotUsed($event);
 152          $this->assertNotEmpty($event->get_name());
 153          // Check completion status.
 154          $completion = new \completion_info($course);
 155          $completiondata = $completion->get_data($cm);
 156          $this->assertEquals(1, $completiondata->completionstate);
 157      }
 158  
 159      /**
 160       * Return false when there are not overrides for this quiz instance.
 161       */
 162      public function test_quiz_is_overriden_calendar_event_no_override() {
 163          global $CFG, $DB;
 164  
 165          $this->resetAfterTest();
 166          $this->setAdminUser();
 167  
 168          $generator = $this->getDataGenerator();
 169          $user = $generator->create_user();
 170          $course = $generator->create_course();
 171          $quizgenerator = $generator->get_plugin_generator('mod_quiz');
 172          $quiz = $quizgenerator->create_instance(['course' => $course->id]);
 173  
 174          $event = new \calendar_event((object)[
 175              'modulename' => 'quiz',
 176              'instance' => $quiz->id,
 177              'userid' => $user->id
 178          ]);
 179  
 180          $this->assertFalse(quiz_is_overriden_calendar_event($event));
 181      }
 182  
 183      /**
 184       * Return false if the given event isn't an quiz module event.
 185       */
 186      public function test_quiz_is_overriden_calendar_event_no_module_event() {
 187          global $CFG, $DB;
 188  
 189          $this->resetAfterTest();
 190          $this->setAdminUser();
 191  
 192          $generator = $this->getDataGenerator();
 193          $user = $generator->create_user();
 194          $course = $generator->create_course();
 195          $quizgenerator = $generator->get_plugin_generator('mod_quiz');
 196          $quiz = $quizgenerator->create_instance(['course' => $course->id]);
 197  
 198          $event = new \calendar_event((object)[
 199              'userid' => $user->id
 200          ]);
 201  
 202          $this->assertFalse(quiz_is_overriden_calendar_event($event));
 203      }
 204  
 205      /**
 206       * Return false if there is overrides for this use but they belong to another quiz
 207       * instance.
 208       */
 209      public function test_quiz_is_overriden_calendar_event_different_quiz_instance() {
 210          global $CFG, $DB;
 211  
 212          $this->resetAfterTest();
 213          $this->setAdminUser();
 214  
 215          $generator = $this->getDataGenerator();
 216          $user = $generator->create_user();
 217          $course = $generator->create_course();
 218          $quizgenerator = $generator->get_plugin_generator('mod_quiz');
 219          $quiz = $quizgenerator->create_instance(['course' => $course->id]);
 220          $quiz2 = $quizgenerator->create_instance(['course' => $course->id]);
 221  
 222          $event = new \calendar_event((object) [
 223              'modulename' => 'quiz',
 224              'instance' => $quiz->id,
 225              'userid' => $user->id
 226          ]);
 227  
 228          $record = (object) [
 229              'quiz' => $quiz2->id,
 230              'userid' => $user->id
 231          ];
 232  
 233          $DB->insert_record('quiz_overrides', $record);
 234  
 235          $this->assertFalse(quiz_is_overriden_calendar_event($event));
 236      }
 237  
 238      /**
 239       * Return true if there is a user override for this event and quiz instance.
 240       */
 241      public function test_quiz_is_overriden_calendar_event_user_override() {
 242          global $CFG, $DB;
 243  
 244          $this->resetAfterTest();
 245          $this->setAdminUser();
 246  
 247          $generator = $this->getDataGenerator();
 248          $user = $generator->create_user();
 249          $course = $generator->create_course();
 250          $quizgenerator = $generator->get_plugin_generator('mod_quiz');
 251          $quiz = $quizgenerator->create_instance(['course' => $course->id]);
 252  
 253          $event = new \calendar_event((object) [
 254              'modulename' => 'quiz',
 255              'instance' => $quiz->id,
 256              'userid' => $user->id
 257          ]);
 258  
 259          $record = (object) [
 260              'quiz' => $quiz->id,
 261              'userid' => $user->id
 262          ];
 263  
 264          $DB->insert_record('quiz_overrides', $record);
 265  
 266          $this->assertTrue(quiz_is_overriden_calendar_event($event));
 267      }
 268  
 269      /**
 270       * Return true if there is a group override for the event and quiz instance.
 271       */
 272      public function test_quiz_is_overriden_calendar_event_group_override() {
 273          global $CFG, $DB;
 274  
 275          $this->resetAfterTest();
 276          $this->setAdminUser();
 277  
 278          $generator = $this->getDataGenerator();
 279          $user = $generator->create_user();
 280          $course = $generator->create_course();
 281          $quizgenerator = $generator->get_plugin_generator('mod_quiz');
 282          $quiz = $quizgenerator->create_instance(['course' => $course->id]);
 283          $group = $this->getDataGenerator()->create_group(array('courseid' => $quiz->course));
 284          $groupid = $group->id;
 285          $userid = $user->id;
 286  
 287          $event = new \calendar_event((object) [
 288              'modulename' => 'quiz',
 289              'instance' => $quiz->id,
 290              'groupid' => $groupid
 291          ]);
 292  
 293          $record = (object) [
 294              'quiz' => $quiz->id,
 295              'groupid' => $groupid
 296          ];
 297  
 298          $DB->insert_record('quiz_overrides', $record);
 299  
 300          $this->assertTrue(quiz_is_overriden_calendar_event($event));
 301      }
 302  
 303      /**
 304       * Test test_quiz_get_user_timeclose().
 305       */
 306      public function test_quiz_get_user_timeclose() {
 307          global $DB;
 308  
 309          $this->resetAfterTest();
 310          $this->setAdminUser();
 311  
 312          $basetimestamp = time(); // The timestamp we will base the enddates on.
 313  
 314          // Create generator, course and quizzes.
 315          $student1 = $this->getDataGenerator()->create_user();
 316          $student2 = $this->getDataGenerator()->create_user();
 317          $student3 = $this->getDataGenerator()->create_user();
 318          $teacher = $this->getDataGenerator()->create_user();
 319          $course = $this->getDataGenerator()->create_course();
 320          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 321  
 322          // Both quizzes close in two hours.
 323          $quiz1 = $quizgenerator->create_instance(array('course' => $course->id, 'timeclose' => $basetimestamp + 7200));
 324          $quiz2 = $quizgenerator->create_instance(array('course' => $course->id, 'timeclose' => $basetimestamp + 7200));
 325          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 326          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 327  
 328          $student1id = $student1->id;
 329          $student2id = $student2->id;
 330          $student3id = $student3->id;
 331          $teacherid = $teacher->id;
 332  
 333          // Users enrolments.
 334          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 335          $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
 336          $this->getDataGenerator()->enrol_user($student1id, $course->id, $studentrole->id, 'manual');
 337          $this->getDataGenerator()->enrol_user($student2id, $course->id, $studentrole->id, 'manual');
 338          $this->getDataGenerator()->enrol_user($student3id, $course->id, $studentrole->id, 'manual');
 339          $this->getDataGenerator()->enrol_user($teacherid, $course->id, $teacherrole->id, 'manual');
 340  
 341          // Create groups.
 342          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 343          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 344          $group1id = $group1->id;
 345          $group2id = $group2->id;
 346          $this->getDataGenerator()->create_group_member(array('userid' => $student1id, 'groupid' => $group1id));
 347          $this->getDataGenerator()->create_group_member(array('userid' => $student2id, 'groupid' => $group2id));
 348  
 349          // Group 1 gets an group override for quiz 1 to close in three hours.
 350          $record1 = (object) [
 351              'quiz' => $quiz1->id,
 352              'groupid' => $group1id,
 353              'timeclose' => $basetimestamp + 10800 // In three hours.
 354          ];
 355          $DB->insert_record('quiz_overrides', $record1);
 356  
 357          // Let's test quiz 1 closes in three hours for user student 1 since member of group 1.
 358          // Quiz 2 closes in two hours.
 359          $this->setUser($student1id);
 360          $params = new \stdClass();
 361  
 362          $comparearray = array();
 363          $object = new \stdClass();
 364          $object->id = $quiz1->id;
 365          $object->usertimeclose = $basetimestamp + 10800; // The overriden timeclose for quiz 1.
 366  
 367          $comparearray[$quiz1->id] = $object;
 368  
 369          $object = new \stdClass();
 370          $object->id = $quiz2->id;
 371          $object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 2.
 372  
 373          $comparearray[$quiz2->id] = $object;
 374  
 375          $this->assertEquals($comparearray, quiz_get_user_timeclose($course->id));
 376  
 377          // Let's test quiz 1 closes in two hours (the original value) for user student 3 since member of no group.
 378          $this->setUser($student3id);
 379          $params = new \stdClass();
 380  
 381          $comparearray = array();
 382          $object = new \stdClass();
 383          $object->id = $quiz1->id;
 384          $object->usertimeclose = $basetimestamp + 7200; // The original timeclose for quiz 1.
 385  
 386          $comparearray[$quiz1->id] = $object;
 387  
 388          $object = new \stdClass();
 389          $object->id = $quiz2->id;
 390          $object->usertimeclose = $basetimestamp + 7200; // The original timeclose for quiz 2.
 391  
 392          $comparearray[$quiz2->id] = $object;
 393  
 394          $this->assertEquals($comparearray, quiz_get_user_timeclose($course->id));
 395  
 396          // User 2 gets an user override for quiz 1 to close in four hours.
 397          $record2 = (object) [
 398              'quiz' => $quiz1->id,
 399              'userid' => $student2id,
 400              'timeclose' => $basetimestamp + 14400 // In four hours.
 401          ];
 402          $DB->insert_record('quiz_overrides', $record2);
 403  
 404          // Let's test quiz 1 closes in four hours for user student 2 since personally overriden.
 405          // Quiz 2 closes in two hours.
 406          $this->setUser($student2id);
 407  
 408          $comparearray = array();
 409          $object = new \stdClass();
 410          $object->id = $quiz1->id;
 411          $object->usertimeclose = $basetimestamp + 14400; // The overriden timeclose for quiz 1.
 412  
 413          $comparearray[$quiz1->id] = $object;
 414  
 415          $object = new \stdClass();
 416          $object->id = $quiz2->id;
 417          $object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 2.
 418  
 419          $comparearray[$quiz2->id] = $object;
 420  
 421          $this->assertEquals($comparearray, quiz_get_user_timeclose($course->id));
 422  
 423          // Let's test a teacher sees the original times.
 424          // Quiz 1 and quiz 2 close in two hours.
 425          $this->setUser($teacherid);
 426  
 427          $comparearray = array();
 428          $object = new \stdClass();
 429          $object->id = $quiz1->id;
 430          $object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 1.
 431  
 432          $comparearray[$quiz1->id] = $object;
 433  
 434          $object = new \stdClass();
 435          $object->id = $quiz2->id;
 436          $object->usertimeclose = $basetimestamp + 7200; // The unchanged timeclose for quiz 2.
 437  
 438          $comparearray[$quiz2->id] = $object;
 439  
 440          $this->assertEquals($comparearray, quiz_get_user_timeclose($course->id));
 441      }
 442  
 443      /**
 444       * This function creates a quiz with some standard (non-random) and some random questions.
 445       * The standard questions are created first and then random questions follow them.
 446       * So in a quiz with 3 standard question and 2 random question, the first random question is at slot 4.
 447       *
 448       * @param int $qnum Number of standard questions that should be created in the quiz.
 449       * @param int $randomqnum Number of random questions that should be created in the quiz.
 450       * @param array $questiontags Tags to be used for random questions.
 451       *      This is an array in the following format:
 452       *      [
 453       *          0 => ['foo', 'bar'],
 454       *          1 => ['baz', 'qux']
 455       *      ]
 456       * @param string[] $unusedtags Some additional tags to be created.
 457       * @return array An array of 2 elements: $quiz and $tagobjects.
 458       *      $tagobjects is an associative array of all created tag objects with its key being tag names.
 459       */
 460      private function setup_quiz_and_tags($qnum, $randomqnum, $questiontags = [], $unusedtags = []) {
 461          global $SITE;
 462  
 463          $tagobjects = [];
 464  
 465          // Get all the tags that need to be created.
 466          $alltags = [];
 467          foreach ($questiontags as $questiontag) {
 468              $alltags = array_merge($alltags, $questiontag);
 469          }
 470          $alltags = array_merge($alltags, $unusedtags);
 471          $alltags = array_unique($alltags);
 472  
 473          // Create tags.
 474          foreach ($alltags as $tagname) {
 475              $tagrecord = array(
 476                  'isstandard' => 1,
 477                  'flag' => 0,
 478                  'rawname' => $tagname,
 479                  'description' => $tagname . ' desc'
 480              );
 481              $tagobjects[$tagname] = $this->getDataGenerator()->create_tag($tagrecord);
 482          }
 483  
 484          // Create a quiz.
 485          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 486          $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0));
 487  
 488          // Create a question category in the system context.
 489          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 490          $cat = $questiongenerator->create_question_category();
 491  
 492          // Setup standard questions.
 493          for ($i = 0; $i < $qnum; $i++) {
 494              $question = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
 495              quiz_add_quiz_question($question->id, $quiz);
 496          }
 497          // Setup random questions.
 498          for ($i = 0; $i < $randomqnum; $i++) {
 499              // Just create a standard question first, so there would be enough questions to pick a random question from.
 500              $question = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
 501              $tagids = [];
 502              if (!empty($questiontags[$i])) {
 503                  foreach ($questiontags[$i] as $tagname) {
 504                      $tagids[] = $tagobjects[$tagname]->id;
 505                  }
 506              }
 507              quiz_add_random_questions($quiz, 0, $cat->id, 1, false, $tagids);
 508          }
 509  
 510          return array($quiz, $tagobjects);
 511      }
 512  
 513      public function test_quiz_retrieve_slot_tags() {
 514          global $DB;
 515  
 516          $this->resetAfterTest();
 517          $this->setAdminUser();
 518  
 519          list($quiz, $tags) = $this->setup_quiz_and_tags(1, 1, [['foo', 'bar']], ['baz']);
 520  
 521          // Get the random question's slotid. It is at the second slot.
 522          $slotid = $DB->get_field('quiz_slots', 'id', array('quizid' => $quiz->id, 'slot' => 2));
 523          $slottags = quiz_retrieve_slot_tags($slotid);
 524  
 525          $this->assertEqualsCanonicalizing(
 526                  [
 527                      ['tagid' => $tags['foo']->id, 'tagname' => $tags['foo']->name],
 528                      ['tagid' => $tags['bar']->id, 'tagname' => $tags['bar']->name]
 529                  ],
 530                  array_map(function($slottag) {
 531                      return ['tagid' => $slottag->tagid, 'tagname' => $slottag->tagname];
 532                  }, $slottags));
 533      }
 534  
 535      public function test_quiz_retrieve_slot_tags_with_removed_tag() {
 536          global $DB;
 537  
 538          $this->resetAfterTest();
 539          $this->setAdminUser();
 540  
 541          list($quiz, $tags) = $this->setup_quiz_and_tags(1, 1, [['foo', 'bar']], ['baz']);
 542  
 543          // Get the random question's slotid. It is at the second slot.
 544          $slotid = $DB->get_field('quiz_slots', 'id', array('quizid' => $quiz->id, 'slot' => 2));
 545          $slottags = quiz_retrieve_slot_tags($slotid);
 546  
 547          // Now remove the foo tag and check again.
 548          \core_tag_tag::delete_tags([$tags['foo']->id]);
 549          $slottags = quiz_retrieve_slot_tags($slotid);
 550  
 551          $this->assertEqualsCanonicalizing(
 552                  [
 553                      ['tagid' => null, 'tagname' => $tags['foo']->name],
 554                      ['tagid' => $tags['bar']->id, 'tagname' => $tags['bar']->name]
 555                  ],
 556                  array_map(function($slottag) {
 557                      return ['tagid' => $slottag->tagid, 'tagname' => $slottag->tagname];
 558                  }, $slottags));
 559      }
 560  
 561      public function test_quiz_retrieve_slot_tags_for_standard_question() {
 562          global $DB;
 563  
 564          $this->resetAfterTest();
 565          $this->setAdminUser();
 566  
 567          list($quiz, $tags) = $this->setup_quiz_and_tags(1, 1, [['foo', 'bar']]);
 568  
 569          // Get the standard question's slotid. It is at the first slot.
 570          $slotid = $DB->get_field('quiz_slots', 'id', array('quizid' => $quiz->id, 'slot' => 1));
 571  
 572          // There should be no slot tags for a non-random question.
 573          $this->assertCount(0, quiz_retrieve_slot_tags($slotid));
 574      }
 575  
 576      public function test_quiz_retrieve_slot_tag_ids() {
 577          global $DB;
 578  
 579          $this->resetAfterTest();
 580          $this->setAdminUser();
 581  
 582          list($quiz, $tags) = $this->setup_quiz_and_tags(1, 1, [['foo', 'bar']], ['baz']);
 583  
 584          // Get the random question's slotid. It is at the second slot.
 585          $slotid = $DB->get_field('quiz_slots', 'id', array('quizid' => $quiz->id, 'slot' => 2));
 586          $tagids = quiz_retrieve_slot_tag_ids($slotid);
 587  
 588          $this->assertEqualsCanonicalizing([$tags['foo']->id, $tags['bar']->id], $tagids);
 589      }
 590  
 591      public function test_quiz_retrieve_slot_tag_ids_for_standard_question() {
 592          global $DB;
 593  
 594          $this->resetAfterTest();
 595          $this->setAdminUser();
 596  
 597          list($quiz, $tags) = $this->setup_quiz_and_tags(1, 1, [['foo', 'bar']], ['baz']);
 598  
 599          // Get the standard question's slotid. It is at the first slot.
 600          $slotid = $DB->get_field('quiz_slots', 'id', array('quizid' => $quiz->id, 'slot' => 1));
 601          $tagids = quiz_retrieve_slot_tag_ids($slotid);
 602  
 603          $this->assertEqualsCanonicalizing([], $tagids);
 604      }
 605  
 606      /**
 607       * Data provider for the get_random_question_summaries test.
 608       */
 609      public function get_quiz_retrieve_tags_for_slot_ids_test_cases() {
 610          return [
 611              'no questions' => [
 612                  'questioncount' => 0,
 613                  'randomquestioncount' => 0,
 614                  'randomquestiontags' => [],
 615                  'unusedtags' => [],
 616                  'removeslottagids' => [],
 617                  'expected' => []
 618              ],
 619              'only regular questions' => [
 620                  'questioncount' => 2,
 621                  'randomquestioncount' => 0,
 622                  'randomquestiontags' => [],
 623                  'unusedtags' => ['unused1', 'unused2'],
 624                  'removeslottagids' => [],
 625                  'expected' => [
 626                      1 => [],
 627                      2 => []
 628                  ]
 629              ],
 630              'only random questions 1' => [
 631                  'questioncount' => 0,
 632                  'randomquestioncount' => 2,
 633                  'randomquestiontags' => [
 634                      0 => ['foo'],
 635                      1 => []
 636                  ],
 637                  'unusedtags' => ['unused1', 'unused2'],
 638                  'removeslottagids' => [],
 639                  'expected' => [
 640                      1 => ['foo'],
 641                      2 => []
 642                  ]
 643              ],
 644              'only random questions 2' => [
 645                  'questioncount' => 0,
 646                  'randomquestioncount' => 2,
 647                  'randomquestiontags' => [
 648                      0 => ['foo', 'bop'],
 649                      1 => ['bar']
 650                  ],
 651                  'unusedtags' => ['unused1', 'unused2'],
 652                  'removeslottagids' => [],
 653                  'expected' => [
 654                      1 => ['foo', 'bop'],
 655                      2 => ['bar']
 656                  ]
 657              ],
 658              'only random questions 3' => [
 659                  'questioncount' => 0,
 660                  'randomquestioncount' => 2,
 661                  'randomquestiontags' => [
 662                      0 => ['foo', 'bop'],
 663                      1 => ['bar', 'foo']
 664                  ],
 665                  'unusedtags' => ['unused1', 'unused2'],
 666                  'removeslottagids' => [],
 667                  'expected' => [
 668                      1 => ['foo', 'bop'],
 669                      2 => ['bar', 'foo']
 670                  ]
 671              ],
 672              'combination of questions 1' => [
 673                  'questioncount' => 2,
 674                  'randomquestioncount' => 2,
 675                  'randomquestiontags' => [
 676                      0 => ['foo'],
 677                      1 => []
 678                  ],
 679                  'unusedtags' => ['unused1', 'unused2'],
 680                  'removeslottagids' => [],
 681                  'expected' => [
 682                      1 => [],
 683                      2 => [],
 684                      3 => ['foo'],
 685                      4 => []
 686                  ]
 687              ],
 688              'combination of questions 2' => [
 689                  'questioncount' => 2,
 690                  'randomquestioncount' => 2,
 691                  'randomquestiontags' => [
 692                      0 => ['foo', 'bop'],
 693                      1 => ['bar']
 694                  ],
 695                  'unusedtags' => ['unused1', 'unused2'],
 696                  'removeslottagids' => [],
 697                  'expected' => [
 698                      1 => [],
 699                      2 => [],
 700                      3 => ['foo', 'bop'],
 701                      4 => ['bar']
 702                  ]
 703              ],
 704              'combination of questions 3' => [
 705                  'questioncount' => 2,
 706                  'randomquestioncount' => 2,
 707                  'randomquestiontags' => [
 708                      0 => ['foo', 'bop'],
 709                      1 => ['bar', 'foo']
 710                  ],
 711                  'unusedtags' => ['unused1', 'unused2'],
 712                  'removeslottagids' => [],
 713                  'expected' => [
 714                      1 => [],
 715                      2 => [],
 716                      3 => ['foo', 'bop'],
 717                      4 => ['bar', 'foo']
 718                  ]
 719              ],
 720              'load from name 1' => [
 721                  'questioncount' => 2,
 722                  'randomquestioncount' => 2,
 723                  'randomquestiontags' => [
 724                      0 => ['foo'],
 725                      1 => []
 726                  ],
 727                  'unusedtags' => ['unused1', 'unused2'],
 728                  'removeslottagids' => [3],
 729                  'expected' => [
 730                      1 => [],
 731                      2 => [],
 732                      3 => ['foo'],
 733                      4 => []
 734                  ]
 735              ],
 736              'load from name 2' => [
 737                  'questioncount' => 2,
 738                  'randomquestioncount' => 2,
 739                  'randomquestiontags' => [
 740                      0 => ['foo', 'bop'],
 741                      1 => ['bar']
 742                  ],
 743                  'unusedtags' => ['unused1', 'unused2'],
 744                  'removeslottagids' => [3],
 745                  'expected' => [
 746                      1 => [],
 747                      2 => [],
 748                      3 => ['foo', 'bop'],
 749                      4 => ['bar']
 750                  ]
 751              ],
 752              'load from name 3' => [
 753                  'questioncount' => 2,
 754                  'randomquestioncount' => 2,
 755                  'randomquestiontags' => [
 756                      0 => ['foo', 'bop'],
 757                      1 => ['bar', 'foo']
 758                  ],
 759                  'unusedtags' => ['unused1', 'unused2'],
 760                  'removeslottagids' => [3],
 761                  'expected' => [
 762                      1 => [],
 763                      2 => [],
 764                      3 => ['foo', 'bop'],
 765                      4 => ['bar', 'foo']
 766                  ]
 767              ]
 768          ];
 769      }
 770  
 771      /**
 772       * Test the quiz_retrieve_tags_for_slot_ids function with various parameter
 773       * combinations.
 774       *
 775       * @dataProvider get_quiz_retrieve_tags_for_slot_ids_test_cases()
 776       * @param int $questioncount The number of regular questions to create
 777       * @param int $randomquestioncount The number of random questions to create
 778       * @param array $randomquestiontags The tags for the random questions
 779       * @param string[] $unusedtags Additional tags to create to populate the DB with data
 780       * @param int[] $removeslottagids Slot numbers to remove tag ids for
 781       * @param array $expected The expected output of tag names indexed by slot number
 782       */
 783      public function test_quiz_retrieve_tags_for_slot_ids_combinations(
 784          $questioncount,
 785          $randomquestioncount,
 786          $randomquestiontags,
 787          $unusedtags,
 788          $removeslottagids,
 789          $expected
 790      ) {
 791          global $DB;
 792  
 793          $this->resetAfterTest();
 794          $this->setAdminUser();
 795  
 796          list($quiz, $tags) = $this->setup_quiz_and_tags(
 797              $questioncount,
 798              $randomquestioncount,
 799              $randomquestiontags,
 800              $unusedtags
 801          );
 802  
 803          $slots = $DB->get_records('quiz_slots', ['quizid' => $quiz->id]);
 804          $slotids = [];
 805          $slotsbynumber = [];
 806          foreach ($slots as $slot) {
 807              $slotids[] = $slot->id;
 808              $slotsbynumber[$slot->slot] = $slot;
 809          }
 810  
 811          if (!empty($removeslottagids)) {
 812              // The slots to remove are the slot numbers not the slot id so we need
 813              // to get the ids for the DB call.
 814              $idstonull = array_map(function($slot) use ($slotsbynumber) {
 815                  return $slotsbynumber[$slot]->id;
 816              }, $removeslottagids);
 817              list($sql, $params) = $DB->get_in_or_equal($idstonull);
 818              // Null out the tagid column to force the code to look up the tag by name.
 819              $DB->set_field_select('quiz_slot_tags', 'tagid', null, "slotid {$sql}", $params);
 820          }
 821  
 822          $slottagsbyslotids = quiz_retrieve_tags_for_slot_ids($slotids);
 823          // Convert the result into an associative array of slotid => [... tag names..]
 824          // to make it easier to compare.
 825          $actual = array_map(function($slottags) {
 826              $names = array_map(function($slottag) {
 827                  return $slottag->tagname;
 828              }, $slottags);
 829              // Make sure the names are sorted for comparison.
 830              sort($names);
 831              return $names;
 832          }, $slottagsbyslotids);
 833  
 834          $formattedexptected = [];
 835          // The expected values are indexed by slot number rather than id so let
 836          // convert it to use the id so that we can compare the results.
 837          foreach ($expected as $slot => $tagnames) {
 838              sort($tagnames);
 839              $slotid = $slotsbynumber[$slot]->id;
 840              $formattedexptected[$slotid] = $tagnames;
 841          }
 842  
 843          $this->assertEquals($formattedexptected, $actual);
 844      }
 845  
 846      public function test_quiz_override_summary() {
 847          global $DB, $PAGE;
 848          $this->resetAfterTest();
 849          $generator = $this->getDataGenerator();
 850          /** @var mod_quiz_generator $quizgenerator */
 851          $quizgenerator = $generator->get_plugin_generator('mod_quiz');
 852          /** @var mod_quiz_renderer $renderer */
 853          $renderer = $PAGE->get_renderer('mod_quiz');
 854  
 855          // Course with quiz and a group - plus some others, to verify they don't get counted.
 856          $course = $generator->create_course();
 857          $quiz = $quizgenerator->create_instance(['course' => $course->id, 'groupmode' => SEPARATEGROUPS]);
 858          $cm = get_coursemodule_from_id('quiz', $quiz->cmid, $course->id);
 859          $group = $generator->create_group(['courseid' => $course->id]);
 860          $othergroup = $generator->create_group(['courseid' => $course->id]);
 861          $otherquiz = $quizgenerator->create_instance(['course' => $course->id]);
 862  
 863          // Initial test (as admin) with no data.
 864          $this->setAdminUser();
 865          $this->assertEquals(['group' => 0, 'user' => 0, 'mode' => 'allgroups'],
 866                  quiz_override_summary($quiz, $cm));
 867          $this->assertEquals(['group' => 0, 'user' => 0, 'mode' => 'onegroup'],
 868                  quiz_override_summary($quiz, $cm, $group->id));
 869  
 870          // Editing teacher.
 871          $teacher = $generator->create_user();
 872          $generator->enrol_user($teacher->id, $course->id, 'editingteacher');
 873  
 874          // Non-editing teacher.
 875          $tutor = $generator->create_user();
 876          $generator->enrol_user($tutor->id, $course->id, 'teacher');
 877          $generator->create_group_member(['userid' => $tutor->id, 'groupid' => $group->id]);
 878  
 879          // Three students.
 880          $student1 = $generator->create_user();
 881          $generator->enrol_user($student1->id, $course->id, 'student');
 882          $generator->create_group_member(['userid' => $student1->id, 'groupid' => $group->id]);
 883  
 884          $student2 = $generator->create_user();
 885          $generator->enrol_user($student2->id, $course->id, 'student');
 886          $generator->create_group_member(['userid' => $student2->id, 'groupid' => $othergroup->id]);
 887  
 888          $student3 = $generator->create_user();
 889          $generator->enrol_user($student3->id, $course->id, 'student');
 890  
 891          // Initial test now users exist, but before overrides.
 892          // Test as teacher.
 893          $this->setUser($teacher);
 894          $this->assertEquals(['group' => 0, 'user' => 0, 'mode' => 'allgroups'],
 895                  quiz_override_summary($quiz, $cm));
 896          $this->assertEquals(['group' => 0, 'user' => 0, 'mode' => 'onegroup'],
 897                  quiz_override_summary($quiz, $cm, $group->id));
 898  
 899          // Test as tutor.
 900          $this->setUser($tutor);
 901          $this->assertEquals(['group' => 0, 'user' => 0, 'mode' => 'somegroups'],
 902                  quiz_override_summary($quiz, $cm));
 903          $this->assertEquals(['group' => 0, 'user' => 0, 'mode' => 'onegroup'],
 904                  quiz_override_summary($quiz, $cm, $group->id));
 905          $this->assertEquals('', $renderer->quiz_override_summary_links($quiz, $cm));
 906  
 907          // Quiz setting overrides for students 1 and 3.
 908          $quizgenerator->create_override(['quiz' => $quiz->id, 'userid' => $student1->id, 'attempts' => 2]);
 909          $quizgenerator->create_override(['quiz' => $quiz->id, 'userid' => $student3->id, 'attempts' => 2]);
 910          $quizgenerator->create_override(['quiz' => $quiz->id, 'groupid' => $group->id, 'attempts' => 3]);
 911          $quizgenerator->create_override(['quiz' => $quiz->id, 'groupid' => $othergroup->id, 'attempts' => 3]);
 912          $quizgenerator->create_override(['quiz' => $otherquiz->id, 'userid' => $student2->id, 'attempts' => 2]);
 913  
 914          // Test as teacher.
 915          $this->setUser($teacher);
 916          $this->assertEquals(['group' => 2, 'user' => 2, 'mode' => 'allgroups'],
 917                  quiz_override_summary($quiz, $cm));
 918          $this->assertEquals('Settings overrides exist (Groups: 2, Users: 2)',
 919                  // Links checked by Behat, so strip them for these tests.
 920                  html_to_text($renderer->quiz_override_summary_links($quiz, $cm), 0, false));
 921          $this->assertEquals(['group' => 1, 'user' => 1, 'mode' => 'onegroup'],
 922                  quiz_override_summary($quiz, $cm, $group->id));
 923          $this->assertEquals('Settings overrides exist (Groups: 1, Users: 1) for this group',
 924                  html_to_text($renderer->quiz_override_summary_links($quiz, $cm, $group->id), 0, false));
 925  
 926          // Test as tutor.
 927          $this->setUser($tutor);
 928          $this->assertEquals(['group' => 1, 'user' => 1, 'mode' => 'somegroups'],
 929                  quiz_override_summary($quiz, $cm));
 930          $this->assertEquals('Settings overrides exist (Groups: 1, Users: 1) for your groups',
 931                  html_to_text($renderer->quiz_override_summary_links($quiz, $cm), 0, false));
 932          $this->assertEquals(['group' => 1, 'user' => 1, 'mode' => 'onegroup'],
 933                  quiz_override_summary($quiz, $cm, $group->id));
 934          $this->assertEquals('Settings overrides exist (Groups: 1, Users: 1) for this group',
 935                  html_to_text($renderer->quiz_override_summary_links($quiz, $cm, $group->id), 0, false));
 936  
 937          // Now set the quiz to be group mode: no groups, and re-test as tutor.
 938          // In this case, the tutor should see all groups.
 939          $DB->set_field('course_modules', 'groupmode', NOGROUPS, ['id' => $cm->id]);
 940          $cm = get_coursemodule_from_id('quiz', $quiz->cmid, $course->id);
 941  
 942          $this->assertEquals(['group' => 2, 'user' => 2, 'mode' => 'allgroups'],
 943                  quiz_override_summary($quiz, $cm));
 944          $this->assertEquals('Settings overrides exist (Groups: 2, Users: 2)',
 945                  html_to_text($renderer->quiz_override_summary_links($quiz, $cm), 0, false));
 946      }
 947  }