MDL-63150 mod_url: Accept a userid override in event action
[moodle.git] / mod / url / tests / lib_test.php
index 571ee2a..76e6d65 100644 (file)
@@ -116,6 +116,55 @@ class mod_url_lib_testcase extends advanced_testcase {
         $this->assertEquals(1, $completiondata->completionstate);
     }
 
+    /**
+     * Test mod_url_core_calendar_provide_event_action with user override
+     */
+    public function test_url_core_calendar_provide_event_action_user_override() {
+        global $CFG, $USER;
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+        $user = $this->getDataGenerator()->create_user();
+        $CFG->enablecompletion = 1;
+
+        // Create the activity.
+        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
+        $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id),
+            array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
+
+        // Get some additional data.
+        $cm = get_coursemodule_from_instance('url', $url->id);
+
+        // Create a calendar event.
+        $event = $this->create_action_event($course->id, $url->id,
+            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
+
+        // Mark the activity as completed.
+        $completion = new completion_info($course);
+        $completion->set_module_viewed($cm);
+
+        // Create an action factory.
+        $factory = new \core_calendar\action_factory();
+
+        // Decorate action event.
+        $actionevent = mod_url_core_calendar_provide_event_action($event, $factory, $USER->id);
+
+        // Decorate action with a userid override.
+        $actionevent2 = mod_url_core_calendar_provide_event_action($event, $factory, $user->id);
+
+        // Ensure result was null because it has been marked as completed for the associated user.
+        // Logic was brought across from the "_already_completed" function.
+        $this->assertNull($actionevent);
+
+        // Confirm the event was decorated.
+        $this->assertNotNull($actionevent2);
+        $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent2);
+        $this->assertEquals(get_string('view'), $actionevent2->get_name());
+        $this->assertInstanceOf('moodle_url', $actionevent2->get_url());
+        $this->assertEquals(1, $actionevent2->get_item_count());
+        $this->assertTrue($actionevent2->is_actionable());
+    }
+
     public function test_url_core_calendar_provide_event_action() {
         $this->resetAfterTest();
         $this->setAdminUser();