Filter out duplicate file DTO returned by the info providers

This commit is contained in:
Jan Böhmer 2023-11-26 22:24:22 +01:00
parent 76295b73c8
commit 0f0adfcf36
3 changed files with 40 additions and 7 deletions

View file

@ -1 +1 @@
1.9.0 1.9.1-dev

View file

@ -186,7 +186,8 @@ final class DTOtoEntityConverter
} }
//Add other images //Add other images
foreach ($dto->images ?? [] as $image) { $images = $this->files_unique($dto->images ?? []);
foreach ($images as $image) {
//Ensure that the image is not the same as the preview image //Ensure that the image is not the same as the preview image
if ($image->url === $dto->preview_image_url) { if ($image->url === $dto->preview_image_url) {
continue; continue;
@ -195,10 +196,10 @@ final class DTOtoEntityConverter
$entity->addAttachment($this->convertFile($image, $image_type)); $entity->addAttachment($this->convertFile($image, $image_type));
} }
//Add datasheets //Add datasheets
$datasheet_type = $this->getDatasheetType(); $datasheet_type = $this->getDatasheetType();
foreach ($dto->datasheets ?? [] as $datasheet) { $datasheets = $this->files_unique($dto->datasheets ?? []);
foreach ($datasheets as $datasheet) {
$entity->addAttachment($this->convertFile($datasheet, $datasheet_type)); $entity->addAttachment($this->convertFile($datasheet, $datasheet_type));
} }
@ -210,6 +211,27 @@ final class DTOtoEntityConverter
return $entity; return $entity;
} }
/**
* Returns the given array of files with all duplicates removed.
* @param FileDTO[] $files
* @return FileDTO[]
*/
private function files_unique(array $files): array
{
$unique = [];
//We use the URL and name as unique identifier. If two file DTO have the same URL and name, they are considered equal
//and get filtered out, if it already exists in the array
foreach ($files as $file) {
//Skip already existing files, to preserve the order. The second condition ensure that we keep the version with a name over the one without a name
if (isset($unique[$file->url]) && $unique[$file->url]->name !== null) {
continue;
}
$unique[$file->url] = $file;
}
return array_values($unique);
}
/** /**
* Get the existing entity of the given class with the given name or create it if it does not exist. * Get the existing entity of the given class with the given name or create it if it does not exist.
* If the name is null, null is returned. * If the name is null, null is returned.

View file

@ -146,11 +146,11 @@ class DTOtoEntityConverterTest extends WebTestCase
$this->assertEquals($type, $entity->getAttachmentType()); $this->assertEquals($type, $entity->getAttachmentType());
} }
public function testConvertPart() public function testConvertPart(): void
{ {
$parameters = [new ParameterDTO('Test', 'Test')]; $parameters = [new ParameterDTO('Test', 'Test')];
$datasheets = [new FileDTO('https://invalid.invalid/file.pdf')]; $datasheets = [new FileDTO('https://invalid.invalid/file.pdf'), new FileDTO('https://invalid.invalid/file.pdf', name: 'TestFile')];
$images = [new FileDTO('https://invalid.invalid/image.png')]; $images = [new FileDTO('https://invalid.invalid/image.png'), new FileDTO('https://invalid.invalid/image2.png', name: 'TestImage2'), new FileDTO('https://invalid.invalid/image2.png')];
$shopping_infos = [new PurchaseInfoDTO('TestDistributor', 'TestOrderNumber', [new PriceDTO(1, "10.0", 'EUR')])]; $shopping_infos = [new PurchaseInfoDTO('TestDistributor', 'TestOrderNumber', [new PriceDTO(1, "10.0", 'EUR')])];
$dto = new PartDetailDTO( $dto = new PartDetailDTO(
@ -179,5 +179,16 @@ class DTOtoEntityConverterTest extends WebTestCase
//The actual content is tested in the corresponding tests //The actual content is tested in the corresponding tests
$this->assertCount(count($parameters), $entity->getParameters()); $this->assertCount(count($parameters), $entity->getParameters());
$this->assertCount(count($shopping_infos), $entity->getOrderdetails()); $this->assertCount(count($shopping_infos), $entity->getOrderdetails());
//Datasheets and images are stored as attachments and the duplicates, should be filtered out
$this->assertCount(3, $entity->getAttachments());
//The attachments should have the name of the named duplicate file
$image1 = $entity->getAttachments()[0];
$this->assertEquals('Main image', $image1->getName());
$image1 = $entity->getAttachments()[1];
$datasheet = $entity->getAttachments()[2];
$this->assertEquals('TestFile', $datasheet->getName());
} }
} }