From b670d3adb687168f83b78d95f9522d91f51abcde Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Fri, 5 Jun 2020 08:57:01 +0100 Subject: [PATCH] MDL-55971 dataformat: method to write exports to file storage. --- lib/classes/dataformat.php | 22 ++++++++++++++++++ lib/tests/dataformat_test.php | 43 +++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/lib/classes/dataformat.php b/lib/classes/dataformat.php index be2f9ec66b1..0bc54536385 100644 --- a/lib/classes/dataformat.php +++ b/lib/classes/dataformat.php @@ -26,6 +26,7 @@ namespace core; use coding_exception; use core_php_time_limit; +use stored_file; /** * Dataformat utility class @@ -144,4 +145,25 @@ class dataformat { 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); + } } diff --git a/lib/tests/dataformat_test.php b/lib/tests/dataformat_test.php index 083b0c04aac..382c0f5dd5e 100644 --- a/lib/tests/dataformat_test.php +++ b/lib/tests/dataformat_test.php @@ -24,20 +24,21 @@ namespace core; +use context_system; use core_component; -use core\dataformat; /** * Dataformat tests * * @package core + * @covers \core\dataformat * @copyright 2020 Paul Holden * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class dataformat_testcase extends \advanced_testcase { /** - * Data provider for {@see test_write_data) + * Data provider to return array of dataformat types * * @return array */ @@ -73,4 +74,42 @@ class dataformat_testcase extends \advanced_testcase { $this->assertFileExists($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()); + } }