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_activity_test extends \advanced_testcase {
1 efrain 33
    /**
34
     * @var \core_search::manager
35
     */
36
    protected $search = null;
37
 
38
    /**
39
     * @var \core_search_generator 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
    /** @var \context[] Array of test contexts */
49
    protected $contexts;
50
 
51
    /** @var \stdClass[] Array of test forum objects */
52
    protected $forums;
53
 
54
    public function setUp(): void {
55
        global $DB;
1441 ariadna 56
        parent::setUp();
1 efrain 57
        $this->resetAfterTest();
58
        set_config('enableglobalsearch', true);
59
 
60
        // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
61
        $search = \testable_core_search::instance();
62
 
63
        $this->generator = self::getDataGenerator()->get_plugin_generator('core_search');
64
        $this->generator->setup();
65
 
66
        $this->setAdminUser();
67
 
68
        // Create course and 2 forums.
69
        $generator = $this->getDataGenerator();
70
        $course = $generator->create_course();
71
        $this->contexts['c1'] = \context_course::instance($course->id);
72
        $this->forums[1] = $generator->create_module('forum', ['course' => $course->id, 'name' => 'Forum 1',
73
                'intro' => '<p>Intro 1</p>', 'introformat' => FORMAT_HTML]);
74
        $this->contexts['f1'] = \context_module::instance($this->forums[1]->cmid);
75
        $this->forums[2] = $generator->create_module('forum', ['course' => $course->id, 'name' => 'Forum 2',
76
                'intro' => '<p>Intro 2</p>', 'introformat' => FORMAT_HTML]);
77
        $this->contexts['f2'] = \context_module::instance($this->forums[2]->cmid);
78
 
79
        // Create another 2 courses (in same category and in a new category) with one forum each.
80
        $this->contexts['cc1']  = \context_coursecat::instance($course->category);
81
        $course2 = $generator->create_course();
82
        $this->contexts['c2'] = \context_course::instance($course2->id);
83
        $this->forums[3] = $generator->create_module('forum', ['course' => $course2->id, 'name' => 'Forum 3',
84
                'intro' => '<p>Intro 3</p>', 'introformat' => FORMAT_HTML]);
85
        $this->contexts['f3'] = \context_module::instance($this->forums[3]->cmid);
86
        $cat2 = $generator->create_category();
87
        $this->contexts['cc2'] = \context_coursecat::instance($cat2->id);
88
        $course3 = $generator->create_course(['category' => $cat2->id]);
89
        $this->contexts['c3'] = \context_course::instance($course3->id);
90
        $this->forums[4] = $generator->create_module('forum', ['course' => $course3->id, 'name' => 'Forum 4',
91
                'intro' => '<p>Intro 4</p>', 'introformat' => FORMAT_HTML]);
92
        $this->contexts['f4'] = \context_module::instance($this->forums[4]->cmid);
93
 
94
        // Hack about with the time modified values.
95
        foreach ($this->forums as $index => $forum) {
96
            $DB->set_field('forum', 'timemodified', $index, ['id' => $forum->id]);
97
        }
98
    }
99
 
100
    public function tearDown(): void {
101
        // For unit tests before PHP 7, teardown is called even on skip. So only do our teardown if we did setup.
102
        if ($this->generator) {
103
            // Moodle DML freaks out if we don't teardown the temp table after each run.
104
            $this->generator->teardown();
105
            $this->generator = null;
106
        }
1441 ariadna 107
        parent::tearDown();
1 efrain 108
    }
109
 
110
    /**
111
     * Test base activity get search fileareas
112
     */
11 efrain 113
    public function test_get_search_fileareas_base(): void {
1 efrain 114
 
115
        $builder = $this->getMockBuilder('\core_search\base_activity');
116
        $builder->disableOriginalConstructor();
117
        $stub = $builder->getMockForAbstractClass();
118
 
119
        $result = $stub->get_search_fileareas();
120
 
121
        $this->assertEquals(array('intro'), $result);
122
    }
123
 
124
    /**
125
     * Test base attach files
126
     */
11 efrain 127
    public function test_attach_files_base(): void {
1 efrain 128
        $filearea = 'intro';
129
        $component = 'mod_forum';
130
        $module = 'forum';
131
 
132
        $course = self::getDataGenerator()->create_course();
133
        $activity = self::getDataGenerator()->create_module('forum', array('course' => $course->id));
134
        $context = \context_module::instance($activity->cmid);
135
        $contextid = $context->id;
136
 
137
        // Create file to add.
138
        $fs = get_file_storage();
139
        $filerecord = array(
140
                'contextid' => $contextid,
141
                'component' => $component,
142
                'filearea' => $filearea,
143
                'itemid' => 0,
144
                'filepath' => '/',
145
                'filename' => 'testfile.txt');
146
        $content = 'All the news that\'s fit to print';
147
        $file = $fs->create_file_from_string($filerecord, $content);
148
 
149
        // Construct the search document.
150
        $rec = new \stdClass();
151
        $rec->courseid = $course->id;
152
        $area = new \core_mocksearch\search\mock_search_area();
153
        $record = $this->generator->create_record($rec);
154
 
155
        $document = $area->get_document($record);
156
        $document->set('itemid', $activity->id);
157
 
158
        // Create a mock from the abstract class,
159
        // with required methods stubbed.
160
        $builder = $this->getMockBuilder('\core_search\base_activity');
161
        $builder->disableOriginalConstructor();
162
        $builder->onlyMethods(array('get_module_name', 'get_component_name'));
163
        $stub = $builder->getMockForAbstractClass();
164
        $stub->method('get_module_name')->willReturn($module);
165
        $stub->method('get_component_name')->willReturn($component);
166
 
167
        // Attach file to our test document.
168
        $stub->attach_files($document);
169
 
170
        // Verify file is attached.
171
        $files = $document->get_files();
172
        $file = array_values($files)[0];
173
 
174
        $this->assertEquals(1, count($files));
175
        $this->assertEquals($content, $file->get_content());
176
    }
177
 
178
    /**
179
     * Tests getting the recordset.
180
     */
11 efrain 181
    public function test_get_document_recordset(): void {
1 efrain 182
        global $USER, $DB;
183
 
184
        // Get all the forums to index (no restriction).
185
        $area = new \mod_forum\search\activity();
186
        $results = self::recordset_to_indexed_array($area->get_document_recordset());
187
 
188
        // Should return all forums.
189
        $this->assertCount(4, $results);
190
 
191
        // Each result should basically have the contents of the forum table. We'll just check
192
        // the key fields for the first one and then the other ones by id only.
193
        $this->assertEquals($this->forums[1]->id, $results[0]->id);
194
        $this->assertEquals(1, $results[0]->timemodified);
195
        $this->assertEquals($this->forums[1]->course, $results[0]->course);
196
        $this->assertEquals('Forum 1', $results[0]->name);
197
        $this->assertEquals('<p>Intro 1</p>', $results[0]->intro);
198
        $this->assertEquals(FORMAT_HTML, $results[0]->introformat);
199
 
200
        $allids = self::records_to_ids($this->forums);
201
        $this->assertEquals($allids, self::records_to_ids($results));
202
 
203
        // Repeat with a time restriction.
204
        $results = self::recordset_to_indexed_array($area->get_document_recordset(3));
205
        $this->assertEquals([$this->forums[3]->id, $this->forums[4]->id],
206
                self::records_to_ids($results));
207
 
208
        // Now use context restrictions. First, the whole site (no change).
209
        $results = self::recordset_to_indexed_array($area->get_document_recordset(
210
                0, \context_system::instance()));
211
        $this->assertEquals($allids, self::records_to_ids($results));
212
 
213
        // Course 1 only.
214
        $results = self::recordset_to_indexed_array($area->get_document_recordset(
215
                0, $this->contexts['c1']));
216
        $this->assertEquals([$this->forums[1]->id, $this->forums[2]->id],
217
                self::records_to_ids($results));
218
 
219
        // Course 2 only.
220
        $results = self::recordset_to_indexed_array($area->get_document_recordset(
221
                0, $this->contexts['c2']));
222
        $this->assertEquals([$this->forums[3]->id], self::records_to_ids($results));
223
 
224
        // Specific forum only.
225
        $results = self::recordset_to_indexed_array($area->get_document_recordset(
226
                0, $this->contexts['f4']));
227
        $this->assertEquals([$this->forums[4]->id], self::records_to_ids($results));
228
 
229
        // Category 1 context (courses 1 and 2).
230
        $results = self::recordset_to_indexed_array($area->get_document_recordset(
231
                0, $this->contexts['cc1']));
232
        $this->assertEquals([$this->forums[1]->id, $this->forums[2]->id, $this->forums[3]->id],
233
                self::records_to_ids($results));
234
 
235
        // Category 2 context (course 3).
236
        $results = self::recordset_to_indexed_array($area->get_document_recordset(
237
                0, $this->contexts['cc2']));
238
        $this->assertEquals([$this->forums[4]->id], self::records_to_ids($results));
239
 
240
        // Combine context restriction (category 1) with timemodified.
241
        $results = self::recordset_to_indexed_array($area->get_document_recordset(
242
                2, $this->contexts['cc1']));
243
        $this->assertEquals([$this->forums[2]->id, $this->forums[3]->id],
244
                self::records_to_ids($results));
245
 
246
        // Find an arbitrary block on the system to get a block context.
247
        $blockid = array_values($DB->get_records('block_instances', null, 'id', 'id', 0, 1))[0]->id;
248
        $blockcontext = \context_block::instance($blockid);
249
 
250
        // Block context (cannot return anything, so always null).
251
        $this->assertNull($area->get_document_recordset(0, $blockcontext));
252
 
253
        // User context (cannot return anything, so always null).
254
        $usercontext = \context_user::instance($USER->id);
255
        $this->assertNull($area->get_document_recordset(0, $usercontext));
256
    }
257
 
258
    /**
259
     * Utility function to convert recordset to array for testing.
260
     *
261
     * @param \moodle_recordset $rs Recordset to convert
262
     * @return array Array indexed by number (0, 1, 2, ...)
263
     */
264
    protected static function recordset_to_indexed_array(\moodle_recordset $rs) {
265
        $results = [];
266
        foreach ($rs as $rec) {
267
            $results[] = $rec;
268
        }
269
        $rs->close();
270
        return $results;
271
    }
272
 
273
    /**
274
     * Utility function to convert records to array of IDs.
275
     *
276
     * @param array $recs Records which should have an 'id' field
277
     * @return array Array of ids
278
     */
279
    protected static function records_to_ids(array $recs) {
280
        $ids = [];
281
        foreach ($recs as $rec) {
282
            $ids[] = $rec->id;
283
        }
284
        return $ids;
285
    }
286
 
287
    /**
288
     * Tests the get_doc_url function.
289
     */
11 efrain 290
    public function test_get_doc_url(): void {
1 efrain 291
        $area = new \mod_forum\search\activity();
292
        $results = self::recordset_to_indexed_array($area->get_document_recordset());
293
 
294
        for ($i = 0; $i < 4; $i++) {
295
            $this->assertEquals(new \moodle_url('/mod/forum/view.php',
296
                    ['id' => $this->forums[$i + 1]->cmid]),
297
                    $area->get_doc_url($area->get_document($results[$i])));
298
        }
299
    }
300
 
301
    /**
302
     * Tests the check_access function.
303
     */
11 efrain 304
    public function test_check_access(): void {
1 efrain 305
        global $CFG;
306
        require_once($CFG->dirroot . '/course/lib.php');
307
 
308
        // Create a test user who can access courses 1 and 2 (everything except forum 4).
309
        $generator = $this->getDataGenerator();
310
        $user = $generator->create_user();
311
        $generator->enrol_user($user->id, $this->forums[1]->course, 'student');
312
        $generator->enrol_user($user->id, $this->forums[3]->course, 'student');
313
        $this->setUser($user);
314
 
315
        // Delete forum 2 and set forum 3 hidden.
316
        course_delete_module($this->forums[2]->cmid);
317
        set_coursemodule_visible($this->forums[3]->cmid, 0);
318
 
319
        // Call check access on all the first three.
320
        $area = new \mod_forum\search\activity();
321
        $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $area->check_access(
322
                $this->forums[1]->id));
323
        $this->assertEquals(\core_search\manager::ACCESS_DELETED, $area->check_access(
324
                $this->forums[2]->id));
325
        $this->assertEquals(\core_search\manager::ACCESS_DENIED, $area->check_access(
326
                $this->forums[3]->id));
327
 
328
        // Note: Do not check forum 4 which is in a course the user can't access; this will return
329
        // ACCESS_GRANTED, but it does not matter because the search engine will not have included
330
        // that context in the list to search. (This is because the $cm->uservisible access flag
331
        // is only valid if the user is known to be able to access the course.)
332
    }
333
 
334
    /**
335
     * Tests the module version of get_contexts_to_reindex, which is supposed to return all the
336
     * activity contexts in order of date added.
337
     */
11 efrain 338
    public function test_get_contexts_to_reindex(): void {
1 efrain 339
        global $DB;
340
 
341
        $this->resetAfterTest();
342
 
343
        // Set up a course with two URLs and a Page.
344
        $generator = $this->getDataGenerator();
345
        $course = $generator->create_course(['fullname' => 'TCourse']);
346
        $url1 = $generator->create_module('url', ['course' => $course->id, 'name' => 'TURL1']);
347
        $url2 = $generator->create_module('url', ['course' => $course->id, 'name' => 'TURL2']);
348
        $page = $generator->create_module('page', ['course' => $course->id, 'name' => 'TPage1']);
349
 
350
        // Hack the items so they have different added times.
351
        $now = time();
352
        $DB->set_field('course_modules', 'added', $now - 3, ['id' => $url2->cmid]);
353
        $DB->set_field('course_modules', 'added', $now - 2, ['id' => $url1->cmid]);
354
        $DB->set_field('course_modules', 'added', $now - 1, ['id' => $page->cmid]);
355
 
356
        // Check the URL contexts are in date order.
357
        $urlarea = new \mod_url\search\activity();
358
        $contexts = iterator_to_array($urlarea->get_contexts_to_reindex(), false);
359
        $this->assertEquals([\context_module::instance($url1->cmid),
360
                \context_module::instance($url2->cmid)], $contexts);
361
 
362
        // Check the Page contexts.
363
        $pagearea = new \mod_page\search\activity();
364
        $contexts = iterator_to_array($pagearea->get_contexts_to_reindex(), false);
365
        $this->assertEquals([\context_module::instance($page->cmid)], $contexts);
366
 
367
        // Check another module area that has no instances.
368
        $glossaryarea = new \mod_glossary\search\activity();
369
        $contexts = iterator_to_array($glossaryarea->get_contexts_to_reindex(), false);
370
        $this->assertEquals([], $contexts);
371
    }
372
 
373
    /**
374
     * Test document icon.
375
     */
11 efrain 376
    public function test_get_doc_icon(): void {
1 efrain 377
        $baseactivity = $this->getMockBuilder('\core_search\base_activity')
378
            ->disableOriginalConstructor()
379
            ->onlyMethods(array('get_module_name'))
380
            ->getMockForAbstractClass();
381
 
382
        $baseactivity->method('get_module_name')->willReturn('test_activity');
383
 
384
        $document = $this->getMockBuilder('\core_search\document')
385
            ->disableOriginalConstructor()
386
            ->getMock();
387
 
388
        $result = $baseactivity->get_doc_icon($document);
389
 
390
        $this->assertEquals('monologo', $result->get_name());
391
        $this->assertEquals('test_activity', $result->get_component());
392
    }
393
}