MDL-64641 calendar: New WS core_calendar_get_allowed_event_types
[moodle.git] / calendar / tests / externallib_test.php
index a520af6..d3a7cf9 100644 (file)
@@ -2634,4 +2634,66 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase {
         $this->assertTrue($data['canmanagegroupentries']);
         $this->assertTrue($data['canmanageentries']);
     }
+
+    /**
+     * Test get_allowed_event_types for admins.
+     */
+    public function test_get_allowed_event_types_for_admins() {
+        global $CFG;
+        $this->resetAfterTest(true);
+        $this->setAdminUser();
+        $CFG->calendar_adminseesall = 1;
+        $data = external_api::clean_returnvalue(
+            core_calendar_external::get_allowed_event_types_returns(),
+            core_calendar_external::get_allowed_event_types()
+        );
+        $this->assertEquals(['user', 'site', 'course', 'category'], $data['allowedeventtypes']);
+    }
+    /**
+     * Test get_allowed_event_types for authenticated users.
+     */
+    public function test_get_allowed_event_types_for_authenticated_users() {
+        $this->resetAfterTest(true);
+        $this->setUser($this->getDataGenerator()->create_user());
+        $data = external_api::clean_returnvalue(
+            core_calendar_external::get_allowed_event_types_returns(),
+            core_calendar_external::get_allowed_event_types()
+        );
+        $this->assertEquals(['user'], $data['allowedeventtypes']);
+    }
+    /**
+     * Test get_allowed_event_types for student users.
+     */
+    public function test_get_allowed_event_types_for_student_users() {
+        global $DB;
+        $this->resetAfterTest(true);
+        $user = $this->getDataGenerator()->create_user();
+        $course = $this->getDataGenerator()->create_course();
+        $role = $DB->get_record('role', array('shortname' => 'student'));
+        $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
+        $this->setUser($user);
+        $data = external_api::clean_returnvalue(
+            core_calendar_external::get_allowed_event_types_returns(),
+            core_calendar_external::get_allowed_event_types($course->id)
+        );
+        $this->assertEquals(['user'], $data['allowedeventtypes']);
+    }
+    /**
+     * Test get_allowed_event_types for teacher users.
+     */
+    public function test_get_allowed_event_types_for_teacher_users() {
+        global $DB;
+        $this->resetAfterTest(true);
+        $user = $this->getDataGenerator()->create_user();
+        $course = $this->getDataGenerator()->create_course(['groupmode' => 1]);
+        $role = $DB->get_record('role', array('shortname' => 'editingteacher'));
+        $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
+        $this->getDataGenerator()->create_group(['courseid' => $course->id]);
+        $this->setUser($user);
+        $data = external_api::clean_returnvalue(
+            core_calendar_external::get_allowed_event_types_returns(),
+            core_calendar_external::get_allowed_event_types($course->id)
+        );
+        $this->assertEquals(['user', 'course', 'group'], $data['allowedeventtypes']);
+    }
 }