From 8c2421cffd7c6c89b25df99ce1650ce322aae547 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Tue, 21 Feb 2023 17:17:37 +0100 Subject: [PATCH 1/2] Update SibEventListener to correctly send DOI emails --- Event/SendinblueListSubscriber.php | 45 +------------- .../Event/SendinblueListSubscriberTest.php | 61 ++----------------- 2 files changed, 7 insertions(+), 99 deletions(-) diff --git a/Event/SendinblueListSubscriber.php b/Event/SendinblueListSubscriber.php index 359216d0..5d11879f 100644 --- a/Event/SendinblueListSubscriber.php +++ b/Event/SendinblueListSubscriber.php @@ -13,10 +13,8 @@ use GuzzleHttp\ClientInterface; use SendinBlue\Client\Api\ContactsApi; -use SendinBlue\Client\ApiException; use SendinBlue\Client\Configuration; use SendinBlue\Client\Model\CreateDoiContact; -use SendinBlue\Client\Model\UpdateContact; use Sulu\Bundle\FormBundle\Entity\Dynamic; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RequestStack; @@ -113,44 +111,6 @@ public function listSubscribe(FormSavePostEvent $event): void return; } - $contact = null; - try { - $contact = $this->contactsApi->getContactInfo($email); - } catch (ApiException $e) { - if (404 !== $e->getCode()) { - throw $e; - } - // Contact does not exist, ignore the exception - } - - if (null !== $contact) { - $updateContact = new UpdateContact(); - - $updateContact->setAttributes( - (object) \array_replace( - (array) $contact->getAttributes(), - [ - 'FIRST_NAME' => $firstName, - 'LAST_NAME' => $lastName, - ] - ) - ); - - /** @var int[] $collectedListIds */ - $collectedListIds = $contact->getListIds(); - foreach ($listIdsByMailTemplate as $mailTemplateId => $listIds) { - $collectedListIds = \array_merge($collectedListIds, $listIds); - } - - $collectedListIds = \array_unique($collectedListIds); - - $updateContact->setListIds($collectedListIds); - - $this->contactsApi->updateContact($email, $updateContact); - - return; - } - foreach ($listIdsByMailTemplate as $mailTemplateId => $listIds) { $createDoiContact = new CreateDoiContact([ 'email' => $email, @@ -158,9 +118,10 @@ public function listSubscribe(FormSavePostEvent $event): void 'includeListIds' => $listIds, 'redirectionUrl' => $redirectionUrl, 'attributes' => [ - 'FIRST_NAME' => $firstName, - 'LAST_NAME' => $lastName, + 'firstname' => $firstName, + 'lastname' => $lastName, ], + 'updateEnabled' => true, ]); $this->contactsApi->createDoiContact($createDoiContact); diff --git a/Tests/Unit/Event/SendinblueListSubscriberTest.php b/Tests/Unit/Event/SendinblueListSubscriberTest.php index d4b26dee..7adf385a 100644 --- a/Tests/Unit/Event/SendinblueListSubscriberTest.php +++ b/Tests/Unit/Event/SendinblueListSubscriberTest.php @@ -17,7 +17,6 @@ use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; use Psr\Http\Message\RequestInterface; -use SendinBlue\Client\ApiException; use Sulu\Bundle\FormBundle\Configuration\FormConfiguration; use Sulu\Bundle\FormBundle\Entity\Dynamic; use Sulu\Bundle\FormBundle\Entity\Form; @@ -78,12 +77,6 @@ public function testlistSubscribeNotExist(): void /** @var RequestInterface $request */ $request = $args[0]; - if ('https://api.sendinblue.com/v3/contacts/john.doe%40example.org' === $request->getUri()->__toString()) { - $self->assertSame('GET', $request->getMethod()); - - throw new ApiException('', 404); - } - if ('https://api.sendinblue.com/v3/contacts/doubleOptinConfirmation' === $request->getUri()->__toString()) { $self->assertSame('POST', $request->getMethod()); @@ -92,12 +85,13 @@ public function testlistSubscribeNotExist(): void $self->assertSame([ 'email' => 'john.doe@example.org', 'attributes' => [ - 'FIRST_NAME' => 'John', - 'LAST_NAME' => 'Doe', + 'firstname' => 'John', + 'lastname' => 'Doe', ], 'includeListIds' => ['789'], 'templateId' => 456, 'redirectionUrl' => 'http://localhost?subscribe=true', + 'updateEnable' => true, ], $json); return new Response(); @@ -105,54 +99,7 @@ public function testlistSubscribeNotExist(): void throw new \RuntimeException('Unexpected request: ' . $request->getUri()->__toString()); }) - ->shouldBeCalledTimes(2); - - // act - $this->sendinblueListSubscriber->listSubscribe($event); - - $this->assertTrue(true); - } - - public function testlistSubscribeAlreadyExist(): void - { - $this->requestStack->push(Request::create('http://localhost/', 'POST')); - $event = $this->createFormSavePostEvent(); - - $self = $this; - $this->client->send(Argument::cetera())->will(function($args) use ($self) { - /** @var RequestInterface $request */ - $request = $args[0]; - - if ('https://api.sendinblue.com/v3/contacts/john.doe%40example.org' === $request->getUri()->__toString() - && 'GET' === $request->getMethod() - ) { - return new Response(200, ['Content-Type' => 'application/json'], \json_encode([ - 'id' => 123, - 'email' => 'john.doe@example.org', - 'attributes' => [], - 'listIds' => [], - ])); - } - - if ('https://api.sendinblue.com/v3/contacts/john.doe%40example.org' === $request->getUri()->__toString() - && 'PUT' === $request->getMethod() - ) { - $json = \json_decode($request->getBody()->getContents(), true); - - $self->assertSame([ - 'attributes' => [ - 'FIRST_NAME' => 'John', - 'LAST_NAME' => 'Doe', - ], - 'listIds' => ['789'], - ], $json); - - return new Response(); - } - - throw new \RuntimeException('Unexpected request (' . $request->getMethod() . '): ' . $request->getUri()->__toString()); - }) - ->shouldBeCalledTimes(2); + ->shouldBeCalledOnce(); // act $this->sendinblueListSubscriber->listSubscribe($event); From 393179602ae3c49950b15dbc5b3ca6e0cd8c3229 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Tue, 21 Feb 2023 17:39:01 +0100 Subject: [PATCH 2/2] Fix ListSubscriber --- Event/SendinblueListSubscriber.php | 3 +-- Tests/Unit/Event/SendinblueListSubscriberTest.php | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Event/SendinblueListSubscriber.php b/Event/SendinblueListSubscriber.php index 5d11879f..4864c9fc 100644 --- a/Event/SendinblueListSubscriber.php +++ b/Event/SendinblueListSubscriber.php @@ -82,7 +82,7 @@ public function listSubscribe(FormSavePostEvent $event): void $email = ''; $firstName = ''; $lastName = ''; - $redirectionUrl = $request->getUriForPath('') . '?subscribe=true'; + $redirectionUrl = $request->getUriForPath($request->getPathInfo()) . '?send=true&subscribe=true'; $listIdsByMailTemplate = []; foreach ($form['fields'] as $field) { @@ -121,7 +121,6 @@ public function listSubscribe(FormSavePostEvent $event): void 'firstname' => $firstName, 'lastname' => $lastName, ], - 'updateEnabled' => true, ]); $this->contactsApi->createDoiContact($createDoiContact); diff --git a/Tests/Unit/Event/SendinblueListSubscriberTest.php b/Tests/Unit/Event/SendinblueListSubscriberTest.php index 7adf385a..6df62e14 100644 --- a/Tests/Unit/Event/SendinblueListSubscriberTest.php +++ b/Tests/Unit/Event/SendinblueListSubscriberTest.php @@ -69,7 +69,7 @@ public function testGetSubscribedEvents(): void public function testlistSubscribeNotExist(): void { - $this->requestStack->push(Request::create('http://localhost/', 'POST')); + $this->requestStack->push(Request::create('http://localhost/newsletter', 'POST')); $event = $this->createFormSavePostEvent(); $self = $this; @@ -90,8 +90,7 @@ public function testlistSubscribeNotExist(): void ], 'includeListIds' => ['789'], 'templateId' => 456, - 'redirectionUrl' => 'http://localhost?subscribe=true', - 'updateEnable' => true, + 'redirectionUrl' => 'http://localhost/newsletter?send=true&subscribe=true', ], $json); return new Response();