Differences Between: [Versions 310 and 311] [Versions 39 and 311]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Tests for the dataformat plugins 19 * 20 * @package core 21 * @copyright 2020 Paul Holden <paulh@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace core; 26 27 use context_system; 28 use core_component; 29 30 /** 31 * Dataformat tests 32 * 33 * @package core 34 * @covers \core\dataformat 35 * @copyright 2020 Paul Holden <paulh@moodle.com> 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class dataformat_test extends \advanced_testcase { 39 40 /** 41 * Data provider to return array of dataformat types 42 * 43 * @return array 44 */ 45 public function write_data_provider(): array { 46 $data = []; 47 48 $dataformats = core_component::get_plugin_list('dataformat'); 49 foreach ($dataformats as $dataformat => $unused) { 50 $data[] = [$dataformat]; 51 } 52 53 return $data; 54 } 55 56 /** 57 * Test writing dataformat export to local file 58 * 59 * @param string $dataformat 60 * @return void 61 * 62 * @dataProvider write_data_provider 63 */ 64 public function test_write_data(string $dataformat): void { 65 $columns = ['fruit', 'colour', 'animal']; 66 $rows = [ 67 ['banana', 'yellow', 'monkey'], 68 ['apple', 'red', 'wolf'], 69 ['melon', 'green', 'aardvark'], 70 ]; 71 72 // Export to file. Assert that the exported file exists and is non-zero in size. 73 $exportfile = dataformat::write_data('My export', $dataformat, $columns, $rows); 74 $this->assertFileExists($exportfile); 75 $this->assertGreaterThan(0, filesize($exportfile)); 76 } 77 78 /** 79 * Test writing dataformat export to filearea 80 * 81 * @param string $dataformat 82 * @return void 83 * 84 * @dataProvider write_data_provider 85 */ 86 public function test_write_data_to_filearea(string $dataformat): void { 87 $this->resetAfterTest(); 88 89 $columns = ['fruit', 'colour', 'animal']; 90 $rows = [ 91 ['banana', 'yellow', 'monkey'], 92 ['apple', 'red', 'wolf'], 93 ['melon', 'green', 'aardvark'], 94 ]; 95 96 // Export to filearea. Assert that the the file exists in file storage and matches the original file record. 97 $filerecord = [ 98 'contextid' => context_system::instance()->id, 99 'component' => 'core_dataformat', 100 'filearea' => 'test', 101 'itemid' => 0, 102 'filepath' => '/', 103 'filename' => 'My export', 104 ]; 105 106 $file = dataformat::write_data_to_filearea($filerecord, $dataformat, $columns, $rows); 107 $this->assertEquals($filerecord['contextid'], $file->get_contextid()); 108 $this->assertEquals($filerecord['component'], $file->get_component()); 109 $this->assertEquals($filerecord['filearea'], $file->get_filearea()); 110 $this->assertEquals($filerecord['itemid'], $file->get_itemid()); 111 $this->assertEquals($filerecord['filepath'], $file->get_filepath()); 112 $this->assertStringStartsWith($filerecord['filename'], $file->get_filename()); 113 $this->assertGreaterThan(0, $file->get_filesize()); 114 } 115 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body