2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Search engine base unit tests.
20 * @package core_search
22 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 require_once(__DIR__
. '/fixtures/testable_core_search.php');
31 * Search engine base unit tests.
33 * @package core_search
35 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class search_engine_testcase
extends advanced_testcase
{
40 public function setUp() {
41 $this->resetAfterTest();
42 set_config('enableglobalsearch', true);
44 // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
45 $search = testable_core_search
::instance();
53 public function test_engine_info() {
54 $engine = new \mock_search\
engine();
56 $this->assertEquals('mock_search', $engine->get_plugin_name());
58 // Resolves to the default one.
59 $this->assertEquals('\\core_search\\document', $engine->get_document_classname());
67 public function test_engine_caches() {
70 $engine = new \mock_search\
engine();
72 $course1 = self
::getDataGenerator()->create_course();
74 $this->assertEquals($course1->id
, $engine->get_course($course1->id
)->id
);
75 $dbreads = $DB->perf_get_reads();
76 $engine->get_course($course1->id
);
77 $this->assertEquals($dbreads, $DB->perf_get_reads());
78 $fakearea1 = \core_search\manager
::generate_areaid('plugintype_unexisting', 'fakearea');
79 $fakearea2 = \core_search\manager
::generate_areaid('mod_unexisting', 'morefake');
80 $this->assertFalse($engine->get_search_area($fakearea1));
81 $this->assertFalse($engine->get_search_area($fakearea2));
82 $this->assertFalse($engine->get_search_area($fakearea2));
84 $areaid = \core_search\manager
::generate_areaid('mod_forum', 'post');
85 $this->assertInstanceOf('\\mod_forum\\search\\post', $engine->get_search_area($areaid));
86 $dbreads = $DB->perf_get_reads();
87 $this->assertInstanceOf('\\mod_forum\\search\\post', $engine->get_search_area($areaid));
88 $this->assertEquals($dbreads, $DB->perf_get_reads());
93 * Tests the core functions related to schema updates.
95 public function test_engine_schema_modification() {
96 // Apply a schema update starting from no version.
97 $engine = new \mock_search\
engine();
98 $engine->check_latest_schema();
99 $updates = $engine->get_and_clear_schema_updates();
100 $this->assertCount(1, $updates);
101 $this->assertEquals(0, $updates[0][0]);
102 $this->assertEquals(\core_search\document
::SCHEMA_VERSION
, $updates[0][1]);
104 // Store older version and check that.
105 $engine->record_applied_schema_version(1066101400);
107 $engine = new \mock_search\
engine();
108 $engine->check_latest_schema();
109 $updates = $engine->get_and_clear_schema_updates();
110 $this->assertCount(1, $updates);
111 $this->assertEquals(1066101400, $updates[0][0]);
112 $this->assertEquals(\core_search\document
::SCHEMA_VERSION
, $updates[0][1]);
114 // Store current version and check no updates.
115 $engine->record_applied_schema_version(\core_search\document
::SCHEMA_VERSION
);
117 $engine = new \mock_search\
engine();
118 $engine->check_latest_schema();
119 $updates = $engine->get_and_clear_schema_updates();
120 $this->assertCount(0, $updates);
124 * Tests the get_supported_orders stub function.
126 public function test_get_supported_orders() {
127 $engine = new \mock_search\
engine();
128 $orders = $engine->get_supported_orders(\context_system
::instance());
129 $this->assertCount(1, $orders);
130 $this->assertArrayHasKey('relevance', $orders);