MDL-54708 message: notification popover respects message preferences
[moodle.git] / message / tests / search_test_sent.php
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/>.
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  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
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  */
37 class message_sent_search_testcase extends advanced_testcase {
39     /**
40      * @var string Area id
41      */
42     protected $messagesentareaid = null;
44     /**
45      * Setting up the test environment
46      * @return void
47      */
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();
56     }
58     /**
59      * Indexing messages contents.
60      *
61      * @return void
62      */
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 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";
87         message_send($message);
89         $messages = $sink->get_messages();
91         $this->assertEquals(1, count($messages));
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);
107         // The +2 is to prevent race conditions.
108         $recordset = $searcharea->get_recordset_by_timestamp(time() + 2);
110         // No new records.
111         $this->assertFalse($recordset->valid());
112         $recordset->close();
113     }
115     /**
116      * Document contents.
117      *
118      * @return void
119      */
120     public function test_message_sent_document() {
122         // Returns the instance as long as the area is supported.
123         $searcharea = \core_search\manager::get_search_area($this->messagesentareaid);
124         $this->assertInstanceOf('\core_message\search\message_sent', $searcharea);
126         $user1 = self::getDataGenerator()->create_user();
127         $user2 = self::getDataGenerator()->create_user();
129         $this->preventResetByRollback();
130         $sink = $this->redirectMessages();
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";
144         message_send($message);
146         $messages = $sink->get_messages();
147         $message = $messages[0];
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->messagesentareaid . '-' . $message->id, $doc->get('id'));
153         $this->assertEquals(SITEID, $doc->get('courseid'));
154         $this->assertEquals($message->useridfrom, $doc->get('owneruserid'));
155         $this->assertEquals($message->useridto, $doc->get('userid'));
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     }
160     /**
161      * Document accesses.
162      *
163      * @return void
164      */
165     public function test_message_sent_access() {
166         global $CFG;
168         // Returns the instance as long as the area is supported.
169         $searcharea = \core_search\manager::get_search_area($this->messagesentareaid);
171         $user1 = self::getDataGenerator()->create_user();
172         $user2 = self::getDataGenerator()->create_user();
173         $user3 = self::getDataGenerator()->create_user();
175         $this->preventResetByRollback();
176         $sink = $this->redirectMessages();
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";
190         $messageid = message_send($message);
192         $messages = $sink->get_messages();
193         $message = $messages[0];
195         $this->setUser($user1);
197         if ($CFG->messaging) {
198             $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($messageid));
199         } else {
200             $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
201         }
203         $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access(-123));
205         message_delete_message($message, $user1->id);
206         $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($messageid));
208         $this->setUser($user2);
209         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
211         $this->setUser($user3);
212         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
214         $this->setGuestUser();
215         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
217         $this->setAdminUser();
218         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
220         delete_user($user2);
222         $this->setUser($user1);
223         $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($messageid));
225     }