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 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 workshopallocation_scheduled;
  18  
  19  /**
  20   * Test for the scheduled allocator.
  21   *
  22   * @package workshopallocation_scheduled
  23   * @copyright 2020 Jaume I University <https://www.uji.es/>
  24   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class scheduled_allocator_test extends \advanced_testcase {
  27  
  28      /** @var \stdClass $course The course where the tests will be run */
  29      private $course;
  30  
  31      /** @var \workshop $workshop The workshop where the tests will be run */
  32      private $workshop;
  33  
  34      /** @var \stdClass $workshopcm The workshop course module instance */
  35      private $workshopcm;
  36  
  37      /** @var \stdClass[] $students An array of student enrolled in $course */
  38      private $students;
  39  
  40      /**
  41       * Tests that student submissions get automatically alocated after the submission deadline and when the workshop
  42       * "Switch to the next phase after the submissions deadline" checkbox is active.
  43       */
  44      public function test_that_allocator_in_executed_on_submission_end_when_phaseswitchassessment_is_active(): void {
  45          global $DB;
  46  
  47          $this->resetAfterTest();
  48  
  49          $this->setup_test_course_and_workshop();
  50  
  51          $this->activate_switch_to_the_next_phase_after_submission_deadline();
  52          $this->set_the_submission_deadline_in_the_past();
  53          $this->activate_the_scheduled_allocator();
  54  
  55          $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
  56  
  57          cron_setup_user();
  58  
  59          // Let the students add submissions.
  60          $this->workshop->switch_phase(\workshop::PHASE_SUBMISSION);
  61  
  62          // Create some submissions.
  63          foreach ($this->students as $student) {
  64              $workshopgenerator->create_submission($this->workshop->id, $student->id);
  65          }
  66  
  67          // No allocations yet.
  68          $this->assertEmpty($this->workshop->get_allocations());
  69  
  70          /* Execute the tasks that will do the transition and allocation thing.
  71           * We expect the workshop cron to do the whole work: change the phase and
  72           * allocate the submissions.
  73           */
  74          $this->execute_workshop_cron_task();
  75  
  76          $workshopdb = $DB->get_record('workshop', ['id' => $this->workshop->id]);
  77          $workshop = new \workshop($workshopdb, $this->workshopcm, $this->course);
  78  
  79          $this->assertEquals(\workshop::PHASE_ASSESSMENT, $workshop->phase);
  80          $this->assertNotEmpty($workshop->get_allocations());
  81      }
  82  
  83      /**
  84       * No allocations are performed if the allocator is not enabled.
  85       */
  86      public function test_that_allocator_is_not_executed_when_its_not_active(): void {
  87          global $DB;
  88  
  89          $this->resetAfterTest();
  90  
  91          $this->setup_test_course_and_workshop();
  92          $this->activate_switch_to_the_next_phase_after_submission_deadline();
  93          $this->set_the_submission_deadline_in_the_past();
  94  
  95          $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
  96  
  97          cron_setup_user();
  98  
  99          // Let the students add submissions.
 100          $this->workshop->switch_phase(\workshop::PHASE_SUBMISSION);
 101  
 102          // Create some submissions.
 103          foreach ($this->students as $student) {
 104              $workshopgenerator->create_submission($this->workshop->id, $student->id);
 105          }
 106  
 107          // No allocations yet.
 108          $this->assertEmpty($this->workshop->get_allocations());
 109  
 110          // Transition to the assessment phase.
 111          $this->execute_workshop_cron_task();
 112  
 113          $workshopdb = $DB->get_record('workshop', ['id' => $this->workshop->id]);
 114          $workshop = new \workshop($workshopdb, $this->workshopcm, $this->course);
 115  
 116          // No allocations too.
 117          $this->assertEquals(\workshop::PHASE_ASSESSMENT, $workshop->phase);
 118          $this->assertEmpty($workshop->get_allocations());
 119      }
 120  
 121      /**
 122       * Activates and configures the scheduled allocator for the workshop.
 123       */
 124      private function activate_the_scheduled_allocator(): void {
 125  
 126          $settings = \workshop_random_allocator_setting::instance_from_object((object)[
 127              'numofreviews' => count($this->students),
 128              'numper' => 1,
 129              'removecurrentuser' => true,
 130              'excludesamegroup' => false,
 131              'assesswosubmission' => true,
 132              'addselfassessment' => false
 133          ]);
 134  
 135          $allocator = new \workshop_scheduled_allocator($this->workshop);
 136  
 137          $storesettingsmethod = new \ReflectionMethod('workshop_scheduled_allocator', 'store_settings');
 138          $storesettingsmethod->setAccessible(true);
 139          $storesettingsmethod->invoke($allocator, true, true, $settings, new \workshop_allocation_result($allocator));
 140      }
 141  
 142      /**
 143       * Creates a minimum common setup to execute tests:
 144       */
 145      protected function setup_test_course_and_workshop(): void {
 146          $this->setAdminUser();
 147  
 148          $datagenerator = $this->getDataGenerator();
 149  
 150          $this->course = $datagenerator->create_course();
 151  
 152          $this->students = [];
 153          for ($i = 0; $i < 10; $i++) {
 154              $this->students[] = $datagenerator->create_and_enrol($this->course);
 155          }
 156  
 157          $workshopdb = $datagenerator->create_module('workshop', [
 158              'course' => $this->course,
 159              'name' => 'Test Workshop',
 160          ]);
 161          $this->workshopcm = get_coursemodule_from_instance('workshop', $workshopdb->id, $this->course->id, false, MUST_EXIST);
 162          $this->workshop = new \workshop($workshopdb, $this->workshopcm, $this->course);
 163      }
 164  
 165      /**
 166       * Executes the workshop cron task.
 167       */
 168      protected function execute_workshop_cron_task(): void {
 169          ob_start();
 170          $cron = new \mod_workshop\task\cron_task();
 171          $cron->execute();
 172          ob_end_clean();
 173      }
 174  
 175      /**
 176       * Executes the scheduled allocator cron task.
 177       */
 178      protected function execute_allocator_cron_task(): void {
 179          ob_start();
 180          $cron = new \workshopallocation_scheduled\task\cron_task();
 181          $cron->execute();
 182          ob_end_clean();
 183      }
 184  
 185      /**
 186       * Activates the "Switch to the next phase after the submissions deadline" flag in the workshop.
 187       */
 188      protected function activate_switch_to_the_next_phase_after_submission_deadline(): void {
 189          global $DB;
 190          $DB->set_field('workshop', 'phaseswitchassessment', 1, ['id' => $this->workshop->id]);
 191      }
 192  
 193      /**
 194       * Sets the submission deadline in a past time.
 195       */
 196      protected function set_the_submission_deadline_in_the_past(): void {
 197          global $DB;
 198          $DB->set_field('workshop', 'submissionend', time() - 1, ['id' => $this->workshop->id]);
 199      }
 200  }