Merge branch 'MDL-69101_39' of https://github.com/timhunt/moodle into MOODLE_39_STABLE
[moodle.git] / search / tests / engine_test.php
blob4fdba57f5a5a61adeaae76fbce88937afc3bcfb8
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.
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/>.
17 /**
18 * Search engine base unit tests.
20 * @package core_search
21 * @category phpunit
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');
29 require_once(__DIR__ . '/fixtures/mock_search_area.php');
31 /**
32 * Search engine base unit tests.
34 * @package core_search
35 * @category phpunit
36 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class search_engine_testcase extends advanced_testcase {
41 public function setUp() {
42 $this->resetAfterTest();
43 set_config('enableglobalsearch', true);
45 // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
46 $search = testable_core_search::instance();
49 /**
50 * Engine basic info.
52 * @return void
54 public function test_engine_info() {
55 $engine = new \mock_search\engine();
57 $this->assertEquals('mock_search', $engine->get_plugin_name());
59 // Resolves to the default one.
60 $this->assertEquals('\\core_search\\document', $engine->get_document_classname());
63 /**
64 * Test engine caches.
66 * @return void
68 public function test_engine_caches() {
69 global $DB;
71 $engine = new \mock_search\engine();
73 $course1 = self::getDataGenerator()->create_course();
75 $this->assertEquals($course1->id, $engine->get_course($course1->id)->id);
76 $dbreads = $DB->perf_get_reads();
77 $engine->get_course($course1->id);
78 $this->assertEquals($dbreads, $DB->perf_get_reads());
79 $fakearea1 = \core_search\manager::generate_areaid('plugintype_unexisting', 'fakearea');
80 $fakearea2 = \core_search\manager::generate_areaid('mod_unexisting', 'morefake');
81 $this->assertFalse($engine->get_search_area($fakearea1));
82 $this->assertFalse($engine->get_search_area($fakearea2));
83 $this->assertFalse($engine->get_search_area($fakearea2));
85 $areaid = \core_search\manager::generate_areaid('mod_forum', 'post');
86 $this->assertInstanceOf('\\mod_forum\\search\\post', $engine->get_search_area($areaid));
87 $dbreads = $DB->perf_get_reads();
88 $this->assertInstanceOf('\\mod_forum\\search\\post', $engine->get_search_area($areaid));
89 $this->assertEquals($dbreads, $DB->perf_get_reads());
93 /**
94 * Tests the core functions related to schema updates.
96 public function test_engine_schema_modification() {
97 // Apply a schema update starting from no version.
98 $engine = new \mock_search\engine();
99 $engine->check_latest_schema();
100 $updates = $engine->get_and_clear_schema_updates();
101 $this->assertCount(1, $updates);
102 $this->assertEquals(0, $updates[0][0]);
103 $this->assertEquals(\core_search\document::SCHEMA_VERSION, $updates[0][1]);
105 // Store older version and check that.
106 $engine->record_applied_schema_version(1066101400);
108 $engine = new \mock_search\engine();
109 $engine->check_latest_schema();
110 $updates = $engine->get_and_clear_schema_updates();
111 $this->assertCount(1, $updates);
112 $this->assertEquals(1066101400, $updates[0][0]);
113 $this->assertEquals(\core_search\document::SCHEMA_VERSION, $updates[0][1]);
115 // Store current version and check no updates.
116 $engine->record_applied_schema_version(\core_search\document::SCHEMA_VERSION);
118 $engine = new \mock_search\engine();
119 $engine->check_latest_schema();
120 $updates = $engine->get_and_clear_schema_updates();
121 $this->assertCount(0, $updates);
125 * Tests the get_supported_orders stub function.
127 public function test_get_supported_orders() {
128 $engine = new \mock_search\engine();
129 $orders = $engine->get_supported_orders(\context_system::instance());
130 $this->assertCount(1, $orders);
131 $this->assertArrayHasKey('relevance', $orders);
135 * Test that search engine sets an icon before render a document.
137 public function test_engine_sets_doc_icon() {
138 $generator = self::getDataGenerator()->get_plugin_generator('core_search');
139 $generator->setup();
141 $area = new core_mocksearch\search\mock_search_area();
142 $engine = new \mock_search\engine();
144 $record = $generator->create_record();
145 $docdata = $area->get_document($record)->export_for_engine();
147 $doc = $engine->to_document($area, $docdata);
149 $this->assertNotNull($doc->get_doc_icon());
151 $generator->teardown();