Correcty handle already existing elements in mass creation

This fixes issue #543
This commit is contained in:
Jan Böhmer 2024-03-10 00:51:37 +01:00
parent 92a8107b9d
commit ed4728fdb1
2 changed files with 59 additions and 22 deletions

View file

@ -24,10 +24,12 @@ namespace App\Tests\Services\ImportExportSystem;
use App\Entity\Attachments\AttachmentContainingDBElement;
use App\Entity\Attachments\AttachmentType;
use App\Entity\LabelSystem\LabelProfile;
use App\Entity\Parts\Category;
use App\Entity\Parts\Part;
use App\Entity\UserSystem\User;
use App\Services\ImportExportSystem\EntityImporter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Validator\ConstraintViolation;
@ -68,10 +70,24 @@ class EntityImporterTest extends WebTestCase
//Check parent
$this->assertNull($results[0]->getMasterPictureAttachment());
$parent = new AttachmentType();
$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());
//Test for addition of existing elements
$errors = [];
$lines = "Node 3\n Node 3.new";
$results = $this->service->massCreation($lines, AttachmentType::class, null, $errors);
$this->assertCount(2, $results);
$this->assertCount(0, $errors);
$this->assertSame('Node 3', $results[0]->getName());
//Node 3 must be an existing entity
$this->assertNotNull($results[0]->getId());
$this->assertSame('Node 3.new', $results[1]->getName());
//Parent must be Node 3
$this->assertSame($results[0], $results[1]->getParent());
}
public function testNonStructuralClass(): void
@ -81,13 +97,9 @@ Test1
Test1.1
Test2
EOT;
//Define a new anonymous class, which is not structural. We can not use User here, because it does some validation
$anonymous_object = new class extends AttachmentContainingDBElement {};
$anonymous_class = get_class($anonymous_object);
$errors = [];
$results = $this->service->massCreation($input, $anonymous_class, null, $errors);
$results = $this->service->massCreation($input, LabelProfile::class, null, $errors);
//Import must not fail, even with non-structural classes
$this->assertCount(3, $results);
@ -112,7 +124,8 @@ Test 2
EOT;
$errors = [];
$parent = new AttachmentType();
$em = self::getContainer()->get(EntityManagerInterface::class);
$parent = $em->find(AttachmentType::class, 1);
$results = $this->service->massCreation($input, AttachmentType::class, $parent, $errors);
//We have 7 elements, and 0 errors
@ -148,13 +161,15 @@ EOT;
public function testMassCreationErrors(): void
{
$errors = [];
//Node 1 and Node 2 are created in datafixtures, so their attemp to create them again must fail.
$lines = "Test 1\nNode 1\nNode 2";
$longName = str_repeat('a', 256);
//The last node is too long, this must trigger a validation error
$lines = "Test 1\nNode 1\n " . $longName;
$results = $this->service->massCreation($lines, AttachmentType::class, null, $errors);
$this->assertCount(1, $results);
$this->assertCount(2, $results);
$this->assertSame('Test 1', $results[0]->getName());
$this->assertCount(2, $errors);
$this->assertSame('Node 1', $errors[0]['entity']->getName());
$this->assertCount(1, $errors);
$this->assertSame($longName, $errors[0]['entity']->getName());
}
public function formatDataProvider(): array