Passed
Push — master ( 63ccb0...2d493d )
by Owen
15:36
created

NoJavascriptLinksTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 16
c 1
b 0
f 0
dl 0
loc 20
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A testNoJavascriptLinks() 0 18 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
6
7
use PhpOffice\PhpSpreadsheet\Cell\Hyperlink;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PhpOffice\PhpSpreadsheet\Writer\Html;
10
use PHPUnit\Framework\TestCase;
11
12
class NoJavascriptLinksTest extends TestCase
13
{
14
    public function testNoJavascriptLinks(): void
15
    {
16
        $spreadsheet = new Spreadsheet();
17
        $sheet = $spreadsheet->getActiveSheet();
18
        $sheet->getCell('A1')->setValue('Click me');
19
        $hyperlink = new Hyperlink('http://www.example.com');
20
        $sheet->getCell('A1')->setHyperlink($hyperlink);
21
        $sheet->getCell('A2')->setValue('JS link');
22
        $hyperlink2 = new Hyperlink('javascript:alert(\'hello1\')');
23
        $sheet->getCell('A2')->setHyperlink($hyperlink2);
24
        $sheet->getCell('A3')->setValue('=HYPERLINK("javascript:alert(\'hello2\')", "jsfunc click")');
25
26
        $writer = new Html($spreadsheet);
27
        $html = $writer->generateHTMLAll();
28
        self::assertStringContainsString('<td class="column0 style0 s"><a href="http://www.example.com" title="">Click me</a></td>', $html, 'http hyperlink retained');
29
        self::assertStringContainsString('<td class="column0 style0 s">javascript:alert(\'hello1\')</td>', $html, 'javascript hyperlink dropped');
30
        self::assertStringContainsString('<td class="column0 style0 f">javascript:alert(\'hello2\')</td>', $html, 'javascript hyperlink function dropped');
31
        $spreadsheet->disconnectWorksheets();
32
    }
33
}
34