2019-09-22 23:47:40 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-09-22 23:47:40 +02:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-09-22 23:47:40 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
use App\Entity\Attachments\DeviceAttachment;
|
|
|
|
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;
|
2019-11-08 22:05:12 +01:00
|
|
|
use App\Entity\Attachments\StorelocationAttachment;
|
|
|
|
use App\Entity\Attachments\SupplierAttachment;
|
|
|
|
use App\Entity\Attachments\UserAttachment;
|
|
|
|
use App\Entity\Devices\Device;
|
|
|
|
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;
|
|
|
|
use App\Entity\Parts\Storelocation;
|
|
|
|
use App\Entity\Parts\Supplier;
|
|
|
|
use App\Entity\PriceInformations\Currency;
|
|
|
|
use App\Entity\UserSystem\Group;
|
|
|
|
use App\Entity\UserSystem\User;
|
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],
|
|
|
|
[DeviceAttachment::class, Device::class],
|
|
|
|
[FootprintAttachment::class, Footprint::class],
|
|
|
|
[GroupAttachment::class, Group::class],
|
|
|
|
[ManufacturerAttachment::class, Manufacturer::class],
|
|
|
|
[MeasurementUnitAttachment::class, MeasurementUnit::class],
|
|
|
|
[PartAttachment::class, Part::class],
|
|
|
|
[StorelocationAttachment::class, Storelocation::class],
|
|
|
|
[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);
|
|
|
|
$this->assertEquals($element, $attachment->getElement());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that all attachment subclasses like PartAttachment or similar returns an exception, when an not allowed
|
|
|
|
* 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
|
|
|
{
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
|
|
|
|
/** @var Attachment $attachment */
|
|
|
|
$attachment = new $attachment_class();
|
2019-11-09 00:47:20 +01:00
|
|
|
if (Device::class !== $allowed_class) {
|
2019-11-08 22:05:12 +01:00
|
|
|
$element = new Device();
|
|
|
|
} else {
|
|
|
|
$element = new Category();
|
|
|
|
}
|
|
|
|
$attachment->setElement($element);
|
|
|
|
}
|
|
|
|
|
2019-10-20 00:24:52 +02:00
|
|
|
public function externalDataProvider()
|
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
|
|
|
|
*/
|
|
|
|
public function testIsExternal($path, $expected)
|
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->assertEquals($expected, $attachment->isExternal());
|
|
|
|
}
|
2019-09-22 23:47:40 +02:00
|
|
|
|
2019-10-20 00:24:52 +02:00
|
|
|
public function extensionDataProvider()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function testGetExtension($path, $originalFilename, $expected)
|
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);
|
|
|
|
$this->assertEquals($expected, $attachment->getExtension());
|
|
|
|
}
|
2019-09-22 23:47:40 +02:00
|
|
|
|
2019-10-20 00:24:52 +02:00
|
|
|
public function pictureDataProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['%MEDIA%/foo/bar.txt', false],
|
|
|
|
['https://test.de/picture.jpeg', true],
|
|
|
|
['https://test.de', true],
|
|
|
|
['http://test.de/google.de', true],
|
|
|
|
['%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
|
|
|
|
*/
|
|
|
|
public function testIsPicture($path, $expected)
|
2019-09-25 16:03:22 +02:00
|
|
|
{
|
|
|
|
$attachment = new PartAttachment();
|
2019-10-20 00:24:52 +02:00
|
|
|
$this->setProtectedProperty($attachment, 'path', $path);
|
|
|
|
$this->assertEquals($expected, $attachment->isPicture());
|
|
|
|
}
|
2019-09-25 16:03:22 +02:00
|
|
|
|
2019-10-20 00:24:52 +02:00
|
|
|
public function builtinDataProvider()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function testIsBuiltIn($path, $expected)
|
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->assertEquals($expected, $attachment->isBuiltIn());
|
|
|
|
}
|
2019-09-22 23:47:40 +02:00
|
|
|
|
2019-10-20 00:24:52 +02:00
|
|
|
public function hostDataProvider()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function testGetHost($path, $expected)
|
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->assertEquals($expected, $attachment->getHost());
|
|
|
|
}
|
2019-09-22 23:47:40 +02:00
|
|
|
|
2019-10-20 00:24:52 +02:00
|
|
|
public function filenameProvider()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function testGetFilename($path, $original_filename, $expected)
|
|
|
|
{
|
|
|
|
$attachment = new PartAttachment();
|
|
|
|
$this->setProtectedProperty($attachment, 'path', $path);
|
|
|
|
$this->setProtectedProperty($attachment, 'original_filename', $original_filename);
|
|
|
|
$this->assertEquals($expected, $attachment->getFilename());
|
2019-09-22 23:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsURL()
|
|
|
|
{
|
|
|
|
$url = '%MEDIA%/test.txt';
|
|
|
|
$this->assertFalse(Attachment::isURL($url));
|
|
|
|
|
|
|
|
$url = 'https://google.de';
|
|
|
|
$this->assertFalse(Attachment::isURL($url));
|
|
|
|
|
|
|
|
$url = 'ftp://google.de';
|
|
|
|
$this->assertTrue(Attachment::isURL($url, false, false));
|
|
|
|
$this->assertFalse(Attachment::isURL($url, false, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setProtectedProperty($object, $property, $value)
|
|
|
|
{
|
|
|
|
$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
|
|
|
}
|