MDL-58543 upgrade: Replay 4 steps missed for some combinations
[moodle.git] / search / tests / engine_test.php
blob380ca7fd374487a1947342a72b4a56e654368a73
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');
30 /**
31 * Search engine base unit tests.
33 * @package core_search
34 * @category phpunit
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();
48 /**
49 * Engine basic info.
51 * @return void
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());
62 /**
63 * Test engine caches.
65 * @return void
67 public function test_engine_caches() {
68 global $DB;
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());