Skip to content

Commit

Permalink
Merge branch '2.x' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	composer.json
  • Loading branch information
bencroker committed May 6, 2024
2 parents 8ca2d04 + 972426a commit 3ef7912
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/services/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions tests/TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 21 additions & 1 deletion tests/pest/Feature/FormTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use craft\helpers\StringHelper;
use putyourlightson\campaign\Campaign;
use putyourlightson\campaign\models\PendingContactModel;
use putyourlightson\campaign\records\ContactMailingListRecord;
use putyourlightson\campaign\services\FormsService;

/**
Expand Down Expand Up @@ -49,7 +51,25 @@
$subscribedMailingLists = $contact->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() {
Expand Down

0 comments on commit 3ef7912

Please sign in to comment.