2016-03-02 00:03:03 +08:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple db search engine tests.
|
|
|
|
*
|
|
|
|
* @package search_simpledb
|
2017-07-25 15:56:16 +02:00
|
|
|
* @category test
|
2016-03-02 00:03:03 +08:00
|
|
|
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
|
|
|
|
require_once($CFG->dirroot . '/search/tests/fixtures/mock_search_area.php');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple search engine base unit tests.
|
|
|
|
*
|
|
|
|
* @package search_simpledb
|
2017-07-25 15:56:16 +02:00
|
|
|
* @category test
|
2016-03-02 00:03:03 +08:00
|
|
|
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
class search_simpledb_engine_testcase extends advanced_testcase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \core_search::manager
|
|
|
|
*/
|
|
|
|
protected $search = null;
|
|
|
|
|
2017-07-25 15:56:16 +02:00
|
|
|
/**
|
|
|
|
* @var \
|
|
|
|
*/
|
|
|
|
protected $engine = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var core_search_generator
|
|
|
|
*/
|
|
|
|
protected $generator = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initial stuff.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-03-02 00:03:03 +08:00
|
|
|
public function setUp() {
|
|
|
|
$this->resetAfterTest();
|
2017-07-25 15:56:16 +02:00
|
|
|
|
|
|
|
if ($this->requires_manual_index_update()) {
|
|
|
|
// We need to update fulltext index manually, which requires an alter table statement.
|
|
|
|
$this->preventResetByRollback();
|
|
|
|
}
|
|
|
|
|
2016-03-02 00:03:03 +08:00
|
|
|
set_config('enableglobalsearch', true);
|
|
|
|
|
|
|
|
// Inject search_simpledb engine into the testable core search as we need to add the mock
|
|
|
|
// search component to it.
|
2017-07-25 15:56:16 +02:00
|
|
|
|
|
|
|
$this->engine = new \search_simpledb\engine();
|
|
|
|
$this->search = testable_core_search::instance($this->engine);
|
|
|
|
$areaid = \core_search\manager::generate_areaid('core_mocksearch', 'mock_search_area');
|
|
|
|
$this->search->add_search_area($areaid, new core_mocksearch\search\mock_search_area());
|
|
|
|
|
|
|
|
$this->generator = self::getDataGenerator()->get_plugin_generator('core_search');
|
|
|
|
$this->generator->setup();
|
|
|
|
|
|
|
|
$this->setAdminUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tearDown
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function tearDown() {
|
|
|
|
// For unit tests before PHP 7, teardown is called even on skip. So only do our teardown if we did setup.
|
|
|
|
if ($this->generator) {
|
|
|
|
// Moodle DML freaks out if we don't teardown the temp table after each run.
|
|
|
|
$this->generator->teardown();
|
|
|
|
$this->generator = null;
|
|
|
|
}
|
2016-03-02 00:03:03 +08:00
|
|
|
}
|
|
|
|
|
2017-07-25 15:56:16 +02:00
|
|
|
/**
|
|
|
|
* Test indexing process.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-03-02 00:03:03 +08:00
|
|
|
public function test_index() {
|
|
|
|
global $DB;
|
|
|
|
|
2017-07-25 15:56:16 +02:00
|
|
|
$record = new \stdClass();
|
|
|
|
$record->timemodified = time() - 1;
|
|
|
|
$this->generator->create_record($record);
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
// Data gets into the search engine.
|
|
|
|
$this->assertTrue($this->search->index());
|
|
|
|
|
|
|
|
// Not anymore as everything was already added.
|
|
|
|
sleep(1);
|
|
|
|
$this->assertFalse($this->search->index());
|
|
|
|
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->generator->create_record();
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
// Indexing again once there is new data.
|
|
|
|
$this->assertTrue($this->search->index());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test search filters.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function test_search() {
|
|
|
|
global $USER, $DB;
|
|
|
|
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->generator->create_record();
|
|
|
|
$record = new \stdClass();
|
|
|
|
$record->title = "Special title";
|
|
|
|
$this->generator->create_record($record);
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
$this->search->index();
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->update_index();
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
$querydata = new stdClass();
|
|
|
|
$querydata->q = 'message';
|
|
|
|
$results = $this->search->search($querydata);
|
|
|
|
$this->assertCount(2, $results);
|
|
|
|
|
|
|
|
// Based on core_mocksearch\search\indexer.
|
|
|
|
$this->assertEquals($USER->id, $results[0]->get('userid'));
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->assertEquals(\context_course::instance(SITEID)->id, $results[0]->get('contextid'));
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
// Do a test to make sure we aren't searching non-query fields, like areaid.
|
2017-07-25 15:56:16 +02:00
|
|
|
$querydata->q = \core_search\manager::generate_areaid('core_mocksearch', 'mock_search_area');
|
2016-03-02 00:03:03 +08:00
|
|
|
$this->assertCount(0, $this->search->search($querydata));
|
|
|
|
$querydata->q = 'message';
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
$beforeadding = time();
|
|
|
|
sleep(1);
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->generator->create_record();
|
2016-03-02 00:03:03 +08:00
|
|
|
$this->search->index();
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->update_index();
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
// Timestart.
|
|
|
|
$querydata->timestart = $beforeadding;
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->assertCount(1, $this->search->search($querydata));
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
// Timeend.
|
|
|
|
unset($querydata->timestart);
|
|
|
|
$querydata->timeend = $beforeadding;
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->assertCount(2, $this->search->search($querydata));
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
// Title.
|
|
|
|
unset($querydata->timeend);
|
2017-07-25 15:56:16 +02:00
|
|
|
$querydata->title = 'Special title';
|
2016-03-02 00:03:03 +08:00
|
|
|
$this->assertCount(1, $this->search->search($querydata));
|
|
|
|
|
|
|
|
// Course IDs.
|
|
|
|
unset($querydata->title);
|
|
|
|
$querydata->courseids = array(SITEID + 1);
|
|
|
|
$this->assertCount(0, $this->search->search($querydata));
|
|
|
|
|
|
|
|
$querydata->courseids = array(SITEID);
|
|
|
|
$this->assertCount(3, $this->search->search($querydata));
|
|
|
|
|
2017-07-25 15:56:16 +02:00
|
|
|
// Now try some area-id combinations.
|
|
|
|
unset($querydata->courseids);
|
|
|
|
$forumpostareaid = \core_search\manager::generate_areaid('mod_forum', 'post');
|
|
|
|
$mockareaid = \core_search\manager::generate_areaid('core_mocksearch', 'mock_search_area');
|
|
|
|
|
|
|
|
$querydata->areaids = array($forumpostareaid);
|
|
|
|
$this->assertCount(0, $this->search->search($querydata));
|
|
|
|
|
|
|
|
$querydata->areaids = array($forumpostareaid, $mockareaid);
|
|
|
|
$this->assertCount(3, $this->search->search($querydata));
|
|
|
|
|
|
|
|
$querydata->areaids = array($mockareaid);
|
|
|
|
$this->assertCount(3, $this->search->search($querydata));
|
|
|
|
|
|
|
|
$querydata->areaids = array();
|
|
|
|
$this->assertCount(3, $this->search->search($querydata));
|
|
|
|
|
2016-03-02 00:03:03 +08:00
|
|
|
// Check that index contents get updated.
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->generator->delete_all();
|
2016-03-02 00:03:03 +08:00
|
|
|
$this->search->index(true);
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->update_index();
|
2016-03-02 00:03:03 +08:00
|
|
|
unset($querydata->title);
|
2017-07-25 15:56:16 +02:00
|
|
|
$querydata->q = '';
|
2016-03-02 00:03:03 +08:00
|
|
|
$this->assertCount(0, $this->search->search($querydata));
|
|
|
|
}
|
|
|
|
|
2017-07-25 15:56:16 +02:00
|
|
|
/**
|
|
|
|
* Test delete function
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-03-02 00:03:03 +08:00
|
|
|
public function test_delete() {
|
2017-07-25 15:56:16 +02:00
|
|
|
|
|
|
|
$this->generator->create_record();
|
|
|
|
$this->generator->create_record();
|
2016-03-02 00:03:03 +08:00
|
|
|
$this->search->index();
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->update_index();
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
$querydata = new stdClass();
|
|
|
|
$querydata->q = 'message';
|
|
|
|
|
|
|
|
$this->assertCount(2, $this->search->search($querydata));
|
|
|
|
|
2017-07-25 15:56:16 +02:00
|
|
|
$areaid = \core_search\manager::generate_areaid('core_mocksearch', 'mock_search_area');
|
2016-03-02 00:03:03 +08:00
|
|
|
$this->search->delete_index($areaid);
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->update_index();
|
2016-03-02 00:03:03 +08:00
|
|
|
$this->assertCount(0, $this->search->search($querydata));
|
|
|
|
}
|
|
|
|
|
2017-07-25 15:56:16 +02:00
|
|
|
/**
|
|
|
|
* Test user is allowed.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-03-02 00:03:03 +08:00
|
|
|
public function test_alloweduserid() {
|
2017-07-25 15:56:16 +02:00
|
|
|
|
|
|
|
$area = new core_mocksearch\search\mock_search_area();
|
|
|
|
|
|
|
|
$record = $this->generator->create_record();
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
// Get the doc and insert the default doc.
|
|
|
|
$doc = $area->get_document($record);
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->engine->add_document($doc);
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
$users = array();
|
|
|
|
$users[] = $this->getDataGenerator()->create_user();
|
|
|
|
$users[] = $this->getDataGenerator()->create_user();
|
|
|
|
$users[] = $this->getDataGenerator()->create_user();
|
|
|
|
|
|
|
|
// Add a record that only user 100 can see.
|
|
|
|
$originalid = $doc->get('id');
|
|
|
|
|
|
|
|
// Now add a custom doc for each user.
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$doc = $area->get_document($record);
|
|
|
|
$doc->set('id', $originalid.'-'.$user->id);
|
|
|
|
$doc->set('owneruserid', $user->id);
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->engine->add_document($doc);
|
2016-03-02 00:03:03 +08:00
|
|
|
}
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->update_index();
|
2016-03-02 00:03:03 +08:00
|
|
|
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->engine->area_index_complete($area->get_area_id());
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
$querydata = new stdClass();
|
|
|
|
$querydata->q = 'message';
|
|
|
|
$querydata->title = $doc->get('title');
|
|
|
|
|
|
|
|
// We are going to go through each user and see if they get the original and the owned doc.
|
|
|
|
foreach ($users as $user) {
|
|
|
|
$this->setUser($user);
|
|
|
|
|
|
|
|
$results = $this->search->search($querydata);
|
|
|
|
$this->assertCount(2, $results);
|
|
|
|
|
|
|
|
$owned = 0;
|
|
|
|
$notowned = 0;
|
|
|
|
|
|
|
|
// We don't know what order we will get the results in, so we are doing this.
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$owneruserid = $result->get('owneruserid');
|
|
|
|
if (empty($owneruserid)) {
|
|
|
|
$notowned++;
|
|
|
|
$this->assertEquals(0, $owneruserid);
|
|
|
|
$this->assertEquals($originalid, $result->get('id'));
|
|
|
|
} else {
|
|
|
|
$owned++;
|
|
|
|
$this->assertEquals($user->id, $owneruserid);
|
|
|
|
$this->assertEquals($originalid.'-'.$user->id, $result->get('id'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals(1, $owned);
|
|
|
|
$this->assertEquals(1, $notowned);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now test a user with no owned results.
|
|
|
|
$otheruser = $this->getDataGenerator()->create_user();
|
|
|
|
$this->setUser($otheruser);
|
|
|
|
|
|
|
|
$results = $this->search->search($querydata);
|
|
|
|
$this->assertCount(1, $results);
|
|
|
|
|
|
|
|
$this->assertEquals(0, $results[0]->get('owneruserid'));
|
|
|
|
$this->assertEquals($originalid, $results[0]->get('id'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_delete_by_id() {
|
2017-07-25 15:56:16 +02:00
|
|
|
|
|
|
|
$this->generator->create_record();
|
|
|
|
$this->generator->create_record();
|
2016-03-02 00:03:03 +08:00
|
|
|
$this->search->index();
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->update_index();
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
$querydata = new stdClass();
|
|
|
|
|
|
|
|
// Then search to make sure they are there.
|
2017-07-25 15:56:16 +02:00
|
|
|
$querydata->q = 'message';
|
2016-03-02 00:03:03 +08:00
|
|
|
$results = $this->search->search($querydata);
|
|
|
|
$this->assertCount(2, $results);
|
|
|
|
|
|
|
|
$first = reset($results);
|
|
|
|
$deleteid = $first->get('id');
|
|
|
|
|
2017-07-25 15:56:16 +02:00
|
|
|
$this->engine->delete_by_id($deleteid);
|
|
|
|
$this->update_index();
|
2016-03-02 00:03:03 +08:00
|
|
|
|
|
|
|
// Check that we don't get a result for it anymore.
|
|
|
|
$results = $this->search->search($querydata);
|
|
|
|
$this->assertCount(1, $results);
|
|
|
|
$result = reset($results);
|
|
|
|
$this->assertNotEquals($deleteid, $result->get('id'));
|
|
|
|
}
|
2017-07-25 15:56:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates mssql fulltext index if necessary.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function update_index() {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
if (!$this->requires_manual_index_update()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$DB->execute("ALTER FULLTEXT INDEX ON t_search_simpledb_index START UPDATE POPULATION");
|
|
|
|
|
|
|
|
$catalogname = $DB->get_prefix() . 'search_simpledb_catalog';
|
|
|
|
$retries = 0;
|
|
|
|
do {
|
|
|
|
// 0.2 seconds.
|
|
|
|
usleep(200000);
|
|
|
|
|
|
|
|
$record = $DB->get_record_sql("SELECT FULLTEXTCATALOGPROPERTY(cat.name, 'PopulateStatus') AS [PopulateStatus]
|
|
|
|
FROM sys.fulltext_catalogs AS cat
|
|
|
|
WHERE cat.name = ?", array($catalogname));
|
|
|
|
$retries++;
|
|
|
|
|
|
|
|
} while ($retries < 100 && $record->populatestatus != '0');
|
|
|
|
|
|
|
|
if ($retries === 100) {
|
|
|
|
// No update after 20 seconds...
|
|
|
|
$this->fail('Sorry, your SQL server fulltext search index is too slow.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mssql with fulltext support requires manual updates.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function requires_manual_index_update() {
|
|
|
|
global $DB;
|
|
|
|
return ($DB->get_dbfamily() === 'mssql' && $DB->is_fulltext_search_supported());
|
|
|
|
}
|
2016-03-02 00:03:03 +08:00
|
|
|
}
|