Issues (2963)

tests/SVGTest.php (3 issues)

1
<?php
2
/**
3
 * SVGTest.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2017 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace LibreNMS\Tests;
27
28
use Illuminate\Support\Str;
29
use RecursiveDirectoryIterator;
30
use RecursiveIteratorIterator;
31
use RecursiveRegexIterator;
32
use RegexIterator;
33
34
/**
35
 * Class SVGTest
36
 *
37
 * @group os
38
 */
39
class SVGTest extends TestCase
40
{
41
    public function testSVGContainsPNG()
42
    {
43
        foreach ($this->getSvgFiles() as $file => $_unused) {
44
            $svg = file_get_contents($file);
0 ignored issues
show
It seems like $file can also be of type null and true; however, parameter $filename of file_get_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            $svg = file_get_contents(/** @scrutinizer ignore-type */ $file);
Loading history...
45
46
            $this->assertFalse(
47
                Str::contains($svg, 'data:image/'),
48
                "$file contains a bitmap image, please use a regular png or valid svg"
49
            );
50
        }
51
    }
52
53
    public function testSVGHasLengthWidth()
54
    {
55
        foreach ($this->getSvgFiles() as $file => $_unused) {
56
            if ($file == 'html/images/safari-pinned-tab.svg') {
57
                continue;
58
            }
59
60
            $svg = file_get_contents($file);
0 ignored issues
show
It seems like $file can also be of type null and true; however, parameter $filename of file_get_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            $svg = file_get_contents(/** @scrutinizer ignore-type */ $file);
Loading history...
61
62
            $this->assertEquals(
63
                0,
64
                preg_match('/<svg[^>]*(length|width)=/', $svg, $matches),
65
                "$file: SVG files must not contain length or width attributes "
66
            );
67
        }
68
    }
69
70
    public function testSVGHasViewBox()
71
    {
72
        foreach ($this->getSvgFiles() as $file => $_unused) {
73
            $svg = file_get_contents($file);
0 ignored issues
show
It seems like $file can also be of type null and true; however, parameter $filename of file_get_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            $svg = file_get_contents(/** @scrutinizer ignore-type */ $file);
Loading history...
74
75
            $this->assertTrue(
76
                Str::contains($svg, 'viewBox'),
77
                "$file: SVG files must have the viewBox attribute set"
78
            );
79
        }
80
    }
81
82
    private function getSvgFiles()
83
    {
84
        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('html/images'));
85
86
        return new RegexIterator($iterator, '/^.+\.svg$/i', RecursiveRegexIterator::GET_MATCH);
87
    }
88
}
89