Files
moodle/mod/chat/tests/lib_test.php
T
Mark Nelson bea71d5732 MDL-57731 mod_chat: added action events
Part of MDL-55611 epic.
2017-04-03 11:36:33 +08:00

154 lines
5.5 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains class containing unit tests for mod/chat/lib.php.
*
* @package mod_chat
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Class containing unit tests for mod/chat/lib.php.
*
* @package mod_chat
* @category test
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_chat_lib_testcase extends advanced_testcase {
public function setUp() {
$this->resetAfterTest();
}
public function test_chat_core_calendar_is_event_visible_chattime_event_as_admin() {
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a chat.
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
// Create a calendar event.
$event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
// Check that we can see the event.
$this->assertTrue(mod_chat_core_calendar_is_event_visible($event));
}
public function test_chat_core_calendar_is_event_visible_chattime_event_as_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));
// Create a calendar event.
$event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
// Log out the user and set force login to true.
\core\session\manager::init_empty_session();
$CFG->forcelogin = true;
// Check that we can't see the event.
$this->assertFalse(mod_chat_core_calendar_is_event_visible($event));
}
public function test_chat_core_calendar_provide_event_action_chattime_event() {
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a chat.
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
// Create a calendar event.
$event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
// 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 was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('enterchat', 'chat'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertTrue($actionevent->is_actionable());
}
public function test_chat_core_calendar_provide_event_action_chattime_event_in_future() {
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a chat.
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id,
'chattime' => time() + DAYSECS));
// Create a calendar event.
$event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
// 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 was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('enterchat', 'chat'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertFalse($actionevent->is_actionable());
}
/**
* Creates an action event.
*
* @param int $courseid
* @param int $instanceid The chat id.
* @param string $eventtype The event type. eg. ASSIGN_EVENT_TYPE_DUE.
* @return bool|\core_calendar\event
*/
private function create_action_event($courseid, $instanceid, $eventtype) {
$event = new stdClass();
$event->name = 'Calendar event';
$event->modulename = 'chat';
$event->courseid = $courseid;
$event->instance = $instanceid;
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->eventtype = $eventtype;
$event->timestart = time();
return \core_calendar\event::create($event);
}
}