MDL-53643 search_solr: Setting correctly test server data
[moodle.git] / search / engine / solr / tests / engine_test.php
CommitLineData
95c6aeaf
DM
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 * Solr earch engine base unit tests.
19 *
20 * Required params:
21 * - define('TEST_SEARCH_SOLR_HOSTNAME', '127.0.0.1');
22 * - define('TEST_SEARCH_SOLR_PORT', '8983');
23 * - define('TEST_SEARCH_SOLR_INDEXNAME', 'unittest');
24 *
25 * Optional params:
26 * - define('TEST_SEARCH_SOLR_USERNAME', '');
27 * - define('TEST_SEARCH_SOLR_PASSWORD', '');
5dc4624c
EM
28 * - define('TEST_SEARCH_SOLR_SSLCERT', '');
29 * - define('TEST_SEARCH_SOLR_SSLKEY', '');
30 * - define('TEST_SEARCH_SOLR_KEYPASSWORD', '');
31 * - define('TEST_SEARCH_SOLR_CAINFOCERT', '');
95c6aeaf
DM
32 *
33 * @package core_search
34 * @category phpunit
35 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38
39defined('MOODLE_INTERNAL') || die();
40
41global $CFG;
42require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
43require_once($CFG->dirroot . '/search/tests/fixtures/mock_search_area.php');
44
45/**
46 * Solr search engine base unit tests.
47 *
48 * @package core_search
49 * @category phpunit
50 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
51 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
52 */
53class search_solr_engine_testcase extends advanced_testcase {
54
55 /**
56 * @var \core_search::manager
57 */
58 protected $search = null;
59
60 public function setUp() {
61 $this->resetAfterTest();
62 set_config('enableglobalsearch', true);
63
64 if (!function_exists('solr_get_version')) {
65 $this->markTestSkipped('Solr extension is not loaded.');
66 }
67
68 if (!defined('TEST_SEARCH_SOLR_HOSTNAME') || !defined('TEST_SEARCH_SOLR_INDEXNAME') ||
69 !defined('TEST_SEARCH_SOLR_PORT')) {
70 $this->markTestSkipped('Solr extension test server not set.');
71 }
72
bd5d2c60
DM
73 set_config('server_hostname', TEST_SEARCH_SOLR_HOSTNAME, 'search_solr');
74 set_config('server_port', TEST_SEARCH_SOLR_PORT, 'search_solr');
95c6aeaf
DM
75 set_config('indexname', TEST_SEARCH_SOLR_INDEXNAME, 'search_solr');
76
77 if (defined('TEST_SEARCH_SOLR_USERNAME')) {
5dc4624c 78 set_config('server_username', TEST_SEARCH_SOLR_USERNAME, 'search_solr');
95c6aeaf
DM
79 }
80
81 if (defined('TEST_SEARCH_SOLR_PASSWORD')) {
5dc4624c 82 set_config('server_password', TEST_SEARCH_SOLR_PASSWORD, 'search_solr');
95c6aeaf
DM
83 }
84
5dc4624c
EM
85 if (defined('TEST_SEARCH_SOLR_SSLCERT')) {
86 set_config('secure', true, 'search_solr');
87 set_config('ssl_cert', TEST_SEARCH_SOLR_SSLCERT, 'search_solr');
88 }
89
90 if (defined('TEST_SEARCH_SOLR_SSLKEY')) {
91 set_config('ssl_key', TEST_SEARCH_SOLR_SSLKEY, 'search_solr');
92 }
93
94 if (defined('TEST_SEARCH_SOLR_KEYPASSWORD')) {
95 set_config('ssl_keypassword', TEST_SEARCH_SOLR_KEYPASSWORD, 'search_solr');
96 }
97
98 if (defined('TEST_SEARCH_SOLR_CAINFOCERT')) {
99 set_config('ssl_cainfo', TEST_SEARCH_SOLR_CAINFOCERT, 'search_solr');
100 }
101
102
95c6aeaf
DM
103 // Inject search solr engine into the testable core search as we need to add the mock
104 // search component to it.
105 $searchengine = new \search_solr\engine();
106 $this->search = testable_core_search::instance($searchengine);
107 $areaid = \core_search\manager::generate_areaid('core_mocksearch', 'role_capabilities');
108 $this->search->add_search_area($areaid, new core_mocksearch\search\role_capabilities());
109
110 $this->setAdminUser();
111
112 // Cleanup before doing anything on it as the index it is out of this test control.
113 $this->search->delete_index();
114
115 // Add moodle fields if they don't exist.
116 $schema = new \search_solr\schema();
117 $schema->setup(false);
118 }
119
120 public function test_connection() {
121 $this->assertTrue($this->search->get_engine()->is_server_ready());
122 }
123
124 public function test_index() {
125 global $DB;
126
127 $noneditingteacherid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
128
129 // Data gets into the search engine.
130 $this->assertTrue($this->search->index());
131
132 // Not anymore as everything was already added.
133 sleep(1);
134 $this->assertFalse($this->search->index());
135
136 assign_capability('moodle/course:renameroles', CAP_ALLOW, $noneditingteacherid, context_system::instance()->id);
137 accesslib_clear_all_caches_for_unit_testing();
138
139 // Indexing again once there is new data.
140 $this->assertTrue($this->search->index());
141 }
142
143 /**
144 * Better keep this not very strict about which or how many results are returned as may depend on solr engine config.
145 *
146 * @return void
147 */
148 public function test_search() {
149 global $USER, $DB;
150
151 $noneditingteacherid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
152
153 $this->search->index();
154
155 $querydata = new stdClass();
156 $querydata->q = 'message';
157 $results = $this->search->search($querydata);
158 $this->assertCount(2, $results);
159
160 // Based on core_mocksearch\search\indexer.
161 $this->assertEquals($USER->id, $results[0]->get('userid'));
162 $this->assertEquals(\context_system::instance()->id, $results[0]->get('contextid'));
163
164 // Testing filters we don't purge cache in between assertions because cache key depends on the whole filters set
165 // and they are different.
166 sleep(1);
167 $beforeadding = time();
168 sleep(1);
169 assign_capability('moodle/course:renameroles', CAP_ALLOW, $noneditingteacherid, context_system::instance()->id);
170 accesslib_clear_all_caches_for_unit_testing();
171 $this->search->index();
172
173 // Timestart.
174 $querydata->timestart = $beforeadding;
175 $this->assertCount(1, $this->search->search($querydata));
176
177 // Timeend.
178 unset($querydata->timestart);
179 $querydata->timeend = $beforeadding;
180 $this->assertCount(2, $this->search->search($querydata));
181
182 // Title.
183 unset($querydata->timeend);
184 $querydata->title = 'moodle/course:renameroles roleid 1';
185 $this->assertCount(1, $this->search->search($querydata));
186 }
187
188 public function test_delete() {
189 $this->search->index();
190
191 $querydata = new stdClass();
192 $querydata->q = 'message';
193
194 $this->assertCount(2, $this->search->search($querydata));
195
196 $areaid = \core_search\manager::generate_areaid('core_mocksearch', 'role_capabilities');
197 $this->search->delete_index($areaid);
198 cache_helper::purge_by_definition('core', 'search_results');
199 $this->assertCount(0, $this->search->search($querydata));
200 }
f6b425e2
EM
201
202 public function test_alloweduserid() {
203 $engine = $this->search->get_engine();
204 $area = new core_mocksearch\search\role_capabilities();
205
206 // Get the first record for the recordset.
207 $recordset = $area->get_recordset_by_timestamp();
208 foreach ($recordset as $r) {
209 $record = $r;
210 break;
211 }
212 $recordset->close();
213
214 // Get the doc and insert the default doc.
215 $doc = $area->get_document($record);
216 $engine->add_document($doc->export_for_engine());
217
218 $users = array();
219 $users[] = $this->getDataGenerator()->create_user();
220 $users[] = $this->getDataGenerator()->create_user();
221 $users[] = $this->getDataGenerator()->create_user();
222
223 // Add a record that only user 100 can see.
224 $originalid = $doc->get('id');
225
226 // Now add a custom doc for each user.
227 foreach ($users as $user) {
228 $doc->set('id', $originalid.'-'.$user->id);
229 $doc->set('owneruserid', $user->id);
230 $engine->add_document($doc->export_for_engine());
231 }
232
0aa3fedb 233 $engine->area_index_complete($area->get_area_id());
f6b425e2
EM
234
235 $querydata = new stdClass();
236 $querydata->q = 'message';
237 $querydata->title = $doc->get('title');
238
239 // We are going to go through each user and see if they get the original and the owned doc.
240 foreach ($users as $user) {
241 $this->setUser($user);
242
243 $results = $this->search->search($querydata);
244 $this->assertCount(2, $results);
245
246 $owned = 0;
247 $notowned = 0;
248
249 // We don't know what order we will get the results in, so we are doing this.
250 foreach ($results as $result) {
251 $owneruserid = $result->get('owneruserid');
252 if (empty($owneruserid)) {
253 $notowned++;
254 $this->assertEquals(0, $owneruserid);
255 $this->assertEquals($originalid, $result->get('id'));
256 } else {
257 $owned++;
258 $this->assertEquals($user->id, $owneruserid);
259 $this->assertEquals($originalid.'-'.$user->id, $result->get('id'));
260 }
261 }
262
263 $this->assertEquals(1, $owned);
264 $this->assertEquals(1, $notowned);
265 }
266
267 // Now test a user with no owned results.
268 $otheruser = $this->getDataGenerator()->create_user();
269 $this->setUser($otheruser);
270
271 $results = $this->search->search($querydata);
272 $this->assertCount(1, $results);
273
274 $this->assertEquals(0, $results[0]->get('owneruserid'));
275 $this->assertEquals($originalid, $results[0]->get('id'));
276 }
95c6aeaf 277}