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
|
|
|
/**
|
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-28 16:06:37 +02:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-09-28 16:06:37 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Tests\Services\Attachments;
|
|
|
|
|
|
|
|
use App\Services\AmountFormatter;
|
|
|
|
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
|
|
|
|
{
|
2020-01-05 15:55:16 +01:00
|
|
|
public static $media_path;
|
|
|
|
public static $footprint_path;
|
2020-01-05 22:49:00 +01:00
|
|
|
protected static $projectDir_orig;
|
|
|
|
protected static $projectDir;
|
2019-09-28 16:06:37 +02:00
|
|
|
/**
|
|
|
|
* @var AmountFormatter
|
|
|
|
*/
|
|
|
|
protected static $service;
|
|
|
|
|
|
|
|
public function __construct($name = null, array $data = [], $dataName = '')
|
|
|
|
{
|
|
|
|
parent::__construct($name, $data, $dataName);
|
|
|
|
|
|
|
|
self::bootKernel();
|
|
|
|
self::$projectDir_orig = realpath(self::$kernel->getProjectDir());
|
|
|
|
self::$projectDir = str_replace('\\', '/', self::$projectDir_orig);
|
2019-11-09 00:47:20 +01:00
|
|
|
self::$media_path = self::$projectDir.'/public/media';
|
|
|
|
self::$footprint_path = self::$projectDir.'/public/img/footprints';
|
2019-09-28 16:06:37 +02:00
|
|
|
}
|
|
|
|
|
2020-01-04 20:24:09 +01:00
|
|
|
public static function setUpBeforeClass(): void
|
2019-09-28 16:06:37 +02:00
|
|
|
{
|
|
|
|
parent::setUpBeforeClass();
|
|
|
|
|
|
|
|
//Get an service instance.
|
|
|
|
self::bootKernel();
|
|
|
|
self::$service = self::$container->get(AttachmentPathResolver::class);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
$this->assertNull(self::$service->parameterToAbsolutePath(null));
|
|
|
|
|
|
|
|
//Absolute path should be returned like they are (we use projectDir here, because we know that this dir exists)
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame(self::$projectDir_orig, self::$service->parameterToAbsolutePath(self::$projectDir));
|
2019-09-28 16:06:37 +02:00
|
|
|
|
|
|
|
//Relative pathes should be resolved
|
2020-08-21 21:36:22 +02:00
|
|
|
$expected = str_replace('\\', '/', self::$projectDir_orig.DIRECTORY_SEPARATOR.'src');
|
2020-06-01 14:51:38 +02:00
|
|
|
$this->assertSame($expected, self::$service->parameterToAbsolutePath('src'));
|
|
|
|
$this->assertSame($expected, self::$service->parameterToAbsolutePath('./src'));
|
2019-09-28 16:06:37 +02:00
|
|
|
|
|
|
|
//Invalid pathes should return null
|
|
|
|
$this->assertNull(self::$service->parameterToAbsolutePath('/this/path/does/not/exist'));
|
|
|
|
$this->assertNull(self::$service->parameterToAbsolutePath('/./this/one/too'));
|
|
|
|
}
|
|
|
|
|
2020-03-29 22:47:25 +02:00
|
|
|
public function placeholderDataProvider(): array
|
2019-09-28 16:06:37 +02:00
|
|
|
{
|
|
|
|
return [
|
2019-11-09 00:47:20 +01:00
|
|
|
['%FOOTPRINTS%/test/test.jpg', self::$footprint_path.'/test/test.jpg'],
|
|
|
|
['%FOOTPRINTS%/test/', self::$footprint_path.'/test/'],
|
|
|
|
['%MEDIA%/test', self::$media_path.'/test'],
|
2019-09-28 16:06:37 +02:00
|
|
|
['%MEDIA%', self::$media_path],
|
|
|
|
['%FOOTPRINTS%', self::$footprint_path],
|
|
|
|
//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
|
|
|
{
|
|
|
|
return [
|
2019-11-09 00:47:20 +01:00
|
|
|
[self::$media_path.'/test/img.jpg', '%MEDIA%/test/img.jpg'],
|
|
|
|
[self::$media_path.'/test/img.jpg', '%BASE%/data/media/test/img.jpg', true],
|
|
|
|
[self::$footprint_path.'/foo.jpg', '%FOOTPRINTS%/foo.jpg'],
|
|
|
|
[self::$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],
|
|
|
|
//More then one placeholder is not allowed
|
2019-11-09 00:47:20 +01:00
|
|
|
[self::$footprint_path.'/test/'.self::$footprint_path, null],
|
2019-09-28 16:06:37 +02:00
|
|
|
//Path must begin with path
|
2019-11-09 00:47:20 +01:00
|
|
|
['/not/root'.self::$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
|
|
|
{
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($expected, self::$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
|
|
|
{
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($expected, self::$service->realPathToPlaceholder($param, $old_method));
|
2019-09-28 16:06:37 +02:00
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|