Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace search_simpledb;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
23
require_once($CFG->dirroot . '/search/tests/fixtures/mock_search_area.php');
24
 
25
/**
26
 * Simple search engine base unit tests.
27
 *
28
 * @package     search_simpledb
29
 * @category    test
30
 * @copyright   2016 David Monllao {@link http://www.davidmonllao.com}
31
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
1441 ariadna 33
final class engine_test extends \advanced_testcase {
1 efrain 34
 
35
    /**
36
     * @var \core_search::manager
37
     */
38
    protected $search = null;
39
 
40
    /**
41
     * @var \
42
     */
43
    protected $engine = null;
44
 
45
    /**
46
     * @var \core_search_generator
47
     */
48
    protected $generator = null;
49
 
50
    /**
51
     * Initial stuff.
52
     *
53
     * @return void
54
     */
55
    public function setUp(): void {
1441 ariadna 56
        parent::setUp();
1 efrain 57
        $this->resetAfterTest();
58
 
59
        if ($this->requires_manual_index_update()) {
60
            // We need to update fulltext index manually, which requires an alter table statement.
61
            $this->preventResetByRollback();
62
        }
63
 
64
        set_config('enableglobalsearch', true);
65
 
66
        // Inject search_simpledb engine into the testable core search as we need to add the mock
67
        // search component to it.
68
 
69
        $this->engine = new \search_simpledb\engine();
70
        $this->search = \testable_core_search::instance($this->engine);
71
 
72
        $this->generator = self::getDataGenerator()->get_plugin_generator('core_search');
73
        $this->generator->setup();
74
 
75
        $this->setAdminUser();
76
    }
77
 
78
    /**
79
     * tearDown
80
     *
81
     * @return void
82
     */
83
    public function tearDown(): void {
84
        // For unit tests before PHP 7, teardown is called even on skip. So only do our teardown if we did setup.
85
        if ($this->generator) {
86
            // Moodle DML freaks out if we don't teardown the temp table after each run.
87
            $this->generator->teardown();
88
            $this->generator = null;
89
        }
1441 ariadna 90
        parent::tearDown();
1 efrain 91
    }
92
 
93
    /**
94
     * Test indexing process.
95
     *
96
     * @return void
97
     */
11 efrain 98
    public function test_index(): void {
1 efrain 99
        global $DB;
100
 
101
        $this->add_mock_search_area();
102
 
103
        $record = new \stdClass();
104
        $record->timemodified = time() - 1;
105
        $this->generator->create_record($record);
106
 
107
        // Data gets into the search engine.
108
        $this->assertTrue($this->search->index());
109
 
110
        // Not anymore as everything was already added.
111
        sleep(1);
112
        $this->assertFalse($this->search->index());
113
 
114
        $this->generator->create_record();
115
 
116
        // Indexing again once there is new data.
117
        $this->assertTrue($this->search->index());
118
    }
119
 
120
    /**
121
     * Test search filters.
122
     *
123
     * @return void
124
     */
11 efrain 125
    public function test_search(): void {
1 efrain 126
        global $USER, $DB;
127
 
128
        $this->add_mock_search_area();
129
 
130
        $this->generator->create_record();
131
        $record = new \stdClass();
132
        $record->title = "Special title";
133
        $this->generator->create_record($record);
134
 
135
        $this->search->index();
136
        $this->update_index();
137
 
138
        $querydata = new \stdClass();
139
        $querydata->q = 'message';
140
        $results = $this->search->search($querydata);
141
        $this->assertCount(2, $results);
142
 
143
        // Based on core_mocksearch\search\indexer.
144
        $this->assertEquals($USER->id, $results[0]->get('userid'));
145
        $this->assertEquals(\context_course::instance(SITEID)->id, $results[0]->get('contextid'));
146
 
147
        // Do a test to make sure we aren't searching non-query fields, like areaid.
148
        $querydata->q = \core_search\manager::generate_areaid('core_mocksearch', 'mock_search_area');
149
        $this->assertCount(0, $this->search->search($querydata));
150
        $querydata->q = 'message';
151
 
152
        sleep(1);
153
        $beforeadding = time();
154
        sleep(1);
155
        $this->generator->create_record();
156
        $this->search->index();
157
        $this->update_index();
158
 
159
        // Timestart.
160
        $querydata->timestart = $beforeadding;
161
        $this->assertCount(1, $this->search->search($querydata));
162
 
163
        // Timeend.
164
        unset($querydata->timestart);
165
        $querydata->timeend = $beforeadding;
166
        $this->assertCount(2, $this->search->search($querydata));
167
 
168
        // Title.
169
        unset($querydata->timeend);
170
        $querydata->title = 'Special title';
171
        $this->assertCount(1, $this->search->search($querydata));
172
 
173
        // Course IDs.
174
        unset($querydata->title);
175
        $querydata->courseids = array(SITEID + 1);
176
        $this->assertCount(0, $this->search->search($querydata));
177
 
178
        $querydata->courseids = array(SITEID);
179
        $this->assertCount(3, $this->search->search($querydata));
180
 
181
        // Now try some area-id combinations.
182
        unset($querydata->courseids);
183
        $forumpostareaid = \core_search\manager::generate_areaid('mod_forum', 'post');
184
        $mockareaid = \core_search\manager::generate_areaid('core_mocksearch', 'mock_search_area');
185
 
186
        $querydata->areaids = array($forumpostareaid);
187
        $this->assertCount(0, $this->search->search($querydata));
188
 
189
        $querydata->areaids = array($forumpostareaid, $mockareaid);
190
        $this->assertCount(3, $this->search->search($querydata));
191
 
192
        $querydata->areaids = array($mockareaid);
193
        $this->assertCount(3, $this->search->search($querydata));
194
 
195
        $querydata->areaids = array();
196
        $this->assertCount(3, $this->search->search($querydata));
197
 
198
        // Check that index contents get updated.
199
        $this->generator->delete_all();
200
        $this->search->index(true);
201
        $this->update_index();
202
        unset($querydata->title);
203
        $querydata->q = '';
204
        $this->assertCount(0, $this->search->search($querydata));
205
    }
206
 
207
    /**
208
     * Test delete function
209
     *
210
     * @return void
211
     */
11 efrain 212
    public function test_delete(): void {
1 efrain 213
 
214
        $this->add_mock_search_area();
215
 
216
        $this->generator->create_record();
217
        $this->generator->create_record();
218
        $this->search->index();
219
        $this->update_index();
220
 
221
        $querydata = new \stdClass();
222
        $querydata->q = 'message';
223
 
224
        $this->assertCount(2, $this->search->search($querydata));
225
 
226
        $areaid = \core_search\manager::generate_areaid('core_mocksearch', 'mock_search_area');
227
        $this->search->delete_index($areaid);
228
        $this->update_index();
229
        $this->assertCount(0, $this->search->search($querydata));
230
    }
231
 
232
    /**
233
     * Test user is allowed.
234
     *
235
     * @return void
236
     */
11 efrain 237
    public function test_alloweduserid(): void {
1 efrain 238
 
239
        $this->add_mock_search_area();
240
 
241
        $area = new \core_mocksearch\search\mock_search_area();
242
 
243
        $record = $this->generator->create_record();
244
 
245
        // Get the doc and insert the default doc.
246
        $doc = $area->get_document($record);
247
        $this->engine->add_document($doc);
248
 
249
        $users = array();
250
        $users[] = $this->getDataGenerator()->create_user();
251
        $users[] = $this->getDataGenerator()->create_user();
252
        $users[] = $this->getDataGenerator()->create_user();
253
 
254
        // Add a record that only user 100 can see.
255
        $originalid = $doc->get('id');
256
 
257
        // Now add a custom doc for each user.
258
        foreach ($users as $user) {
259
            $doc = $area->get_document($record);
260
            $doc->set('id', $originalid.'-'.$user->id);
261
            $doc->set('owneruserid', $user->id);
262
            $this->engine->add_document($doc);
263
        }
264
        $this->update_index();
265
 
266
        $this->engine->area_index_complete($area->get_area_id());
267
 
268
        $querydata = new \stdClass();
269
        $querydata->q = 'message';
270
        $querydata->title = $doc->get('title');
271
 
272
        // We are going to go through each user and see if they get the original and the owned doc.
273
        foreach ($users as $user) {
274
            $this->setUser($user);
275
 
276
            $results = $this->search->search($querydata);
277
            $this->assertCount(2, $results);
278
 
279
            $owned = 0;
280
            $notowned = 0;
281
 
282
            // We don't know what order we will get the results in, so we are doing this.
283
            foreach ($results as $result) {
284
                $owneruserid = $result->get('owneruserid');
285
                if (empty($owneruserid)) {
286
                    $notowned++;
287
                    $this->assertEquals(0, $owneruserid);
288
                    $this->assertEquals($originalid, $result->get('id'));
289
                } else {
290
                    $owned++;
291
                    $this->assertEquals($user->id, $owneruserid);
292
                    $this->assertEquals($originalid.'-'.$user->id, $result->get('id'));
293
                }
294
            }
295
 
296
            $this->assertEquals(1, $owned);
297
            $this->assertEquals(1, $notowned);
298
        }
299
 
300
        // Now test a user with no owned results.
301
        $otheruser = $this->getDataGenerator()->create_user();
302
        $this->setUser($otheruser);
303
 
304
        $results = $this->search->search($querydata);
305
        $this->assertCount(1, $results);
306
 
307
        $this->assertEquals(0, $results[0]->get('owneruserid'));
308
        $this->assertEquals($originalid, $results[0]->get('id'));
309
    }
310
 
11 efrain 311
    public function test_delete_by_id(): void {
1 efrain 312
 
313
        $this->add_mock_search_area();
314
 
315
        $this->generator->create_record();
316
        $this->generator->create_record();
317
        $this->search->index();
318
        $this->update_index();
319
 
320
        $querydata = new \stdClass();
321
 
322
        // Then search to make sure they are there.
323
        $querydata->q = 'message';
324
        $results = $this->search->search($querydata);
325
        $this->assertCount(2, $results);
326
 
327
        $first = reset($results);
328
        $deleteid = $first->get('id');
329
 
330
        $this->engine->delete_by_id($deleteid);
331
        $this->update_index();
332
 
333
        // Check that we don't get a result for it anymore.
334
        $results = $this->search->search($querydata);
335
        $this->assertCount(1, $results);
336
        $result = reset($results);
337
        $this->assertNotEquals($deleteid, $result->get('id'));
338
    }
339
 
340
    /**
341
     * Tries out deleting data for a context or a course.
342
     */
11 efrain 343
    public function test_deleted_contexts_and_courses(): void {
1 efrain 344
        // Create some courses and activities.
345
        $generator = $this->getDataGenerator();
346
        $course1 = $generator->create_course(['fullname' => 'C1', 'summary' => 'xyzzy']);
347
        $course1page1 = $generator->create_module('page', ['course' => $course1, 'name' => 'C1P1', 'content' => 'xyzzy']);
348
        $generator->create_module('page', ['course' => $course1, 'name' => 'C1P2', 'content' => 'xyzzy']);
349
        $course2 = $generator->create_course(['fullname' => 'C2', 'summary' => 'xyzzy']);
350
        $course2page = $generator->create_module('page', ['course' => $course2, 'name' => 'C2P', 'content' => 'xyzzy']);
351
        $course2pagecontext = \context_module::instance($course2page->cmid);
352
 
353
        $this->search->index();
354
 
355
        // By default we have all data in the index.
356
        $this->assert_raw_index_contents('xyzzy', ['C1', 'C1P1', 'C1P2', 'C2', 'C2P']);
357
 
358
        // Say we delete the course2pagecontext...
359
        $this->engine->delete_index_for_context($course2pagecontext->id);
360
        $this->assert_raw_index_contents('xyzzy', ['C1', 'C1P1', 'C1P2', 'C2']);
361
 
362
        // Now delete the second course...
363
        $this->engine->delete_index_for_course($course2->id);
364
        $this->assert_raw_index_contents('xyzzy', ['C1', 'C1P1', 'C1P2']);
365
 
366
        // Finally let's delete using Moodle functions to check that works. Single context first.
367
        course_delete_module($course1page1->cmid);
368
        $this->assert_raw_index_contents('xyzzy', ['C1', 'C1P2']);
369
        delete_course($course1, false);
370
        $this->assert_raw_index_contents('xyzzy', []);
371
    }
372
 
373
    /**
374
     * Check the contents of the index.
375
     *
376
     * @param string $searchword Word to match within the content field
377
     * @param string[] $expected Array of expected result titles, in alphabetical order
378
     */
379
    protected function assert_raw_index_contents(string $searchword, array $expected) {
380
        global $DB;
381
        $results = $DB->get_records_select('search_simpledb_index',
382
                $DB->sql_like('content', '?'), ['%' . $searchword . '%'], "id, {$DB->sql_order_by_text('title')}");
383
        $titles = array_map(function($x) {
384
            return $x->title;
385
        }, $results);
386
        sort($titles);
387
        $this->assertEquals($expected, $titles);
388
    }
389
 
390
    /**
391
     * Adds a mock search area to the search system.
392
     */
393
    protected function add_mock_search_area() {
394
        $areaid = \core_search\manager::generate_areaid('core_mocksearch', 'mock_search_area');
395
        $this->search->add_search_area($areaid, new \core_mocksearch\search\mock_search_area());
396
    }
397
 
398
    /**
399
     * Updates mssql fulltext index if necessary.
400
     *
401
     * @return bool
402
     */
403
    private function update_index() {
404
        global $DB;
405
 
406
        if (!$this->requires_manual_index_update()) {
407
            return;
408
        }
409
 
410
        $DB->execute("ALTER FULLTEXT INDEX ON {search_simpledb_index} START UPDATE POPULATION");
411
 
412
        $catalogname = $DB->get_prefix() . 'search_simpledb_catalog';
413
        $retries = 0;
414
        do {
415
            // 0.2 seconds.
416
            usleep(200000);
417
 
418
            $record = $DB->get_record_sql("SELECT FULLTEXTCATALOGPROPERTY(cat.name, 'PopulateStatus') AS [PopulateStatus]
419
                                             FROM sys.fulltext_catalogs AS cat
420
                                            WHERE cat.name = ?", array($catalogname));
421
            $retries++;
422
 
423
        } while ($retries < 100 && $record->populatestatus != '0');
424
 
425
        if ($retries === 100) {
426
            // No update after 20 seconds...
427
            $this->fail('Sorry, your SQL server fulltext search index is too slow.');
428
        }
429
    }
430
 
431
    /**
432
     * Mssql with fulltext support requires manual updates.
433
     *
434
     * @return bool
435
     */
436
    private function requires_manual_index_update() {
437
        global $DB;
438
        return ($DB->get_dbfamily() === 'mssql' && $DB->is_fulltext_search_supported());
439
    }
440
}