1 | <?php |
||
2 | /* |
||
3 | * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
||
4 | * |
||
5 | * Copyright (C) 2019 - 2022 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 | declare(strict_types=1); |
||
22 | |||
23 | /** |
||
24 | * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
||
25 | * |
||
26 | * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
||
27 | * |
||
28 | * This program is free software: you can redistribute it and/or modify |
||
29 | * it under the terms of the GNU Affero General Public License as published |
||
30 | * by the Free Software Foundation, either version 3 of the License, or |
||
31 | * (at your option) any later version. |
||
32 | * |
||
33 | * This program is distributed in the hope that it will be useful, |
||
34 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
35 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
36 | * GNU Affero General Public License for more details. |
||
37 | * |
||
38 | * You should have received a copy of the GNU Affero General Public License |
||
39 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
40 | */ |
||
41 | |||
42 | namespace App\Tests\Services\LabelSystem; |
||
43 | |||
44 | use App\Entity\LabelSystem\LabelOptions; |
||
45 | use App\Entity\Parts\Part; |
||
46 | use App\Entity\Parts\PartLot; |
||
47 | use App\Entity\Parts\Storelocation; |
||
48 | use App\Services\LabelSystem\SandboxedTwigProvider; |
||
49 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
||
50 | use Twig\Sandbox\SecurityError; |
||
51 | |||
52 | class SandboxedTwigProviderTest extends WebTestCase |
||
53 | { |
||
54 | /** |
||
55 | * @var SandboxedTwigProvider |
||
56 | */ |
||
57 | private $service; |
||
58 | |||
59 | protected function setUp(): void |
||
60 | { |
||
61 | self::bootKernel(); |
||
62 | $this->service = self::$container->get(SandboxedTwigProvider::class); |
||
63 | } |
||
64 | |||
65 | public function twigDataProvider(): array |
||
66 | { |
||
67 | return [ |
||
68 | [' {% for i in range(1, 3) %} |
||
69 | {{ part.id }} |
||
70 | {{ part.name }} |
||
71 | {{ part.lastModified | format_datetime }} |
||
72 | {% endfor %} |
||
73 | '], |
||
74 | [' {% if part.category %} |
||
75 | {{ part.category }} |
||
76 | {% endif %} |
||
77 | '], |
||
78 | [' {% set a = random(1, 3) %} |
||
79 | {{ 1 + 2 | abs }} |
||
80 | {{ "test" | capitalize | escape | lower | raw }} |
||
81 | {{ "\n" | nl2br | trim | title | url_encode | reverse }} |
||
82 | '], |
||
83 | [' |
||
84 | {{ location.isRoot}} {{ location.isChildOf(location) }} {{ location.comment }} {{ location.level }} |
||
85 | {{ location.fullPath }} {% set arr = location.pathArray %} {% set child = location.children %} {{location.childrenNotSelectable}} |
||
86 | '], |
||
87 | [' |
||
88 | {{ part.reviewNeeded }} {{ part.tags }} {{ part.mass }} |
||
89 | '], |
||
90 | ]; |
||
91 | } |
||
92 | |||
93 | public function twigNotAllowedDataProvider(): array |
||
94 | { |
||
95 | return [ |
||
96 | ['{% block test %} {% endblock %}'], |
||
97 | ['{% deprecated test %}'], |
||
98 | ['{% flush %}'], |
||
99 | ["{{ part.setName('test') }}"], |
||
100 | ['{{ part.setCategory(null) }}'], |
||
101 | ]; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @dataProvider twigDataProvider |
||
106 | */ |
||
107 | public function testTwigFeatures(string $twig): void |
||
108 | { |
||
109 | $options = new LabelOptions(); |
||
110 | $options->setSupportedElement('part'); |
||
111 | $options->setLines($twig); |
||
112 | $options->setLinesMode('twig'); |
||
113 | |||
114 | $twig = $this->service->getTwig($options); |
||
115 | $str = $twig->render('lines', [ |
||
116 | 'part' => new Part(), |
||
117 | 'lot' => new PartLot(), |
||
118 | 'location' => new Storelocation(), |
||
119 | ]); |
||
120 | |||
121 | $this->assertIsString($str); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @dataProvider twigNotAllowedDataProvider |
||
126 | */ |
||
127 | public function testTwigForbidden(string $twig): void |
||
128 | { |
||
129 | $this->expectException(SecurityError::class); |
||
130 | |||
131 | $options = new LabelOptions(); |
||
132 | $options->setSupportedElement('part'); |
||
133 | $options->setLines($twig); |
||
134 | $options->setLinesMode('twig'); |
||
135 | |||
136 | $twig = $this->service->getTwig($options); |
||
137 | $str = $twig->render('lines', [ |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
138 | 'part' => new Part(), |
||
139 | 'lot' => new PartLot(), |
||
140 | 'location' => new Storelocation(), |
||
141 | ]); |
||
142 | } |
||
143 | } |
||
144 |