mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-55971 dataformat: method to write exports to file storage.
This commit is contained in:
parent
a04309d40e
commit
b670d3adb6
2 changed files with 63 additions and 2 deletions
|
@ -26,6 +26,7 @@ namespace core;
|
||||||
|
|
||||||
use coding_exception;
|
use coding_exception;
|
||||||
use core_php_time_limit;
|
use core_php_time_limit;
|
||||||
|
use stored_file;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dataformat utility class
|
* Dataformat utility class
|
||||||
|
@ -144,4 +145,25 @@ class dataformat {
|
||||||
|
|
||||||
return $filepath;
|
return $filepath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes a formatted data file to file storage
|
||||||
|
*
|
||||||
|
* @param array $filerecord File record for storage, 'filename' extension should be omitted as it's added by the dataformat
|
||||||
|
* @param string $dataformat
|
||||||
|
* @param array $columns
|
||||||
|
* @param Iterable $iterator Iterable set of records to write
|
||||||
|
* @param callable|null $callback Optional callback method to apply to each record prior to writing
|
||||||
|
* @return stored_file
|
||||||
|
*/
|
||||||
|
public static function write_data_to_filearea(array $filerecord, string $dataformat, array $columns, Iterable $iterator,
|
||||||
|
callable $callback = null): stored_file {
|
||||||
|
|
||||||
|
$filepath = self::write_data($filerecord['filename'], $dataformat, $columns, $iterator, $callback);
|
||||||
|
|
||||||
|
// Update filename of returned file record.
|
||||||
|
$filerecord['filename'] = basename($filepath);
|
||||||
|
|
||||||
|
return get_file_storage()->create_file_from_pathname($filerecord, $filepath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,20 +24,21 @@
|
||||||
|
|
||||||
namespace core;
|
namespace core;
|
||||||
|
|
||||||
|
use context_system;
|
||||||
use core_component;
|
use core_component;
|
||||||
use core\dataformat;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dataformat tests
|
* Dataformat tests
|
||||||
*
|
*
|
||||||
* @package core
|
* @package core
|
||||||
|
* @covers \core\dataformat
|
||||||
* @copyright 2020 Paul Holden <paulh@moodle.com>
|
* @copyright 2020 Paul Holden <paulh@moodle.com>
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
*/
|
*/
|
||||||
class dataformat_testcase extends \advanced_testcase {
|
class dataformat_testcase extends \advanced_testcase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Data provider for {@see test_write_data)
|
* Data provider to return array of dataformat types
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
@ -73,4 +74,42 @@ class dataformat_testcase extends \advanced_testcase {
|
||||||
$this->assertFileExists($exportfile);
|
$this->assertFileExists($exportfile);
|
||||||
$this->assertGreaterThan(0, filesize($exportfile));
|
$this->assertGreaterThan(0, filesize($exportfile));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test writing dataformat export to filearea
|
||||||
|
*
|
||||||
|
* @param string $dataformat
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @dataProvider write_data_provider
|
||||||
|
*/
|
||||||
|
public function test_write_data_to_filearea(string $dataformat): void {
|
||||||
|
$this->resetAfterTest();
|
||||||
|
|
||||||
|
$columns = ['fruit', 'colour', 'animal'];
|
||||||
|
$rows = [
|
||||||
|
['banana', 'yellow', 'monkey'],
|
||||||
|
['apple', 'red', 'wolf'],
|
||||||
|
['melon', 'green', 'aardvark'],
|
||||||
|
];
|
||||||
|
|
||||||
|
// Export to filearea. Assert that the the file exists in file storage and matches the original file record.
|
||||||
|
$filerecord = [
|
||||||
|
'contextid' => context_system::instance()->id,
|
||||||
|
'component' => 'core_dataformat',
|
||||||
|
'filearea' => 'test',
|
||||||
|
'itemid' => 0,
|
||||||
|
'filepath' => '/',
|
||||||
|
'filename' => 'My export',
|
||||||
|
];
|
||||||
|
|
||||||
|
$file = dataformat::write_data_to_filearea($filerecord, $dataformat, $columns, $rows);
|
||||||
|
$this->assertEquals($filerecord['contextid'], $file->get_contextid());
|
||||||
|
$this->assertEquals($filerecord['component'], $file->get_component());
|
||||||
|
$this->assertEquals($filerecord['filearea'], $file->get_filearea());
|
||||||
|
$this->assertEquals($filerecord['itemid'], $file->get_itemid());
|
||||||
|
$this->assertEquals($filerecord['filepath'], $file->get_filepath());
|
||||||
|
$this->assertStringStartsWith($filerecord['filename'], $file->get_filename());
|
||||||
|
$this->assertGreaterThan(0, $file->get_filesize());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue