forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_test.php
424 lines (352 loc) · 14.3 KB
/
content_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
<?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 content bank contenttype class.
*
* @package core_contentbank
* @category test
* @copyright 2020 Amaia Anabitarte <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_contentbank;
use stdClass;
use context_system;
use contenttype_testable\contenttype as contenttype;
/**
* Test for content bank contenttype class.
*
* @package core_contentbank
* @category test
* @copyright 2020 Amaia Anabitarte <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_contentbank\content
*
*/
class content_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');
require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
}
/**
* Tests for behaviour of get_name().
*
* @covers ::get_name
*/
public function test_get_name(): void {
$this->resetAfterTest();
// Create content.
$record = new stdClass();
$record->name = 'Test content';
$record->configdata = '';
$contenttype = new contenttype(context_system::instance());
$content = $contenttype->create_content($record);
$this->assertEquals($record->name, $content->get_name());
}
/**
* Data provider for test_set_name.
*
* @return array
*/
public static function set_name_provider(): array {
return [
'Standard name' => ['New name', 'New name'],
'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017'],
'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle'],
'Name with tags' => ['This is <b>bold</b>', 'This is bold'],
'Long name' => [str_repeat('a', 100), str_repeat('a', 100)],
'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)],
'Empty name' => ['', 'Old name'],
'Blanks only' => [' ', 'Old name'],
];
}
/**
* Tests for 'set_name' behaviour.
*
* @dataProvider set_name_provider
* @param string $newname The name to set
* @param string $expected The name result
*
* @covers ::set_name
*/
public function test_set_name(string $newname, string $expected): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$oldname = "Old name";
$context = context_system::instance();
// Create content.
$record = new stdClass();
$record->name = $oldname;
$contenttype = new contenttype($context);
$content = $contenttype->create_content($record);
$this->assertEquals($oldname, $content->get_name());
$content->set_name($newname);
$this->assertEquals($expected, $content->get_name());
$record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
$this->assertEquals($expected, $record->name);
}
/**
* Tests for behaviour of get_content_type().
*
* @covers ::get_content_type
*/
public function test_get_content_type(): void {
$this->resetAfterTest();
// Create content.
$record = new stdClass();
$record->name = 'Test content';
$record->configdata = '';
$contenttype = new contenttype(context_system::instance());
$content = $contenttype->create_content($record);
$this->assertEquals('contenttype_testable', $content->get_content_type());
}
/**
* Tests for 'configdata' behaviour.
*
* @covers ::set_configdata
*/
public function test_configdata_changes(): void {
$this->resetAfterTest();
$configdata = "{img: 'icon.svg'}";
// Create content.
$record = new stdClass();
$record->configdata = $configdata;
$contenttype = new contenttype(context_system::instance());
$content = $contenttype->create_content($record);
$this->assertEquals($configdata, $content->get_configdata());
$configdata = "{alt: 'Name'}";
$content->set_configdata($configdata);
$this->assertEquals($configdata, $content->get_configdata());
}
/**
* Tests for 'set_contextid' behaviour.
*
* @covers ::set_contextid
*/
public function test_set_contextid(): void {
$this->resetAfterTest();
$this->setAdminUser();
$context = context_system::instance();
$course = $this->getDataGenerator()->create_course();
$newcontext = \context_course::instance($course->id);
// Add some content to the content bank.
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$contents = $generator->generate_contentbank_data('contenttype_testable', 3, 0, $context);
$content = reset($contents);
$oldcontextid = $content->get_contextid();
$file = $content->get_file();
$this->assertEquals($oldcontextid, $file->get_contextid());
$this->assertEquals($context->id, $oldcontextid);
$this->assertNotEquals($newcontext->id, $oldcontextid);
$content->set_contextid($newcontext->id);
$file = $content->get_file();
$this->assertEquals($newcontext->id, $content->get_contextid());
$this->assertEquals($newcontext->id, $file->get_contextid());
}
/**
* Tests for set_visibility behaviour
*
* @covers ::set_visibility
*/
public function test_set_visibility(): void {
$this->resetAfterTest();
$this->setAdminUser();
$context = context_system::instance();
$oldvisibility = content::VISIBILITY_PUBLIC;
$newvisibility = content::VISIBILITY_UNLISTED;
$illegalvisibility = -1;
$record = new stdClass();
$record->visibility = $oldvisibility;
$contenttype = new contenttype($context);
$content = $contenttype->create_content($record);
$this->assertEquals($oldvisibility, $content->get_visibility());
$content->set_visibility($newvisibility);
$this->assertEquals($newvisibility, $content->get_visibility());
$content->set_visibility($illegalvisibility);
$this->assertEquals($newvisibility, $content->get_visibility());
}
/**
* Tests for 'import_file' behaviour when replacing a file.
*
* @covers ::import_file
*/
public function test_import_file_replace(): void {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
$context = context_system::instance();
// Add some content to the content bank.
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$contents = $generator->generate_contentbank_data('contenttype_testable', 3, 0, $context);
$content = reset($contents);
$originalfile = $content->get_file();
// Create a dummy file.
$filerecord = array(
'contextid' => $context->id,
'component' => 'contentbank',
'filearea' => 'draft',
'itemid' => $content->get_id(),
'filepath' => '/',
'filename' => 'example.txt'
);
$fs = get_file_storage();
$file = $fs->create_file_from_string($filerecord, 'Dummy content ');
$importedfile = $content->import_file($file);
$this->assertEquals($originalfile->get_filename(), $importedfile->get_filename());
$this->assertEquals($originalfile->get_filearea(), $importedfile->get_filearea());
$this->assertEquals($originalfile->get_filepath(), $importedfile->get_filepath());
$this->assertEquals($originalfile->get_mimetype(), $importedfile->get_mimetype());
$this->assertEquals($file->get_userid(), $importedfile->get_userid());
$this->assertEquals($file->get_contenthash(), $importedfile->get_contenthash());
}
/**
* Tests for 'import_file' behaviour when uploading a new file.
*
* @covers ::import_file
*/
public function test_import_file_upload(): void {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
$context = context_system::instance();
$type = new contenttype($context);
$record = (object)[
'name' => 'content name',
'usercreated' => $USER->id,
];
$content = $type->create_content($record);
// Create a dummy file.
$filerecord = array(
'contextid' => $context->id,
'component' => 'contentbank',
'filearea' => 'draft',
'itemid' => $content->get_id(),
'filepath' => '/',
'filename' => 'example.txt'
);
$fs = get_file_storage();
$file = $fs->create_file_from_string($filerecord, 'Dummy content ');
$importedfile = $content->import_file($file);
$this->assertEquals($file->get_filename(), $importedfile->get_filename());
$this->assertEquals($file->get_userid(), $importedfile->get_userid());
$this->assertEquals($file->get_mimetype(), $importedfile->get_mimetype());
$this->assertEquals($file->get_contenthash(), $importedfile->get_contenthash());
$this->assertEquals('public', $importedfile->get_filearea());
$this->assertEquals('/', $importedfile->get_filepath());
$contentfile = $content->get_file($file);
$this->assertEquals($importedfile->get_id(), $contentfile->get_id());
}
/**
* Tests for 'get_content_type_instance'
*
* @covers ::get_content_type_instance
*/
public function test_get_content_type_instance(): void {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
$context = context_system::instance();
$type = new contenttype($context);
$record = (object)[
'name' => 'content name',
'usercreated' => $USER->id,
];
$content = $type->create_content($record);
$contenttype = $content->get_content_type_instance();
$this->assertInstanceOf(get_class($type), $contenttype);
}
/**
* Tests for 'is_view_allowed'.
*
* @covers ::is_view_allowed
*/
public function test_is_view_allowed(): void {
$this->resetAfterTest();
$this->setAdminUser();
$context = context_system::instance();
$userauthor = $this->getDataGenerator()->create_user();
$userother = $this->getDataGenerator()->create_user();
$contenttype = new contenttype($context);
$unlistedrecord = new stdClass();
$unlistedrecord->visibility = content::VISIBILITY_UNLISTED;
$unlistedrecord->usercreated = $userauthor->id;
$unlistedcontent = $contenttype->create_content($unlistedrecord);
$publicrecord = new stdClass();
$publicrecord->visibility = content::VISIBILITY_PUBLIC;
$publicrecord->usercreated = $userauthor->id;
$publiccontent = $contenttype->create_content($publicrecord);
$this->setUser($userother);
$this->assertFalse($unlistedcontent->is_view_allowed());
$this->assertTrue($publiccontent->is_view_allowed());
$this->setUser($userauthor);
$this->assertTrue($unlistedcontent->is_view_allowed());
$this->assertTrue($publiccontent->is_view_allowed());
$this->setAdminUser();
$this->assertTrue($unlistedcontent->is_view_allowed());
$this->assertTrue($publiccontent->is_view_allowed());
}
/**
* Tests for 'get_uses' behaviour.
*
* @covers ::get_uses
*/
public function test_get_uses(): void {
$this->resetAfterTest();
$this->setAdminUser();
$context = context_system::instance();
// Add some content to the content bank.
$generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
$contents = $generator->generate_contentbank_data('contenttype_testable', 3, 0, $context);
$content1 = array_shift($contents);
// Check content has no references for now.
$this->assertCount(0, $content1->get_uses());
// Add a link to the previous content.
$cbfile = $content1->get_file();
$cbrecord = array(
'contextid' => $cbfile->get_contextid(),
'component' => $cbfile->get_component(),
'filearea' => $cbfile->get_filearea(),
'itemid' => $cbfile->get_itemid(),
'filepath' => $cbfile->get_filepath(),
'filename' => $cbfile->get_filename(),
);
$fs = get_file_storage();
$ref = $fs->pack_reference($cbrecord);
$aliasrecord = new stdClass();
$aliasrecord->contextid = $context->id;
$aliasrecord->component = 'core';
$aliasrecord->filearea = 'phpunit';
$aliasrecord->filepath = '/foo/';
$aliasrecord->filename = 'one.txt';
$aliasrecord->itemid = 0;
$repos = \repository::get_instances(['type' => 'contentbank']);
$cbrepo = reset($repos);
$this->assertInstanceOf('repository', $cbrepo);
$alias = $fs->create_file_from_reference($aliasrecord, $cbrepo->id, $ref);
// Check content now has one reference (the previous alias).
$contentuses1 = $content1->get_uses();
$this->assertCount(1, $contentuses1);
$reffile = reset($contentuses1);
$this->assertEquals($alias, $reffile);
// Check a different content hasn't any reference.
$content2 = array_shift($contents);
$this->assertCount(0, $content2->get_uses());
}
}