mirror of
https://github.com/moodle/moodle.git
synced 2025-08-10 03:16:42 +02:00
MDL-60981 core_search: Add get_contexts_to_reindex API
This new API returns a list of contexts for each search area. This allows the areas to be reindexed in a sensible order (roughly speaking, newest first) and also allows this to be controlled by each area. An implementation in the forum module means that forums are ordered by the date of the most recent discussion, so that active forums will be reindexed early even if they were created a long time ago.
This commit is contained in:
parent
9993c1d02c
commit
25564a784b
9 changed files with 302 additions and 0 deletions
|
@ -334,4 +334,43 @@ class search_base_activity_testcase extends advanced_testcase {
|
|||
// that context in the list to search. (This is because the $cm->uservisible access flag
|
||||
// is only valid if the user is known to be able to access the course.)
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the module version of get_contexts_to_reindex, which is supposed to return all the
|
||||
* activity contexts in order of date added.
|
||||
*/
|
||||
public function test_get_contexts_to_reindex() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Set up a course with two URLs and a Page.
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course(['fullname' => 'TCourse']);
|
||||
$url1 = $generator->create_module('url', ['course' => $course->id, 'name' => 'TURL1']);
|
||||
$url2 = $generator->create_module('url', ['course' => $course->id, 'name' => 'TURL2']);
|
||||
$page = $generator->create_module('page', ['course' => $course->id, 'name' => 'TPage1']);
|
||||
|
||||
// Hack the items so they have different added times.
|
||||
$now = time();
|
||||
$DB->set_field('course_modules', 'added', $now - 3, ['id' => $url2->cmid]);
|
||||
$DB->set_field('course_modules', 'added', $now - 2, ['id' => $url1->cmid]);
|
||||
$DB->set_field('course_modules', 'added', $now - 1, ['id' => $page->cmid]);
|
||||
|
||||
// Check the URL contexts are in date order.
|
||||
$urlarea = new \mod_url\search\activity();
|
||||
$contexts = iterator_to_array($urlarea->get_contexts_to_reindex(), false);
|
||||
$this->assertEquals([\context_module::instance($url1->cmid),
|
||||
\context_module::instance($url2->cmid)], $contexts);
|
||||
|
||||
// Check the Page contexts.
|
||||
$pagearea = new \mod_page\search\activity();
|
||||
$contexts = iterator_to_array($pagearea->get_contexts_to_reindex(), false);
|
||||
$this->assertEquals([\context_module::instance($page->cmid)], $contexts);
|
||||
|
||||
// Check another module area that has no instances.
|
||||
$glossaryarea = new \mod_glossary\search\activity();
|
||||
$contexts = iterator_to_array($glossaryarea->get_contexts_to_reindex(), false);
|
||||
$this->assertEquals([], $contexts);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -384,6 +384,52 @@ class base_block_testcase extends advanced_testcase {
|
|||
$this->assertEquals(\core_search\manager::ACCESS_DELETED, $area->check_access($blockid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the block version of get_contexts_to_reindex, which is supposed to return all the
|
||||
* block contexts in order of date added.
|
||||
*/
|
||||
public function test_get_contexts_to_reindex() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Create course and activity module.
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course();
|
||||
$coursecontext = \context_course::instance($course->id);
|
||||
$page = $generator->create_module('page', ['course' => $course->id]);
|
||||
$pagecontext = \context_module::instance($page->cmid);
|
||||
|
||||
// Create blocks on course page, with time modified non-sequential.
|
||||
$configdata = base64_encode(serialize(new \stdClass()));
|
||||
$instance = (object)['blockname' => 'mockblock', 'parentcontextid' => $coursecontext->id,
|
||||
'showinsubcontexts' => 0, 'pagetypepattern' => 'course-view-*', 'defaultweight' => 0,
|
||||
'timecreated' => 1, 'timemodified' => 100, 'configdata' => $configdata];
|
||||
$blockid1 = $DB->insert_record('block_instances', $instance);
|
||||
$context1 = \context_block::instance($blockid1);
|
||||
$instance->timemodified = 120;
|
||||
$blockid2 = $DB->insert_record('block_instances', $instance);
|
||||
$context2 = \context_block::instance($blockid2);
|
||||
$instance->timemodified = 110;
|
||||
$blockid3 = $DB->insert_record('block_instances', $instance);
|
||||
$context3 = \context_block::instance($blockid3);
|
||||
|
||||
// Create another block on the activity page (not included).
|
||||
$instance->parentcontextid = $pagecontext->id;
|
||||
$blockid4 = $DB->insert_record('block_instances', $instance);
|
||||
\context_block::instance($blockid4);
|
||||
|
||||
// Check list of contexts.
|
||||
$area = new block_mockblock\search\area();
|
||||
$contexts = iterator_to_array($area->get_contexts_to_reindex(), false);
|
||||
$expected = [
|
||||
$context2,
|
||||
$context3,
|
||||
$context1
|
||||
];
|
||||
$this->assertEquals($expected, $contexts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a search document object from the fake search area.
|
||||
*
|
||||
|
|
|
@ -130,4 +130,13 @@ class search_base_testcase extends advanced_testcase {
|
|||
$this->assertEquals(1, count($files));
|
||||
$this->assertEquals($content, $file->get_content());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the base version (stub) of get_contexts_to_reindex.
|
||||
*/
|
||||
public function test_get_contexts_to_reindex() {
|
||||
$area = new core_mocksearch\search\mock_search_area();
|
||||
$this->assertEquals([\context_system::instance()],
|
||||
iterator_to_array($area->get_contexts_to_reindex(), false));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue