fix: prevent race condition on newsletter consent confirmation

This commit is contained in:
Björn Fromme
2026-07-14 09:47:22 +02:00
parent b95944701e
commit 6c4ea07e6e
3 changed files with 39 additions and 4 deletions
+8 -1
View File
@@ -166,6 +166,10 @@ class NewsletterManagerTest extends TestCase
default => null,
};
});
$consents
->method('findByEmailAndListIds')
->with('[email protected]', [1, 2])
->willReturn([1 => $existingConsent, 2 => $existingOtherConsent]);
$repository->expects(self::once())->method('deletePendingByEmail')->with('[email protected]')->willReturn(0);
$entityManager->expects(self::never())->method('persist');
$entityManager->expects(self::once())->method('flush');
@@ -223,6 +227,7 @@ class NewsletterManagerTest extends TestCase
$mailjet->expects(self::once())->method('ensureSubscribed')->with('[email protected]', 2);
$consents->method('findActiveByEmail')->with('[email protected]')->willReturn($existingConsent);
$consents->method('findOneByEmailAndListId')->willReturn(null);
$consents->method('findByEmailAndListIds')->willReturn([]);
$repository->expects(self::once())->method('deletePendingByEmail')->with('[email protected]')->willReturn(0);
$repository->expects(self::never())->method('findPendingByEmail');
$entityManager->expects(self::once())->method('persist')->with(self::isInstanceOf(NewsletterConsent::class));
@@ -254,6 +259,7 @@ class NewsletterManagerTest extends TestCase
$mailjet->expects(self::once())->method('ensureSubscribed')->with('[email protected]', 2);
$consents->method('findActiveByEmail')->with('[email protected]')->willReturn($existingConsent);
$consents->method('findOneByEmailAndListId')->with('[email protected]', 2)->willReturn(null);
$consents->method('findByEmailAndListIds')->willReturn([]);
$repository->expects(self::once())->method('deletePendingByEmail')->with('[email protected]')->willReturn(0);
$entityManager->expects(self::once())->method('persist')->with(self::isInstanceOf(NewsletterConsent::class));
$entityManager->expects(self::once())->method('flush');
@@ -338,7 +344,7 @@ class NewsletterManagerTest extends TestCase
$repository->expects(self::once())->method('findByTokenHash')->with(hash('sha256', $token))->willReturn($confirmation);
$repository->expects(self::never())->method('deletePendingByEmail');
$consents->expects(self::once())->method('findOneByEmailAndListId')->with('[email protected]', 10321569)->willReturn(null);
$consents->expects(self::once())->method('findByEmailAndListIds')->with('[email protected]', [10321569])->willReturn([]);
$mailjet->expects(self::once())->method('upsertContact')->with('[email protected]', 'Mia', 'Muster');
$mailjet->expects(self::once())->method('ensureSubscribed')->with('[email protected]', 10321569);
$entityManager->expects(self::once())->method('persist')->with(self::isInstanceOf(NewsletterConsent::class));
@@ -370,6 +376,7 @@ class NewsletterManagerTest extends TestCase
$repository->expects(self::once())->method('findByTokenHash')->with(hash('sha256', $token))->willReturn($confirmation);
$repository->expects(self::never())->method('deletePendingByEmail');
$consents->method('findOneByEmailAndListId')->willReturn(null);
$consents->method('findByEmailAndListIds')->with('[email protected]', [1, 2])->willReturn([]);
$mailjet
->expects(self::exactly(2))
->method('ensureSubscribed')