1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Html; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; |
9
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class HtmlImage2Test extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testCanInsertImageGoodProtocol(): void |
15
|
|
|
{ |
16
|
|
|
if (getenv('SKIP_URL_IMAGE_TEST') === '1') { |
17
|
|
|
self::markTestSkipped('Skipped due to setting of environment variable'); |
18
|
|
|
} |
19
|
|
|
$imagePath = 'https://phpspreadsheet.readthedocs.io/en/latest/topics/images/01-03-filter-icon-1.png'; |
20
|
|
|
$html = '<table> |
21
|
|
|
<tr> |
22
|
|
|
<td><img src="' . $imagePath . '" alt="test image voilà"></td> |
23
|
|
|
</tr> |
24
|
|
|
</table>'; |
25
|
|
|
$filename = HtmlHelper::createHtml($html); |
26
|
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true); |
27
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
28
|
|
|
|
29
|
|
|
/** @var Drawing $drawing */ |
30
|
|
|
$drawing = $firstSheet->getDrawingCollection()[0]; |
31
|
|
|
self::assertEquals($imagePath, $drawing->getPath()); |
32
|
|
|
self::assertEquals('A1', $drawing->getCoordinates()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testCantInsertImageNotFound(): void |
36
|
|
|
{ |
37
|
|
|
if (getenv('SKIP_URL_IMAGE_TEST') === '1') { |
38
|
|
|
self::markTestSkipped('Skipped due to setting of environment variable'); |
39
|
|
|
} |
40
|
|
|
$imagePath = 'https://phpspreadsheet.readthedocs.io/en/latest/topics/images/xxx01-03-filter-icon-1.png'; |
41
|
|
|
$html = '<table> |
42
|
|
|
<tr> |
43
|
|
|
<td><img src="' . $imagePath . '" alt="test image voilà"></td> |
44
|
|
|
</tr> |
45
|
|
|
</table>'; |
46
|
|
|
$filename = HtmlHelper::createHtml($html); |
47
|
|
|
$spreadsheet = HtmlHelper::loadHtmlIntoSpreadsheet($filename, true); |
48
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
49
|
|
|
$drawingCollection = $firstSheet->getDrawingCollection(); |
50
|
|
|
self::assertCount(0, $drawingCollection); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
#[DataProvider('providerBadProtocol')] |
54
|
|
|
public function testCannotInsertImageBadProtocol(string $imagePath): void |
55
|
|
|
{ |
56
|
|
|
$this->expectException(SpreadsheetException::class); |
57
|
|
|
$this->expectExceptionMessage('Invalid protocol for linked drawing'); |
58
|
|
|
$html = '<table> |
59
|
|
|
<tr> |
60
|
|
|
<td><img src="' . $imagePath . '" alt="test image voilà"></td> |
61
|
|
|
</tr> |
62
|
|
|
</table>'; |
63
|
|
|
$filename = HtmlHelper::createHtml($html); |
64
|
|
|
HtmlHelper::loadHtmlIntoSpreadsheet($filename, true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public static function providerBadProtocol(): array |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
'unknown protocol' => ['httpx://example.com/image.png'], |
71
|
|
|
'embedded whitespace' => ['ht tp://example.com/image.png'], |
72
|
|
|
'control character' => ["\x14http://example.com/image.png"], |
73
|
|
|
'mailto' => ['mailto:[email protected]'], |
74
|
|
|
'mailto whitespace' => ['mail to:[email protected]'], |
75
|
|
|
'phar' => ['phar://example.com/image.phar'], |
76
|
|
|
'phar control' => ["\x14phar://example.com/image.phar"], |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|