Merge branch 'MDL-63765-master' of git://github.com/damyon/moodle
[moodle.git] / search / tests / base_test.php
blob0d7cb18c1b5cbe8721fb91e2c82505c80ae6b6da
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 * @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();
27 global $CFG;
28 require_once(__DIR__ . '/fixtures/testable_core_search.php');
29 require_once($CFG->dirroot . '/search/tests/fixtures/mock_search_area.php');
31 /**
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 {
39 /**
40 * @var \core_search::manager
42 protected $search = null;
44 /**
45 * @var Instace of core_search_generator.
47 protected $generator = null;
49 /**
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;
74 /**
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);
88 /**
89 * Test base attach files
91 public function test_attach_files_base() {
92 $filearea = 'search';
93 $component = 'mod_test';
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);
107 // Construct the search document.
108 $rec = new \stdClass();
109 $rec->contextid = 1;
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));
144 * Test default document icon.
146 public function test_get_default_doc_icon() {
147 $basearea = $this->getMockBuilder('\core_search\base')
148 ->disableOriginalConstructor()
149 ->getMockForAbstractClass();
151 $document = $this->getMockBuilder('\core_search\document')
152 ->disableOriginalConstructor()
153 ->getMock();
155 $result = $basearea->get_doc_icon($document);
157 $this->assertEquals('i/empty', $result->get_name());
158 $this->assertEquals('moodle', $result->get_component());