See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 402] [Versions 401 and 403]
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 dataformat_ods; 18 19 use core\dataformat; 20 21 /** 22 * Tests for the dataformat_ods writer 23 * 24 * @package dataformat_ods 25 * @copyright 2022 Marina Glancy 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 */ 28 class writer_test extends \advanced_testcase { 29 30 /** 31 * Test writing data whose content contains an image with pluginfile.php source 32 */ 33 public function test_write_data(): void { 34 $columns = ['fruit', 'colour', 'animal']; 35 $rows = [ 36 ['banana', 'yellow', 'monkey'], 37 ['apple', 'red', 'wolf'], 38 ['melon', 'green', 'aardvark'], 39 ]; 40 41 // Export to file. 42 $exportfile = dataformat::write_data('My export', 'ods', $columns, $rows); 43 44 // Read the file. 45 $odscells = $this->get_ods_rows_content(file_get_contents($exportfile)); 46 47 $this->assertEquals(array_merge([$columns], $rows), $odscells); 48 } 49 50 /** 51 * Get ods rows from binary content 52 * @param string $content 53 * @return array 54 * @throws \Box\Spout\Common\Exception\IOException 55 * @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException 56 */ 57 private function get_ods_rows_content($content) { 58 $reader = \Box\Spout\Reader\Common\Creator\ReaderFactory::createFromType(\Box\Spout\Common\Type::ODS); 59 $file = tempnam(sys_get_temp_dir(), 'ods_'); 60 $handle = fopen($file, "w"); 61 fwrite($handle, $content); 62 $reader->open($file); 63 /** @var \Box\Spout\Reader\ODS\Sheet[] $sheets */ 64 $sheets = $reader->getSheetIterator(); 65 $rowscellsvalues = []; 66 foreach ($sheets as $sheet) { 67 /** @var \Box\Spout\Common\Entity\Row[] $rows */ 68 $rows = $sheet->getRowIterator(); 69 foreach ($rows as $row) { 70 $thisvalues = []; 71 foreach ($row->getCells() as $cell) { 72 $thisvalues[] = $cell->getValue(); 73 } 74 $rowscellsvalues[] = $thisvalues; 75 } 76 } 77 78 return $rowscellsvalues; 79 } 80 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body