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/>.
18 * Sent message global search unit tests.
21 * @copyright 2016 Devang Gaur
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 require_once($CFG->dirroot
. '/search/tests/fixtures/testable_core_search.php');
31 * Provides the unit tests for sent message global search.
34 * @copyright 2016 Devang Gaur
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class message_sent_search_testcase
extends advanced_testcase
{
42 protected $messagesentareaid = null;
45 * Setting up the test environment
48 public function setUp() {
49 $this->resetAfterTest(true);
50 set_config('enableglobalsearch', true);
52 $this->messagesentareaid
= \core_search\manager
::generate_areaid('core_message', 'message_sent');
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();
59 * Indexing messages contents.
63 public function test_message_sent_indexing() {
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);
69 $user1 = self
::getDataGenerator()->create_user();
70 $user2 = self
::getDataGenerator()->create_user();
72 $this->preventResetByRollback();
73 $sink = $this->redirectMessages();
75 $message = new \core\message\
message();
76 $message->courseid
= SITEID
;
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";
88 message_send($message);
90 $messages = $sink->get_messages();
92 $this->assertEquals(1, count($messages));
95 $recordset = $searcharea->get_recordset_by_timestamp(0);
96 $this->assertTrue($recordset->valid());
98 foreach ($recordset as $record) {
99 $this->assertInstanceOf('stdClass', $record);
100 $doc = $searcharea->get_document($record);
101 $this->assertInstanceOf('\core_search\document', $doc);
104 // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
106 $this->assertEquals(1, $nrecords);
108 // The +2 is to prevent race conditions.
109 $recordset = $searcharea->get_recordset_by_timestamp(time() +
2);
112 $this->assertFalse($recordset->valid());
121 public function test_message_sent_document() {
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);
127 $user1 = self
::getDataGenerator()->create_user();
128 $user2 = self
::getDataGenerator()->create_user();
130 $this->preventResetByRollback();
131 $sink = $this->redirectMessages();
133 $message = new \core\message\
message();
134 $message->courseid
= SITEID
;
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";
146 message_send($message);
148 $messages = $sink->get_messages();
149 $message = $messages[0];
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'));
167 public function test_message_sent_access() {
170 // Returns the instance as long as the area is supported.
171 $searcharea = \core_search\manager
::get_search_area($this->messagesentareaid
);
173 $user1 = self
::getDataGenerator()->create_user();
174 $user2 = self
::getDataGenerator()->create_user();
175 $user3 = self
::getDataGenerator()->create_user();
177 $this->preventResetByRollback();
178 $sink = $this->redirectMessages();
180 $message = new \core\message\
message();
181 $message->courseid
= SITEID
;
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";
193 $messageid = message_send($message);
195 $messages = $sink->get_messages();
196 $message = $messages[0];
198 $this->setUser($user1);
200 if ($CFG->messaging
) {
201 $this->assertEquals(\core_search\manager
::ACCESS_GRANTED
, $searcharea->check_access($messageid));
203 $this->assertEquals(\core_search\manager
::ACCESS_DENIED
, $searcharea->check_access($messageid));
206 $this->assertEquals(\core_search\manager
::ACCESS_DELETED
, $searcharea->check_access(-123));
208 message_delete_message($message, $user1->id
);
209 $this->assertEquals(\core_search\manager
::ACCESS_DELETED
, $searcharea->check_access($messageid));
211 $this->setUser($user2);
212 $this->assertEquals(\core_search\manager
::ACCESS_DENIED
, $searcharea->check_access($messageid));
214 $this->setUser($user3);
215 $this->assertEquals(\core_search\manager
::ACCESS_DENIED
, $searcharea->check_access($messageid));
217 $this->setGuestUser();
218 $this->assertEquals(\core_search\manager
::ACCESS_DENIED
, $searcharea->check_access($messageid));
220 $this->setAdminUser();
221 $this->assertEquals(\core_search\manager
::ACCESS_DENIED
, $searcharea->check_access($messageid));
225 $this->setUser($user1);
226 $this->assertEquals(\core_search\manager
::ACCESS_DELETED
, $searcharea->check_access($messageid));