Skip to content

Commit 04ababf

Browse files
downacefranmomu
andauthored
[Uploadable] Fix appendNumber renaming for files without extension (#2460)
* Fix `appendNumber` renaming for files without extension Fixes #2228 * [Coding Standards fix] Fix `appendNumber` renaming for files without extension Fixes #2228 * Update CHANGELOG.md #2460 Co-authored-by: Fran Moreno <[email protected]> * Update tests/Gedmo/Uploadable/UploadableEntityTest.php #2460 Co-authored-by: Fran Moreno <[email protected]> Co-authored-by: Fran Moreno <[email protected]>
1 parent 2e8b776 commit 04ababf

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ a release.
2727
- Dropped support for doctrine/mongodb-odm < 2.3.
2828
- Make doctrine/cache an optional dependency.
2929

30+
## Fixed
31+
- Loggable: Fix `appendNumber` renaming for files without extension (#2228)
32+
3033
## [3.6.0] - 2022-03-19
3134
### Added
3235
- Translatable: Add defaultTranslationValue option to allow null or string value (#2167). TranslatableListener can hydrate object properties with null value, but it may cause a Type error for non-nullable getter upon a missing translation.

src/Uploadable/UploadableListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ public function moveFile(FileInfoInterface $fileInfo, $path, $filenameGeneratorC
445445
$info['fileExtension'] = substr($info['filePath'], strrpos($info['filePath'], '.'));
446446
$info['fileWithoutExt'] = substr($info['filePath'], 0, strrpos($info['filePath'], '.'));
447447
} else {
448-
$info['fileWithoutExt'] = $info['fileName'];
448+
$info['fileWithoutExt'] = $info['filePath'];
449449
}
450450

451451
// Save the original filename for later use

tests/Gedmo/Uploadable/UploadableEntityTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,32 @@ public function testRemoveFileIfItsNotAFileThenReturnFalse(): void
477477
static::assertFalse($this->listener->removeFile('non_existent_file'));
478478
}
479479

480-
public function testMoveFileUsingAppendNumberOptionAppendsNumberToFilenameIfItAlreadyExists(): void
480+
public function dataProvider_testMoveFileUsingAppendNumberOptionAppendsNumberToFilenameIfItAlreadyExists(): array
481+
{
482+
return [
483+
'With extension' => [
484+
'Filename' => 'test.txt',
485+
'Expected filename' => 'test-2.txt',
486+
],
487+
'Without extension' => [
488+
'Filename' => 'test',
489+
'Expected filename' => 'test-2',
490+
],
491+
];
492+
}
493+
494+
/**
495+
* @dataProvider dataProvider_testMoveFileUsingAppendNumberOptionAppendsNumberToFilenameIfItAlreadyExists
496+
*/
497+
public function testMoveFileUsingAppendNumberOptionAppendsNumberToFilenameIfItAlreadyExists(string $filename, string $expectedFilename): void
481498
{
482499
$file = new FileAppendNumber();
483500
$file2 = new FileAppendNumber();
484501

485502
$file->setTitle('test');
486503
$file2->setTitle('test2');
487504

488-
$fileInfo = $this->generateUploadedFile();
505+
$fileInfo = $this->generateUploadedFile(false, $filename);
489506

490507
$this->listener->addEntityFileInfo($file, $fileInfo);
491508

@@ -499,9 +516,7 @@ public function testMoveFileUsingAppendNumberOptionAppendsNumberToFilenameIfItAl
499516

500517
$this->em->refresh($file2);
501518

502-
$filename = substr($file2->getFilePath(), strrpos($file2->getFilePath(), '/') + 1);
503-
504-
static::assertSame('test-2.txt', $filename);
519+
static::assertSame($expectedFilename, basename($file2->getFilePath()));
505520
}
506521

507522
public function testMoveFileUsingAppendNumberOptionAppendsNumberToFilenameIfItAlreadyExistsRelativePath(): void

0 commit comments

Comments
 (0)