Skip to content

Commit

Permalink
MDL-63134 mod_chat: Check if the module is visible to the user
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Feb 11, 2019
1 parent 659a617 commit 5848b3a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mod/chat/lib.php
Expand Up @@ -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);
Expand Down
70 changes: 70 additions & 0 deletions mod/chat/tests/lib_test.php
Expand Up @@ -39,6 +39,76 @@ public function setUp() {
$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();

Expand Down

0 comments on commit 5848b3a

Please sign in to comment.