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 namespace mod_data; 18 19 use context_module; 20 use mod_data\local\exporter\csv_entries_exporter; 21 use mod_data\local\exporter\ods_entries_exporter; 22 use mod_data\local\exporter\utils; 23 24 /** 25 * Unit tests for entries_exporter and csv_entries_exporter classes. 26 * 27 * Also {@see entries_export_test} class which provides module tests for exporting entries. 28 * 29 * @package mod_data 30 * @covers \mod_data\local\exporter\entries_exporter 31 * @covers \mod_data\local\exporter\csv_entries_exporter 32 * @copyright 2023 ISB Bayern 33 * @author Philipp Memmel 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class entries_exporter_test extends \advanced_testcase { 37 38 /** 39 * Tests get_records_count method. 40 * 41 * @covers \mod_data\local\exporter\entries_exporter::get_records_count 42 * @dataProvider get_records_count_provider 43 * @param array $rows the rows from the data provider to be tested by the exporter 44 * @param int $expectedcount the expected count of records to be exported 45 */ 46 public function test_get_records_count(array $rows, int $expectedcount): void { 47 $exporter = new csv_entries_exporter(); 48 foreach ($rows as $row) { 49 $exporter->add_row($row); 50 } 51 $this->assertEquals($expectedcount, $exporter->get_records_count()); 52 } 53 54 /** 55 * Data provider method for self::test_get_records_count. 56 * 57 * @return array data for testing 58 */ 59 public function get_records_count_provider(): array { 60 return [ 61 'onlyheader' => [ 62 'rows' => [ 63 ['numberfield', 'textfield', 'filefield1', 'filefield2', 'picturefield'] 64 ], 65 'expectedcount' => 0 // Only header present, so we expect record count 0. 66 ], 67 'onerecord' => [ 68 'rows' => [ 69 ['numberfield', 'textfield', 'filefield1', 'filefield2', 'picturefield'], 70 ['3', 'a simple text', 'samplefile.png', 'samplefile_1.png', 'picturefile.png'] 71 ], 72 'expectedcount' => 1 73 ], 74 'tworecords' => [ 75 'rows' => [ 76 ['numberfield', 'textfield', 'filefield1', 'filefield2', 'picturefield'], 77 ['3', 'a simple text', 'samplefile.png', 'samplefile_1.png', 'picturefile.png'], 78 ['5', 'a supersimple text', 'anotherfile.png', 'someotherfile.png', 'andapicture.png'] 79 ], 80 'expectedcount' => 2 81 ] 82 ]; 83 } 84 85 /** 86 * Tests adding of files to the exporter to be included in the exported zip archive. 87 * 88 * @dataProvider add_file_from_string_provider 89 * @covers \mod_data\local\exporter\entries_exporter::add_file_from_string 90 * @covers \mod_data\local\exporter\entries_exporter::file_exists 91 * @param array $files array of filename and filecontent to be tested for exporting 92 * @param bool $success if the exporting of files should be successful 93 */ 94 public function test_add_file_from_string(array $files, bool $success): void { 95 $exporter = new csv_entries_exporter(); 96 foreach ($files as $file) { 97 if (empty($file['subdir'])) { 98 $exporter->add_file_from_string($file['filename'], $file['filecontent']); 99 $this->assertEquals($exporter->file_exists($file['filename']), $success); 100 } else { 101 $exporter->add_file_from_string($file['filename'], $file['filecontent'], $file['subdir']); 102 $this->assertEquals($exporter->file_exists($file['filename'], $file['subdir']), $success); 103 } 104 } 105 } 106 107 /** 108 * Data provider method for self::test_add_file_from_string. 109 * 110 * @return array data for testing 111 */ 112 public function add_file_from_string_provider(): array { 113 return [ 114 'one file' => [ 115 'files' => [ 116 [ 117 'filename' => 'testfile.txt', 118 'filecontent' => 'somecontent' 119 ], 120 ], 121 'success' => true 122 ], 123 'more files, also with subdirs' => [ 124 'files' => [ 125 [ 126 'filename' => 'testfile.txt', 127 'filecontent' => 'somecontent' 128 ], 129 [ 130 'filename' => 'testfile2.txt', 131 'filecontent' => 'someothercontent', 132 'subdir' => 'testsubdir' 133 ], 134 [ 135 'filename' => 'testfile3.txt', 136 'filecontent' => 'someverydifferentcontent', 137 'subdir' => 'files/foo/bar' 138 ], 139 [ 140 'filename' => 'testfile4.txt', 141 'filecontent' => 'someverydifferentcontent', 142 'subdir' => 'files/foo/bar/' 143 ], 144 [ 145 'filename' => 'testfile5.txt', 146 'filecontent' => 'someverydifferentcontent', 147 'subdir' => '/files/foo/bar/' 148 ], 149 ], 150 'success' => true 151 ], 152 'nocontent' => [ 153 'files' => [ 154 [ 155 'filename' => '', 156 'filecontent' => '' 157 ] 158 ], 159 'success' => false 160 ] 161 ]; 162 } 163 164 /** 165 * Tests if unique filenames are being created correctly. 166 * 167 * @covers \mod_data\local\exporter\entries_exporter::create_unique_filename 168 * @dataProvider create_unique_filename_provider 169 * @param string $inputfilename the name of the file which should be converted into a unique filename 170 * @param string $resultfilename the maybe changed $inputfilename, so that it is unique in the exporter 171 */ 172 public function test_create_unique_filename(string $inputfilename, string $resultfilename): void { 173 $exporter = new csv_entries_exporter(); 174 $exporter->add_file_from_string('test.txt', 'somecontent'); 175 $exporter->add_file_from_string('foo.txt', 'somecontent'); 176 $exporter->add_file_from_string('foo_1.txt', 'somecontent'); 177 $exporter->add_file_from_string('foo_2.txt', 'somecontent'); 178 $exporter->add_file_from_string('foo', 'somecontent'); 179 $exporter->add_file_from_string('foo_1', 'somecontent'); 180 $exporter->add_file_from_string('sample_5.txt', 'somecontent'); 181 $exporter->add_file_from_string('bar_1.txt', 'somecontent'); 182 $this->assertEquals($resultfilename, $exporter->create_unique_filename($inputfilename)); 183 } 184 185 /** 186 * Data provider method for self::test_create_unique_filename. 187 * 188 * @return array data for testing 189 */ 190 public function create_unique_filename_provider(): array { 191 return [ 192 'does not exist yet' => [ 193 'inputfilename' => 'someuniquename.txt', 194 'resultfilename' => 'someuniquename.txt' 195 ], 196 'already exists' => [ 197 'inputfilename' => 'test.txt', 198 'resultfilename' => 'test_1.txt' 199 ], 200 'already exists, other numbers as well' => [ 201 'inputfilename' => 'foo.txt', 202 'resultfilename' => 'foo_3.txt' 203 ], 204 'file with _5 suffix already exists' => [ 205 'inputfilename' => 'sample_5.txt', 206 'resultfilename' => 'sample_5_1.txt' 207 ], 208 'file with _1 suffix already exists' => [ 209 'inputfilename' => 'bar_1.txt', 210 'resultfilename' => 'bar_1_1.txt' 211 ], 212 'file without extension unique' => [ 213 'inputfilename' => 'test', 214 'resultfilename' => 'test' 215 ], 216 'file without extension not unique' => [ 217 'inputfilename' => 'foo', 218 'resultfilename' => 'foo_2' 219 ] 220 ]; 221 } 222 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body