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.
   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  namespace quiz_statistics\event\observer;
  17  
  18  defined('MOODLE_INTERNAL') || die();
  19  
  20  global $CFG;
  21  require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.php');
  22  
  23  use core\task\manager;
  24  use quiz_statistics\task\recalculate;
  25  use quiz_statistics\tests\statistics_helper;
  26  use quiz_statistics\tests\statistics_test_trait;
  27  
  28  /**
  29   * Unit tests for attempt_submitted observer
  30   *
  31   * @package   quiz_statistics
  32   * @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
  33   * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @covers    \quiz_statistics\event\observer\attempt_submitted
  36   */
  37  class attempt_submitted_test extends \advanced_testcase {
  38      use \quiz_question_helper_test_trait;
  39      use statistics_test_trait;
  40  
  41  
  42      /**
  43       * Attempting a quiz should queue the recalculation task for that quiz in 1 hour's time.
  44       *
  45       * @return void
  46       */
  47      public function test_queue_task_on_submission(): void {
  48          [$user, $quiz] = $this->create_test_data();
  49  
  50          $tasks = manager::get_adhoc_tasks(recalculate::class);
  51          $this->assertEmpty($tasks);
  52  
  53          $this->attempt_quiz($quiz, $user);
  54  
  55          $tasks = manager::get_adhoc_tasks(recalculate::class);
  56          $this->assertCount(1, $tasks);
  57          $task = reset($tasks);
  58          $this->assert_task_is_queued_for_quiz($task, $quiz);
  59      }
  60  
  61      /**
  62       * Attempting a quiz multiple times should only queue one instance of the task.
  63       *
  64       * @return void
  65       */
  66      public function test_queue_single_task_for_multiple_submissions(): void {
  67          [$user1, $quiz] = $this->create_test_data();
  68          $user2 = $this->getDataGenerator()->create_user();
  69  
  70          $tasks = manager::get_adhoc_tasks(recalculate::class);
  71          $this->assertEmpty($tasks);
  72  
  73          $this->attempt_quiz($quiz, $user1);
  74          $this->attempt_quiz($quiz, $user2);
  75  
  76          $tasks = manager::get_adhoc_tasks(recalculate::class);
  77          $this->assertCount(1, $tasks);
  78          $task = reset($tasks);
  79          $this->assert_task_is_queued_for_quiz($task, $quiz);
  80      }
  81  
  82      /**
  83       * Attempting the quiz again after processing the task should queue a new task.
  84       *
  85       * @return void
  86       */
  87      public function test_queue_new_task_after_processing(): void {
  88          [$user1, $quiz, $course] = $this->create_test_data();
  89          $user2 = $this->getDataGenerator()->create_user();
  90  
  91          $tasks = manager::get_adhoc_tasks(recalculate::class);
  92          $this->assertEmpty($tasks);
  93  
  94          $this->attempt_quiz($quiz, $user1);
  95  
  96          $tasks = manager::get_adhoc_tasks(recalculate::class);
  97          $this->assertCount(1, $tasks);
  98  
  99          $this->expectOutputRegex("~Re-calculating statistics for quiz {$quiz->name} \({$quiz->id}\) " .
 100              "from course {$course->shortname} \({$course->id}\) with 1 attempts~");
 101          statistics_helper::run_pending_recalculation_tasks();
 102  
 103          $tasks = manager::get_adhoc_tasks(recalculate::class);
 104          $this->assertEmpty($tasks);
 105  
 106          $this->attempt_quiz($quiz, $user2);
 107  
 108          $tasks = manager::get_adhoc_tasks(recalculate::class);
 109          $this->assertCount(1, $tasks);
 110  
 111          $task = reset($tasks);
 112          $this->assert_task_is_queued_for_quiz($task, $quiz);
 113      }
 114  
 115      /**
 116       * Attempting different quizzes will queue a task for each.
 117       *
 118       * @return void
 119       */
 120      public function test_queue_separate_tasks_for_multiple_quizzes(): void {
 121          [$user1, $quiz1] = $this->create_test_data();
 122          [$user2, $quiz2] = $this->create_test_data();
 123  
 124          $tasks = manager::get_adhoc_tasks(recalculate::class);
 125          $this->assertEmpty($tasks);
 126  
 127          $this->attempt_quiz($quiz1, $user1);
 128          $this->attempt_quiz($quiz2, $user2);
 129  
 130          $tasks = manager::get_adhoc_tasks(recalculate::class);
 131          $this->assertCount(2, $tasks);
 132          $task1 = array_shift($tasks);
 133          $this->assert_task_is_queued_for_quiz($task1, $quiz1);
 134          $task2 = array_shift($tasks);
 135          $this->assert_task_is_queued_for_quiz($task2, $quiz2);
 136      }
 137  }