1 |
efrain |
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_excel;
|
|
|
18 |
|
|
|
19 |
use core\dataformat;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Tests for the dataformat_excel writer
|
|
|
23 |
*
|
|
|
24 |
* @package dataformat_excel
|
|
|
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', 'excel', $columns, $rows);
|
|
|
43 |
|
|
|
44 |
// Read the file.
|
|
|
45 |
$excelcells = $this->get_excel(file_get_contents($exportfile));
|
|
|
46 |
|
|
|
47 |
$this->assertEquals(array_merge([$columns], $rows), $excelcells);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Get an Excel object to check the content
|
|
|
52 |
*
|
|
|
53 |
* @param string $content
|
|
|
54 |
* @return array two-dimensional array with cell values
|
|
|
55 |
*/
|
|
|
56 |
private function get_excel(string $content) {
|
|
|
57 |
$file = tempnam(sys_get_temp_dir(), 'excel_');
|
|
|
58 |
$handle = fopen($file, "w");
|
|
|
59 |
fwrite($handle, $content);
|
|
|
60 |
/** @var \OpenSpout\Reader\XLSX\Reader $reader */
|
|
|
61 |
$reader = \OpenSpout\Reader\Common\Creator\ReaderFactory::createFromFileByMimeType($file);
|
|
|
62 |
$reader->open($file);
|
|
|
63 |
|
|
|
64 |
/** @var \OpenSpout\Reader\XLSX\Sheet[] $sheets */
|
|
|
65 |
$sheets = $reader->getSheetIterator();
|
|
|
66 |
$rowscellsvalues = [];
|
|
|
67 |
foreach ($sheets as $sheet) {
|
|
|
68 |
/** @var \OpenSpout\Common\Entity\Row[] $rows */
|
|
|
69 |
$rows = $sheet->getRowIterator();
|
|
|
70 |
foreach ($rows as $row) {
|
|
|
71 |
$thisvalues = [];
|
|
|
72 |
foreach ($row->getCells() as $cell) {
|
|
|
73 |
$thisvalues[] = $cell->getValue();
|
|
|
74 |
}
|
|
|
75 |
$rowscellsvalues[] = $thisvalues;
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return $rowscellsvalues;
|
|
|
80 |
}
|
|
|
81 |
}
|