From ada27f6b7e6a0b20de8dcad0da5d5130949028cc Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Tue, 5 Mar 2019 19:09:43 +1100 Subject: [PATCH] MDL-63152 mod_workshop: check if the module is visible to the user --- mod/workshop/lib.php | 5 ++ mod/workshop/tests/lib_test.php | 104 ++++++++++++++++++++++++++++++-- 2 files changed, 105 insertions(+), 4 deletions(-) diff --git a/mod/workshop/lib.php b/mod/workshop/lib.php index b4438fc13fa0c..f0a2453165b6b 100644 --- a/mod/workshop/lib.php +++ b/mod/workshop/lib.php @@ -1796,6 +1796,11 @@ function mod_workshop_core_calendar_provide_event_action(calendar_event $event, $cm = get_fast_modinfo($event->courseid)->instances['workshop'][$event->instance]; + if (!$cm->uservisible) { + // The module is not visible to the user for any reason. + return null; + } + return $factory->create_instance( get_string('viewworkshopsummary', 'workshop'), new \moodle_url('/mod/workshop/view.php', array('id' => $cm->id)), diff --git a/mod/workshop/tests/lib_test.php b/mod/workshop/tests/lib_test.php index 1bf8960932a9a..714bc1dc3dd86 100644 --- a/mod/workshop/tests/lib_test.php +++ b/mod/workshop/tests/lib_test.php @@ -56,6 +56,32 @@ public function test_workshop_core_calendar_provide_event_action_open() { $this->assertTrue($actionevent->is_actionable()); } + /** + * Test calendar event provide action open for a non user. + */ + public function test_workshop_core_calendar_provide_event_action_open_for_non_user() { + global $CFG; + + $this->resetAfterTest(); + $this->setAdminUser(); + + $now = time(); + $course = $this->getDataGenerator()->create_course(); + $workshop = $this->getDataGenerator()->create_module('workshop', ['course' => $course->id, + 'submissionstart' => $now - DAYSECS, 'submissionend' => $now + DAYSECS]); + $event = $this->create_action_event($course->id, $workshop->id, WORKSHOP_EVENT_TYPE_SUBMISSION_OPEN); + + // 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(); + + $factory = new \core_calendar\action_factory(); + $actionevent = mod_workshop_core_calendar_provide_event_action($event, $factory); + + // Confirm the event is not shown at all. + $this->assertNull($actionevent); + } + /** * Test calendar event provide action closed. */ @@ -78,10 +104,33 @@ public function test_workshop_core_calendar_provide_event_action_closed() { $this->assertTrue($actionevent->is_actionable()); } + /** + * Test calendar event provide action closed for a non user. + */ + public function test_workshop_core_calendar_provide_event_action_closed_for_non_user() { + global $CFG; + + $this->resetAfterTest(); + $this->setAdminUser(); + + $course = $this->getDataGenerator()->create_course(); + $workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $course->id, + 'submissionend' => time() - DAYSECS)); + $event = $this->create_action_event($course->id, $workshop->id, WORKSHOP_EVENT_TYPE_SUBMISSION_OPEN); + + // 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(); + + $factory = new \core_calendar\action_factory(); + $actionevent = mod_workshop_core_calendar_provide_event_action($event, $factory); + + // Confirm the event is not shown at all. + $this->assertNull($actionevent); + } + /** * Test calendar event action open in future. - * - * @throws coding_exception */ public function test_workshop_core_calendar_provide_event_action_open_in_future() { $this->resetAfterTest(); @@ -102,10 +151,33 @@ public function test_workshop_core_calendar_provide_event_action_open_in_future( $this->assertTrue($actionevent->is_actionable()); } + /** + * Test calendar event action open in future for a non user. + */ + public function test_workshop_core_calendar_provide_event_action_open_in_future_for_non_user() { + global $CFG; + + $this->resetAfterTest(); + $this->setAdminUser(); + + $course = $this->getDataGenerator()->create_course(); + $workshop = $this->getDataGenerator()->create_module('workshop', ['course' => $course->id, + 'submissionstart' => time() + DAYSECS]); + $event = $this->create_action_event($course->id, $workshop->id, WORKSHOP_EVENT_TYPE_SUBMISSION_OPEN); + + // 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(); + + $factory = new \core_calendar\action_factory(); + $actionevent = mod_workshop_core_calendar_provide_event_action($event, $factory); + + // Confirm the event is not shown at all. + $this->assertNull($actionevent); + } + /** * Test calendar event with no time specified. - * - * @throws coding_exception */ public function test_workshop_core_calendar_provide_event_action_no_time_specified() { $this->resetAfterTest(); @@ -125,6 +197,30 @@ public function test_workshop_core_calendar_provide_event_action_no_time_specifi $this->assertTrue($actionevent->is_actionable()); } + /** + * Test calendar event with no time specified for a non user. + */ + public function test_workshop_core_calendar_provide_event_action_no_time_specified_for_non_user() { + global $CFG; + + $this->resetAfterTest(); + $this->setAdminUser(); + + $course = $this->getDataGenerator()->create_course(); + $workshop = $this->getDataGenerator()->create_module('workshop', ['course' => $course->id]); + $event = $this->create_action_event($course->id, $workshop->id, WORKSHOP_EVENT_TYPE_SUBMISSION_OPEN); + + // 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(); + + $factory = new \core_calendar\action_factory(); + $actionevent = mod_workshop_core_calendar_provide_event_action($event, $factory); + + // Confirm the event is not shown at all. + $this->assertNull($actionevent); + } + /** * Creates an action event. *