mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-25 08:08:20 +00:00
3eedf9e2f0
Change "mixed" declarations to more accurate types in test members; in particular, change those that would be flagged if we were to run Phpstan at level 9 (we currently run level 8). I may or may not follow up with source code (over 700 level-9 problems remain for src), but, as with strict typing, there is no reason to avoid the effort for test members. It was necessary to update some doc blocks in src to accommodate this change. However, no executable code is touched.
135 lines
4.1 KiB
PHP
135 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Security;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
|
|
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xls;
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
|
use PHPUnit\Framework\TestCase;
|
|
use XMLReader;
|
|
|
|
class XmlScannerTest extends TestCase
|
|
{
|
|
/**
|
|
* @dataProvider providerValidXML
|
|
*/
|
|
public function testValidXML(string $filename, string $expectedResult): void
|
|
{
|
|
$reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml());
|
|
$result = $reader->scanFile($filename);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerValidXML(): array
|
|
{
|
|
$tests = [];
|
|
$glob = glob('tests/data/Reader/Xml/XEETestValid*.xml');
|
|
self::assertNotFalse($glob);
|
|
foreach ($glob as $file) {
|
|
$filename = realpath($file);
|
|
$expectedResult = file_get_contents($file);
|
|
$tests[basename($file)] = [$filename, $expectedResult];
|
|
}
|
|
|
|
return $tests;
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerInvalidXML
|
|
*/
|
|
public function testInvalidXML(string $filename): void
|
|
{
|
|
$this->expectException(ReaderException::class);
|
|
|
|
$reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml());
|
|
$expectedResult = 'FAILURE: Should throw an Exception rather than return a value';
|
|
$result = $reader->scanFile($filename);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerInvalidXML(): array
|
|
{
|
|
$tests = [];
|
|
$glob = glob('tests/data/Reader/Xml/XEETestInvalidUTF*.xml');
|
|
self::assertNotFalse($glob);
|
|
foreach ($glob as $file) {
|
|
$filename = realpath($file);
|
|
$tests[basename($file)] = [$filename];
|
|
}
|
|
|
|
return $tests;
|
|
}
|
|
|
|
public function testGetSecurityScannerForXmlBasedReader(): void
|
|
{
|
|
$fileReader = new Xlsx();
|
|
$scanner = $fileReader->getSecurityScanner();
|
|
|
|
// Must return an object...
|
|
self::assertIsObject($scanner);
|
|
// ... of the correct type
|
|
self::assertInstanceOf(XmlScanner::class, $scanner);
|
|
}
|
|
|
|
public function testGetSecurityScannerForNonXmlBasedReader(): void
|
|
{
|
|
$fileReader = new Xls();
|
|
$scanner = $fileReader->getSecurityScanner();
|
|
// Must return a null...
|
|
self::assertNull($scanner);
|
|
}
|
|
|
|
public function testGetSecurityScannerForNonXmlBasedReader2(): void
|
|
{
|
|
$this->expectException(ReaderException::class);
|
|
$this->expectExceptionMessage('Security scanner is unexpectedly null');
|
|
$fileReader = new Xls();
|
|
$fileReader->getSecurityScannerOrThrow();
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerValidXMLForCallback
|
|
*/
|
|
public function testSecurityScanWithCallback(string $filename, string $expectedResult): void
|
|
{
|
|
$fileReader = new Xlsx();
|
|
$scanner = $fileReader->getSecurityScannerOrThrow();
|
|
$scanner->setAdditionalCallback('strrev');
|
|
$xml = $scanner->scanFile($filename);
|
|
|
|
self::assertEquals(strrev($expectedResult), $xml);
|
|
}
|
|
|
|
public static function providerValidXMLForCallback(): array
|
|
{
|
|
$tests = [];
|
|
$glob = glob('tests/data/Reader/Xml/SecurityScannerWithCallback*.xml');
|
|
self::assertNotFalse($glob);
|
|
foreach ($glob as $file) {
|
|
$tests[basename($file)] = [realpath($file), file_get_contents($file)];
|
|
}
|
|
|
|
return $tests;
|
|
}
|
|
|
|
public function testLibxmlDisableEntityLoaderIsRestoredWithoutShutdown(): void
|
|
{
|
|
$reader = new Xlsx();
|
|
unset($reader);
|
|
|
|
$reader = new XMLReader();
|
|
$opened = $reader->open('tests/data/Reader/Xml/SecurityScannerWithCallbackExample.xml');
|
|
self::assertTrue($opened);
|
|
}
|
|
|
|
public function testEncodingAllowsMixedCase(): void
|
|
{
|
|
$scanner = new XmlScanner();
|
|
$output = $scanner->scan($input = '<?xml version="1.0" encoding="utf-8"?><foo>bar</foo>');
|
|
self::assertSame($input, $output);
|
|
}
|
|
}
|