Merge branch 'MDL-59039-master' of https://github.com/sammarshallou/moodle

This commit is contained in:
David Monllao 2017-07-11 21:49:03 +02:00
commit afabbd110e
13 changed files with 302 additions and 36 deletions

View file

@ -122,4 +122,8 @@ class mock_search_area extends \core_search\base {
public function get_context_url(\core_search\document $doc) {
return new \moodle_url('/index.php');
}
public function get_visible_name($lazyload = false) {
return 'Mock search area';
}
}

View file

@ -29,6 +29,12 @@ defined('MOODLE_INTERNAL') || die;
class engine extends \core_search\engine {
/** @var int If set, waits when adding each document (microseconds) */
protected $adddelay = 0;
/** @var \core_search\document[] Documents added */
protected $added = [];
public function is_installed() {
return true;
}
@ -38,7 +44,11 @@ class engine extends \core_search\engine {
}
public function add_document($document, $fileindexing = false) {
// No need to implement.
if ($this->adddelay) {
usleep($this->adddelay);
}
$this->added[] = $document;
return true;
}
public function execute_query($data, $usercontexts, $limit = 0) {
@ -64,4 +74,25 @@ class engine extends \core_search\engine {
public function get_query_total_count() {
return 0;
}
/**
* Sets an add delay to simulate time taken indexing.
*
* @param float $seconds Delay in seconds for each document
*/
public function set_add_delay($seconds) {
$this->adddelay = (int)($seconds * 1000000);
}
/**
* Gets the list of indexed (added) documents since last time this function
* was called.
*
* @return \core_search\document[] List of documents, in order added.
*/
public function get_and_clear_added_documents() {
$added = $this->added;
$this->added = [];
return $added;
}
}

View file

@ -116,6 +116,7 @@ class search_manager_testcase extends advanced_testcase {
$configs = $search->get_areas_config(array($this->forumpostareaid => $searcharea));
$this->assertEquals($start, $configs[$this->forumpostareaid]->indexingstart);
$this->assertEquals($end, $configs[$this->forumpostareaid]->indexingend);
$this->assertEquals(false, $configs[$this->forumpostareaid]->partial);
try {
$fakeareaid = \core_search\manager::generate_areaid('mod_unexisting', 'chihuaquita');
@ -132,6 +133,7 @@ class search_manager_testcase extends advanced_testcase {
$this->assertEquals(0, $config[$varname . '_indexingstart']);
$this->assertEquals(0, $config[$varname . '_indexingend']);
$this->assertEquals(0, $config[$varname . '_lastindexrun']);
$this->assertEquals(0, $config[$varname . '_partial']);
// No caching.
$configs = $search->get_areas_config(array($this->forumpostareaid => $searcharea));
$this->assertEquals(0, $configs[$this->forumpostareaid]->indexingstart);
@ -151,6 +153,114 @@ class search_manager_testcase extends advanced_testcase {
$this->assertEquals(0, $configs[$this->forumpostareaid]->indexingend);
}
/**
* Tests the get_last_indexing_duration method in the base area class.
*/
public function test_get_last_indexing_duration() {
$this->resetAfterTest();
$search = testable_core_search::instance();
$searcharea = $search->get_search_area($this->forumpostareaid);
// When never indexed, the duration is false.
$this->assertSame(false, $searcharea->get_last_indexing_duration());
// Set the start/end times.
list($componentname, $varname) = $searcharea->get_config_var_name();
$start = time() - 100;
$end = time();
set_config($varname . '_indexingstart', $start, $componentname);
set_config($varname . '_indexingend', $end, $componentname);
// The duration should now be 100.
$this->assertSame(100, $searcharea->get_last_indexing_duration());
}
/**
* Tests that partial indexing works correctly.
*/
public function test_partial_indexing() {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
// Create a course and a forum.
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$forum = $generator->create_module('forum', ['course' => $course->id]);
// Index everything up to current. Ensure the course is older than current second so it
// definitely doesn't get indexed again next time.
$this->waitForSecond();
$search = testable_core_search::instance();
$search->index(false, 0);
$searcharea = $search->get_search_area($this->forumpostareaid);
list($componentname, $varname) = $searcharea->get_config_var_name();
$this->assertFalse(get_config($componentname, $varname . '_partial'));
// Add 3 discussions to the forum.
$now = time();
$generator->get_plugin_generator('mod_forum')->create_discussion(['course' => $course->id,
'forum' => $forum->id, 'userid' => $USER->id, 'timemodified' => $now,
'name' => 'Frog']);
$generator->get_plugin_generator('mod_forum')->create_discussion(['course' => $course->id,
'forum' => $forum->id, 'userid' => $USER->id, 'timemodified' => $now + 1,
'name' => 'Toad']);
$generator->get_plugin_generator('mod_forum')->create_discussion(['course' => $course->id,
'forum' => $forum->id, 'userid' => $USER->id, 'timemodified' => $now + 2,
'name' => 'Zombie']);
time_sleep_until($now + 3);
// Clear the count of added documents.
$search->get_engine()->get_and_clear_added_documents();
// Make the search engine delay while indexing each document.
$search->get_engine()->set_add_delay(1.2);
// Index with a limit of 2 seconds - it should index 2 of the documents (after the second
// one, it will have taken 2.4 seconds so it will stop).
$search->index(false, 2);
$added = $search->get_engine()->get_and_clear_added_documents();
$this->assertCount(2, $added);
$this->assertEquals('Frog', $added[0]->get('title'));
$this->assertEquals('Toad', $added[1]->get('title'));
$this->assertEquals(1, get_config($componentname, $varname . '_partial'));
// Add a label.
$generator->create_module('label', ['course' => $course->id, 'intro' => 'Vampire']);
// Wait to next second (so as to not reindex the label more than once, as it will now
// be timed before the indexing run).
$this->waitForSecond();
// Next index with 1 second limit should do the label and not the forum - the logic is,
// if it spent ages indexing an area last time, do that one last on next run.
$search->index(false, 1);
$added = $search->get_engine()->get_and_clear_added_documents();
$this->assertCount(1, $added);
$this->assertEquals('Vampire', $added[0]->get('title'));
// Index again with a 2 second limit - it will redo last post for safety (because of other
// things possibly having the same time second), and then do the remaining one. (Note:
// because it always does more than one second worth of items, it would actually index 2
// posts even if the limit were less than 2.)
$search->index(false, 2);
$added = $search->get_engine()->get_and_clear_added_documents();
$this->assertCount(2, $added);
$this->assertEquals('Toad', $added[0]->get('title'));
$this->assertEquals('Zombie', $added[1]->get('title'));
$this->assertFalse(get_config($componentname, $varname . '_partial'));
// Index again - there should be nothing to index this time.
$search->index(false, 2);
$added = $search->get_engine()->get_and_clear_added_documents();
$this->assertCount(0, $added);
$this->assertFalse(get_config($componentname, $varname . '_partial'));
}
/**
* Adding this test here as get_areas_user_accesses process is the same, results just depend on the context level.
*