MDL-57731 mod_chat: added action events
[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
42 public function test_chat_core_calendar_is_event_visible_chattime_event_as_admin() {
43 $this->setAdminUser();
44
45 // Create a course.
46 $course = $this->getDataGenerator()->create_course();
47
48 // Create a chat.
49 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
50
51 // Create a calendar event.
52 $event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
53
54 // Check that we can see the event.
55 $this->assertTrue(mod_chat_core_calendar_is_event_visible($event));
56 }
57
58 public function test_chat_core_calendar_is_event_visible_chattime_event_as_non_user() {
59 global $CFG;
60
61 $this->setAdminUser();
62
63 // Create a course.
64 $course = $this->getDataGenerator()->create_course();
65
66 // Create a chat.
67 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
68
69 // Create a calendar event.
70 $event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
71
72 // Log out the user and set force login to true.
73 \core\session\manager::init_empty_session();
74 $CFG->forcelogin = true;
75
76 // Check that we can't see the event.
77 $this->assertFalse(mod_chat_core_calendar_is_event_visible($event));
78 }
79
80 public function test_chat_core_calendar_provide_event_action_chattime_event() {
81 $this->setAdminUser();
82
83 // Create a course.
84 $course = $this->getDataGenerator()->create_course();
85
86 // Create a chat.
87 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
88
89 // Create a calendar event.
90 $event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
91
92 // Create an action factory.
93 $factory = new \core_calendar\action_factory();
94
95 // Decorate action event.
96 $actionevent = mod_chat_core_calendar_provide_event_action($event, $factory);
97
98 // Confirm the event was decorated.
99 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
100 $this->assertEquals(get_string('enterchat', 'chat'), $actionevent->get_name());
101 $this->assertInstanceOf('moodle_url', $actionevent->get_url());
102 $this->assertEquals(1, $actionevent->get_item_count());
103 $this->assertTrue($actionevent->is_actionable());
104 }
105
106 public function test_chat_core_calendar_provide_event_action_chattime_event_in_future() {
107 $this->setAdminUser();
108
109 // Create a course.
110 $course = $this->getDataGenerator()->create_course();
111
112 // Create a chat.
113 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id,
114 'chattime' => time() + DAYSECS));
115
116 // Create a calendar event.
117 $event = $this->create_action_event($course->id, $chat->id, CHAT_EVENT_TYPE_CHATTIME);
118
119 // Create an action factory.
120 $factory = new \core_calendar\action_factory();
121
122 // Decorate action event.
123 $actionevent = mod_chat_core_calendar_provide_event_action($event, $factory);
124
125 // Confirm the event was decorated.
126 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
127 $this->assertEquals(get_string('enterchat', 'chat'), $actionevent->get_name());
128 $this->assertInstanceOf('moodle_url', $actionevent->get_url());
129 $this->assertEquals(1, $actionevent->get_item_count());
130 $this->assertFalse($actionevent->is_actionable());
131 }
132
133 /**
134 * Creates an action event.
135 *
136 * @param int $courseid
137 * @param int $instanceid The chat id.
138 * @param string $eventtype The event type. eg. ASSIGN_EVENT_TYPE_DUE.
139 * @return bool|\core_calendar\event
140 */
141 private function create_action_event($courseid, $instanceid, $eventtype) {
142 $event = new stdClass();
143 $event->name = 'Calendar event';
144 $event->modulename = 'chat';
145 $event->courseid = $courseid;
146 $event->instance = $instanceid;
147 $event->type = CALENDAR_EVENT_TYPE_ACTION;
148 $event->eventtype = $eventtype;
149 $event->timestart = time();
150
151 return \core_calendar\event::create($event);
152 }
153}