MDL-66147 mod_assign: external get_participant supports relative dates
[moodle.git] / mod / assign / tests / externallib_test.php
index dda4e5e..89cd0fd 100644 (file)
@@ -2467,6 +2467,66 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
         $this->assertEquals($group->name, $result['groupname']);
     }
 
+    /**
+     * Test get_participant() when relative dates mode is enabled on the course.
+     *
+     * @dataProvider get_participant_relative_dates_provider
+     * @param array $courseconfig the config to use when creating the course.
+     * @param array $assignconfig the config to use when creating the assignment.
+     * @param array $enrolconfig the enrolement to create.
+     * @param array $expectedproperties array of expected assign properties.
+     */
+    public function test_get_participant_relative_dates(array $courseconfig, array $assignconfig, array $enrolconfig,
+            array $expectedproperties) {
+        $this->resetAfterTest();
+
+        set_config('enablecourserelativedates', true); // Enable relative dates at site level.
+
+        $course = $this->getDataGenerator()->create_course($courseconfig);
+        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
+        $assignconfig['course'] = $course->id;
+        $instance = $generator->create_instance($assignconfig);
+        $cm = get_coursemodule_from_instance('assign', $instance->id);
+        $context = context_module::instance($cm->id);
+        $assign = new assign($context, $cm, $course);
+
+        $user = $this->getDataGenerator()->create_and_enrol($course, ...array_values($enrolconfig));
+
+        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher', null, 'manual', time() - 50 * DAYSECS);
+
+        $this->setUser($teacher);
+        $result = mod_assign_external::get_participant($assign->get_instance()->id, $user->id, false);
+        $result = external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result);
+
+        foreach ($expectedproperties as $propertyname => $propertyval) {
+            $this->assertEquals($propertyval, $result[$propertyname]);
+        }
+    }
+
+    /**
+     * The test_get_participant_relative_dates data provider.
+     */
+    public function get_participant_relative_dates_provider() {
+        $timenow = time();
+
+        return [
+            'Student whose enrolment starts after the course start date, relative dates mode enabled' => [
+                'courseconfig' => ['relativedatesmode' => true, 'startdate' => $timenow - 10 * DAYSECS],
+                'assignconfig' => ['duedate' => $timenow + 4 * DAYSECS],
+                'enrolconfig' => ['shortname' => 'student', 'userparams' => null, 'method' => 'manual',
+                    'startdate' => $timenow - 8 * DAYSECS],
+                'expectedproperties' => ['duedate' => $timenow + 6 * DAYSECS]
+            ],
+            'Student whose enrolment starts before the course start date, relative dates mode enabled' => [
+                'courseconfig' => ['relativedatesmode' => true, 'startdate' => $timenow - 10 * DAYSECS],
+                'assignconfig' => ['duedate' => $timenow + 4 * DAYSECS],
+                'enrolconfig' => ['shortname' => 'student', 'userparams' => null, 'method' => 'manual',
+                    'startdate' => $timenow - 12 * DAYSECS],
+                'expectedproperties' => ['duedate' => $timenow + 4 * DAYSECS]
+            ],
+        ];
+    }
+
     /**
      * Test for mod_assign_external::list_participants().
      *