2019-09-22 23:47:40 +02:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-11-08 22:05:12 +01:00
|
|
|
namespace App\Tests\Entity\Attachments;
|
2019-09-22 23:47:40 +02:00
|
|
|
|
|
|
|
use App\Entity\Attachments\Attachment;
|
2019-11-08 22:05:12 +01:00
|
|
|
use App\Entity\Attachments\AttachmentType;
|
|
|
|
use App\Entity\Attachments\AttachmentTypeAttachment;
|
|
|
|
use App\Entity\Attachments\CategoryAttachment;
|
|
|
|
use App\Entity\Attachments\CurrencyAttachment;
|
2022-12-18 20:34:25 +01:00
|
|
|
use App\Entity\Attachments\ProjectAttachment;
|
2019-11-08 22:05:12 +01:00
|
|
|
use App\Entity\Attachments\FootprintAttachment;
|
|
|
|
use App\Entity\Attachments\GroupAttachment;
|
|
|
|
use App\Entity\Attachments\ManufacturerAttachment;
|
|
|
|
use App\Entity\Attachments\MeasurementUnitAttachment;
|
2019-09-22 23:47:40 +02:00
|
|
|
use App\Entity\Attachments\PartAttachment;
|
2023-09-04 22:57:40 +02:00
|
|
|
use App\Entity\Attachments\StorageLocationAttachment;
|
2019-11-08 22:05:12 +01:00
|
|
|
use App\Entity\Attachments\SupplierAttachment;
|
|
|
|
use App\Entity\Attachments\UserAttachment;
|
2022-12-18 20:34:25 +01:00
|
|
|
use App\Entity\ProjectSystem\Project;
|
2019-11-08 22:05:12 +01:00
|
|
|
use App\Entity\Parts\Category;
|
|
|
|
use App\Entity\Parts\Footprint;
|
|
|
|
use App\Entity\Parts\Manufacturer;
|
|
|
|
use App\Entity\Parts\MeasurementUnit;
|
|
|
|
use App\Entity\Parts\Part;
|
2023-09-04 22:57:40 +02:00
|
|
|
use App\Entity\Parts\StorageLocation;
|
2019-11-08 22:05:12 +01:00
|
|
|
use App\Entity\Parts\Supplier;
|
|
|
|
use App\Entity\PriceInformations\Currency;
|
|
|
|
use App\Entity\UserSystem\Group;
|
|
|
|
use App\Entity\UserSystem\User;
|
2020-01-05 22:49:00 +01:00
|
|
|
use InvalidArgumentException;
|
2019-09-22 23:47:40 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use ReflectionClass;
|
|
|
|
|
|
|
|
class AttachmentTest extends TestCase
|
|
|
|
{
|
2019-11-09 00:47:20 +01:00
|
|
|
public function testEmptyState(): void
|
2019-11-08 22:05:12 +01:00
|
|
|
{
|
|
|
|
$attachment = new PartAttachment();
|
|
|
|
|
|
|
|
$this->assertNull($attachment->getAttachmentType());
|
|
|
|
$this->assertFalse($attachment->isPicture());
|
|
|
|
$this->assertFalse($attachment->isExternal());
|
|
|
|
$this->assertFalse($attachment->isSecure());
|
|
|
|
$this->assertFalse($attachment->isBuiltIn());
|
|
|
|
$this->assertFalse($attachment->is3DModel());
|
|
|
|
$this->assertFalse($attachment->getShowInTable());
|
|
|
|
$this->assertEmpty($attachment->getPath());
|
|
|
|
$this->assertEmpty($attachment->getName());
|
|
|
|
$this->assertEmpty($attachment->getURL());
|
|
|
|
$this->assertEmpty($attachment->getExtension());
|
|
|
|
$this->assertNull($attachment->getElement());
|
|
|
|
$this->assertEmpty($attachment->getFilename());
|
|
|
|
}
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
public function subClassesDataProvider(): array
|
2019-11-08 22:05:12 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
[AttachmentTypeAttachment::class, AttachmentType::class],
|
|
|
|
[CategoryAttachment::class, Category::class],
|
|
|
|
[CurrencyAttachment::class, Currency::class],
|
2022-12-18 20:34:25 +01:00
|
|
|
[ProjectAttachment::class, Project::class],
|
2019-11-08 22:05:12 +01:00
|
|
|
[FootprintAttachment::class, Footprint::class],
|
|
|
|
[GroupAttachment::class, Group::class],
|
|
|
|
[ManufacturerAttachment::class, Manufacturer::class],
|
|
|
|
[MeasurementUnitAttachment::class, MeasurementUnit::class],
|
|
|
|
[PartAttachment::class, Part::class],
|
2023-09-04 22:57:40 +02:00
|
|
|
[StorageLocationAttachment::class, StorageLocation::class],
|
2019-11-08 22:05:12 +01:00
|
|
|
[SupplierAttachment::class, Supplier::class],
|
2019-11-09 00:47:20 +01:00
|
|
|
[UserAttachment::class, User::class],
|
2019-11-08 22:05:12 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider subClassesDataProvider
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function testSetElement(string $attachment_class, string $allowed_class): void
|
2019-11-08 22:05:12 +01:00
|
|
|
{
|
|
|
|
/** @var Attachment $attachment */
|
|
|
|
$attachment = new $attachment_class();
|
|
|
|
$element = new $allowed_class();
|
|
|
|
|
|
|
|
//This must not throw an exception
|
|
|
|
$attachment->setElement($element);
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($element, $attachment->getElement());
|
2019-11-08 22:05:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-15 23:14:53 +02:00
|
|
|
* Test that all attachment subclasses like PartAttachment or similar returns an exception, when a not allowed
|
2019-11-08 22:05:12 +01:00
|
|
|
* element is passed.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-11-08 22:05:12 +01:00
|
|
|
* @dataProvider subClassesDataProvider
|
|
|
|
* @depends testSetElement
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function testSetElementExceptionOnSubClasses(string $attachment_class, string $allowed_class): void
|
2019-11-08 22:05:12 +01:00
|
|
|
{
|
2020-01-05 22:49:00 +01:00
|
|
|
$this->expectException(InvalidArgumentException::class);
|
2019-11-08 22:05:12 +01:00
|
|
|
|
|
|
|
/** @var Attachment $attachment */
|
|
|
|
$attachment = new $attachment_class();
|
2022-12-18 20:34:25 +01:00
|
|
|
if (Project::class !== $allowed_class) {
|
|
|
|
$element = new Project();
|
2019-11-08 22:05:12 +01:00
|
|
|
} else {
|
|
|
|
$element = new Category();
|
|
|
|
}
|
|
|
|
$attachment->setElement($element);
|
|
|
|
}
|
|
|
|
|
2020-03-29 22:47:25 +02:00
|
|
|
public function externalDataProvider(): array
|
2019-09-22 23:47:40 +02:00
|
|
|
{
|
2019-10-20 00:24:52 +02:00
|
|
|
return [
|
|
|
|
['', false],
|
|
|
|
['%MEDIA%/foo/bar.txt', false],
|
|
|
|
['%BASE%/foo/bar.jpg', false],
|
|
|
|
['%FOOTPRINTS%/foo/bar.jpg', false],
|
|
|
|
['%FOOTPRINTS3D%/foo/bar.jpg', false],
|
|
|
|
['%SECURE%/test.txt', false],
|
|
|
|
['%test%/foo/bar.ghp', true],
|
|
|
|
['foo%MEDIA%/foo.jpg', true],
|
2019-11-09 00:47:20 +01:00
|
|
|
['foo%MEDIA%/%BASE%foo.jpg', true],
|
2019-10-20 00:24:52 +02:00
|
|
|
];
|
2019-09-22 23:47:40 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 00:24:52 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider externalDataProvider
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testIsExternal($path, $expected): void
|
2019-09-22 23:47:40 +02:00
|
|
|
{
|
|
|
|
$attachment = new PartAttachment();
|
2019-10-20 00:24:52 +02:00
|
|
|
$this->setProtectedProperty($attachment, 'path', $path);
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($expected, $attachment->isExternal());
|
2019-10-20 00:24:52 +02:00
|
|
|
}
|
2019-09-22 23:47:40 +02:00
|
|
|
|
2020-03-29 22:47:25 +02:00
|
|
|
public function extensionDataProvider(): array
|
2019-10-20 00:24:52 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
['%MEDIA%/foo/bar.txt', null, 'txt'],
|
|
|
|
['%MEDIA%/foo/bar.JPeg', null, 'jpeg'],
|
|
|
|
['%MEDIA%/foo/bar.JPeg', 'test.txt', 'txt'],
|
|
|
|
['%MEDIA%/foo/bar', null, ''],
|
|
|
|
['%MEDIA%/foo.bar', 'bar', ''],
|
|
|
|
['http://google.de', null, null],
|
|
|
|
['https://foo.bar', null, null],
|
|
|
|
['https://foo.bar/test.jpeg', null, null],
|
|
|
|
['test', null, null],
|
|
|
|
['test.txt', null, null],
|
|
|
|
];
|
2019-09-22 23:47:40 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 00:24:52 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider extensionDataProvider
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testGetExtension($path, $originalFilename, $expected): void
|
2019-09-22 23:47:40 +02:00
|
|
|
{
|
|
|
|
$attachment = new PartAttachment();
|
2019-10-20 00:24:52 +02:00
|
|
|
$this->setProtectedProperty($attachment, 'path', $path);
|
|
|
|
$this->setProtectedProperty($attachment, 'original_filename', $originalFilename);
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($expected, $attachment->getExtension());
|
2019-10-20 00:24:52 +02:00
|
|
|
}
|
2019-09-22 23:47:40 +02:00
|
|
|
|
2020-03-29 22:47:25 +02:00
|
|
|
public function pictureDataProvider(): array
|
2019-10-20 00:24:52 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
['%MEDIA%/foo/bar.txt', false],
|
|
|
|
['https://test.de/picture.jpeg', true],
|
2023-07-29 18:50:18 +02:00
|
|
|
['https://test.de/picture.png?test=fdsj&width=34', true],
|
|
|
|
['https://invalid.invalid/file.txt', false],
|
|
|
|
['http://infsf.inda/file.zip?test', false],
|
2019-10-20 00:24:52 +02:00
|
|
|
['https://test.de', true],
|
2023-07-29 18:50:18 +02:00
|
|
|
['https://invalid.com/invalid/pic', true],
|
2019-10-20 00:24:52 +02:00
|
|
|
['%MEDIA%/foo/bar.jpeg', true],
|
|
|
|
['%MEDIA%/foo/bar.webp', true],
|
|
|
|
['%MEDIA%/foo', false],
|
|
|
|
['%SECURE%/foo.txt/test', false],
|
|
|
|
];
|
2019-09-22 23:47:40 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 00:24:52 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider pictureDataProvider
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testIsPicture($path, $expected): void
|
2019-09-25 16:03:22 +02:00
|
|
|
{
|
|
|
|
$attachment = new PartAttachment();
|
2019-10-20 00:24:52 +02:00
|
|
|
$this->setProtectedProperty($attachment, 'path', $path);
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($expected, $attachment->isPicture());
|
2019-10-20 00:24:52 +02:00
|
|
|
}
|
2019-09-25 16:03:22 +02:00
|
|
|
|
2020-03-29 22:47:25 +02:00
|
|
|
public function builtinDataProvider(): array
|
2019-10-20 00:24:52 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
['', false],
|
|
|
|
['%MEDIA%/foo/bar.txt', false],
|
|
|
|
['%BASE%/foo/bar.txt', false],
|
|
|
|
['/', false],
|
|
|
|
['https://google.de', false],
|
2019-11-09 00:47:20 +01:00
|
|
|
['%FOOTPRINTS%/foo/bar.txt', true],
|
2019-10-20 00:24:52 +02:00
|
|
|
];
|
2019-09-25 16:03:22 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 00:24:52 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider builtinDataProvider
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testIsBuiltIn($path, $expected): void
|
2019-09-22 23:47:40 +02:00
|
|
|
{
|
|
|
|
$attachment = new PartAttachment();
|
2019-10-20 00:24:52 +02:00
|
|
|
$this->setProtectedProperty($attachment, 'path', $path);
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($expected, $attachment->isBuiltIn());
|
2019-10-20 00:24:52 +02:00
|
|
|
}
|
2019-09-22 23:47:40 +02:00
|
|
|
|
2020-03-29 22:47:25 +02:00
|
|
|
public function hostDataProvider(): array
|
2019-10-20 00:24:52 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
['%MEDIA%/foo/bar.txt', null],
|
|
|
|
['https://www.google.de/test.txt', 'www.google.de'],
|
|
|
|
['https://foo.bar/test?txt=test', 'foo.bar'],
|
|
|
|
];
|
2019-09-22 23:47:40 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 00:24:52 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider hostDataProvider
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testGetHost($path, $expected): void
|
2019-09-22 23:47:40 +02:00
|
|
|
{
|
|
|
|
$attachment = new PartAttachment();
|
2019-10-20 00:24:52 +02:00
|
|
|
$this->setProtectedProperty($attachment, 'path', $path);
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($expected, $attachment->getHost());
|
2019-10-20 00:24:52 +02:00
|
|
|
}
|
2019-09-22 23:47:40 +02:00
|
|
|
|
2020-03-29 22:47:25 +02:00
|
|
|
public function filenameProvider(): array
|
2019-10-20 00:24:52 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
['%MEDIA%/foo/bar.txt', null, 'bar.txt'],
|
|
|
|
['%MEDIA%/foo/bar.JPeg', 'test.txt', 'test.txt'],
|
2019-11-09 00:47:20 +01:00
|
|
|
['https://www.google.de/test.txt', null, null],
|
2019-10-20 00:24:52 +02:00
|
|
|
];
|
|
|
|
}
|
2019-09-24 16:36:41 +02:00
|
|
|
|
2019-10-20 00:24:52 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider filenameProvider
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testGetFilename($path, $original_filename, $expected): void
|
2019-10-20 00:24:52 +02:00
|
|
|
{
|
|
|
|
$attachment = new PartAttachment();
|
|
|
|
$this->setProtectedProperty($attachment, 'path', $path);
|
|
|
|
$this->setProtectedProperty($attachment, 'original_filename', $original_filename);
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($expected, $attachment->getFilename());
|
2019-09-22 23:47:40 +02:00
|
|
|
}
|
|
|
|
|
2023-10-15 00:50:43 +02:00
|
|
|
public function testSetURL(): void
|
|
|
|
{
|
|
|
|
$attachment = new PartAttachment();
|
|
|
|
|
|
|
|
//Set URL
|
|
|
|
$attachment->setURL('https://google.de');
|
|
|
|
$this->assertSame('https://google.de', $attachment->getURL());
|
|
|
|
|
|
|
|
//Ensure that an empty url does not overwrite the existing one
|
|
|
|
$attachment->setPath('%MEDIA%/foo/bar.txt');
|
|
|
|
$attachment->setURL(' ');
|
|
|
|
$this->assertSame('%MEDIA%/foo/bar.txt', $attachment->getPath());
|
|
|
|
|
|
|
|
//Ensure that spaces get replaced by %20
|
|
|
|
$attachment->setURL('https://google.de/test file.txt');
|
|
|
|
$this->assertSame('https://google.de/test%20file.txt', $attachment->getURL());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetURLForbiddenURL(): void
|
|
|
|
{
|
|
|
|
$attachment = new PartAttachment();
|
|
|
|
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$attachment->setURL('%MEDIA%/foo/bar.txt');
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testIsURL(): void
|
2019-09-22 23:47:40 +02:00
|
|
|
{
|
|
|
|
$url = '%MEDIA%/test.txt';
|
2020-05-31 15:03:27 +02:00
|
|
|
$this->assertFalse(Attachment::isValidURL($url));
|
2019-09-22 23:47:40 +02:00
|
|
|
|
|
|
|
$url = 'https://google.de';
|
2020-05-31 15:03:27 +02:00
|
|
|
$this->assertFalse(Attachment::isValidURL($url));
|
2019-09-22 23:47:40 +02:00
|
|
|
|
|
|
|
$url = 'ftp://google.de';
|
2020-05-31 15:03:27 +02:00
|
|
|
$this->assertTrue(Attachment::isValidURL($url, false, false));
|
|
|
|
$this->assertFalse(Attachment::isValidURL($url, false, true));
|
2019-09-22 23:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* Sets a protected property on a given object via reflection.
|
2019-09-22 23:47:40 +02:00
|
|
|
*
|
2019-11-09 00:47:20 +01:00
|
|
|
* @param object $object - instance in which protected value is being modified
|
2019-09-22 23:47:40 +02:00
|
|
|
* @param string $property - property on instance being modified
|
2019-11-09 00:47:20 +01:00
|
|
|
* @param mixed $value - new value of the property being modified
|
2019-09-22 23:47:40 +02:00
|
|
|
*/
|
2023-06-11 14:18:53 +02:00
|
|
|
public function setProtectedProperty(object $object, string $property, mixed $value): void
|
2019-09-22 23:47:40 +02:00
|
|
|
{
|
|
|
|
$reflection = new ReflectionClass($object);
|
|
|
|
$reflection_property = $reflection->getProperty($property);
|
|
|
|
$reflection_property->setAccessible(true);
|
|
|
|
$reflection_property->setValue($object, $value);
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|