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