forked from moodle/moodle
/
contentbank_test.php
674 lines (584 loc) · 25.6 KB
/
contentbank_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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
<?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/>.
/**
* Test for extensions manager.
*
* @package core_contentbank
* @category test
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_contentbank;
defined('MOODLE_INTERNAL') || die();
use advanced_testcase;
use context_block;
use context_course;
use context_coursecat;
use context_module;
use context_system;
use context_user;
use Exception;
global $CFG;
require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
/**
* Test for extensions manager.
*
* @package core_contentbank
* @category test
* @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_contentbank\contentbank
*/
class contentbank_test extends advanced_testcase {
/**
* Setup to ensure that fixtures are loaded.
*/
public static function setupBeforeClass(): void {
global $CFG;
require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
}
/**
* Data provider for test_get_extension_supporter.
*
* @return array
*/
public function get_extension_provider() {
return [
'H5P file' => ['something.h5p', '.h5p'],
'PDF file' => ['something.pdf', '.pdf']
];
}
/**
* Tests for get_extension() function.
*
* @dataProvider get_extension_provider
* @param string $filename The filename given
* @param string $expected The extension of the file
*
* @covers ::get_extension
*/
public function test_get_extension(string $filename, string $expected) {
$this->resetAfterTest();
$cb = new contentbank();
$extension = $cb->get_extension($filename);
$this->assertEquals($expected, $extension);
}
/**
* Data provider for test_load_context_supported_extensions.
*
* @return array
*/
public function get_extension_supporters_provider() {
return [
'H5P first' => [['.h5p' => ['h5p', 'testable']], '.h5p', 'h5p'],
'Testable first (but upload not implemented)' => [['.h5p' => ['testable', 'h5p']], '.h5p', 'h5p'],
];
}
/**
* Tests for get_extension_supporter() function with admin permissions.
*
* @dataProvider get_extension_supporters_provider
* @param array $supporters The content type plugin supporters for each extension
* @param string $extension The extension of the file given
* @param string $expected The supporter contenttype of the file
*
* @covers ::load_context_supported_extensions
*/
public function test_get_extension_supporter_for_admins(array $supporters, string $extension, string $expected) {
$this->resetAfterTest();
$cb = new contentbank();
$systemcontext = context_system::instance();
// All contexts allowed for admins.
$this->setAdminUser();
$contextsupporters = $cb->load_context_supported_extensions($systemcontext);
$this->assertArrayHasKey($extension, $contextsupporters);
$this->assertEquals($expected, $contextsupporters[$extension]);
}
/**
* Tests for get_extension_supporter() function with user default permissions.
*
* @dataProvider get_extension_supporters_provider
* @param array $supporters The content type plugin supporters for each extension
* @param string $extension The extension of the file given
* @param string $expected The supporter contenttype of the file
*
* @covers ::load_context_supported_extensions
*/
public function test_get_extension_supporter_for_users(array $supporters, string $extension, string $expected) {
$this->resetAfterTest();
$cb = new contentbank();
$systemcontext = context_system::instance();
// Set a user with no permissions.
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
// Users with no capabilities can't upload content.
$contextsupporters = $cb->load_context_supported_extensions($systemcontext);
$this->assertEquals([], $contextsupporters);
}
/**
* Tests for get_extension_supporter() function with teacher defaul permissions.
*
* @dataProvider get_extension_supporters_provider
* @param array $supporters The content type plugin supporters for each extension
* @param string $extension The extension of the file given
* @param string $expected The supporter contenttype of the file
*
* @covers ::load_context_supported_extensions
*/
public function test_get_extension_supporter_for_teachers(array $supporters, string $extension, string $expected) {
$this->resetAfterTest();
$cb = new contentbank();
$course = $this->getDataGenerator()->create_course();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$this->setUser($teacher);
$coursecontext = context_course::instance($course->id);
// Teachers has permission in their context to upload supported by H5P content type.
$contextsupporters = $cb->load_context_supported_extensions($coursecontext);
$this->assertArrayHasKey($extension, $contextsupporters);
$this->assertEquals($expected, $contextsupporters[$extension]);
}
/**
* Tests for get_extension_supporter() function.
*
* @dataProvider get_extension_supporters_provider
* @param array $supporters The content type plugin supporters for each extension
* @param string $extension The extension of the file given
* @param string $expected The supporter contenttype of the file
*
* @covers ::get_extension_supporter
*/
public function test_get_extension_supporter(array $supporters, string $extension, string $expected) {
$this->resetAfterTest();
$cb = new contentbank();
$systemcontext = context_system::instance();
$this->setAdminUser();
$supporter = $cb->get_extension_supporter($extension, $systemcontext);
$this->assertEquals($expected, $supporter);
}
/**
* Test the behaviour of search_contents().
*
* @dataProvider search_contents_provider
* @param string $search String to search.
* @param string $where Context where to search.
* @param int $expectedresult Expected result.
* @param array $contexts List of contexts where to create content.
*/
public function test_search_contents(?string $search, string $where, int $expectedresult, array $contexts = [],
array $contenttypes = null): void {
global $DB, $CFG;
$this->resetAfterTest();
$this->setAdminUser();
// Create users.
$managerroleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
$manager = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->role_assign($managerroleid, $manager->id);
// Create a category and a course.
$coursecat = $this->getDataGenerator()->create_category();
$course = $this->getDataGenerator()->create_course();
$existingcontexts = [];
$existingcontexts['system'] = \context_system::instance();
$existingcontexts['category'] = \context_coursecat::instance($coursecat->id);
$existingcontexts['course'] = \context_course::instance($course->id);
if (empty($where)) {
$contextid = 0;
} else {
$contextid = $existingcontexts[$where]->id;
}
// Add some content to the content bank.
$filepath = $CFG->dirroot . '/h5p/tests/fixtures/filltheblanks.h5p';
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
foreach ($contexts as $context) {
$contextinstance = $existingcontexts[$context];
$records = $generator->generate_contentbank_data('contenttype_h5p', 3,
$manager->id, $contextinstance, false, $filepath);
}
// Search for some content.
$cb = new contentbank();
$contents = $cb->search_contents($search, $contextid, $contenttypes);
$this->assertCount($expectedresult, $contents);
if (!empty($contents) && !empty($search)) {
foreach ($contents as $content) {
$this->assertStringContainsString($search, $content->get_name());
}
}
}
/**
* Data provider for test_search_contents().
*
* @return array
*/
public function search_contents_provider(): array {
return [
'Search all content in all contexts' => [
null,
'',
9,
['system', 'category', 'course']
],
'Search in all contexts for existing string in all contents' => [
'content',
'',
9,
['system', 'category', 'course']
],
'Search in all contexts for unexisting string in all contents' => [
'chocolate',
'',
0,
['system', 'category', 'course']
],
'Search in all contexts for existing string in some contents' => [
'1',
'',
3,
['system', 'category', 'course']
],
'Search in all contexts for existing string in some contents (create only 1 context)' => [
'1',
'',
1,
['system']
],
'Search in system context for existing string in all contents' => [
'content',
'system',
3,
['system', 'category', 'course']
],
'Search in category context for unexisting string in all contents' => [
'chocolate',
'category',
0,
['system', 'category', 'course']
],
'Search in course context for existing string in some contents' => [
'1',
'course',
1,
['system', 'category', 'course']
],
'Search in system context' => [
null,
'system',
3,
['system', 'category', 'course']
],
'Search in course context with existing content' => [
null,
'course',
3,
['system', 'category', 'course']
],
'Search in course context without existing content' => [
null,
'course',
0,
['system', 'category']
],
'Search in an empty contentbank' => [
null,
'',
0,
[]
],
'Search in a context in an empty contentbank' => [
null,
'system',
0,
[]
],
'Search for a string in an empty contentbank' => [
'content',
'',
0,
[]
],
'Search with unexisting content-type' => [
null,
'course',
0,
['system', 'category', 'course'],
['contenttype_unexisting'],
],
];
}
/**
* Test create_content_from_file function.
*
* @covers ::create_content_from_file
*/
public function test_create_content_from_file() {
global $USER, $CFG;
$this->resetAfterTest();
$this->setAdminUser();
$systemcontext = \context_system::instance();
$name = 'greeting-card.h5p';
// Create a dummy H5P file.
$dummyh5p = array(
'contextid' => $systemcontext->id,
'component' => 'contentbank',
'filearea' => 'public',
'itemid' => 1,
'filepath' => '/',
'filename' => $name,
'userid' => $USER->id
);
$path = $CFG->dirroot . '/h5p/tests/fixtures/' . $name;
$dummyh5pfile = \core_h5p\helper::create_fake_stored_file_from_path($path);
$cb = new contentbank();
$content = $cb->create_content_from_file($systemcontext, $USER->id, $dummyh5pfile);
$this->assertEquals('contenttype_h5p', $content->get_content_type());
$this->assertInstanceOf('\\contenttype_h5p\\content', $content);
$this->assertEquals($name, $content->get_name());
}
/**
* Test the behaviour of delete_contents().
*
* @covers ::delete_contents
*/
public function test_delete_contents() {
global $DB;
$this->resetAfterTest();
$cb = new \core_contentbank\contentbank();
// Create a category and two courses.
$systemcontext = context_system::instance();
$coursecat = $this->getDataGenerator()->create_category();
$coursecatcontext = context_coursecat::instance($coursecat->id);
$course1 = $this->getDataGenerator()->create_course();
$course1context = context_course::instance($course1->id);
$course2 = $this->getDataGenerator()->create_course();
$course2context = context_course::instance($course2->id);
// Add some content to the content bank.
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$systemcontent = $generator->generate_contentbank_data(null, 3, 0, $systemcontext);
$categorycontent = $generator->generate_contentbank_data(null, 3, 0, $coursecatcontext);
$course1content = $generator->generate_contentbank_data(null, 3, 0, $course1context);
$course2content = $generator->generate_contentbank_data(null, 3, 0, $course2context);
// Check the content has been created as expected.
$this->assertEquals(12, $DB->count_records('contentbank_content'));
// Check the system content is deleted as expected and the rest of the content is not.
$this->assertTrue($cb->delete_contents($systemcontext));
$this->assertEquals(0, $DB->count_records('contentbank_content', ['contextid' => $systemcontext->id]));
// And the rest of the context content exists.
$this->assertEquals(9, $DB->count_records('contentbank_content'));
// Check the course category content is deleted as expected and the rest of the content is not.
$this->assertTrue($cb->delete_contents($coursecatcontext));
$this->assertEquals(0, $DB->count_records('contentbank_content', ['contextid' => $coursecatcontext->id]));
// And the rest of the context content exists.
$this->assertEquals(6, $DB->count_records('contentbank_content'));
// Check the course content is deleted as expected and the rest of the content is not.
$this->assertTrue($cb->delete_contents($course1context));
$this->assertEquals(0, $DB->count_records('contentbank_content', ['contextid' => $course1context->id]));
// And the rest of the context content exists.
$this->assertEquals(3, $DB->count_records('contentbank_content'));
}
/**
* Test the behaviour of delete_contents() for empty content bank.
*
* @covers ::delete_contents
*/
public function test_delete_contents_for_empty_contentbank() {
$this->resetAfterTest();
$cb = new \core_contentbank\contentbank();
// Create a category and two courses.
$systemcontext = \context_system::instance();
$coursecat = $this->getDataGenerator()->create_category();
$coursecatcontext = \context_coursecat::instance($coursecat->id);
$course = $this->getDataGenerator()->create_course();
$coursecontext = \context_course::instance($course->id);
// Check there's no error when trying to delete content from an empty content bank.
$this->assertTrue($cb->delete_contents($systemcontext));
$this->assertTrue($cb->delete_contents($coursecatcontext));
$this->assertTrue($cb->delete_contents($coursecontext));
}
/**
* Test the behaviour of move_contents().
*
* @covers ::move_contents
*/
public function test_move_contents() {
global $DB;
$this->resetAfterTest();
$cb = new \core_contentbank\contentbank();
// Create a category and two courses.
$course1 = $this->getDataGenerator()->create_course();
$course1context = context_course::instance($course1->id);
$course2 = $this->getDataGenerator()->create_course();
$course2context = context_course::instance($course2->id);
// Add some content to the content bank.
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$course1content = $generator->generate_contentbank_data(null, 3, 0, $course1context);
$course2content = $generator->generate_contentbank_data(null, 3, 0, $course2context);
// Check the content has been created as expected.
$this->assertEquals(6, $DB->count_records('contentbank_content'));
$this->assertEquals(3, $DB->count_records('contentbank_content', ['contextid' => $course1context->id]));
// Check the content is moved to another context as expected and the rest of the content is not.
$this->assertTrue($cb->move_contents($course1context, $course2context));
$this->assertEquals(6, $DB->count_records('contentbank_content'));
$this->assertEquals(0, $DB->count_records('contentbank_content', ['contextid' => $course1context->id]));
$this->assertEquals(6, $DB->count_records('contentbank_content', ['contextid' => $course2context->id]));
}
/**
* Test the behaviour of move_contents() for empty content bank.
*
* @covers ::move_contents
*/
public function test_move_contents_for_empty_contentbank() {
$this->resetAfterTest();
$cb = new \core_contentbank\contentbank();
// Create a category and two courses.
$systemcontext = \context_system::instance();
$course = $this->getDataGenerator()->create_course();
$coursecontext = \context_course::instance($course->id);
// Check there's no error when trying to move content context from an empty content bank.
$this->assertTrue($cb->delete_contents($systemcontext, $coursecontext));
}
/**
* Data provider for get_contenttypes_with_capability_feature.
*
* @return array
*/
public function get_contenttypes_with_capability_feature_provider(): array {
return [
'no-contenttypes_enabled' => [
'contenttypesenabled' => [],
'contenttypescanfeature' => [],
],
'contenttype_enabled_noeditable' => [
'contenttypesenabled' => ['testable'],
'contenttypescanfeature' => [],
],
'contenttype_enabled_editable' => [
'contenttypesenabled' => ['testable'],
'contenttypescanfeature' => ['testable'],
],
'no-contenttype_enabled_editable' => [
'contenttypesenabled' => [],
'contenttypescanfeature' => ['testable'],
],
];
}
/**
* Tests for get_contenttypes_with_capability_feature() function.
*
* @dataProvider get_contenttypes_with_capability_feature_provider
* @param array $contenttypesenabled Content types enabled.
* @param array $contenttypescanfeature Content types the user has the permission to use the feature.
*
* @covers ::get_contenttypes_with_capability_feature
*/
public function test_get_contenttypes_with_capability_feature(array $contenttypesenabled, array $contenttypescanfeature): void {
$this->resetAfterTest();
$cb = new contentbank();
$plugins = [];
// Content types not enabled where the user has permission to use a feature.
if (empty($contenttypesenabled) && !empty($contenttypescanfeature)) {
$enabled = false;
// Mock core_plugin_manager class and the method get_plugins_of_type.
$pluginmanager = $this->getMockBuilder(\core_plugin_manager::class)
->disableOriginalConstructor()
->onlyMethods(['get_plugins_of_type'])
->getMock();
// Replace protected singletoninstance reference (core_plugin_manager property) with mock object.
$ref = new \ReflectionProperty(\core_plugin_manager::class, 'singletoninstance');
$ref->setAccessible(true);
$ref->setValue(null, $pluginmanager);
// Return values of get_plugins_of_type method.
foreach ($contenttypescanfeature as $contenttypepluginname) {
$contenttypeplugin = new \stdClass();
$contenttypeplugin->name = $contenttypepluginname;
$contenttypeplugin->type = 'contenttype';
// Add the feature to the fake content type.
$classname = "\\contenttype_$contenttypepluginname\\contenttype";
$classname::$featurestotest = ['test2'];
$plugins[] = $contenttypeplugin;
}
// Set expectations and return values.
$pluginmanager->expects($this->once())
->method('get_plugins_of_type')
->with('contenttype')
->willReturn($plugins);
} else {
$enabled = true;
// Get access to private property enabledcontenttypes.
$rc = new \ReflectionClass(\core_contentbank\contentbank::class);
$rcp = $rc->getProperty('enabledcontenttypes');
$rcp->setAccessible(true);
foreach ($contenttypesenabled as $contenttypename) {
$plugins["\\contenttype_$contenttypename\\contenttype"] = $contenttypename;
// Add to the testable contenttype the feature to test.
if (in_array($contenttypename, $contenttypescanfeature)) {
$classname = "\\contenttype_$contenttypename\\contenttype";
$classname::$featurestotest = ['test2'];
}
}
// Set as enabled content types only those in the test.
$rcp->setValue($cb, $plugins);
}
$actual = $cb->get_contenttypes_with_capability_feature('test2', null, $enabled);
$this->assertEquals($contenttypescanfeature, array_values($actual));
}
/**
* Test the behaviour of get_content_from_id()
*
* @covers ::get_content_from_id
*/
public function test_get_content_from_id() {
$this->resetAfterTest();
$cb = new \core_contentbank\contentbank();
// Create a category and two courses.
$systemcontext = context_system::instance();
// Add some content to the content bank.
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$contents = $generator->generate_contentbank_data(null, 3, 0, $systemcontext);
$content = reset($contents);
// Get the content instance form id.
$newinstance = $cb->get_content_from_id($content->get_id());
$this->assertEquals($content->get_id(), $newinstance->get_id());
// Now produce and exception with an innexistent id.
$this->expectException(Exception::class);
$cb->get_content_from_id(0);
}
/**
* Test the behaviour of is_context_allowed().
*
* @covers ::is_context_allowed
*/
public function test_is_context_allowed(): void {
$this->resetAfterTest();
$cb = new contentbank();
// System context.
$this->assertTrue($cb->is_context_allowed(context_system::instance()));
// User context.
$user = $this->getDataGenerator()->create_user();
$this->assertFalse($cb->is_context_allowed(context_user::instance($user->id)));
// Category context.
$category = $this->getDataGenerator()->create_category();
$this->assertTrue($cb->is_context_allowed(context_coursecat::instance($category->id)));
// Course context.
$course = $this->getDataGenerator()->create_course(['category' => $category->id]);
$coursecontext = context_course::instance($course->id);
$this->assertTrue($cb->is_context_allowed($coursecontext));
// Module context.
$module = $this->getDataGenerator()->create_module('page', ['course' => $course->id]);
$this->assertFalse($cb->is_context_allowed(context_module::instance($module->cmid)));
// Block context.
$block = $this->getDataGenerator()->create_block('online_users', ['parentcontextid' => $coursecontext->id]);
$this->assertFalse($cb->is_context_allowed(context_block::instance($block->id)));
}
}