Skip to content

Commit

Permalink
Merge branch 'MDL-76494-master' of https://github.com/marinaglancy/mo…
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyatregubov committed Dec 2, 2022
2 parents 8ddbc7b + 8bababa commit e925e29
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 2 deletions.
81 changes: 81 additions & 0 deletions dataformat/excel/tests/writer_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace dataformat_excel;

use core\dataformat;

/**
* Tests for the dataformat_excel writer
*
* @package dataformat_excel
* @copyright 2022 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class writer_test extends \advanced_testcase {

/**
* Test writing data whose content contains an image with pluginfile.php source
*/
public function test_write_data(): void {
$columns = ['fruit', 'colour', 'animal'];
$rows = [
['banana', 'yellow', 'monkey'],
['apple', 'red', 'wolf'],
['melon', 'green', 'aardvark'],
];

// Export to file.
$exportfile = dataformat::write_data('My export', 'excel', $columns, $rows);

// Read the file.
$excelcells = $this->get_excel(file_get_contents($exportfile));

$this->assertEquals(array_merge([$columns], $rows), $excelcells);
}

/**
* Get an Excel object to check the content
*
* @param string $content
* @return array two-dimensional array with cell values
*/
private function get_excel(string $content) {
$file = tempnam(sys_get_temp_dir(), 'excel_');
$handle = fopen($file, "w");
fwrite($handle, $content);
/** @var \Box\Spout\Reader\XLSX\Reader $reader */
$reader = \Box\Spout\Reader\Common\Creator\ReaderFactory::createFromType(\Box\Spout\Common\Type::XLSX);
$reader->open($file);

/** @var \Box\Spout\Reader\XLSX\Sheet[] $sheets */
$sheets = $reader->getSheetIterator();
$rowscellsvalues = [];
foreach ($sheets as $sheet) {
/** @var \Box\Spout\Common\Entity\Row[] $rows */
$rows = $sheet->getRowIterator();
foreach ($rows as $row) {
$thisvalues = [];
foreach ($row->getCells() as $cell) {
$thisvalues[] = $cell->getValue();
}
$rowscellsvalues[] = $thisvalues;
}
}

return $rowscellsvalues;
}
}
80 changes: 80 additions & 0 deletions dataformat/ods/tests/writer_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace dataformat_ods;

use core\dataformat;

/**
* Tests for the dataformat_ods writer
*
* @package dataformat_ods
* @copyright 2022 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class writer_test extends \advanced_testcase {

/**
* Test writing data whose content contains an image with pluginfile.php source
*/
public function test_write_data(): void {
$columns = ['fruit', 'colour', 'animal'];
$rows = [
['banana', 'yellow', 'monkey'],
['apple', 'red', 'wolf'],
['melon', 'green', 'aardvark'],
];

// Export to file.
$exportfile = dataformat::write_data('My export', 'ods', $columns, $rows);

// Read the file.
$odscells = $this->get_ods_rows_content(file_get_contents($exportfile));

$this->assertEquals(array_merge([$columns], $rows), $odscells);
}

/**
* Get ods rows from binary content
* @param string $content
* @return array
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException
*/
private function get_ods_rows_content($content) {
$reader = \Box\Spout\Reader\Common\Creator\ReaderFactory::createFromType(\Box\Spout\Common\Type::ODS);
$file = tempnam(sys_get_temp_dir(), 'ods_');
$handle = fopen($file, "w");
fwrite($handle, $content);
$reader->open($file);
/** @var \Box\Spout\Reader\ODS\Sheet[] $sheets */
$sheets = $reader->getSheetIterator();
$rowscellsvalues = [];
foreach ($sheets as $sheet) {
/** @var \Box\Spout\Common\Entity\Row[] $rows */
$rows = $sheet->getRowIterator();
foreach ($rows as $row) {
$thisvalues = [];
foreach ($row->getCells() as $cell) {
$thisvalues[] = $cell->getValue();
}
$rowscellsvalues[] = $thisvalues;
}
}

return $rowscellsvalues;
}
}
1 change: 1 addition & 0 deletions lib/spout/readme_moodle.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ MDL-73624 needs to fix with a couple of minor changes to
Writer/WriterAbstract.php. The changes replace rawurldecode() with
rawurlencode() in lines 143 and 144.
by Meirza <[email protected]>
MDL-76494 compatibility for PHP 8.1

2021/09/01
----------
Expand Down
4 changes: 2 additions & 2 deletions lib/spout/src/Spout/Common/Helper/GlobalFunctionsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ public function is_readable($fileName)
* @see basename()
*
* @param string $path
* @param string|null $suffix
* @param string $suffix
* @return string
*/
public function basename($path, $suffix = null)
public function basename($path, $suffix = '')
{
return \basename($path, $suffix);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/spout/src/Spout/Reader/CSV/RowIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function __construct(
*
* @return void
*/
#[\ReturnTypeWillChange]
public function rewind()
{
$this->rewindAndSkipBom();
Expand Down Expand Up @@ -114,6 +115,7 @@ protected function rewindAndSkipBom()
*
* @return bool
*/
#[\ReturnTypeWillChange]
public function valid()
{
return ($this->filePointer && !$this->hasReachedEndOfFile);
Expand All @@ -126,6 +128,7 @@ public function valid()
* @throws \Box\Spout\Common\Exception\EncodingConversionException If unable to convert data to UTF-8
* @return void
*/
#[\ReturnTypeWillChange]
public function next()
{
$this->hasReachedEndOfFile = $this->globalFunctionsHelper->feof($this->filePointer);
Expand Down Expand Up @@ -224,6 +227,7 @@ protected function isEmptyLine($lineData)
*
* @return Row|null
*/
#[\ReturnTypeWillChange]
public function current()
{
return $this->rowBuffer;
Expand All @@ -235,6 +239,7 @@ public function current()
*
* @return int
*/
#[\ReturnTypeWillChange]
public function key()
{
return $this->numReadRows;
Expand Down
5 changes: 5 additions & 0 deletions lib/spout/src/Spout/Reader/CSV/SheetIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct($sheet)
*
* @return void
*/
#[\ReturnTypeWillChange]
public function rewind()
{
$this->hasReadUniqueSheet = false;
Expand All @@ -41,6 +42,7 @@ public function rewind()
*
* @return bool
*/
#[\ReturnTypeWillChange]
public function valid()
{
return (!$this->hasReadUniqueSheet);
Expand All @@ -52,6 +54,7 @@ public function valid()
*
* @return void
*/
#[\ReturnTypeWillChange]
public function next()
{
$this->hasReadUniqueSheet = true;
Expand All @@ -63,6 +66,7 @@ public function next()
*
* @return \Box\Spout\Reader\CSV\Sheet
*/
#[\ReturnTypeWillChange]
public function current()
{
return $this->sheet;
Expand All @@ -74,6 +78,7 @@ public function current()
*
* @return int
*/
#[\ReturnTypeWillChange]
public function key()
{
return 1;
Expand Down
6 changes: 6 additions & 0 deletions lib/spout/src/Spout/Reader/ODS/RowIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function __construct(
* @throws \Box\Spout\Reader\Exception\IteratorNotRewindableException If the iterator is rewound more than once
* @return void
*/
#[\ReturnTypeWillChange]
public function rewind()
{
// Because sheet and row data is located in the file, we can't rewind both the
Expand All @@ -142,6 +143,7 @@ public function rewind()
*
* @return bool
*/
#[\ReturnTypeWillChange]
public function valid()
{
return (!$this->hasReachedEndOfFile);
Expand All @@ -155,6 +157,7 @@ public function valid()
* @throws \Box\Spout\Common\Exception\IOException If unable to read the sheet data XML
* @return void
*/
#[\ReturnTypeWillChange]
public function next()
{
if ($this->doesNeedDataForNextRowToBeProcessed()) {
Expand Down Expand Up @@ -356,6 +359,7 @@ protected function isEmptyRow($currentRow, $lastReadCell)
*
* @return Row
*/
#[\ReturnTypeWillChange]
public function current()
{
return $this->rowBuffer;
Expand All @@ -367,6 +371,7 @@ public function current()
*
* @return int
*/
#[\ReturnTypeWillChange]
public function key()
{
return $this->lastRowIndexProcessed;
Expand All @@ -377,6 +382,7 @@ public function key()
*
* @return void
*/
#[\ReturnTypeWillChange]
public function end()
{
$this->xmlReader->close();
Expand Down
6 changes: 6 additions & 0 deletions lib/spout/src/Spout/Reader/ODS/SheetIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function __construct($filePath, $optionsManager, $escaper, $settingsHelpe
* @throws \Box\Spout\Common\Exception\IOException If unable to open the XML file containing sheets' data
* @return void
*/
#[\ReturnTypeWillChange]
public function rewind()
{
$this->xmlReader->close();
Expand Down Expand Up @@ -131,6 +132,7 @@ private function readSheetsVisibility()
*
* @return bool
*/
#[\ReturnTypeWillChange]
public function valid()
{
return $this->hasFoundSheet;
Expand All @@ -142,6 +144,7 @@ public function valid()
*
* @return void
*/
#[\ReturnTypeWillChange]
public function next()
{
$this->hasFoundSheet = $this->xmlReader->readUntilNodeFound(self::XML_NODE_TABLE);
Expand All @@ -157,6 +160,7 @@ public function next()
*
* @return \Box\Spout\Reader\ODS\Sheet
*/
#[\ReturnTypeWillChange]
public function current()
{
$escapedSheetName = $this->xmlReader->getAttribute(self::XML_ATTRIBUTE_TABLE_NAME);
Expand Down Expand Up @@ -214,6 +218,7 @@ private function isSheetVisible($sheetStyleName)
*
* @return int
*/
#[\ReturnTypeWillChange]
public function key()
{
return $this->currentSheetIndex + 1;
Expand All @@ -224,6 +229,7 @@ public function key()
*
* @return void
*/
#[\ReturnTypeWillChange]
public function end()
{
$this->xmlReader->close();
Expand Down
2 changes: 2 additions & 0 deletions lib/spout/src/Spout/Reader/Wrapper/XMLReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ protected function fileExistsWithinZip($zipStreamURI)
* @throws \Box\Spout\Reader\Exception\XMLProcessingException If an error/warning occurred
* @return bool TRUE on success or FALSE on failure
*/
#[\ReturnTypeWillChange]
public function read()
{
$this->useXMLInternalErrors();
Expand Down Expand Up @@ -119,6 +120,7 @@ public function readUntilNodeFound($nodeName)
* @throws \Box\Spout\Reader\Exception\XMLProcessingException If an error/warning occurred
* @return bool TRUE on success or FALSE on failure
*/
#[\ReturnTypeWillChange]
public function next($localName = null)
{
$this->useXMLInternalErrors();
Expand Down
Loading

0 comments on commit e925e29

Please sign in to comment.