weekly back-to-dev release 5.0dev
[moodle.git] / mod / chat / tests / format_message_test.php
blobaa36b66aeef048c41c2e83ad3a8c314c884b99af
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.
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/>.
17 namespace mod_chat;
19 defined('MOODLE_INTERNAL') || die();
21 global $CFG;
22 require_once($CFG->dirroot . '/mod/chat/lib.php');
24 /**
25 * Tests for format_message.
27 * @package mod_chat
28 * @copyright 2016 Andrew NIcols
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 class format_message_test extends \advanced_testcase {
33 const USER_CURRENT = 1;
34 const USER_OTHER = 2;
36 public function chat_format_message_manually_provider() {
37 $dateregexp = '\d{2}:\d{2}';
38 return [
39 'Beep everyone' => [
40 'message' => 'beep all',
41 'issystem' => false,
42 'willreturn' => true,
43 'expecttext' => "/^{$dateregexp}: " . get_string('messagebeepseveryone', 'chat', '__CURRENTUSER__') . ': /',
44 'refreshusers' => false,
45 'beep' => true,
47 'Beep the current user' => [
48 'message' => 'beep __CURRENTUSER__',
49 'issystem' => false,
50 'willreturn' => true,
51 'expecttext' => "/^{$dateregexp}: " . get_string('messagebeepsyou', 'chat', '__CURRENTUSER__') . ': /',
52 'refreshusers' => false,
53 'beep' => true,
55 'Beep another user' => [
56 'message' => 'beep __OTHERUSER__',
57 'issystem' => false,
58 'willreturn' => false,
59 'expecttext' => null,
60 'refreshusers' => null,
61 'beep' => null,
63 'Malformed beep' => [
64 'message' => 'beep',
65 'issystem' => false,
66 'willreturn' => true,
67 'expecttext' => "/^{$dateregexp} __CURRENTUSER_FIRST__: beep$/",
68 'refreshusers' => false,
69 'beep' => false,
71 '/me says' => [
72 'message' => '/me writes a test',
73 'issystem' => false,
74 'willreturn' => true,
75 'expecttext' => "/^{$dateregexp}: \*\*\* __CURRENTUSER_FIRST__ writes a test$/",
76 'refreshusers' => false,
77 'beep' => false,
79 'Invalid command' => [
80 'message' => '/help',
81 'issystem' => false,
82 'willreturn' => true,
83 'expecttext' => "/^{$dateregexp} __CURRENTUSER_FIRST__: \/help$/",
84 'refreshusers' => false,
85 'beep' => false,
87 'To user' => [
88 'message' => 'To Bernard:I love tests',
89 'issystem' => false,
90 'willreturn' => true,
91 'expecttext' => "/^{$dateregexp}: __CURRENTUSER_FIRST__ " . get_string('saidto', 'chat') . " Bernard: I love tests$/",
92 'refreshusers' => false,
93 'beep' => false,
95 'To user trimmed' => [
96 'message' => 'To Bernard: I love tests',
97 'issystem' => false,
98 'willreturn' => true,
99 'expecttext' => "/^{$dateregexp}: __CURRENTUSER_FIRST__ " . get_string('saidto', 'chat') . " Bernard: I love tests$/",
100 'refreshusers' => false,
101 'beep' => false,
103 'System: enter' => [
104 'message' => 'enter',
105 'issystem' => true,
106 'willreturn' => true,
107 'expecttext' => "/^{$dateregexp}: " . get_string('messageenter', 'chat', '__CURRENTUSER__') . "$/",
108 'refreshusers' => true,
109 'beep' => false,
111 'System: exit' => [
112 'message' => 'exit',
113 'issystem' => true,
114 'willreturn' => true,
115 'expecttext' => "/^{$dateregexp}: " . get_string('messageexit', 'chat', '__CURRENTUSER__') . "$/",
116 'refreshusers' => true,
117 'beep' => false,
123 * @dataProvider chat_format_message_manually_provider
125 public function test_chat_format_message_manually($messagetext, $issystem, $willreturn,
126 $expecttext, $refreshusers, $expectbeep): void {
128 $this->resetAfterTest();
130 $course = $this->getDataGenerator()->create_course();
131 $currentuser = $this->getDataGenerator()->create_user();
132 $this->setUser($currentuser);
133 $otheruser = $this->getDataGenerator()->create_user();
135 // Replace the message texts.
136 // These can't be done in the provider because it runs before the
137 // test starts.
138 $messagetext = str_replace('__CURRENTUSER__', $currentuser->id, $messagetext);
139 $messagetext = str_replace('__OTHERUSER__', $otheruser->id, $messagetext);
141 $message = (object) [
142 'message' => $messagetext,
143 'timestamp' => time(),
144 'issystem' => $issystem,
147 $result = chat_format_message_manually($message, $course->id, $currentuser, $currentuser);
149 if (!$willreturn) {
150 $this->assertFalse($result);
151 } else {
152 $this->assertNotFalse($result);
153 if (!empty($expecttext)) {
154 $expecttext = str_replace('__CURRENTUSER__', fullname($currentuser), $expecttext);
155 $expecttext = str_replace('__CURRENTUSER_FIRST__', $currentuser->firstname, $expecttext);
156 $this->assertMatchesRegularExpression($expecttext, $result->text);
159 $this->assertEquals($refreshusers, $result->refreshusers);
160 $this->assertEquals($expectbeep, $result->beep);