2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 defined('MOODLE_INTERNAL') ||
die();
22 require_once($CFG->dirroot
. '/mod/chat/lib.php');
25 * Tests for format_message.
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;
36 public function chat_format_message_manually_provider() {
37 $dateregexp = '\d{2}:\d{2}';
40 'message' => 'beep all',
43 'expecttext' => "/^{$dateregexp}: " . get_string('messagebeepseveryone', 'chat', '__CURRENTUSER__') . ': /',
44 'refreshusers' => false,
47 'Beep the current user' => [
48 'message' => 'beep __CURRENTUSER__',
51 'expecttext' => "/^{$dateregexp}: " . get_string('messagebeepsyou', 'chat', '__CURRENTUSER__') . ': /',
52 'refreshusers' => false,
55 'Beep another user' => [
56 'message' => 'beep __OTHERUSER__',
58 'willreturn' => false,
60 'refreshusers' => null,
67 'expecttext' => "/^{$dateregexp} __CURRENTUSER_FIRST__: beep$/",
68 'refreshusers' => false,
72 'message' => '/me writes a test',
75 'expecttext' => "/^{$dateregexp}: \*\*\* __CURRENTUSER_FIRST__ writes a test$/",
76 'refreshusers' => false,
79 'Invalid command' => [
83 'expecttext' => "/^{$dateregexp} __CURRENTUSER_FIRST__: \/help$/",
84 'refreshusers' => false,
88 'message' => 'To Bernard:I love tests',
91 'expecttext' => "/^{$dateregexp}: __CURRENTUSER_FIRST__ " . get_string('saidto', 'chat') . " Bernard: I love tests$/",
92 'refreshusers' => false,
95 'To user trimmed' => [
96 'message' => 'To Bernard: I love tests',
99 'expecttext' => "/^{$dateregexp}: __CURRENTUSER_FIRST__ " . get_string('saidto', 'chat') . " Bernard: I love tests$/",
100 'refreshusers' => false,
104 'message' => 'enter',
106 'willreturn' => true,
107 'expecttext' => "/^{$dateregexp}: " . get_string('messageenter', 'chat', '__CURRENTUSER__') . "$/",
108 'refreshusers' => true,
114 'willreturn' => true,
115 'expecttext' => "/^{$dateregexp}: " . get_string('messageexit', 'chat', '__CURRENTUSER__') . "$/",
116 'refreshusers' => true,
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
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);
150 $this->assertFalse($result);
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
);