From 0f0adfcf362302ca4a52b18021283b64e531f437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sun, 26 Nov 2023 22:24:22 +0100 Subject: [PATCH] Filter out duplicate file DTO returned by the info providers --- VERSION | 2 +- .../DTOtoEntityConverter.php | 28 +++++++++++++++++-- .../DTOtoEntityConverterTest.php | 17 +++++++++-- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/VERSION b/VERSION index f8e233b2..a5543c60 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.9.0 +1.9.1-dev diff --git a/src/Services/InfoProviderSystem/DTOtoEntityConverter.php b/src/Services/InfoProviderSystem/DTOtoEntityConverter.php index c88ab429..80bda9e2 100644 --- a/src/Services/InfoProviderSystem/DTOtoEntityConverter.php +++ b/src/Services/InfoProviderSystem/DTOtoEntityConverter.php @@ -186,7 +186,8 @@ final class DTOtoEntityConverter } //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 if ($image->url === $dto->preview_image_url) { continue; @@ -195,10 +196,10 @@ final class DTOtoEntityConverter $entity->addAttachment($this->convertFile($image, $image_type)); } - //Add datasheets $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)); } @@ -210,6 +211,27 @@ final class DTOtoEntityConverter 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. * If the name is null, null is returned. diff --git a/tests/Services/InfoProviderSystem/DTOtoEntityConverterTest.php b/tests/Services/InfoProviderSystem/DTOtoEntityConverterTest.php index 5a9002c3..658f5135 100644 --- a/tests/Services/InfoProviderSystem/DTOtoEntityConverterTest.php +++ b/tests/Services/InfoProviderSystem/DTOtoEntityConverterTest.php @@ -146,11 +146,11 @@ class DTOtoEntityConverterTest extends WebTestCase $this->assertEquals($type, $entity->getAttachmentType()); } - public function testConvertPart() + public function testConvertPart(): void { $parameters = [new ParameterDTO('Test', 'Test')]; - $datasheets = [new FileDTO('https://invalid.invalid/file.pdf')]; - $images = [new FileDTO('https://invalid.invalid/image.png')]; + $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'), 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')])]; $dto = new PartDetailDTO( @@ -179,5 +179,16 @@ class DTOtoEntityConverterTest extends WebTestCase //The actual content is tested in the corresponding tests $this->assertCount(count($parameters), $entity->getParameters()); $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()); } }