diff --git a/src/Services/Attachments/AttachmentURLGenerator.php b/src/Services/Attachments/AttachmentURLGenerator.php index c22cefe4..e505408f 100644 --- a/src/Services/Attachments/AttachmentURLGenerator.php +++ b/src/Services/Attachments/AttachmentURLGenerator.php @@ -112,12 +112,12 @@ class AttachmentURLGenerator /** * Returns a URL to a thumbnail of the attachment file. * For external files the original URL is returned. - * @return string|null The URL or null if the attachment file is not existing + * @return string|null The URL or null if the attachment file is not existing or is invalid */ public function getThumbnailURL(Attachment $attachment, string $filter_name = 'thumbnail_sm'): ?string { if (!$attachment->isPicture()) { - throw new InvalidArgumentException('Thumbnail creation only works for picture attachments!'); + return null; } if (!$attachment->hasInternal()){ diff --git a/src/Services/ImportExportSystem/EntityImporter.php b/src/Services/ImportExportSystem/EntityImporter.php index cecab12d..11915cfb 100644 --- a/src/Services/ImportExportSystem/EntityImporter.php +++ b/src/Services/ImportExportSystem/EntityImporter.php @@ -57,6 +57,7 @@ class EntityImporter /** * Creates many entries at once, based on a (text) list of name. * The created entities are not persisted to database yet, so you have to do it yourself. + * It returns all entities in the hierachy chain (even if they are already persisted). * * @template T of AbstractNamedDBElement * @param string $lines The list of names seperated by \n @@ -132,32 +133,38 @@ class EntityImporter //We can only use the getNewEntityFromPath function, if the repository is a StructuralDBElementRepository if ($repo instanceof StructuralDBElementRepository) { $entities = $repo->getNewEntityFromPath($new_path); - $entity = end($entities); - if ($entity === false) { + if ($entities === []) { throw new InvalidArgumentException('getNewEntityFromPath returned an empty array!'); } } else { //Otherwise just create a new entity $entity = new $class_name; $entity->setName($name); + $entities = [$entity]; } //Validate entity - $tmp = $this->validator->validate($entity); - //If no error occured, write entry to DB: - if (0 === count($tmp)) { - $valid_entities[] = $entity; - } else { //Otherwise log error - $errors[] = [ - 'entity' => $entity, - 'violations' => $tmp, - ]; + foreach ($entities as $entity) { + $tmp = $this->validator->validate($entity); + //If no error occured, write entry to DB: + if (0 === count($tmp)) { + $valid_entities[] = $entity; + } else { //Otherwise log error + $errors[] = [ + 'entity' => $entity, + 'violations' => $tmp, + ]; + } } - $last_element = $entity; + $last_element = end($entities); + if ($last_element === false) { + $last_element = null; + } } - return $valid_entities; + //Only return objects once + return array_values(array_unique($valid_entities)); } /** diff --git a/tests/Services/ImportExportSystem/EntityImporterTest.php b/tests/Services/ImportExportSystem/EntityImporterTest.php index 7c6f4d5d..fd5e8b9e 100644 --- a/tests/Services/ImportExportSystem/EntityImporterTest.php +++ b/tests/Services/ImportExportSystem/EntityImporterTest.php @@ -75,8 +75,8 @@ class EntityImporterTest extends WebTestCase $em = self::getContainer()->get(EntityManagerInterface::class); $parent = $em->find(AttachmentType::class, 1); $results = $this->service->massCreation($lines, AttachmentType::class, $parent, $errors); - $this->assertCount(3, $results); - $this->assertSame($parent, $results[0]->getParent()); + $this->assertCount(4, $results); + $this->assertSame("Test 1", $results[1]->getName()); //Test for addition of existing elements $errors = []; @@ -113,6 +113,31 @@ EOT; } + public function testMassCreationArrow(): void + { + $input = << Test1.1 + Test1 -> Test1.2 + Test2 -> Test2.1 + Test1 + Test1.3 + EOT; + + $errors = []; + $results = $this->service->massCreation($input, AttachmentType::class, null, $errors); + + //We have 6 elements, and 0 errors + $this->assertCount(0, $errors); + $this->assertCount(6, $results); + + $this->assertEquals('Test1', $results[0]->getName()); + $this->assertEquals('Test1.1', $results[1]->getName()); + $this->assertEquals('Test1.2', $results[2]->getName()); + $this->assertEquals('Test2', $results[3]->getName()); + $this->assertEquals('Test2.1', $results[4]->getName()); + $this->assertEquals('Test1.3', $results[5]->getName()); + } + public function testMassCreationNested(): void { $input = <<assertCount(0, $errors); - $this->assertCount(7, $results); + $this->assertCount(8, $results); - $element1 = $results[0]; - $element11 = $results[1]; - $element111 = $results[2]; - $element112 = $results[3]; - $element12 = $results[4]; - $element121 = $results[5]; - $element2 = $results[6]; + $element1 = $results[1]; + $element11 = $results[2]; + $element111 = $results[3]; + $element112 = $results[4]; + $element12 = $results[5]; + $element121 = $results[6]; + $element2 = $results[7]; $this->assertSame('Test 1', $element1->getName()); $this->assertSame('Test 1.1', $element11->getName()); diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf index 3f0387df..195366b4 100644 --- a/translations/messages.en.xlf +++ b/translations/messages.en.xlf @@ -7157,12 +7157,15 @@ Exampletown mass_creation.lines.placeholder - Element 1 + +Element 3 + +Element 1 -> Element 1.1 +Element 1 -> Element 1.2]]>