Skip to content

Commit a67d47e

Browse files
authored
Merge pull request mautic#14928 from biozshock/edit-entity-twice-5.x
Properly process repeating edit requests to the same data.
2 parents 3e00c36 + ea455c8 commit a67d47e

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

app/bundles/ApiBundle/Controller/CommonApiController.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,18 @@ protected function processBatchForm(Request $request, $key, $entity, $params, $m
410410
$errors[$key] = $formResponse;
411411
}
412412

413-
$this->doctrine->getManager()->detach($entity);
413+
$lastEntityIndex = -1;
414+
foreach ($entities as $index => $moreEntities) {
415+
if ($moreEntities !== $entity) {
416+
continue;
417+
}
418+
419+
$lastEntityIndex = $index;
420+
}
421+
422+
if (-1 === $lastEntityIndex || $lastEntityIndex === $key) {
423+
$this->doctrine->getManager()->detach($entity);
424+
}
414425

415426
$this->inBatchMode = false;
416427
}

app/bundles/LeadBundle/Tests/Controller/Api/LeadApiControllerFunctionalTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,47 @@ public function testBatchDncAddAndRemove(): void
842842
$this->assertSame(Response::HTTP_OK, $clientResponse->getStatusCode());
843843
}
844844

845+
public function testBatchEditEndpointSameContact(): void
846+
{
847+
$contact1 = new Lead();
848+
$contact1->setEmail('batcheditcontact1@gmail.com');
849+
$this->em->persist($contact1);
850+
851+
$contact2 = new Lead();
852+
$contact2->setEmail('batcheditcontact2@gmail.com');
853+
$this->em->persist($contact2);
854+
855+
$this->em->flush();
856+
$this->em->clear();
857+
858+
$payload = [
859+
['points' => 1, 'id' => $contact1->getId()],
860+
['points' => 1, 'id' => $contact2->getId()],
861+
['points' => 2, 'id' => $contact1->getId()],
862+
];
863+
864+
$this->client->request(Request::METHOD_PATCH, '/api/contacts/batch/edit', $payload);
865+
$clientResponse = $this->client->getResponse();
866+
867+
self::assertResponseIsSuccessful($clientResponse->getContent());
868+
869+
$response = json_decode($clientResponse->getContent(), true);
870+
871+
self::assertCount(3, $response['contacts']);
872+
873+
$this->assertEquals(Response::HTTP_OK, $response['statusCodes'][0]);
874+
$this->assertSame($contact1->getId(), $response['contacts'][0]['id']);
875+
$this->assertSame(2, $response['contacts'][0]['points']);
876+
877+
$this->assertEquals(Response::HTTP_OK, $response['statusCodes'][1]);
878+
$this->assertSame($contact2->getId(), $response['contacts'][1]['id']);
879+
$this->assertSame(1, $response['contacts'][1]['points']);
880+
881+
$this->assertEquals(Response::HTTP_OK, $response['statusCodes'][2]);
882+
$this->assertSame($contact1->getId(), $response['contacts'][2]['id']);
883+
$this->assertSame(2, $response['contacts'][2]['points']);
884+
}
885+
845886
public function testAddAndRemoveDncToExistingContact(): void
846887
{
847888
// Create contact

0 commit comments

Comments
 (0)