Passed
Push — master ( ec0d02...c0f595 )
by Jan
06:31 queued 10s
created

twigNotAllowedDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by 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 Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace App\Tests\Services\LabelSystem;
22
23
use App\Entity\LabelSystem\LabelOptions;
24
use App\Entity\Parts\Part;
25
use App\Entity\Parts\PartLot;
26
use App\Entity\Parts\Storelocation;
27
use App\Services\LabelSystem\Barcodes\BarcodeExampleElementsGenerator;
28
use App\Services\LabelSystem\SandboxedTwigProvider;
29
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
31
use Twig\Sandbox\SecurityError;
32
33
class SandboxedTwigProviderTest extends WebTestCase
34
{
35
    /** @var SandboxedTwigProvider */
36
    private $service;
37
38
    public function setUp(): void
39
    {
40
        self::bootKernel();
41
        $this->service = self::$container->get(SandboxedTwigProvider::class);
42
    }
43
44
    public function twigDataProvider(): array
45
    {
46
        return [
47
            [' {% for i in range(1, 3) %}
48
                    {{ part.id }}
49
                    {{ part.name }}
50
                    {{ part.lastModified | format_datetime }}
51
               {% endfor %}
52
            '],
53
            [' {% if part.category %}
54
                   {{ part.category }}
55
               {% endif %}
56
            '],
57
            [' {% set a = random(1, 3) %}
58
               {{ 1 + 2 | abs }}
59
               {{ "test" | capitalize | escape | lower | raw }}
60
               {{ "\n"  | nl2br | trim | title | url_encode | reverse }}
61
            '],
62
            ['
63
                {{ location.isRoot}} {{ location.isChildOf(location) }} {{ location.comment }} {{ location.level }}
64
                {{ location.fullPath }} {% set arr =  location.pathArray %} {% set child = location.children %} {{location.childrenNotSelectable}}
65
            '],
66
            ['
67
                {{ part.reviewNeeded }} {{ part.tags }} {{ part.mass }}
68
            ']
69
        ];
70
    }
71
72
    public function twigNotAllowedDataProvider(): array
73
    {
74
        return [
75
            ["{% block test %} {% endblock %}"],
76
            ["{% deprecated test %}"],
77
            ["{% flush %}"],
78
            ["{{ part.setName('test') }}"],
79
            ["{{ part.setCategory(null) }}"]
80
        ];
81
    }
82
83
84
    /**
85
     * @dataProvider twigDataProvider
86
     */
87
    public function testTwigFeatures(string $twig)
88
    {
89
        $options = new LabelOptions();
90
        $options->setSupportedElement('part');
91
        $options->setLines($twig);
92
        $options->setLinesMode('twig');
93
94
        $twig = $this->service->getTwig($options);
95
        $str = $twig->render('lines', [
96
            'part' => new Part(),
97
            'lot' => new PartLot(),
98
            'location' => new Storelocation(),
99
        ]);
100
101
        $this->assertIsString($str);
102
    }
103
104
    /**
105
     * @dataProvider twigNotAllowedDataProvider
106
     */
107
    public function testTwigForbidden(string $twig)
108
    {
109
        $this->expectException(SecurityError::class);
110
111
        $options = new LabelOptions();
112
        $options->setSupportedElement('part');
113
        $options->setLines($twig);
114
        $options->setLinesMode('twig');
115
116
        $twig = $this->service->getTwig($options);
117
        $str = $twig->render('lines', [
0 ignored issues
show
Unused Code introduced by
The assignment to $str is dead and can be removed.
Loading history...
118
            'part' => new Part(),
119
            'lot' => new PartLot(),
120
            'location' => new Storelocation(),
121
        ]);
122
    }
123
}
124