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
21 * @copyright 2017 Matt Porritt <mattp@catalyst-au.net>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 require_once(__DIR__
. '/fixtures/testable_core_search.php');
29 require_once($CFG->dirroot
. '/search/tests/fixtures/mock_search_area.php');
32 * Search engine base unit tests.
34 * @package core_search
35 * @copyright 2017 Matt Porritt <mattp@catalyst-au.net>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class search_base_testcase
extends advanced_testcase
{
40 * @var \core_search::manager
42 protected $search = null;
45 * @var Instace of core_search_generator.
47 protected $generator = null;
50 * @var Instace of testable_engine.
52 protected $engine = null;
54 public function setUp() {
55 $this->resetAfterTest();
56 set_config('enableglobalsearch', true);
58 // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
59 $search = testable_core_search
::instance();
61 $this->generator
= self
::getDataGenerator()->get_plugin_generator('core_search');
62 $this->generator
->setup();
65 public function tearDown() {
66 // For unit tests before PHP 7, teardown is called even on skip. So only do our teardown if we did setup.
67 if ($this->generator
) {
68 // Moodle DML freaks out if we don't teardown the temp table after each run.
69 $this->generator
->teardown();
70 $this->generator
= null;
75 * Test base get search fileareas
77 public function test_get_search_fileareas_base() {
79 $builder = $this->getMockBuilder('\core_search\base');
80 $builder->disableOriginalConstructor();
81 $stub = $builder->getMockForAbstractClass();
83 $result = $stub->get_search_fileareas();
85 $this->assertEquals(array(), $result);
89 * Test base attach files
91 public function test_attach_files_base() {
93 $component = 'mod_test';
95 // Create file to add.
96 $fs = get_file_storage();
99 'component' => $component,
100 'filearea' => $filearea,
103 'filename' => 'testfile.txt');
104 $content = 'All the news that\'s fit to print';
105 $file = $fs->create_file_from_string($filerecord, $content);
107 // Construct the search document.
108 $rec = new \
stdClass();
110 $area = new core_mocksearch\search\
mock_search_area();
111 $record = $this->generator
->create_record($rec);
112 $document = $area->get_document($record);
114 // Create a mock from the abstract class,
115 // with required methods stubbed.
116 $builder = $this->getMockBuilder('\core_search\base');
117 $builder->disableOriginalConstructor();
118 $builder->setMethods(array('get_search_fileareas', 'get_component_name'));
119 $stub = $builder->getMockForAbstractClass();
120 $stub->method('get_search_fileareas')->willReturn(array($filearea));
121 $stub->method('get_component_name')->willReturn($component);
123 // Attach file to our test document.
124 $stub->attach_files($document);
126 // Verify file is attached.
127 $files = $document->get_files();
128 $file = array_values($files)[0];
130 $this->assertEquals(1, count($files));
131 $this->assertEquals($content, $file->get_content());
135 * Tests the base version (stub) of get_contexts_to_reindex.
137 public function test_get_contexts_to_reindex() {
138 $area = new core_mocksearch\search\
mock_search_area();
139 $this->assertEquals([\context_system
::instance()],
140 iterator_to_array($area->get_contexts_to_reindex(), false));