| 1 | 
           efrain | 
           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 | 
                   \core\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 | 
                   \core\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->invoke($allocator, true, true, $settings, new \workshop_allocation_result($allocator));
  | 
        
        
            | 
            | 
           139 | 
               }
  | 
        
        
            | 
            | 
           140 | 
              | 
        
        
            | 
            | 
           141 | 
               /**
  | 
        
        
            | 
            | 
           142 | 
                * Creates a minimum common setup to execute tests:
  | 
        
        
            | 
            | 
           143 | 
                */
  | 
        
        
            | 
            | 
           144 | 
               protected function setup_test_course_and_workshop(): void {
  | 
        
        
            | 
            | 
           145 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           146 | 
              | 
        
        
            | 
            | 
           147 | 
                   $datagenerator = $this->getDataGenerator();
  | 
        
        
            | 
            | 
           148 | 
              | 
        
        
            | 
            | 
           149 | 
                   $this->course = $datagenerator->create_course();
  | 
        
        
            | 
            | 
           150 | 
              | 
        
        
            | 
            | 
           151 | 
                   $this->students = [];
  | 
        
        
            | 
            | 
           152 | 
                   for ($i = 0; $i < 10; $i++) {
  | 
        
        
            | 
            | 
           153 | 
                       $this->students[] = $datagenerator->create_and_enrol($this->course);
  | 
        
        
            | 
            | 
           154 | 
                   }
  | 
        
        
            | 
            | 
           155 | 
              | 
        
        
            | 
            | 
           156 | 
                   $workshopdb = $datagenerator->create_module('workshop', [
  | 
        
        
            | 
            | 
           157 | 
                       'course' => $this->course,
  | 
        
        
            | 
            | 
           158 | 
                       'name' => 'Test Workshop',
  | 
        
        
            | 
            | 
           159 | 
                   ]);
  | 
        
        
            | 
            | 
           160 | 
                   $this->workshopcm = get_coursemodule_from_instance('workshop', $workshopdb->id, $this->course->id, false, MUST_EXIST);
  | 
        
        
            | 
            | 
           161 | 
                   $this->workshop = new \workshop($workshopdb, $this->workshopcm, $this->course);
  | 
        
        
            | 
            | 
           162 | 
               }
  | 
        
        
            | 
            | 
           163 | 
              | 
        
        
            | 
            | 
           164 | 
               /**
  | 
        
        
            | 
            | 
           165 | 
                * Executes the workshop cron task.
  | 
        
        
            | 
            | 
           166 | 
                */
  | 
        
        
            | 
            | 
           167 | 
               protected function execute_workshop_cron_task(): void {
  | 
        
        
            | 
            | 
           168 | 
                   ob_start();
  | 
        
        
            | 
            | 
           169 | 
                   $cron = new \mod_workshop\task\cron_task();
  | 
        
        
            | 
            | 
           170 | 
                   $cron->execute();
  | 
        
        
            | 
            | 
           171 | 
                   ob_end_clean();
  | 
        
        
            | 
            | 
           172 | 
               }
  | 
        
        
            | 
            | 
           173 | 
              | 
        
        
            | 
            | 
           174 | 
               /**
  | 
        
        
            | 
            | 
           175 | 
                * Executes the scheduled allocator cron task.
  | 
        
        
            | 
            | 
           176 | 
                */
  | 
        
        
            | 
            | 
           177 | 
               protected function execute_allocator_cron_task(): void {
  | 
        
        
            | 
            | 
           178 | 
                   ob_start();
  | 
        
        
            | 
            | 
           179 | 
                   $cron = new \workshopallocation_scheduled\task\cron_task();
  | 
        
        
            | 
            | 
           180 | 
                   $cron->execute();
  | 
        
        
            | 
            | 
           181 | 
                   ob_end_clean();
  | 
        
        
            | 
            | 
           182 | 
               }
  | 
        
        
            | 
            | 
           183 | 
              | 
        
        
            | 
            | 
           184 | 
               /**
  | 
        
        
            | 
            | 
           185 | 
                * Activates the "Switch to the next phase after the submissions deadline" flag in the workshop.
  | 
        
        
            | 
            | 
           186 | 
                */
  | 
        
        
            | 
            | 
           187 | 
               protected function activate_switch_to_the_next_phase_after_submission_deadline(): void {
  | 
        
        
            | 
            | 
           188 | 
                   global $DB;
  | 
        
        
            | 
            | 
           189 | 
                   $DB->set_field('workshop', 'phaseswitchassessment', 1, ['id' => $this->workshop->id]);
  | 
        
        
            | 
            | 
           190 | 
               }
  | 
        
        
            | 
            | 
           191 | 
              | 
        
        
            | 
            | 
           192 | 
               /**
  | 
        
        
            | 
            | 
           193 | 
                * Sets the submission deadline in a past time.
  | 
        
        
            | 
            | 
           194 | 
                */
  | 
        
        
            | 
            | 
           195 | 
               protected function set_the_submission_deadline_in_the_past(): void {
  | 
        
        
            | 
            | 
           196 | 
                   global $DB;
  | 
        
        
            | 
            | 
           197 | 
                   $DB->set_field('workshop', 'submissionend', time() - 1, ['id' => $this->workshop->id]);
  | 
        
        
            | 
            | 
           198 | 
               }
  | 
        
        
            | 
            | 
           199 | 
           }
  |