MDL-63134 mod_chat: Check if the module is visible to the user

This commit is contained in:
Shamim Rezaie 2019-02-11 15:50:49 +11:00
parent 46b495fdc4
commit 0bd93fe52a
2 changed files with 76 additions and 0 deletions

View File

@ -1464,6 +1464,12 @@ function mod_chat_core_calendar_provide_event_action(calendar_event $event,
}
$cm = get_fast_modinfo($event->courseid, $user->id)->instances['chat'][$event->instance];
if (!$cm->uservisible) {
// The module is not visible to the user for any reason.
return null;
}
$chattime = $DB->get_field('chat', 'chattime', array('id' => $event->instance));
$usertimezone = core_date::get_user_timezone($user);
$chattimemidnight = usergetmidnight($chattime, $usertimezone);

View File

@ -39,6 +39,76 @@ class mod_chat_lib_testcase extends advanced_testcase {
$this->resetAfterTest();
}
/*
* The chat's event should not be shown to a user when the user cannot view the chat at all.
*/
public function test_chat_core_calendar_provide_event_action_in_hidden_section() {
global $CFG;
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a student.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
// Create a chat.
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id,
'chattime' => usergetmidnight(time())));
// Create a calendar event.
$event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
// 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_chat_core_calendar_provide_event_action($event, $factory, $student->id);
// Confirm the event is not shown at all.
$this->assertNull($actionevent);
}
/*
* The chat's event should not be shown to a user who does not have permission to view the chat at all.
*/
public function test_chat_core_calendar_provide_event_action_for_non_user() {
global $CFG;
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a chat.
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id,
'chattime' => usergetmidnight(time())));
// Create a calendar event.
$event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
// 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.
$actionevent = mod_chat_core_calendar_provide_event_action($event, $factory);
// Confirm the event is not shown at all.
$this->assertNull($actionevent);
}
public function test_chat_core_calendar_provide_event_action_chattime_event_yesterday() {
$this->setAdminUser();