MDL-59834 Global Search: Indexing user messages
[moodle.git] / message / tests / search_test_sent.php
CommitLineData
26c0d75f
DG
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 * Sent message global search unit tests.
19 *
20 * @package core
21 * @copyright 2016 Devang Gaur
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
28require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
29
30/**
31 * Provides the unit tests for sent message global search.
32 *
33 * @package core
34 * @copyright 2016 Devang Gaur
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class message_sent_search_testcase extends advanced_testcase {
38
39 /**
40 * @var string Area id
41 */
42 protected $messagesentareaid = null;
43
44 /**
45 * Setting up the test environment
46 * @return void
47 */
48 public function setUp() {
49 $this->resetAfterTest(true);
50 set_config('enableglobalsearch', true);
51
52 $this->messagesentareaid = \core_search\manager::generate_areaid('core_message', 'message_sent');
53
54 // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
55 $search = testable_core_search::instance();
56 }
57
58 /**
59 * Indexing messages contents.
60 *
61 * @return void
62 */
63 public function test_message_sent_indexing() {
64
65 // Returns the instance as long as the area is supported.
66 $searcharea = \core_search\manager::get_search_area($this->messagesentareaid);
67 $this->assertInstanceOf('\core_message\search\message_sent', $searcharea);
68
69 $user1 = self::getDataGenerator()->create_user();
70 $user2 = self::getDataGenerator()->create_user();
71
72 $this->preventResetByRollback();
73 $sink = $this->redirectMessages();
74
880fc15b
EM
75 $message = new \core\message\message();
76 $message->courseid = SITEID;
26c0d75f
DG
77 $message->userfrom = $user1;
78 $message->userto = $user2;
79 $message->subject = "Test Subject";
80 $message->smallmessage = "Test small messsage";
81 $message->fullmessage = "Test full messsage";
82 $message->fullmessageformat = 0;
83 $message->fullmessagehtml = null;
84 $message->notification = 0;
85 $message->component = 'moodle';
86 $message->name = "instantmessage";
87
88 message_send($message);
89
90 $messages = $sink->get_messages();
91
92 $this->assertEquals(1, count($messages));
93
94 // All records.
95 $recordset = $searcharea->get_recordset_by_timestamp(0);
96 $this->assertTrue($recordset->valid());
97 $nrecords = 0;
98 foreach ($recordset as $record) {
99 $this->assertInstanceOf('stdClass', $record);
100 $doc = $searcharea->get_document($record);
101 $this->assertInstanceOf('\core_search\document', $doc);
102 $nrecords++;
103 }
104 // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
105 $recordset->close();
106 $this->assertEquals(1, $nrecords);
107
108 // The +2 is to prevent race conditions.
109 $recordset = $searcharea->get_recordset_by_timestamp(time() + 2);
110
111 // No new records.
112 $this->assertFalse($recordset->valid());
113 $recordset->close();
114 }
115
116 /**
117 * Document contents.
118 *
119 * @return void
120 */
121 public function test_message_sent_document() {
122
123 // Returns the instance as long as the area is supported.
124 $searcharea = \core_search\manager::get_search_area($this->messagesentareaid);
125 $this->assertInstanceOf('\core_message\search\message_sent', $searcharea);
126
127 $user1 = self::getDataGenerator()->create_user();
128 $user2 = self::getDataGenerator()->create_user();
129
130 $this->preventResetByRollback();
131 $sink = $this->redirectMessages();
132
880fc15b
EM
133 $message = new \core\message\message();
134 $message->courseid = SITEID;
26c0d75f
DG
135 $message->userfrom = $user1;
136 $message->userto = $user2;
137 $message->subject = "Test Subject";
138 $message->smallmessage = "Test small messsage";
139 $message->fullmessage = "Test full messsage";
140 $message->fullmessageformat = 0;
141 $message->fullmessagehtml = null;
142 $message->notification = 0;
143 $message->component = "moodle";
144 $message->name = "instantmessage";
145
146 message_send($message);
147
148 $messages = $sink->get_messages();
149 $message = $messages[0];
150
151 $doc = $searcharea->get_document($message);
152 $this->assertInstanceOf('\core_search\document', $doc);
153 $this->assertEquals($message->id, $doc->get('itemid'));
154 $this->assertEquals($this->messagesentareaid . '-' . $message->id, $doc->get('id'));
155 $this->assertEquals(SITEID, $doc->get('courseid'));
156 $this->assertEquals($message->useridfrom, $doc->get('owneruserid'));
157 $this->assertEquals($message->useridto, $doc->get('userid'));
158 $this->assertEquals(content_to_text($message->subject, false), $doc->get('title'));
159 $this->assertEquals(content_to_text($message->smallmessage, false), $doc->get('content'));
160 }
161
162 /**
163 * Document accesses.
164 *
165 * @return void
166 */
167 public function test_message_sent_access() {
168 global $CFG;
169
170 // Returns the instance as long as the area is supported.
171 $searcharea = \core_search\manager::get_search_area($this->messagesentareaid);
172
173 $user1 = self::getDataGenerator()->create_user();
174 $user2 = self::getDataGenerator()->create_user();
175 $user3 = self::getDataGenerator()->create_user();
176
177 $this->preventResetByRollback();
178 $sink = $this->redirectMessages();
179
880fc15b
EM
180 $message = new \core\message\message();
181 $message->courseid = SITEID;
26c0d75f
DG
182 $message->userfrom = $user1;
183 $message->userto = $user2;
184 $message->subject = "Test Subject";
185 $message->smallmessage = "Test small messsage";
186 $message->fullmessage = "Test full messsage";
187 $message->fullmessageformat = 0;
188 $message->fullmessagehtml = null;
189 $message->notification = 0;
190 $message->component = "moodle";
191 $message->name = "instantmessage";
192
193 $messageid = message_send($message);
194
195 $messages = $sink->get_messages();
196 $message = $messages[0];
197
198 $this->setUser($user1);
199
200 if ($CFG->messaging) {
201 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($messageid));
202 } else {
203 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
204 }
205
206 $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access(-123));
207
208 message_delete_message($message, $user1->id);
209 $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($messageid));
210
211 $this->setUser($user2);
212 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
213
214 $this->setUser($user3);
215 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
216
217 $this->setGuestUser();
218 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
219
220 $this->setAdminUser();
221 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
222
223 delete_user($user2);
224
225 $this->setUser($user1);
226 $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($messageid));
227
228 }
bc60052d
MP
229
230 /**
231 * Test sent deleted user.
232 * Tests the case where a sent message for a deleted user
233 * is attempted to be added to the index.
234 *
235 * @return void
236 */
237 public function test_message_sent_deleted_user() {
238
239 // Returns the instance as long as the area is supported.
240 $searcharea = \core_search\manager::get_search_area($this->messagesentareaid);
241 $this->assertInstanceOf('\core_message\search\message_sent', $searcharea);
242
243 $user1 = self::getDataGenerator()->create_user();
244 $user2 = self::getDataGenerator()->create_user();
245
246 $this->preventResetByRollback();
247 $sink = $this->redirectMessages();
248
249 $message = new \core\message\message();
250 $message->courseid = SITEID;
251 $message->userfrom = $user1;
252 $message->userto = $user2;
253 $message->subject = "Test Subject";
254 $message->smallmessage = "Test small messsage";
255 $message->fullmessage = "Test full messsage";
256 $message->fullmessageformat = 0;
257 $message->fullmessagehtml = null;
258 $message->notification = 0;
259 $message->component = "moodle";
260 $message->name = "instantmessage";
261
262 message_send($message);
263
264 $messages = $sink->get_messages();
265 $message = $messages[0];
266
267 // Delete user.
268 delete_user($user1);
269
270 $doc = $searcharea->get_document($message);
271
272 $this->assertFalse($doc);
273
274 }
26c0d75f 275}