-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge IBX-1392: Implemented image filenames sanitization (#52)
- Loading branch information
Showing
8 changed files
with
160 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+2.77 KB
tests/integration/Core/Repository/FieldType/_fixtures/1234eeee1234-image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+11.9 KB
tests/integration/Core/Repository/FieldType/_fixtures/2222eeee1111-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Core\IO\FilePathNormalizer; | ||
|
||
use Ibexa\Core\IO\FilePathNormalizer\Flysystem; | ||
use Ibexa\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class FlysystemTest extends TestCase | ||
{ | ||
/** @var \Ibexa\Core\IO\FilePathNormalizer\Flysystem */ | ||
private $filePathNormalizer; | ||
|
||
/** @var \Ibexa\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter|\PHPUnit\Framework\MockObject\MockObject */ | ||
private $slugConverter; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->slugConverter = $this->createMock(SlugConverter::class); | ||
$this->filePathNormalizer = new Flysystem($this->slugConverter); | ||
} | ||
|
||
/** | ||
* @dataProvider providerForTestNormalizePath | ||
*/ | ||
public function testNormalizePath( | ||
string $originalPath, | ||
string $fileName, | ||
string $sluggedFileName, | ||
string $regex | ||
): void { | ||
$this->slugConverter | ||
->expects(self::once()) | ||
->method('convert') | ||
->with($fileName) | ||
->willReturn($sluggedFileName); | ||
|
||
$normalizedPath = $this->filePathNormalizer->normalizePath($originalPath); | ||
|
||
self::assertStringEndsWith($sluggedFileName, $normalizedPath); | ||
self::assertRegExp($regex, $normalizedPath); | ||
} | ||
|
||
public function providerForTestNormalizePath(): array | ||
{ | ||
$defaultPattern = '/\/[0-9a-f]{12}-'; | ||
|
||
return [ | ||
'No special chars' => [ | ||
'4/3/2/234/1/image.jpg', | ||
'image.jpg', | ||
'image.jpg', | ||
$defaultPattern . 'image.jpg/', | ||
], | ||
'Spaces in the filename' => [ | ||
'4/3/2/234/1/image with spaces.jpg', | ||
'image with spaces.jpg', | ||
'image-with-spaces.jpg', | ||
$defaultPattern . 'image-with-spaces.jpg/', | ||
], | ||
'Encoded spaces in the name' => [ | ||
'4/3/2/234/1/image%20+no+spaces.jpg', | ||
'image%20+no+spaces.jpg', | ||
'image-20-nospaces.jpg', | ||
$defaultPattern . 'image-20-nospaces.jpg/', | ||
], | ||
'Special chars in the name' => [ | ||
'4/3/2/234/1/image%20+no+spaces?.jpg', | ||
'image%20+no+spaces?.jpg', | ||
'image-20-nospaces.jpg', | ||
$defaultPattern . 'image-20-nospaces.jpg/', | ||
], | ||
'Already hashed name' => [ | ||
'4/3/2/234/1/14ff44718877-hashed.jpg', | ||
'14ff44718877-hashed.jpg', | ||
'14ff44718877-hashed.jpg', | ||
'/^4\/3\/2\/234\/1\/14ff44718877-hashed.jpg$/', | ||
], | ||
]; | ||
} | ||
} |