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() {