1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Security; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xls; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class XmlScannerTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @dataProvider providerValidXML |
14
|
|
|
* |
15
|
|
|
* @param mixed $filename |
16
|
|
|
* @param mixed $expectedResult |
17
|
|
|
*/ |
18
|
|
|
public function testValidXML($filename, $expectedResult) |
19
|
|
|
{ |
20
|
|
|
$reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml()); |
21
|
|
|
$result = $reader->scanFile($filename); |
22
|
|
|
self::assertEquals($expectedResult, $result); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function providerValidXML() |
26
|
|
|
{ |
27
|
|
|
$tests = []; |
28
|
|
|
foreach (glob(__DIR__ . '/../../../data/Reader/Xml/XEETestValid*.xml') as $file) { |
29
|
|
|
$tests[basename($file)] = [realpath($file), file_get_contents($file)]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $tests; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @dataProvider providerInvalidXML |
37
|
|
|
* |
38
|
|
|
* @param mixed $filename |
39
|
|
|
*/ |
40
|
|
|
public function testInvalidXML($filename) |
41
|
|
|
{ |
42
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class); |
43
|
|
|
|
44
|
|
|
$reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml()); |
45
|
|
|
$expectedResult = 'FAILURE: Should throw an Exception rather than return a value'; |
46
|
|
|
$result = $reader->scanFile($filename); |
47
|
|
|
self::assertEquals($expectedResult, $result); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function providerInvalidXML() |
51
|
|
|
{ |
52
|
|
|
$tests = []; |
53
|
|
|
foreach (glob(__DIR__ . '/../../../data/Reader/Xml/XEETestInvalidUTF*.xml') as $file) { |
54
|
|
|
$tests[basename($file)] = [realpath($file)]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $tests; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testGetSecurityScannerForXmlBasedReader() |
61
|
|
|
{ |
62
|
|
|
$fileReader = new Xlsx(); |
63
|
|
|
$scanner = $fileReader->getSecuritySCanner(); |
64
|
|
|
|
65
|
|
|
// Must return an object... |
66
|
|
|
$this->assertInternalType('object', $scanner); |
67
|
|
|
// ... of the correct type |
68
|
|
|
$this->assertInstanceOf(XmlScanner::class, $scanner); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGetSecurityScannerForNonXmlBasedReader() |
72
|
|
|
{ |
73
|
|
|
$fileReader = new Xls(); |
74
|
|
|
$scanner = $fileReader->getSecuritySCanner(); |
75
|
|
|
// Must return a null... |
76
|
|
|
$this->assertNull($scanner); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @dataProvider providerValidXMLForCallback |
81
|
|
|
* |
82
|
|
|
* @param mixed $filename |
83
|
|
|
* @param mixed $expectedResult |
84
|
|
|
*/ |
85
|
|
|
public function testSecurityScanWithCallback($filename, $expectedResult) |
86
|
|
|
{ |
87
|
|
|
$fileReader = new Xlsx(); |
88
|
|
|
$scanner = $fileReader->getSecuritySCanner(); |
89
|
|
|
$scanner->setAdditionalCallback('strrev'); |
90
|
|
|
$xml = $scanner->scanFile($filename); |
91
|
|
|
|
92
|
|
|
$this->assertEquals(strrev($expectedResult), $xml); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function providerValidXMLForCallback() |
96
|
|
|
{ |
97
|
|
|
$tests = []; |
98
|
|
|
foreach (glob(__DIR__ . '/../../../data/Reader/Xml/SecurityScannerWithCallback*.xml') as $file) { |
99
|
|
|
$tests[basename($file)] = [realpath($file), file_get_contents($file)]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $tests; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|