From 40706739277b21225502a95d0e547d76c70b9b46 Mon Sep 17 00:00:00 2001 From: bencroker Date: Thu, 2 May 2024 09:42:46 -0600 Subject: [PATCH 1/2] 2.15.2 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d80ac3a..b1093ca7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Release Notes for Campaign -## 2.15.2 - Unreleased +## 2.15.2 - 2024-05-02 ### Fixed From 972426aeec72aaeccb751b41bd56add5b4d02d3c Mon Sep 17 00:00:00 2001 From: bencroker Date: Mon, 6 May 2024 17:04:37 -0600 Subject: [PATCH 2/2] Truncate source if too long --- CHANGELOG.md | 6 ++++++ composer.json | 2 +- src/services/FormsService.php | 17 ++++++++++++++++- tests/TESTS.md | 1 + tests/pest/Feature/FormTest.php | 22 +++++++++++++++++++++- 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1093ca7..5328c5f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Release Notes for Campaign +## 2.15.3 - 2024-05-06 + +### Fixed + +- Fixed a bug in which contact subscriptions were failing when the referrer URL was longer than 255 characters ([#473](https://github.com/putyourlightson/craft-campaign/issues/473)). + ## 2.15.2 - 2024-05-02 ### Fixed diff --git a/composer.json b/composer.json index 3caf01bb..aae527b6 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "putyourlightson/craft-campaign", "description": "Send and manage email campaigns, contacts and mailing lists.", - "version": "2.15.2", + "version": "2.15.3", "type": "craft-plugin", "homepage": "https://putyourlightson.com/plugins/campaign", "license": "proprietary", diff --git a/src/services/FormsService.php b/src/services/FormsService.php index b771d420..42e566ed 100755 --- a/src/services/FormsService.php +++ b/src/services/FormsService.php @@ -173,6 +173,7 @@ public function createAndSubscribeContact(string $email, array|null $fieldValues } $contact->setScenario(Element::SCENARIO_LIVE); + $source = $this->getTruncatedSource($source); // If subscribe verification required if ($mailingList->getMailingListType()->subscribeVerificationRequired) { @@ -216,7 +217,7 @@ public function createAndSubscribeContact(string $email, array|null $fieldValues public function subscribeContact(ContactElement $contact, MailingListElement $mailingList, string $sourceType = null, string $source = null, bool $verified = null): void { $sourceType = $sourceType ?? ''; - $source = $source ?? ''; + $source = $this->getTruncatedSource($source); $verified = $verified ?? false; // Fire a before event @@ -327,4 +328,18 @@ public function sendEmail(string $email, string $subject, string $htmlBody, stri return $message->send(); } + + /** + * Returns a truncated source if longer than the allowed length. + */ + private function getTruncatedSource(?string $source): string + { + if ($source === null) { + return ''; + } + + $suffix = '...'; + + return StringHelper::truncate($source, 255 - strlen($suffix), $suffix); + } } diff --git a/tests/TESTS.md b/tests/TESTS.md index d43695ba..97a8dd16 100644 --- a/tests/TESTS.md +++ b/tests/TESTS.md @@ -34,6 +34,7 @@ _Tests interacting with contacts via forms._ ![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) A verify subscribe email is sent to a pending contact on subscribe. ![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) A verify unsubscribe email is sent to a contact on unsubscribe. ![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Subscribing and then unsubscribing a contact works. +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Subscribing results in a truncated source if too long. ![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Updating a contact modifies its last activity timestamp. ### [Import](pest/Feature/ImportTest.php) diff --git a/tests/pest/Feature/FormTest.php b/tests/pest/Feature/FormTest.php index 4d85233b..5096178d 100644 --- a/tests/pest/Feature/FormTest.php +++ b/tests/pest/Feature/FormTest.php @@ -1,7 +1,9 @@ getSubscribedMailingLists(); expect($subscribedMailingLists) - ->toHaveCount(0); + ->toBeEmpty(); +}); + +test('Subscribing results in a truncated source if too long', function() { + $mailingList = createMailingList(); + $contact = createContact(); + $source = StringHelper::randomString(252); + Campaign::$plugin->forms->subscribeContact($contact, $mailingList, 'web', $source . StringHelper::randomString(10)); + + /** @var ContactMailingListRecord $contactMailingListRecord */ + $contactMailingListRecord = ContactMailingListRecord::find() + ->where([ + 'contactId' => $contact->id, + 'mailingListId' => $mailingList->id, + ]) + ->one(); + + expect($contactMailingListRecord->source) + ->toBe($source . '...'); }); test('Updating a contact modifies its last activity timestamp', function() {