2019-09-28 16:06:37 +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-09-28 16:06:37 +02:00
|
|
|
namespace App\Tests\Services\Attachments;
|
|
|
|
|
2022-12-18 17:28:42 +01:00
|
|
|
use App\Services\Formatters\AmountFormatter;
|
2019-09-28 16:06:37 +02:00
|
|
|
use App\Services\Attachments\AttachmentPathResolver;
|
2020-01-05 22:49:00 +01:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
2019-09-28 16:06:37 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
|
|
|
|
|
|
class AttachmentPathResolverTest extends WebTestCase
|
|
|
|
{
|
2023-04-15 22:05:29 +02:00
|
|
|
protected string $media_path;
|
|
|
|
protected string $footprint_path;
|
2022-08-14 19:09:07 +02:00
|
|
|
protected $projectDir_orig;
|
|
|
|
protected $projectDir;
|
2019-09-28 16:06:37 +02:00
|
|
|
/**
|
|
|
|
* @var AmountFormatter
|
|
|
|
*/
|
2022-08-14 19:09:07 +02:00
|
|
|
protected $service;
|
2019-09-28 16:06:37 +02:00
|
|
|
|
2022-08-14 19:09:07 +02:00
|
|
|
public function setUp(): void
|
2019-09-28 16:06:37 +02:00
|
|
|
{
|
2023-04-15 23:14:53 +02:00
|
|
|
//Get a service instance.
|
2019-09-28 16:06:37 +02:00
|
|
|
self::bootKernel();
|
|
|
|
|
2022-08-14 19:09:07 +02:00
|
|
|
$this->projectDir_orig = realpath(self::$kernel->getProjectDir());
|
|
|
|
$this->projectDir = str_replace('\\', '/', $this->projectDir_orig);
|
|
|
|
$this->media_path = $this->projectDir.'/public/media';
|
|
|
|
$this->footprint_path = $this->projectDir.'/public/img/footprints';
|
2019-09-28 16:06:37 +02:00
|
|
|
|
2022-08-14 19:09:07 +02:00
|
|
|
$this->service = self::getContainer()->get(AttachmentPathResolver::class);
|
2019-09-28 16:06:37 +02:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testParameterToAbsolutePath(): void
|
2019-09-28 16:06:37 +02:00
|
|
|
{
|
|
|
|
//If null is passed, null must be returned
|
2022-08-14 19:09:07 +02:00
|
|
|
$this->assertNull($this->service->parameterToAbsolutePath(null));
|
2019-09-28 16:06:37 +02:00
|
|
|
|
|
|
|
//Absolute path should be returned like they are (we use projectDir here, because we know that this dir exists)
|
2022-08-14 19:09:07 +02:00
|
|
|
$this->assertSame($this->projectDir_orig, $this->service->parameterToAbsolutePath($this->projectDir));
|
2019-09-28 16:06:37 +02:00
|
|
|
|
|
|
|
//Relative pathes should be resolved
|
2022-08-14 19:09:07 +02:00
|
|
|
$expected = str_replace('\\', '/', $this->projectDir_orig.DIRECTORY_SEPARATOR.'src');
|
|
|
|
$this->assertSame($expected, $this->service->parameterToAbsolutePath('src'));
|
|
|
|
$this->assertSame($expected, $this->service->parameterToAbsolutePath('./src'));
|
2019-09-28 16:06:37 +02:00
|
|
|
|
|
|
|
//Invalid pathes should return null
|
2022-08-14 19:09:07 +02:00
|
|
|
$this->assertNull($this->service->parameterToAbsolutePath('/this/path/does/not/exist'));
|
|
|
|
$this->assertNull($this->service->parameterToAbsolutePath('/./this/one/too'));
|
2019-09-28 16:06:37 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 22:47:25 +02:00
|
|
|
public function placeholderDataProvider(): array
|
2019-09-28 16:06:37 +02:00
|
|
|
{
|
2022-08-14 19:09:07 +02:00
|
|
|
//We need to do initialization (again), as dataprovider is called before setUp()
|
|
|
|
self::bootKernel();
|
|
|
|
$this->projectDir_orig = realpath(self::$kernel->getProjectDir());
|
|
|
|
$this->projectDir = str_replace('\\', '/', $this->projectDir_orig);
|
|
|
|
$this->media_path = $this->projectDir.'/public/media';
|
|
|
|
$this->footprint_path = $this->projectDir.'/public/img/footprints';
|
|
|
|
|
2019-09-28 16:06:37 +02:00
|
|
|
return [
|
2022-08-14 19:09:07 +02:00
|
|
|
['%FOOTPRINTS%/test/test.jpg', $this->footprint_path.'/test/test.jpg'],
|
|
|
|
['%FOOTPRINTS%/test/', $this->footprint_path.'/test/'],
|
|
|
|
['%MEDIA%/test', $this->media_path.'/test'],
|
|
|
|
['%MEDIA%', $this->media_path],
|
|
|
|
['%FOOTPRINTS%', $this->footprint_path],
|
2019-09-28 16:06:37 +02:00
|
|
|
//Footprints 3D are disabled
|
|
|
|
['%FOOTPRINTS_3D%', null],
|
|
|
|
//Check that invalid pathes return null
|
|
|
|
['/no/placeholder', null],
|
|
|
|
['%INVALID_PLACEHOLDER%', null],
|
|
|
|
['%FOOTPRINTS/test/', null], //Malformed placeholder
|
|
|
|
['/wrong/%FOOTRPINTS%/', null], //Placeholder not at beginning
|
|
|
|
['%FOOTPRINTS%/%MEDIA%', null], //No more than one placholder
|
|
|
|
['%FOOTPRINTS%/%FOOTPRINTS%', null],
|
|
|
|
['%FOOTPRINTS%/../../etc/passwd', null],
|
2019-11-09 00:47:20 +01:00
|
|
|
['%FOOTPRINTS%/0\..\test', null],
|
2019-09-28 16:06:37 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-03-29 22:47:25 +02:00
|
|
|
public function realPathDataProvider(): array
|
2019-09-28 16:06:37 +02:00
|
|
|
{
|
2022-08-14 19:09:07 +02:00
|
|
|
//We need to do initialization (again), as dataprovider is called before setUp()
|
|
|
|
self::bootKernel();
|
|
|
|
$this->projectDir_orig = realpath(self::$kernel->getProjectDir());
|
|
|
|
$this->projectDir = str_replace('\\', '/', $this->projectDir_orig);
|
|
|
|
$this->media_path = $this->projectDir.'/public/media';
|
|
|
|
$this->footprint_path = $this->projectDir.'/public/img/footprints';
|
|
|
|
|
2019-09-28 16:06:37 +02:00
|
|
|
return [
|
2022-08-14 19:09:07 +02:00
|
|
|
[$this->media_path.'/test/img.jpg', '%MEDIA%/test/img.jpg'],
|
|
|
|
[$this->media_path.'/test/img.jpg', '%BASE%/data/media/test/img.jpg', true],
|
|
|
|
[$this->footprint_path.'/foo.jpg', '%FOOTPRINTS%/foo.jpg'],
|
|
|
|
[$this->footprint_path.'/foo.jpg', '%FOOTPRINTS%/foo.jpg', true],
|
2019-09-28 16:06:37 +02:00
|
|
|
//Every kind of absolute path, that is not based with our placeholder dirs must be invald
|
|
|
|
['/etc/passwd', null],
|
|
|
|
['C:\\not\\existing.txt', null],
|
2023-04-15 23:14:53 +02:00
|
|
|
//More than one placeholder is not allowed
|
2022-08-14 19:09:07 +02:00
|
|
|
[$this->footprint_path.'/test/'.$this->footprint_path, null],
|
2019-09-28 16:06:37 +02:00
|
|
|
//Path must begin with path
|
2022-08-14 19:09:07 +02:00
|
|
|
['/not/root'.$this->footprint_path, null],
|
2019-09-28 16:06:37 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider placeholderDataProvider
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testPlaceholderToRealPath($param, $expected): void
|
2019-09-28 16:06:37 +02:00
|
|
|
{
|
2022-08-14 19:09:07 +02:00
|
|
|
$this->assertSame($expected, $this->service->placeholderToRealPath($param));
|
2019-09-28 16:06:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider realPathDataProvider
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testRealPathToPlaceholder($param, $expected, $old_method = false): void
|
2019-09-28 16:06:37 +02:00
|
|
|
{
|
2022-08-14 19:09:07 +02:00
|
|
|
$this->assertSame($expected, $this->service->realPathToPlaceholder($param, $old_method));
|
2019-09-28 16:06:37 +02:00
|
|
|
}
|
2023-01-09 21:46:05 +01:00
|
|
|
|
2023-04-15 19:33:39 +02:00
|
|
|
public function germanFootprintPathdDataProvider(): ?\Generator
|
2023-01-09 21:46:05 +01:00
|
|
|
{
|
|
|
|
self::bootKernel();
|
|
|
|
$this->projectDir_orig = realpath(self::$kernel->getProjectDir());
|
|
|
|
$this->projectDir = str_replace('\\', '/', $this->projectDir_orig);
|
|
|
|
$this->footprint_path = $this->projectDir.'/public/img/footprints';
|
|
|
|
|
|
|
|
yield [$this->footprint_path. '/Active/Diodes/THT/DIODE_P600.png', '%FOOTPRINTS%/Aktiv/Dioden/Bedrahtet/DIODE_P600.png'];
|
|
|
|
yield [$this->footprint_path . '/Passive/Resistors/THT/Carbon/RESISTOR-CARBON_0207.png', '%FOOTPRINTS%/Passiv/Widerstaende/Bedrahtet/Kohleschicht/WIDERSTAND-KOHLE_0207.png'];
|
|
|
|
yield [$this->footprint_path . '/Optics/LEDs/THT/LED-GREEN_3MM.png', '%FOOTPRINTS%/Optik/LEDs/Bedrahtet/LED-GRUEN_3MM.png'];
|
|
|
|
yield [$this->footprint_path . '/Passive/Capacitors/TrimmerCapacitors/TRIMMER_CAPACITOR-RED_TZ03F.png', '%FOOTPRINTS%/Passiv/Kondensatoren/Trimmkondensatoren/TRIMMKONDENSATOR-ROT_TZ03F.png'];
|
|
|
|
yield [$this->footprint_path . '/Active/ICs/TO/IC_TO126.png', '%FOOTPRINTS%/Aktiv/ICs/TO/IC_TO126.png'];
|
|
|
|
yield [$this->footprint_path . '/Electromechanics/Switches_Buttons/RotarySwitches/ROTARY_SWITCH_DIP10.png', '%FOOTPRINTS%/Elektromechanik/Schalter_Taster/Drehschalter/DREHSCHALTER_DIP10.png'];
|
|
|
|
yield [$this->footprint_path . '/Electromechanics/Connectors/DINConnectors/SOCKET_DIN_MAB_4.png', '%FOOTPRINTS%/Elektromechanik/Verbinder/Rundsteckverbinder/BUCHSE_DIN_MAB_4.png'];
|
2023-01-09 23:05:36 +01:00
|
|
|
|
|
|
|
//Leave english pathes untouched
|
|
|
|
yield [$this->footprint_path . '/Passive/Capacitors/CAPACITOR_CTS_A_15MM.png', '%FOOTPRINTS%/Passive/Capacitors/CAPACITOR_CTS_A_15MM.png'];
|
2023-01-09 21:46:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider germanFootprintPathdDataProvider
|
|
|
|
*/
|
|
|
|
public function testConversionOfGermanFootprintPaths(string $expected, string $input): void
|
|
|
|
{
|
|
|
|
$this->assertSame($expected, $this->service->placeholderToRealPath($input));
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|