MDL-54708 message: notification popover respects message preferences
[moodle.git] / message / tests / search_test_received.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 * received 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 received messages 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_received_search_testcase extends advanced_testcase {
38
39 /**
40 * @var string Area id
41 */
42 protected $messagereceivedareaid = 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->messagereceivedareaid = \core_search\manager::generate_areaid('core_message', 'message_received');
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_received_indexing() {
64
65 // Returns the instance as long as the area is supported.
66 $searcharea = \core_search\manager::get_search_area($this->messagereceivedareaid);
67 $this->assertInstanceOf('\core_message\search\message_received', $searcharea);
68
69 $user1 = self::getDataGenerator()->create_user();
70 $user2 = self::getDataGenerator()->create_user();
71
72 $this->preventResetByRollback();
73 $sink = $this->redirectMessages();
74
75 $message = new StdClass();
76 $message->userfrom = $user1;
77 $message->userto = $user2;
78 $message->subject = "Test Subject";
79 $message->smallmessage = "Test small messsage";
80 $message->fullmessage = "Test full messsage";
81 $message->fullmessageformat = 0;
82 $message->fullmessagehtml = null;
83 $message->notification = 0;
84 $message->component = "moodle";
85 $message->name = "instantmessage";
86
87 message_send($message);
88
89 $messages = $sink->get_messages();
90
91 $this->assertEquals(1, count($messages));
92
93 // All records.
94 $recordset = $searcharea->get_recordset_by_timestamp(0);
95 $this->assertTrue($recordset->valid());
96 $nrecords = 0;
97 foreach ($recordset as $record) {
98 $this->assertInstanceOf('stdClass', $record);
99 $doc = $searcharea->get_document($record);
100 $this->assertInstanceOf('\core_search\document', $doc);
101 $nrecords++;
102 }
103 // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
104 $recordset->close();
105 $this->assertEquals(1, $nrecords);
106
107 // The +2 is to prevent race conditions.
108 $recordset = $searcharea->get_recordset_by_timestamp(time() + 2);
109
110 // No new records.
111 $this->assertFalse($recordset->valid());
112 $recordset->close();
113 }
114
115 /**
116 * Document contents.
117 *
118 * @return void
119 */
120 public function test_message_received_document() {
121
122 // Returns the instance as long as the area is supported.
123 $searcharea = \core_search\manager::get_search_area($this->messagereceivedareaid);
124 $this->assertInstanceOf('\core_message\search\message_received', $searcharea);
125
126 $user1 = self::getDataGenerator()->create_user();
127 $user2 = self::getDataGenerator()->create_user();
128
129 $this->preventResetByRollback();
130 $sink = $this->redirectMessages();
131
132 $message = new StdClass();
133 $message->userfrom = $user1;
134 $message->userto = $user2;
135 $message->subject = "Test Subject";
136 $message->smallmessage = "Test small messsage";
137 $message->fullmessage = "Test full messsage";
138 $message->fullmessageformat = 0;
139 $message->fullmessagehtml = null;
140 $message->notification = 0;
141 $message->component = "moodle";
142 $message->name = "instantmessage";
143
144 message_send($message);
145
146 $messages = $sink->get_messages();
147 $message = $messages[0];
148
149 $doc = $searcharea->get_document($message);
150 $this->assertInstanceOf('\core_search\document', $doc);
151 $this->assertEquals($message->id, $doc->get('itemid'));
152 $this->assertEquals($this->messagereceivedareaid . '-' . $message->id, $doc->get('id'));
153 $this->assertEquals(SITEID, $doc->get('courseid'));
154 $this->assertEquals($message->useridfrom, $doc->get('userid'));
155 $this->assertEquals($message->useridto, $doc->get('owneruserid'));
156 $this->assertEquals(content_to_text($message->subject, false), $doc->get('title'));
157 $this->assertEquals(content_to_text($message->smallmessage, false), $doc->get('content'));
158 }
159
160 /**
161 * Document accesses.
162 *
163 * @return void
164 */
165 public function test_message_received_access() {
166 global $CFG;
167
168 // Returns the instance as long as the area is supported.
169 $searcharea = \core_search\manager::get_search_area($this->messagereceivedareaid);
170
171 $user1 = self::getDataGenerator()->create_user();
172 $user2 = self::getDataGenerator()->create_user();
173 $user3 = self::getDataGenerator()->create_user();
174
175 $this->preventResetByRollback();
176 $sink = $this->redirectMessages();
177
178 $message = new StdClass();
179 $message->userfrom = $user1;
180 $message->userto = $user2;
181 $message->subject = "Test Subject";
182 $message->smallmessage = "Test small messsage";
183 $message->fullmessage = "Test full messsage";
184 $message->fullmessageformat = 0;
185 $message->fullmessagehtml = null;
186 $message->notification = 0;
187 $message->component = "moodle";
188 $message->name = "instantmessage";
189
190 $messageid = message_send($message);
191
192 $messages = $sink->get_messages();
193 $message = $messages[0];
194
195 $this->setUser($user1);
196 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
197 $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access(-123));
198
199 $this->setUser($user2);
200 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($messageid));
201
202 if ($CFG->messaging) {
203 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($messageid));
204 } else {
205 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
206 }
207
208 message_delete_message($message, $user2->id);
209 $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($messageid));
210
211 $this->setUser($user3);
212 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
213
214 $this->setGuestUser();
215 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
216
217 $this->setAdminUser();
218 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
219
220 delete_user($user1);
221
222 $this->setUser($user2);
223 $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($messageid));
224
225 }
226}