weekly release 3.6dev
[moodle.git] / mod / chat / tests / lib_test.php
CommitLineData
bea71d57
MN
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Contains class containing unit tests for mod/chat/lib.php.
19 *
20 * @package mod_chat
21 * @category test
22 * @copyright 2017 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * Class containing unit tests for mod/chat/lib.php.
30 *
31 * @package mod_chat
32 * @category test
33 * @copyright 2017 Mark Nelson <markn@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class mod_chat_lib_testcase extends advanced_testcase {
37
38 public function setUp() {
39 $this->resetAfterTest();
40 }
41
3b2be06b 42 public function test_chat_core_calendar_provide_event_action_chattime_event_yesterday() {
bea71d57
MN
43 $this->setAdminUser();
44
45 // Create a course.
46 $course = $this->getDataGenerator()->create_course();
47
48 // Create a chat.
3b2be06b
AN
49 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id,
50 'chattime' => time() - DAYSECS));
bea71d57
MN
51
52 // Create a calendar event.
53 $event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
54
73be2a88
AN
55 // Create an action factory.
56 $factory = new \core_calendar\action_factory();
57
73be2a88
AN
58 // Decorate action event.
59 $actionevent = mod_chat_core_calendar_provide_event_action($event, $factory);
60
3b2be06b 61 // Confirm the event is not shown at all.
73be2a88 62 $this->assertNull($actionevent);
bea71d57
MN
63 }
64
3b2be06b 65 public function test_chat_core_calendar_provide_event_action_chattime_event_today() {
bea71d57
MN
66 $this->setAdminUser();
67
68 // Create a course.
69 $course = $this->getDataGenerator()->create_course();
70
71 // Create a chat.
3b2be06b
AN
72 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id,
73 'chattime' => usergetmidnight(time())));
74
75 // Create a calendar event.
76 $event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
77
78 // Create an action factory.
79 $factory = new \core_calendar\action_factory();
80
81 // Decorate action event.
82 $actionevent = mod_chat_core_calendar_provide_event_action($event, $factory);
83
84 // Confirm the event was decorated.
85 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
86 $this->assertEquals(get_string('enterchat', 'chat'), $actionevent->get_name());
87 $this->assertInstanceOf('moodle_url', $actionevent->get_url());
88 $this->assertEquals(1, $actionevent->get_item_count());
89 $this->assertTrue($actionevent->is_actionable());
90 }
91
92 public function test_chat_core_calendar_provide_event_action_chattime_event_tonight() {
93 $this->setAdminUser();
94
95 // Create a course.
96 $course = $this->getDataGenerator()->create_course();
97
98 // Create a chat.
99 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id,
100 'chattime' => usergetmidnight(time()) + (23 * HOURSECS)));
bea71d57
MN
101
102 // Create a calendar event.
103 $event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
104
105 // Create an action factory.
106 $factory = new \core_calendar\action_factory();
107
108 // Decorate action event.
109 $actionevent = mod_chat_core_calendar_provide_event_action($event, $factory);
110
111 // Confirm the event was decorated.
112 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
113 $this->assertEquals(get_string('enterchat', 'chat'), $actionevent->get_name());
114 $this->assertInstanceOf('moodle_url', $actionevent->get_url());
115 $this->assertEquals(1, $actionevent->get_item_count());
116 $this->assertTrue($actionevent->is_actionable());
117 }
118
3b2be06b 119 public function test_chat_core_calendar_provide_event_action_chattime_event_tomorrow() {
bea71d57
MN
120 $this->setAdminUser();
121
122 // Create a course.
123 $course = $this->getDataGenerator()->create_course();
124
125 // Create a chat.
126 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id,
127 'chattime' => time() + DAYSECS));
128
129 // Create a calendar event.
130 $event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
131
132 // Create an action factory.
133 $factory = new \core_calendar\action_factory();
134
135 // Decorate action event.
136 $actionevent = mod_chat_core_calendar_provide_event_action($event, $factory);
137
138 // Confirm the event was decorated.
139 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
140 $this->assertEquals(get_string('enterchat', 'chat'), $actionevent->get_name());
141 $this->assertInstanceOf('moodle_url', $actionevent->get_url());
142 $this->assertEquals(1, $actionevent->get_item_count());
143 $this->assertFalse($actionevent->is_actionable());
144 }
145
146 /**
147 * Creates an action event.
148 *
149 * @param int $courseid
150 * @param int $instanceid The chat id.
151 * @param string $eventtype The event type. eg. ASSIGN_EVENT_TYPE_DUE.
e1cd93ce 152 * @return bool|calendar_event
bea71d57
MN
153 */
154 private function create_action_event($courseid, $instanceid, $eventtype) {
155 $event = new stdClass();
156 $event->name = 'Calendar event';
157 $event->modulename = 'chat';
158 $event->courseid = $courseid;
159 $event->instance = $instanceid;
160 $event->type = CALENDAR_EVENT_TYPE_ACTION;
161 $event->eventtype = $eventtype;
162 $event->timestart = time();
163
e1cd93ce 164 return calendar_event::create($event);
bea71d57
MN
165 }
166}