-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Anton Fedurtsya <[email protected]>
- Loading branch information
Showing
4 changed files
with
134 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © OXID eSales AG. All rights reserved. | ||
* See LICENSE file for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OxidEsales\MediaLibrary\Validation\Validator; | ||
|
||
use OxidEsales\MediaLibrary\Media\DataType\FilePathInterface; | ||
use OxidEsales\MediaLibrary\Validation\Exception\ValidationFailedException; | ||
|
||
class FileNameValidator implements FilePathValidatorInterface | ||
{ | ||
//@todo: setting | ||
public const FORBIDDEN_CHARACTERS = '/<>:"|?*\\'; | ||
|
||
public function validateFile(FilePathInterface $filePath): void | ||
{ | ||
$fileName = $filePath->getFileName(); | ||
|
||
$this->checkFilenameNotEmpty($fileName); | ||
$this->checkFilenameDoesNotStartWithDot($fileName); | ||
$this->checkForbiddenCharacters($fileName); | ||
} | ||
|
||
private function checkForbiddenCharacters(string $fileName): void | ||
{ | ||
$forbiddenCharacters = str_split(self::FORBIDDEN_CHARACTERS); | ||
foreach ($forbiddenCharacters as $oneForbiddenCharacter) { | ||
if (strpos($fileName, $oneForbiddenCharacter) !== false) { | ||
throw new ValidationFailedException("Forbidden character found: " . $oneForbiddenCharacter); | ||
} | ||
} | ||
} | ||
|
||
public function checkFilenameNotEmpty(string $fileName): void | ||
{ | ||
if (!$fileName) { | ||
throw new ValidationFailedException("Filename cannot be empty"); | ||
} | ||
} | ||
|
||
public function checkFilenameDoesNotStartWithDot(string $fileName): void | ||
{ | ||
if ($fileName[0] === '.') { | ||
throw new ValidationFailedException("Filename cannot start with ."); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © OXID eSales AG. All rights reserved. | ||
* See LICENSE file for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OxidEsales\MediaLibrary\Tests\Unit\Validation\Validator; | ||
|
||
use OxidEsales\MediaLibrary\Media\DataType\FilePath; | ||
use OxidEsales\MediaLibrary\Validation\Exception\ValidationFailedException; | ||
use OxidEsales\MediaLibrary\Validation\Validator\FileNameValidator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FileNameValidatorTest extends TestCase | ||
{ | ||
public function testRegularFileNameDoesntThrowExceptions(): void | ||
{ | ||
$filePathStub = $this->createConfiguredStub(FilePath::class, [ | ||
'getFileName' => uniqid() | ||
]); | ||
|
||
$sut = new FileNameValidator(); | ||
$sut->validateFile($filePathStub); | ||
|
||
$this->addToAssertionCount(1); | ||
} | ||
|
||
public static function goodFileNamesDataProvider(): \Generator | ||
{ | ||
yield "regular file name" => [ | ||
'baseName' => uniqid(), | ||
'extension' => uniqid() | ||
]; | ||
} | ||
|
||
public static function badFileNamesDataProvider(): \Generator | ||
{ | ||
yield "empty" => [ | ||
'fileName' => '' | ||
]; | ||
|
||
yield "starts with dot" => [ | ||
'fileName' => '.' . uniqid() | ||
]; | ||
} | ||
|
||
public static function forbiddenCharactersDataProvider(): \Generator | ||
{ | ||
$characters = str_split(FileNameValidator::FORBIDDEN_CHARACTERS); | ||
|
||
foreach ($characters as $character) { | ||
yield ['fileName' => uniqid() . $character . uniqid()]; | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider badFileNamesDataProvider | ||
* @dataProvider forbiddenCharactersDataProvider | ||
*/ | ||
public function testFileNameEmptyThrowsException(string $fileName): void | ||
{ | ||
$filePathStub = $this->createConfiguredStub(FilePath::class, [ | ||
'getFileName' => $fileName | ||
]); | ||
|
||
$this->expectException(ValidationFailedException::class); | ||
|
||
$sut = new FileNameValidator(); | ||
$sut->validateFile($filePathStub); | ||
} | ||
} |