MDL-63139 mod_forum: Check if the module is visible to the user
authorShamim Rezaie <shamim@moodle.com>
Fri, 7 Sep 2018 06:55:19 +0000 (16:55 +1000)
committerShamim Rezaie <shamim@moodle.com>
Fri, 7 Sep 2018 06:55:19 +0000 (16:55 +1000)
mod/forum/lib.php
mod/forum/tests/lib_test.php

index f6f4adc..0f624d8 100644 (file)
@@ -8343,6 +8343,12 @@ function mod_forum_core_calendar_provide_event_action(calendar_event $event,
     }
 
     $cm = get_fast_modinfo($event->courseid, $userid)->instances['forum'][$event->instance];
+
+    if (!$cm->uservisible) {
+        // The module is not visible to the user for any reason.
+        return null;
+    }
+
     $context = context_module::instance($cm->id);
 
     if (!has_capability('mod/forum:viewdiscussion', $context, $userid)) {
index 2496b78..cce7861 100644 (file)
@@ -3350,6 +3350,43 @@ class mod_forum_lib_testcase extends advanced_testcase {
         $this->assertTrue($actionevent->is_actionable());
     }
 
+    public function test_forum_core_calendar_provide_event_action_in_hidden_section() {
+        global $CFG;
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Create a course.
+        $course = $this->getDataGenerator()->create_course();
+
+        // Create a student.
+        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
+
+        // Create the activity.
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id,
+                'completionreplies' => 5, 'completiondiscussions' => 2));
+
+        // Create a calendar event.
+        $event = $this->create_action_event($course->id, $forum->id,
+                \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
+
+        // Set sections 0 as hidden.
+        set_section_visible($course->id, 0, 0);
+
+        // Now, log out.
+        $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
+        $this->setUser();
+
+        // Create an action factory.
+        $factory = new \core_calendar\action_factory();
+
+        // Decorate action event for the student.
+        $actionevent = mod_forum_core_calendar_provide_event_action($event, $factory, $student->id);
+
+        // Confirm the event is not shown at all.
+        $this->assertNull($actionevent);
+    }
+
     public function test_forum_core_calendar_provide_event_action_for_user() {
         global $CFG;