diff --git a/src/Model/SentEmail.php b/src/Model/SentEmail.php index fc071ba..e2d9cd1 100644 --- a/src/Model/SentEmail.php +++ b/src/Model/SentEmail.php @@ -109,8 +109,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 6fb604b..c2521e1 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), ]); @@ -194,7 +195,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; @@ -214,7 +215,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; @@ -238,7 +239,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); @@ -264,7 +265,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), ]); @@ -362,7 +363,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); } @@ -430,7 +431,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); } @@ -670,7 +671,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); } @@ -730,7 +731,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); } @@ -768,7 +769,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')); } @@ -784,10 +785,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), ]); @@ -837,14 +838,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) @@ -852,6 +853,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); } }