Merge branch 'MDL-70422-310' of https://github.com/paulholden/moodle into MOODLE_310_...
[moodle.git] / search / tests / base_test.php
CommitLineData
4e921569
MP
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.
13//
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/>.
16
17/**
18 * Search engine base unit tests.
19 *
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
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
28require_once(__DIR__ . '/fixtures/testable_core_search.php');
29require_once($CFG->dirroot . '/search/tests/fixtures/mock_search_area.php');
30
31/**
32 * Search engine base unit tests.
33 *
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
37 */
38class search_base_testcase extends advanced_testcase {
39 /**
40 * @var \core_search::manager
41 */
42 protected $search = null;
43
44 /**
45 * @var Instace of core_search_generator.
46 */
47 protected $generator = null;
48
49 /**
50 * @var Instace of testable_engine.
51 */
52 protected $engine = null;
53
d81a9480 54 public function setUp(): void {
4e921569
MP
55 $this->resetAfterTest();
56 set_config('enableglobalsearch', true);
57
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();
60
61 $this->generator = self::getDataGenerator()->get_plugin_generator('core_search');
62 $this->generator->setup();
63 }
64
d81a9480 65 public function tearDown(): void {
4e921569
MP
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;
71 }
72 }
73
74 /**
75 * Test base get search fileareas
76 */
77 public function test_get_search_fileareas_base() {
78
79 $builder = $this->getMockBuilder('\core_search\base');
80 $builder->disableOriginalConstructor();
81 $stub = $builder->getMockForAbstractClass();
82
83 $result = $stub->get_search_fileareas();
84
85 $this->assertEquals(array(), $result);
86 }
87
88 /**
89 * Test base attach files
90 */
91 public function test_attach_files_base() {
92 $filearea = 'search';
93 $component = 'mod_test';
94
95 // Create file to add.
96 $fs = get_file_storage();
97 $filerecord = array(
98 'contextid' => 1,
99 'component' => $component,
100 'filearea' => $filearea,
101 'itemid' => 1,
102 'filepath' => '/',
103 'filename' => 'testfile.txt');
104 $content = 'All the news that\'s fit to print';
105 $file = $fs->create_file_from_string($filerecord, $content);
106
107 // Construct the search document.
108 $rec = new \stdClass();
cfa00fc5 109 $rec->contextid = 1;
4e921569
MP
110 $area = new core_mocksearch\search\mock_search_area();
111 $record = $this->generator->create_record($rec);
112 $document = $area->get_document($record);
113
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);
122
123 // Attach file to our test document.
124 $stub->attach_files($document);
125
126 // Verify file is attached.
127 $files = $document->get_files();
128 $file = array_values($files)[0];
129
130 $this->assertEquals(1, count($files));
131 $this->assertEquals($content, $file->get_content());
132 }
25564a78 133
134 /**
135 * Tests the base version (stub) of get_contexts_to_reindex.
136 */
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));
141 }
66f145ef
DM
142
143 /**
144 * Test default document icon.
145 */
146 public function test_get_default_doc_icon() {
147 $basearea = $this->getMockBuilder('\core_search\base')
148 ->disableOriginalConstructor()
149 ->getMockForAbstractClass();
150
151 $document = $this->getMockBuilder('\core_search\document')
152 ->disableOriginalConstructor()
153 ->getMock();
154
155 $result = $basearea->get_doc_icon($document);
156
157 $this->assertEquals('i/empty', $result->get_name());
158 $this->assertEquals('moodle', $result->get_component());
159 }
e9074ee3
DM
160
161 /**
162 * Test base search area category names.
163 */
164 public function test_get_category_names() {
165 $builder = $this->getMockBuilder('\core_search\base');
166 $builder->disableOriginalConstructor();
167 $stub = $builder->getMockForAbstractClass();
168
b16bb491 169 $expected = ['core-other'];
e9074ee3
DM
170 $this->assertEquals($expected, $stub->get_category_names());
171 }
2085e860
DM
172
173 /**
174 * Test getting all required search area setting names.
175 */
176 public function test_get_settingnames() {
177 $expected = array('_enabled', '_indexingstart', '_indexingend', '_lastindexrun',
178 '_docsignored', '_docsprocessed', '_recordsprocessed', '_partial');
179 $this->assertEquals($expected, \core_search\base::get_settingnames());
180 }
4e921569 181}