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