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 | declare(strict_types=1); |
||
22 | |||
23 | namespace App\Tests\Entity\Attachments; |
||
24 | |||
25 | use App\Entity\Attachments\Attachment; |
||
26 | use App\Entity\Attachments\AttachmentType; |
||
27 | use App\Entity\Attachments\AttachmentTypeAttachment; |
||
28 | use App\Entity\Attachments\CategoryAttachment; |
||
29 | use App\Entity\Attachments\CurrencyAttachment; |
||
30 | use App\Entity\Attachments\ProjectAttachment; |
||
31 | use App\Entity\Attachments\FootprintAttachment; |
||
32 | use App\Entity\Attachments\GroupAttachment; |
||
33 | use App\Entity\Attachments\ManufacturerAttachment; |
||
34 | use App\Entity\Attachments\MeasurementUnitAttachment; |
||
35 | use App\Entity\Attachments\PartAttachment; |
||
36 | use App\Entity\Attachments\StorelocationAttachment; |
||
37 | use App\Entity\Attachments\SupplierAttachment; |
||
38 | use App\Entity\Attachments\UserAttachment; |
||
39 | use App\Entity\ProjectSystem\Project; |
||
40 | use App\Entity\Parts\Category; |
||
41 | use App\Entity\Parts\Footprint; |
||
42 | use App\Entity\Parts\Manufacturer; |
||
43 | use App\Entity\Parts\MeasurementUnit; |
||
44 | use App\Entity\Parts\Part; |
||
45 | use App\Entity\Parts\Storelocation; |
||
46 | use App\Entity\Parts\Supplier; |
||
47 | use App\Entity\PriceInformations\Currency; |
||
48 | use App\Entity\UserSystem\Group; |
||
49 | use App\Entity\UserSystem\User; |
||
50 | use InvalidArgumentException; |
||
51 | use PHPUnit\Framework\TestCase; |
||
0 ignored issues
–
show
|
|||
52 | use ReflectionClass; |
||
53 | |||
54 | class AttachmentTest extends TestCase |
||
55 | { |
||
56 | public function testEmptyState(): void |
||
57 | { |
||
58 | $attachment = new PartAttachment(); |
||
59 | |||
60 | $this->assertNull($attachment->getAttachmentType()); |
||
61 | $this->assertFalse($attachment->isPicture()); |
||
62 | $this->assertFalse($attachment->isExternal()); |
||
63 | $this->assertFalse($attachment->isSecure()); |
||
64 | $this->assertFalse($attachment->isBuiltIn()); |
||
65 | $this->assertFalse($attachment->is3DModel()); |
||
66 | $this->assertFalse($attachment->getShowInTable()); |
||
67 | $this->assertEmpty($attachment->getPath()); |
||
68 | $this->assertEmpty($attachment->getName()); |
||
69 | $this->assertEmpty($attachment->getURL()); |
||
70 | $this->assertEmpty($attachment->getExtension()); |
||
71 | $this->assertNull($attachment->getElement()); |
||
72 | $this->assertEmpty($attachment->getFilename()); |
||
73 | } |
||
74 | |||
75 | public function subClassesDataProvider(): array |
||
76 | { |
||
77 | return [ |
||
78 | [AttachmentTypeAttachment::class, AttachmentType::class], |
||
79 | [CategoryAttachment::class, Category::class], |
||
80 | [CurrencyAttachment::class, Currency::class], |
||
81 | [ProjectAttachment::class, Project::class], |
||
82 | [FootprintAttachment::class, Footprint::class], |
||
83 | [GroupAttachment::class, Group::class], |
||
84 | [ManufacturerAttachment::class, Manufacturer::class], |
||
85 | [MeasurementUnitAttachment::class, MeasurementUnit::class], |
||
86 | [PartAttachment::class, Part::class], |
||
87 | [StorelocationAttachment::class, Storelocation::class], |
||
88 | [SupplierAttachment::class, Supplier::class], |
||
89 | [UserAttachment::class, User::class], |
||
90 | ]; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @dataProvider subClassesDataProvider |
||
95 | */ |
||
96 | public function testSetElement(string $attachment_class, string $allowed_class): void |
||
97 | { |
||
98 | /** @var Attachment $attachment */ |
||
99 | $attachment = new $attachment_class(); |
||
100 | $element = new $allowed_class(); |
||
101 | |||
102 | //This must not throw an exception |
||
103 | $attachment->setElement($element); |
||
104 | $this->assertSame($element, $attachment->getElement()); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Test that all attachment subclasses like PartAttachment or similar returns an exception, when an not allowed |
||
109 | * element is passed. |
||
110 | * |
||
111 | * @dataProvider subClassesDataProvider |
||
112 | * @depends testSetElement |
||
113 | */ |
||
114 | public function testSetElementExceptionOnSubClasses(string $attachment_class, string $allowed_class): void |
||
115 | { |
||
116 | $this->expectException(InvalidArgumentException::class); |
||
117 | |||
118 | /** @var Attachment $attachment */ |
||
119 | $attachment = new $attachment_class(); |
||
120 | if (Project::class !== $allowed_class) { |
||
121 | $element = new Project(); |
||
122 | } else { |
||
123 | $element = new Category(); |
||
124 | } |
||
125 | $attachment->setElement($element); |
||
126 | } |
||
127 | |||
128 | public function externalDataProvider(): array |
||
129 | { |
||
130 | return [ |
||
131 | ['', false], |
||
132 | ['%MEDIA%/foo/bar.txt', false], |
||
133 | ['%BASE%/foo/bar.jpg', false], |
||
134 | ['%FOOTPRINTS%/foo/bar.jpg', false], |
||
135 | ['%FOOTPRINTS3D%/foo/bar.jpg', false], |
||
136 | ['%SECURE%/test.txt', false], |
||
137 | ['%test%/foo/bar.ghp', true], |
||
138 | ['foo%MEDIA%/foo.jpg', true], |
||
139 | ['foo%MEDIA%/%BASE%foo.jpg', true], |
||
140 | ]; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @dataProvider externalDataProvider |
||
145 | */ |
||
146 | public function testIsExternal($path, $expected): void |
||
147 | { |
||
148 | $attachment = new PartAttachment(); |
||
149 | $this->setProtectedProperty($attachment, 'path', $path); |
||
150 | $this->assertSame($expected, $attachment->isExternal()); |
||
151 | } |
||
152 | |||
153 | public function extensionDataProvider(): array |
||
154 | { |
||
155 | return [ |
||
156 | ['%MEDIA%/foo/bar.txt', null, 'txt'], |
||
157 | ['%MEDIA%/foo/bar.JPeg', null, 'jpeg'], |
||
158 | ['%MEDIA%/foo/bar.JPeg', 'test.txt', 'txt'], |
||
159 | ['%MEDIA%/foo/bar', null, ''], |
||
160 | ['%MEDIA%/foo.bar', 'bar', ''], |
||
161 | ['http://google.de', null, null], |
||
162 | ['https://foo.bar', null, null], |
||
163 | ['https://foo.bar/test.jpeg', null, null], |
||
164 | ['test', null, null], |
||
165 | ['test.txt', null, null], |
||
166 | ]; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @dataProvider extensionDataProvider |
||
171 | */ |
||
172 | public function testGetExtension($path, $originalFilename, $expected): void |
||
173 | { |
||
174 | $attachment = new PartAttachment(); |
||
175 | $this->setProtectedProperty($attachment, 'path', $path); |
||
176 | $this->setProtectedProperty($attachment, 'original_filename', $originalFilename); |
||
177 | $this->assertSame($expected, $attachment->getExtension()); |
||
178 | } |
||
179 | |||
180 | public function pictureDataProvider(): array |
||
181 | { |
||
182 | return [ |
||
183 | ['%MEDIA%/foo/bar.txt', false], |
||
184 | ['https://test.de/picture.jpeg', true], |
||
185 | ['https://test.de', true], |
||
186 | ['http://test.de/google.de', true], |
||
187 | ['%MEDIA%/foo/bar.jpeg', true], |
||
188 | ['%MEDIA%/foo/bar.webp', true], |
||
189 | ['%MEDIA%/foo', false], |
||
190 | ['%SECURE%/foo.txt/test', false], |
||
191 | ]; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @dataProvider pictureDataProvider |
||
196 | */ |
||
197 | public function testIsPicture($path, $expected): void |
||
198 | { |
||
199 | $attachment = new PartAttachment(); |
||
200 | $this->setProtectedProperty($attachment, 'path', $path); |
||
201 | $this->assertSame($expected, $attachment->isPicture()); |
||
202 | } |
||
203 | |||
204 | public function builtinDataProvider(): array |
||
205 | { |
||
206 | return [ |
||
207 | ['', false], |
||
208 | ['%MEDIA%/foo/bar.txt', false], |
||
209 | ['%BASE%/foo/bar.txt', false], |
||
210 | ['/', false], |
||
211 | ['https://google.de', false], |
||
212 | ['%FOOTPRINTS%/foo/bar.txt', true], |
||
213 | ]; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @dataProvider builtinDataProvider |
||
218 | */ |
||
219 | public function testIsBuiltIn($path, $expected): void |
||
220 | { |
||
221 | $attachment = new PartAttachment(); |
||
222 | $this->setProtectedProperty($attachment, 'path', $path); |
||
223 | $this->assertSame($expected, $attachment->isBuiltIn()); |
||
224 | } |
||
225 | |||
226 | public function hostDataProvider(): array |
||
227 | { |
||
228 | return [ |
||
229 | ['%MEDIA%/foo/bar.txt', null], |
||
230 | ['https://www.google.de/test.txt', 'www.google.de'], |
||
231 | ['https://foo.bar/test?txt=test', 'foo.bar'], |
||
232 | ]; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @dataProvider hostDataProvider |
||
237 | */ |
||
238 | public function testGetHost($path, $expected): void |
||
239 | { |
||
240 | $attachment = new PartAttachment(); |
||
241 | $this->setProtectedProperty($attachment, 'path', $path); |
||
242 | $this->assertSame($expected, $attachment->getHost()); |
||
243 | } |
||
244 | |||
245 | public function filenameProvider(): array |
||
246 | { |
||
247 | return [ |
||
248 | ['%MEDIA%/foo/bar.txt', null, 'bar.txt'], |
||
249 | ['%MEDIA%/foo/bar.JPeg', 'test.txt', 'test.txt'], |
||
250 | ['https://www.google.de/test.txt', null, null], |
||
251 | ]; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @dataProvider filenameProvider |
||
256 | */ |
||
257 | public function testGetFilename($path, $original_filename, $expected): void |
||
258 | { |
||
259 | $attachment = new PartAttachment(); |
||
260 | $this->setProtectedProperty($attachment, 'path', $path); |
||
261 | $this->setProtectedProperty($attachment, 'original_filename', $original_filename); |
||
262 | $this->assertSame($expected, $attachment->getFilename()); |
||
263 | } |
||
264 | |||
265 | public function testIsURL(): void |
||
266 | { |
||
267 | $url = '%MEDIA%/test.txt'; |
||
268 | $this->assertFalse(Attachment::isValidURL($url)); |
||
269 | |||
270 | $url = 'https://google.de'; |
||
271 | $this->assertFalse(Attachment::isValidURL($url)); |
||
272 | |||
273 | $url = 'ftp://google.de'; |
||
274 | $this->assertTrue(Attachment::isValidURL($url, false, false)); |
||
275 | $this->assertFalse(Attachment::isValidURL($url, false, true)); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Sets a protected property on a given object via reflection. |
||
280 | * |
||
281 | * @param object $object - instance in which protected value is being modified |
||
282 | * @param string $property - property on instance being modified |
||
283 | * @param mixed $value - new value of the property being modified |
||
284 | */ |
||
285 | public function setProtectedProperty($object, $property, $value): void |
||
286 | { |
||
287 | $reflection = new ReflectionClass($object); |
||
288 | $reflection_property = $reflection->getProperty($property); |
||
289 | $reflection_property->setAccessible(true); |
||
290 | $reflection_property->setValue($object, $value); |
||
291 | } |
||
292 | } |
||
293 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths