forked from ts-navghane/sparkpost-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sparkpost tracking config option #5
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
26c07f2
Adding an option to enable/disable Sparkpost tracking
escopecz a4a474b
Adding installation and config optioin to the readme
escopecz 4f67cd9
CS fix
escopecz d99efa8
STAN fixes
escopecz 5965fd3
The test assertions were failing but the test was still green. We can…
escopecz 81ddc1e
Fixing mock types
escopecz 9927000
Merge remote-tracking branch 'origin/main' into tracking-config
escopecz bb78d49
This line isn't necessary
escopecz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,46 @@ | ||
### Mautic Sparkpost Plugin | ||
# Mautic Sparkpost Plugin | ||
|
||
This plugin enable Mautic 5 to run Sparkpost as an email transport. Features: | ||
- API transport. This transport can send up to 2000 emails per API request which makes it very fast compared to SMTP. | ||
- Bounce webhook handling. This plugin will unsubscribe contacts in Mautic based on the hard bounces while Sparkpost will take care of the soft bounce retrieals. | ||
|
||
## Installation | ||
|
||
#### Mautic Mailer DSN Scheme | ||
There are several ways how to install this plugin. Here are the options from best to worst. | ||
|
||
### Via Composer | ||
|
||
This is the best option for Mautic instances that were installed via Composer (recommended way to install Mautic) | ||
|
||
Steps: | ||
1. `composer install acquia/mc-cs-plugin-sparkpost` | ||
2. `bin/console mautic:plugins:install` | ||
|
||
### Via Git | ||
|
||
This option is useful for development or testing of this plugin as you'll be able to checkout different branches of this repository. | ||
|
||
Steps: | ||
1. `cd plugins` | ||
2. `git clone [email protected]:acquia/mc-cs-plugin-sparkpost.git SparkpostBundle` | ||
3. `cd ..` | ||
4. `bin/console mautic:plugins:install` | ||
|
||
### Via SFTP | ||
|
||
You should reconsider using this method as the other two above are way better, but this is also possible. | ||
|
||
Steps: | ||
1. [Download this plugin](https://github.com/acquia/mc-cs-plugin-sparkpost/archive/refs/heads/main.zip) | ||
2. Rename the folder `mc-cs-plugin-sparkpost-main` to `SparkpostBundle` | ||
3. Upload this folder to the `plugins` directory of your Mautic files. | ||
4. `bin/console mautic:plugins:install` | ||
|
||
## Configuration | ||
|
||
After the plugin is installed go to the Mautic's global configuration, the Email settings and configure the DSN. | ||
|
||
### Mautic Mailer DSN Scheme | ||
`mautic+sparkpost+api` | ||
|
||
#### Mautic Mailer DSN Example | ||
|
@@ -16,7 +51,15 @@ This plugin enable Mautic 5 to run Sparkpost as an email transport. Features: | |
|
||
<img width="1105" alt="sparkpost-email-dsn-example" src="Assets/img/sparkpost-email-dsn-example.png"> | ||
|
||
### Testing | ||
### Sparkpost tracking | ||
|
||
The Sparkpost tracking is disabled by default as then the email open and clicks would be tracked twice. Once by Sparkpost, second time by Mautic. This can create some unexpected behavior. The Sparkpost tracking is disabled by default, but you can enable it by adding this row to the Mautic configuration file located at `config/local.php`: | ||
|
||
```php | ||
'sparkpost_tracking_enabled' => true, | ||
``` | ||
|
||
## Testing | ||
|
||
To run all tests `composer phpunit` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,29 +21,33 @@ class SparkpostTransportTest extends MauticMysqlTestCase | |
|
||
protected function setUp(): void | ||
{ | ||
$this->configParams['mailer_dsn'] = 'mautic+sparkpost+api://:some_api@some_host:25?region=us'; | ||
$this->configParams['messenger_dsn_email'] = 'sync://'; | ||
$this->configParams['mailer_custom_headers'] = ['x-global-custom-header' => 'value123']; | ||
$this->configParams['mailer_from_email'] = '[email protected]'; | ||
$this->configParams['mailer_from_name'] = 'Admin'; | ||
$this->configParams['mailer_dsn'] = 'mautic+sparkpost+api://:some_api@some_host:25?region=us'; | ||
$this->configParams['messenger_dsn_email'] = 'sync://'; | ||
$this->configParams['mailer_custom_headers'] = ['x-global-custom-header' => 'value123']; | ||
$this->configParams['mailer_from_email'] = '[email protected]'; | ||
$this->configParams['mailer_from_name'] = 'Admin'; | ||
$this->configParams['sparkpost_tracking_enabled'] = 'testEmailSendToContactSync' === $this->getName() ? $this->getProvidedData()[0] : false; | ||
parent::setUp(); | ||
$this->translator = self::getContainer()->get('translator'); | ||
} | ||
|
||
public function testEmailSendToContactSync(): void | ||
/** | ||
* @dataProvider provideTrackingConfig | ||
*/ | ||
public function testEmailSendToContactSync(bool $expectedTrackingConfig): void | ||
{ | ||
$expectedResponses = [ | ||
function ($method, $url, $options): MockResponse { | ||
function ($method, $url, $options) use ($expectedTrackingConfig): MockResponse { | ||
Assert::assertSame(Request::METHOD_POST, $method); | ||
Assert::assertSame('https://api.sparkpost.com/api/v1/utils/content-previewer/', $url); | ||
$this->assertSparkpostRequestBody($options['body']); | ||
$this->assertSparkpostRequestBody($options['body'], $expectedTrackingConfig); | ||
|
||
return new MockResponse('{"results": {"subject": "Hello there!", "html": "This is test body for {contactfield=email}!"}}'); | ||
}, | ||
function ($method, $url, $options): MockResponse { | ||
function ($method, $url, $options) use ($expectedTrackingConfig): MockResponse { | ||
Assert::assertSame(Request::METHOD_POST, $method); | ||
Assert::assertSame('https://api.sparkpost.com/api/v1/transmissions/', $url); | ||
$this->assertSparkpostRequestBody($options['body']); | ||
$this->assertSparkpostRequestBody($options['body'], $expectedTrackingConfig); | ||
|
||
return new MockResponse('{"results": {"total_rejected_recipients": 0, "total_accepted_recipients": 1, "id": "11668787484950529"}}'); | ||
}, | ||
|
@@ -90,6 +94,15 @@ function ($method, $url, $options): MockResponse { | |
Assert::assertSame('', $email->getReplyTo()[0]->getName()); | ||
} | ||
|
||
/** | ||
* @return array<string, bool[]> | ||
*/ | ||
public function provideTrackingConfig(): iterable | ||
{ | ||
yield 'sparkpost_tracking_enabled is TRUE' => [true]; | ||
yield 'sparkpost_tracking_enabled is FALSE' => [false]; | ||
} | ||
|
||
public function testTestTransportButton(): void | ||
{ | ||
$expectedResponses = [ | ||
|
@@ -127,7 +140,7 @@ private function assertSparkpostTestRequestBody(string $body): void | |
Assert::assertSame('Hi! This is a test email from Mautic. Testing...testing...1...2...3!', $bodyArray['content']['text']); | ||
} | ||
|
||
private function assertSparkpostRequestBody(string $body): void | ||
private function assertSparkpostRequestBody(string $body, bool $expectedTrackingConfig): void | ||
{ | ||
$bodyArray = json_decode($body, true); | ||
Assert::assertSame('Admin User <[email protected]>', $bodyArray['content']['from']); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -4,6 +4,7 @@ | |||
|
||||
namespace MauticPlugin\SparkpostBundle\Tests\Unit\Mailer\Factory; | ||||
|
||||
use Mautic\CoreBundle\Helper\CoreParametersHelper; | ||||
use Mautic\EmailBundle\Model\TransportCallback; | ||||
use MauticPlugin\SparkpostBundle\Mailer\Factory\SparkpostTransportFactory; | ||||
use MauticPlugin\SparkpostBundle\Mailer\Transport\SparkpostTransport; | ||||
|
@@ -22,19 +23,22 @@ class SparkpostTransportFactoryTest extends TestCase | |||
{ | ||||
private SparkpostTransportFactory $sparkpostTransportFactory; | ||||
|
||||
private TranslatorInterface|MockObject $translatorMock; | ||||
private TranslatorInterface&MockObject $translatorMock; | ||||
private CoreParametersHelper&MockObject $coreParametersHelper; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We cannot use PHP's intersection types until we bump minimum PHP version to mc-cs-plugin-sparkpost/composer.json Line 16 in 487da41
|
||||
|
||||
protected function setUp(): void | ||||
{ | ||||
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class); | ||||
$this->translatorMock = $this->createMock(TranslatorInterface::class); | ||||
$transportCallbackMock = $this->createMock(TransportCallback::class); | ||||
$httpClientMock = $this->createMock(HttpClientInterface::class); | ||||
$loggerMock = $this->createMock(LoggerInterface::class); | ||||
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class); | ||||
$this->translatorMock = $this->createMock(TranslatorInterface::class); | ||||
$transportCallbackMock = $this->createMock(TransportCallback::class); | ||||
$this->coreParametersHelper = $this->createMock(CoreParametersHelper::class); | ||||
$httpClientMock = $this->createMock(HttpClientInterface::class); | ||||
$loggerMock = $this->createMock(LoggerInterface::class); | ||||
|
||||
$this->sparkpostTransportFactory = new SparkpostTransportFactory( | ||||
$transportCallbackMock, | ||||
$this->translatorMock, | ||||
$this->coreParametersHelper, | ||||
$eventDispatcherMock, | ||||
$httpClientMock, | ||||
$loggerMock | ||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter
$expectedTrackingConfig
is not used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch! It turns out the assertions were muted by a code that was catching generic
Exception
. There were already several assertions failing but the tests were green. Fixed it.