1 |
efrain |
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 a helper class providing util methods for testing.
|
|
|
19 |
*
|
|
|
20 |
* @package core_message
|
|
|
21 |
* @copyright 2018 Jake Dallimore <markn@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace core_message\tests;
|
|
|
26 |
|
11 |
efrain |
27 |
use stdClass;
|
1 |
efrain |
28 |
|
|
|
29 |
/**
|
|
|
30 |
* The helper class providing util methods for testing.
|
|
|
31 |
*
|
|
|
32 |
* @copyright 2018 Jake Dallimore <markn@moodle.com>
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class helper {
|
|
|
36 |
/**
|
11 |
efrain |
37 |
* Send a fake message.
|
|
|
38 |
*
|
|
|
39 |
* {@link message_send()} does not support transaction, this function will simulate a message
|
|
|
40 |
* sent from a user to another. We should stop using it once {@link message_send()} will support
|
|
|
41 |
* transactions. This is not clean at all, this is just used to add rows to the table.
|
|
|
42 |
*
|
|
|
43 |
* @param \stdClass $userfrom user object of the one sending the message.
|
|
|
44 |
* @param \stdClass $userto user object of the one receiving the message.
|
|
|
45 |
* @param string $message message to send.
|
|
|
46 |
* @param int $notification if the message is a notification.
|
|
|
47 |
* @param int $time the time the message was sent
|
|
|
48 |
* @return int the id of the message
|
|
|
49 |
*/
|
|
|
50 |
public static function send_fake_message(
|
|
|
51 |
stdClass $userfrom,
|
|
|
52 |
stdClass $userto,
|
|
|
53 |
string $message = 'Hello world!',
|
|
|
54 |
int $notification = 0,
|
|
|
55 |
int $time = 0,
|
|
|
56 |
): int {
|
|
|
57 |
global $DB;
|
|
|
58 |
|
|
|
59 |
if (empty($time)) {
|
|
|
60 |
$time = time();
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
if ($notification) {
|
|
|
64 |
$record = new \stdClass();
|
|
|
65 |
$record->useridfrom = $userfrom->id;
|
|
|
66 |
$record->useridto = $userto->id;
|
|
|
67 |
$record->subject = 'No subject';
|
|
|
68 |
$record->fullmessage = $message;
|
|
|
69 |
$record->smallmessage = $message;
|
|
|
70 |
$record->timecreated = $time;
|
|
|
71 |
|
|
|
72 |
return $DB->insert_record('notifications', $record);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
if ($userfrom->id == $userto->id) {
|
|
|
76 |
// It's a self conversation.
|
|
|
77 |
$conversation = \core_message\api::get_self_conversation($userfrom->id);
|
|
|
78 |
if (empty($conversation)) {
|
|
|
79 |
$conversation = \core_message\api::create_conversation(
|
|
|
80 |
\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
|
|
|
81 |
[$userfrom->id],
|
|
|
82 |
);
|
|
|
83 |
}
|
|
|
84 |
$conversationid = $conversation->id;
|
|
|
85 |
} else if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
|
|
|
86 |
// It's an individual conversation between two different users.
|
|
|
87 |
$conversation = \core_message\api::create_conversation(
|
|
|
88 |
\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
|
|
|
89 |
[
|
|
|
90 |
$userfrom->id,
|
|
|
91 |
$userto->id,
|
|
|
92 |
]
|
|
|
93 |
);
|
|
|
94 |
$conversationid = $conversation->id;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
// Ok, send the message.
|
|
|
98 |
$record = (object) [
|
|
|
99 |
'useridfrom' => $userfrom->id,
|
|
|
100 |
'conversationid' => $conversationid,
|
|
|
101 |
'subject' => 'No subject',
|
|
|
102 |
'fullmessage' => $message,
|
|
|
103 |
'smallmessage' => $message,
|
|
|
104 |
'timecreated' => $time,
|
|
|
105 |
];
|
|
|
106 |
|
|
|
107 |
return $DB->insert_record('messages', $record);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
1 |
efrain |
111 |
* Sends a message to a conversation.
|
|
|
112 |
*
|
|
|
113 |
* @param \stdClass $userfrom user object of the one sending the message.
|
|
|
114 |
* @param int $convid id of the conversation in which we'll send the message.
|
|
|
115 |
* @param string $message message to send.
|
|
|
116 |
* @param int $time the time the message was sent.
|
|
|
117 |
* @return int the id of the message which was sent.
|
|
|
118 |
* @throws \dml_exception if the conversation doesn't exist.
|
|
|
119 |
*/
|
|
|
120 |
public static function send_fake_message_to_conversation(\stdClass $userfrom, int $convid, string $message = 'Hello world!',
|
|
|
121 |
int $time = null): int {
|
|
|
122 |
global $DB;
|
|
|
123 |
$conversationrec = $DB->get_record('message_conversations', ['id' => $convid], 'id', MUST_EXIST);
|
|
|
124 |
$conversationid = $conversationrec->id;
|
|
|
125 |
$time = $time ?? time();
|
|
|
126 |
$record = new \stdClass();
|
|
|
127 |
$record->useridfrom = $userfrom->id;
|
|
|
128 |
$record->conversationid = $conversationid;
|
|
|
129 |
$record->subject = 'No subject';
|
|
|
130 |
$record->fullmessage = $message;
|
|
|
131 |
$record->smallmessage = $message;
|
|
|
132 |
$record->timecreated = $time;
|
|
|
133 |
return $DB->insert_record('messages', $record);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Send a fake unread notification.
|
|
|
138 |
*
|
|
|
139 |
* message_send() does not support transaction, this function will simulate a message
|
|
|
140 |
* sent from a user to another. We should stop using it once message_send() will support
|
|
|
141 |
* transactions. This is not clean at all, this is just used to add rows to the table.
|
|
|
142 |
*
|
|
|
143 |
* @param stdClass $userfrom user object of the one sending the message.
|
|
|
144 |
* @param stdClass $userto user object of the one receiving the message.
|
|
|
145 |
* @param string $message message to send.
|
|
|
146 |
* @param int $timecreated time the message was created.
|
|
|
147 |
* @return int the id of the message
|
|
|
148 |
*/
|
|
|
149 |
public static function send_fake_unread_notification(\stdClass $userfrom, \stdClass $userto, string $message = 'Hello world!',
|
|
|
150 |
int $timecreated = 0): int {
|
|
|
151 |
global $DB;
|
|
|
152 |
|
|
|
153 |
$record = new \stdClass();
|
|
|
154 |
$record->useridfrom = $userfrom->id;
|
|
|
155 |
$record->useridto = $userto->id;
|
|
|
156 |
$record->notification = 1;
|
|
|
157 |
$record->subject = 'No subject';
|
|
|
158 |
$record->fullmessage = $message;
|
|
|
159 |
$record->smallmessage = $message;
|
|
|
160 |
$record->timecreated = $timecreated ? $timecreated : time();
|
|
|
161 |
$record->customdata = json_encode(['datakey' => 'data']);
|
|
|
162 |
|
|
|
163 |
return $DB->insert_record('notifications', $record);
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Send a fake read notification.
|
|
|
168 |
*
|
|
|
169 |
* message_send() does not support transaction, this function will simulate a message
|
|
|
170 |
* sent from a user to another. We should stop using it once message_send() will support
|
|
|
171 |
* transactions. This is not clean at all, this is just used to add rows to the table.
|
|
|
172 |
*
|
|
|
173 |
* @param stdClass $userfrom user object of the one sending the message.
|
|
|
174 |
* @param stdClass $userto user object of the one receiving the message.
|
|
|
175 |
* @param string $message message to send.
|
|
|
176 |
* @param int $timecreated time the message was created.
|
|
|
177 |
* @param int $timeread the the message was read
|
|
|
178 |
* @return int the id of the message
|
|
|
179 |
*/
|
|
|
180 |
public static function send_fake_read_notification(\stdClass $userfrom, \stdClass $userto, string $message = 'Hello world!',
|
|
|
181 |
int $timecreated = 0, int $timeread = 0): int {
|
|
|
182 |
global $DB;
|
|
|
183 |
|
|
|
184 |
$record = new \stdClass();
|
|
|
185 |
$record->useridfrom = $userfrom->id;
|
|
|
186 |
$record->useridto = $userto->id;
|
|
|
187 |
$record->notification = 1;
|
|
|
188 |
$record->subject = 'No subject';
|
|
|
189 |
$record->fullmessage = $message;
|
|
|
190 |
$record->smallmessage = $message;
|
|
|
191 |
$record->timecreated = $timecreated ? $timecreated : time();
|
|
|
192 |
$record->timeread = $timeread ? $timeread : time();
|
|
|
193 |
|
|
|
194 |
$record->id = $DB->insert_record('notifications', $record);
|
|
|
195 |
|
|
|
196 |
// Mark it as read.
|
|
|
197 |
\core_message\api::mark_notification_as_read($record);
|
|
|
198 |
|
|
|
199 |
return $record->id;
|
|
|
200 |
}
|
|
|
201 |
}
|