From de660211b16be14f65d76559137f6349c075a754 Mon Sep 17 00:00:00 2001 From: Jon Baker Date: Wed, 26 Aug 2020 09:51:20 -0500 Subject: [PATCH 1/3] Adding comment about setting queue in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index aba1bd7..9aba9fa 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ When you are in a dev environment (i.e. using the `.test` domain with Valet, or ## Events -When an email is sent, viewed, or a link is clicked, its tracking information is counted in the database using the jdavidbakr\MailTracker\Model\SentEmail model. This processing is done via dispatched jobs to the queue in order to prevent the database from being overwhelmed in an email blast situation. You may choose the queue that these events are dispatched via the `mail-tracker.tracker-queue` config setting, or leave it `null` to use the default queue. +When an email is sent, viewed, or a link is clicked, its tracking information is counted in the database using the jdavidbakr\MailTracker\Model\SentEmail model. This processing is done via dispatched jobs to the queue in order to prevent the database from being overwhelmed in an email blast situation. You may choose the queue that these events are dispatched via the `mail-tracker.tracker-queue` config setting, or leave it `null` to use the default queue. By using a non-default queue, you can prioritize application-critical tasks above these tracking tasks. You may want to do additional processing on these events, so an event is fired in these cases: From 96595c402fc39c495ab8c188486ab3d39352867a Mon Sep 17 00:00:00 2001 From: Jon Baker Date: Fri, 4 Sep 2020 09:43:08 -0500 Subject: [PATCH 2/3] Laravel 8 Support --- .travis.yml | 11 +++++++++-- composer.json | 7 ++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index cd55d10..36b5353 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,14 +11,21 @@ php: - 7.4 env: - - LARAVEL_VERSION=7.0.* + - LARAVEL_VERSION=7.x-dev TESTBENCH=5.x-dev + - LARAVEL_VERSION=8.x-dev TESTBENCH=6.x-dev + +jobs: + exclude: + - php: 7.2 + env: LARAVEL_VERSION=8.x-dev TESTBENCH=6.x-dev before_install: - - echo "memory_limit=2G" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini + - echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini - cp ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ~/xdebug.ini - phpenv config-rm xdebug.ini - composer global require hirak/prestissimo --update-no-dev - composer require "laravel/framework:${LARAVEL_VERSION}" --no-update --prefer-dist + - composer require "orchestra/testbench:${TESTBENCH}" --no-update --prefer-dist install: travis_retry composer install --no-interaction --prefer-dist diff --git a/composer.json b/composer.json index 40f0f96..b805b3c 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "jdavidbakr", "MailTracker" ], + "minimum-stability": "dev", "homepage": "https://github.com/jdavidbakr/MailTracker", "license": "MIT", "authors": [ @@ -17,15 +18,15 @@ } ], "require": { - "php": ">=7.2.5", - "laravel/framework": "~7.0", + "php": ">=7.2.0", + "illuminate/support": "~7.0|~8.0", "guzzlehttp/guzzle": "^5.3.1|^6.2.1", "aws/aws-php-sns-message-validator": "^1.1", "aws/aws-sdk-php": "^3.133" }, "require-dev": { "phpunit/phpunit": "~8.0", - "orchestra/testbench": "~5.0", + "orchestra/testbench": "~5.0|~6.0", "mockery/mockery": "^1.1" }, "suggest": { From 914c612c597ee9c307e529aa01caa0deae632349 Mon Sep 17 00:00:00 2001 From: Jon Baker Date: Tue, 8 Sep 2020 08:54:37 -0500 Subject: [PATCH 3/3] Allow for header content to contain colons --- src/Model/SentEmail.php | 5 +++- tests/MailTrackerTest.php | 49 ++++++++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/Model/SentEmail.php b/src/Model/SentEmail.php index d117434..023bef5 100644 --- a/src/Model/SentEmail.php +++ b/src/Model/SentEmail.php @@ -99,8 +99,11 @@ public function getSmtpInfoAttribute() public function getHeader($key) { $headers = collect(preg_split("/\r\n|\n|\r/", $this->headers)) + ->filter(function ($header) { + return preg_match("/:/", $header); + }) ->transform(function ($header) { - list($key, $value) = explode(":", $header.":"); + list($key, $value) = explode(":", $header, 2); return collect([ 'key' => trim($key), 'value' => trim($value) diff --git a/tests/MailTrackerTest.php b/tests/MailTrackerTest.php index 0192a4a..8e818a6 100644 --- a/tests/MailTrackerTest.php +++ b/tests/MailTrackerTest.php @@ -30,6 +30,7 @@ use jdavidbakr\MailTracker\Exceptions\BadUrlLink; use jdavidbakr\MailTracker\Events\LinkClickedEvent; use Aws\Sns\MessageValidator as SNSMessageValidator; +use jdavidbakr\MailTracker\Model\SentEmailUrlClicked; use jdavidbakr\MailTracker\Events\EmailDeliveredEvent; use Illuminate\Foundation\Testing\DatabaseTransactions; use jdavidbakr\MailTracker\Events\ComplaintMessageEvent; @@ -60,10 +61,10 @@ public function testSendMessage() { // Create an old email to purge Config::set('mail-tracker.expire-days', 1); - $old_email = \jdavidbakr\MailTracker\Model\SentEmail::create([ + $old_email = SentEmail::create([ 'hash' => Str::random(32), ]); - $old_url = \jdavidbakr\MailTracker\Model\SentEmailUrlClicked::create([ + $old_url = SentEmailUrlClicked::create([ 'sent_email_id' => $old_email->id, 'hash' => Str::random(32), ]); @@ -193,7 +194,7 @@ public function testPing() { Config::set('mail-tracker.tracker-queue', 'alt-queue'); Bus::fake(); - $track = \jdavidbakr\MailTracker\Model\SentEmail::create([ + $track = SentEmail::create([ 'hash' => Str::random(32), ]); $pings = $track->opens; @@ -213,7 +214,7 @@ public function testLegacyLink() { Config::set('mail-tracker.tracker-queue', 'alt-queue'); Bus::fake(); - $track = \jdavidbakr\MailTracker\Model\SentEmail::create([ + $track = SentEmail::create([ 'hash' => Str::random(32), ]); $clicks = $track->clicks; @@ -237,7 +238,7 @@ public function testLink() { Config::set('mail-tracker.tracker-queue', 'alt-queue'); Bus::fake(); - $track = \jdavidbakr\MailTracker\Model\SentEmail::create([ + $track = SentEmail::create([ 'hash' => Str::random(32), ]); $redirect = 'http://'.Str::random(15).'.com/'.Str::random(10).'/'.Str::random(10).'/'.rand(0, 100).'/'.rand(0, 100).'?page='.rand(0, 100).'&x='.Str::random(32); @@ -263,7 +264,7 @@ public function it_throws_exception_on_invalid_link() { $this->disableExceptionHandling(); $this->expectException(BadUrlLink::class); - $track = \jdavidbakr\MailTracker\Model\SentEmail::create([ + $track = SentEmail::create([ 'hash' => Str::random(32), ]); @@ -360,7 +361,7 @@ public function it_retrieves_the_mesage_id_from_ses_mail_default() $tracker->beforeSendPerformed($event); $tracker->sendPerformed($event); - $sent_email = \jdavidbakr\MailTracker\Model\SentEmail::orderBy('id', 'desc')->first(); + $sent_email = SentEmail::orderBy('id', 'desc')->first(); $this->assertEquals('aws-mailer-hash', $sent_email->message_id); } @@ -428,7 +429,7 @@ public function it_retrieves_the_mesage_id_from_ses_mail_driver() $tracker->beforeSendPerformed($event); $tracker->sendPerformed($event); - $sent_email = \jdavidbakr\MailTracker\Model\SentEmail::orderBy('id', 'desc')->first(); + $sent_email = SentEmail::orderBy('id', 'desc')->first(); $this->assertEquals('aws-mailer-hash', $sent_email->message_id); } @@ -668,7 +669,7 @@ public function it_handles_ampersands_in_links() 'clicks' => 1, ]); - $track = \jdavidbakr\MailTracker\Model\SentEmail::whereHash($hash)->first(); + $track = SentEmail::whereHash($hash)->first(); $this->assertNotNull($track); $this->assertEquals(1, $track->clicks); } @@ -728,7 +729,7 @@ public function it_handles_apostrophes_in_links() 'clicks' => 1, ]); - $track = \jdavidbakr\MailTracker\Model\SentEmail::whereHash($hash)->first(); + $track = SentEmail::whereHash($hash)->first(); $this->assertNotNull($track); $this->assertEquals(1, $track->clicks); } @@ -766,7 +767,7 @@ public function it_retrieves_header_data() } catch (Swift_TransportException $e) { } - $track = \jdavidbakr\MailTracker\Model\SentEmail::orderBy('id', 'desc')->first(); + $track = SentEmail::orderBy('id', 'desc')->first(); $this->assertEquals($header_test, $track->getHeader('X-Header-Test')); } @@ -782,10 +783,10 @@ public function it_handles_secondary_connection() $this->app['migrator']->setConnection('secondary'); $this->artisan('migrate', ['--database' => 'secondary']); - $old_email = \jdavidbakr\MailTracker\Model\SentEmail::create([ + $old_email = SentEmail::create([ 'hash' => Str::random(32), ]); - $old_url = \jdavidbakr\MailTracker\Model\SentEmailUrlClicked::create([ + $old_url = SentEmailUrlClicked::create([ 'sent_email_id' => $old_email->id, 'hash' => Str::random(32), ]); @@ -835,14 +836,14 @@ public function it_handles_secondary_connection() public function it_can_retrieve_url_clicks_from_eloquent() { Event::fake(); - $track = \jdavidbakr\MailTracker\Model\SentEmail::create([ + $track = SentEmail::create([ 'hash' => Str::random(32), ]); $message_id = Str::random(32); $track->message_id = $message_id; $track->save(); - $urlClick = \jdavidbakr\MailTracker\Model\SentEmailUrlClicked::create([ + $urlClick = SentEmailUrlClicked::create([ 'sent_email_id' => $track->id, 'url' => 'https://example.com', 'hash' => Str::random(32) @@ -850,6 +851,22 @@ public function it_can_retrieve_url_clicks_from_eloquent() $urlClick->save(); $this->assertTrue($track->urlClicks->count() === 1); $this->assertInstanceOf(\Illuminate\Support\Collection::class, $track->urlClicks); - $this->assertInstanceOf(\jdavidbakr\MailTracker\Model\SentEmailUrlClicked::class, $track->urlClicks->first()); + $this->assertInstanceOf(SentEmailUrlClicked::class, $track->urlClicks->first()); + } + + /** + * @test + */ + public function it_handles_headers_with_colons() + { + $headerData = '{"some_id":2,"some_othger_id":"0dd75231-31bb-4e67-8ab7-a83315f75a44","some_field":"A Field Value"}'; + $track = SentEmail::create([ + 'hash' => Str::random(32), + 'headers' => 'X-MyHeader: '.$headerData, + ]); + + $retrieval = $track->getHeader('X-MyHeader'); + + $this->assertEquals($headerData, $retrieval); } }