Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core_search;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once(__DIR__ . '/fixtures/testable_core_search.php');
23
require_once($CFG->dirroot . '/search/tests/fixtures/mock_search_area.php');
24
 
25
/**
26
 * Search engine base unit tests.
27
 *
28
 * @package     core_search
29
 * @copyright   2017 Matt Porritt <mattp@catalyst-au.net>
30
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
1441 ariadna 32
final class base_test extends \advanced_testcase {
1 efrain 33
    /**
34
     * @var \core_search::manager
35
     */
36
    protected $search = null;
37
 
38
    /**
39
     * @var Instace of core_search_generator.
40
     */
41
    protected $generator = null;
42
 
43
    /**
44
     * @var Instace of testable_engine.
45
     */
46
    protected $engine = null;
47
 
48
    public function setUp(): void {
1441 ariadna 49
        parent::setUp();
1 efrain 50
        $this->resetAfterTest();
51
        set_config('enableglobalsearch', true);
52
 
53
        // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
54
        $search = \testable_core_search::instance();
55
 
56
        $this->generator = self::getDataGenerator()->get_plugin_generator('core_search');
57
        $this->generator->setup();
58
    }
59
 
60
    public function tearDown(): void {
61
        // For unit tests before PHP 7, teardown is called even on skip. So only do our teardown if we did setup.
62
        if ($this->generator) {
63
            // Moodle DML freaks out if we don't teardown the temp table after each run.
64
            $this->generator->teardown();
65
            $this->generator = null;
66
        }
1441 ariadna 67
        parent::tearDown();
1 efrain 68
    }
69
 
70
    /**
71
     * Test base get search fileareas
72
     */
11 efrain 73
    public function test_get_search_fileareas_base(): void {
1 efrain 74
 
75
        $builder = $this->getMockBuilder('\core_search\base');
76
        $builder->disableOriginalConstructor();
77
        $stub = $builder->getMockForAbstractClass();
78
 
79
        $result = $stub->get_search_fileareas();
80
 
81
        $this->assertEquals(array(), $result);
82
    }
83
 
84
    /**
85
     * Test base attach files
86
     */
11 efrain 87
    public function test_attach_files_base(): void {
1 efrain 88
        $filearea = 'search';
89
        $component = 'mod_test';
90
 
91
        // Create file to add.
92
        $fs = get_file_storage();
93
        $filerecord = array(
94
                'contextid' => 1,
95
                'component' => $component,
96
                'filearea' => $filearea,
97
                'itemid' => 1,
98
                'filepath' => '/',
99
                'filename' => 'testfile.txt');
100
        $content = 'All the news that\'s fit to print';
101
        $file = $fs->create_file_from_string($filerecord, $content);
102
 
103
        // Construct the search document.
104
        $rec = new \stdClass();
105
        $rec->contextid = 1;
106
        $area = new \core_mocksearch\search\mock_search_area();
107
        $record = $this->generator->create_record($rec);
108
        $document = $area->get_document($record);
109
 
110
        // Create a mock from the abstract class,
111
        // with required methods stubbed.
112
        $builder = $this->getMockBuilder('\core_search\base');
113
        $builder->disableOriginalConstructor();
114
        $builder->onlyMethods(array('get_search_fileareas', 'get_component_name'));
115
        $stub = $builder->getMockForAbstractClass();
116
        $stub->method('get_search_fileareas')->willReturn(array($filearea));
117
        $stub->method('get_component_name')->willReturn($component);
118
 
119
        // Attach file to our test document.
120
        $stub->attach_files($document);
121
 
122
        // Verify file is attached.
123
        $files = $document->get_files();
124
        $file = array_values($files)[0];
125
 
126
        $this->assertEquals(1, count($files));
127
        $this->assertEquals($content, $file->get_content());
128
    }
129
 
130
    /**
131
     * Tests the base version (stub) of get_contexts_to_reindex.
132
     */
11 efrain 133
    public function test_get_contexts_to_reindex(): void {
1 efrain 134
        $area = new \core_mocksearch\search\mock_search_area();
135
        $this->assertEquals([\context_system::instance()],
136
                iterator_to_array($area->get_contexts_to_reindex(), false));
137
    }
138
 
139
    /**
140
     * Test default document icon.
141
     */
11 efrain 142
    public function test_get_default_doc_icon(): void {
1 efrain 143
        $basearea = $this->getMockBuilder('\core_search\base')
144
            ->disableOriginalConstructor()
145
            ->getMockForAbstractClass();
146
 
147
        $document = $this->getMockBuilder('\core_search\document')
148
            ->disableOriginalConstructor()
149
            ->getMock();
150
 
151
        $result = $basearea->get_doc_icon($document);
152
 
153
        $this->assertEquals('i/empty', $result->get_name());
154
        $this->assertEquals('moodle', $result->get_component());
155
    }
156
 
157
    /**
158
     * Test base search area category names.
159
     */
11 efrain 160
    public function test_get_category_names(): void {
1 efrain 161
        $builder = $this->getMockBuilder('\core_search\base');
162
        $builder->disableOriginalConstructor();
163
        $stub = $builder->getMockForAbstractClass();
164
 
165
        $expected = ['core-other'];
166
        $this->assertEquals($expected, $stub->get_category_names());
167
    }
168
 
169
    /**
170
     * Test getting all required search area setting names.
171
     */
11 efrain 172
    public function test_get_settingnames(): void {
1 efrain 173
        $expected = array('_enabled', '_indexingstart', '_indexingend', '_lastindexrun',
174
            '_docsignored', '_docsprocessed', '_recordsprocessed', '_partial');
175
        $this->assertEquals($expected, \core_search\base::get_settingnames());
176
    }
177
}