mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
MDL-69143 contentbank: add import file method to content
This commit is contained in:
parent
94fdac9117
commit
3dfbd5a16f
8 changed files with 295 additions and 12 deletions
|
@ -189,4 +189,88 @@ class core_contenttype_content_testcase extends \advanced_testcase {
|
|||
$this->assertEquals($newcontext->id, $content->get_contextid());
|
||||
$this->assertEquals($newcontext->id, $file->get_contextid());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,8 @@ namespace core_contentbank;
|
|||
|
||||
use stdClass;
|
||||
use context_system;
|
||||
use context_user;
|
||||
use file_exception;
|
||||
use contenttype_testable\contenttype as contenttype;
|
||||
/**
|
||||
* Test for content bank contenttype class.
|
||||
|
@ -183,6 +185,111 @@ class core_contenttype_contenttype_testcase extends \advanced_testcase {
|
|||
$this->assertInstanceOf('\\contenttype_testable\\content', $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for behaviour of upload_content() with a file and a record.
|
||||
*
|
||||
* @dataProvider upload_content_provider
|
||||
* @param bool $userecord if a predefined record has to be used.
|
||||
*
|
||||
* @covers ::upload_content
|
||||
*/
|
||||
public function test_upload_content(bool $userecord): void {
|
||||
global $USER;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$dummy = [
|
||||
'contextid' => context_user::instance($USER->id)->id,
|
||||
'component' => 'user',
|
||||
'filearea' => 'draft',
|
||||
'itemid' => 1,
|
||||
'filepath' => '/',
|
||||
'filename' => 'file.h5p',
|
||||
'userid' => $USER->id,
|
||||
];
|
||||
$fs = get_file_storage();
|
||||
$dummyfile = $fs->create_file_from_string($dummy, 'Dummy content');
|
||||
|
||||
// Create content.
|
||||
if ($userecord) {
|
||||
$record = new stdClass();
|
||||
$record->name = 'Test content';
|
||||
$record->configdata = '';
|
||||
$record->contenttype = '';
|
||||
$checkname = $record->name;
|
||||
} else {
|
||||
$record = null;
|
||||
$checkname = $dummyfile->get_filename();
|
||||
}
|
||||
|
||||
$contenttype = new contenttype(context_system::instance());
|
||||
$content = $contenttype->upload_content($dummyfile, $record);
|
||||
|
||||
$this->assertEquals('contenttype_testable', $content->get_content_type());
|
||||
$this->assertEquals($checkname, $content->get_name());
|
||||
$this->assertInstanceOf('\\contenttype_testable\\content', $content);
|
||||
|
||||
$file = $content->get_file();
|
||||
$this->assertEquals($dummyfile->get_filename(), $file->get_filename());
|
||||
$this->assertEquals($dummyfile->get_userid(), $file->get_userid());
|
||||
$this->assertEquals($dummyfile->get_mimetype(), $file->get_mimetype());
|
||||
$this->assertEquals($dummyfile->get_contenthash(), $file->get_contenthash());
|
||||
$this->assertEquals('contentbank', $file->get_component());
|
||||
$this->assertEquals('public', $file->get_filearea());
|
||||
$this->assertEquals('/', $file->get_filepath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_rename_content.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function upload_content_provider() {
|
||||
return [
|
||||
'With record' => [true],
|
||||
'Without record' => [false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for behaviour of upload_content() with a file wrong file.
|
||||
*
|
||||
* @covers ::upload_content
|
||||
*/
|
||||
public function test_upload_content_exception(): void {
|
||||
global $USER, $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
// The testing contenttype thows exception if filename is "error.*".
|
||||
$dummy = [
|
||||
'contextid' => context_user::instance($USER->id)->id,
|
||||
'component' => 'user',
|
||||
'filearea' => 'draft',
|
||||
'itemid' => 1,
|
||||
'filepath' => '/',
|
||||
'filename' => 'error.txt',
|
||||
'userid' => $USER->id,
|
||||
];
|
||||
$fs = get_file_storage();
|
||||
$dummyfile = $fs->create_file_from_string($dummy, 'Dummy content');
|
||||
|
||||
$contenttype = new contenttype(context_system::instance());
|
||||
$cbcontents = $DB->count_records('contentbank_content');
|
||||
|
||||
// We need to capture the exception to check no content is created.
|
||||
try {
|
||||
$content = $contenttype->upload_content($dummyfile);
|
||||
$this->assertTrue(false);
|
||||
} catch (file_exception $e) {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
$this->assertEquals($cbcontents, $DB->count_records('contentbank_content'));
|
||||
$this->assertEquals(1, $DB->count_records('files', ['contenthash' => $dummyfile->get_contenthash()]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the behaviour of can_delete().
|
||||
*/
|
||||
|
|
20
contentbank/tests/fixtures/testable_content.php
vendored
20
contentbank/tests/fixtures/testable_content.php
vendored
|
@ -25,6 +25,9 @@
|
|||
|
||||
namespace contenttype_testable;
|
||||
|
||||
use file_exception;
|
||||
use stored_file;
|
||||
|
||||
/**
|
||||
* Testable content plugin class.
|
||||
*
|
||||
|
@ -33,4 +36,21 @@ namespace contenttype_testable;
|
|||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class content extends \core_contentbank\content {
|
||||
|
||||
/**
|
||||
* Import a file as a valid content.
|
||||
*
|
||||
* This method will thow an error if the filename is "error.*"
|
||||
*
|
||||
* @param stored_file $file File to store in the content file area.
|
||||
* @return stored_file|null the stored content file or null if the file is discarted.
|
||||
* @throws file_exception if the filename contains the word "error"
|
||||
*/
|
||||
public function import_file(stored_file $file): ?stored_file {
|
||||
$filename = $file->get_filename();
|
||||
if (strrpos($filename, 'error') !== false) {
|
||||
throw new file_exception('yourerrorthanks', 'contenttype_test');
|
||||
}
|
||||
return parent::import_file($file);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue