. /** * Contains class containing unit tests for mod/chat/lib.php. * * @package mod_chat * @category test * @copyright 2017 Mark Nelson * @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 * @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_provide_event_action_chattime_event_yesterday() { $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 is not shown at all. $this->assertNull($actionevent); } public function test_chat_core_calendar_provide_event_action_chattime_event_today() { $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); // 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_tonight() { $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()) + (23 * HOURSECS))); // 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_tomorrow() { $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|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 calendar_event::create($event); } }