Skip to content

Commit

Permalink
Merge pull request #3 from Potelo/update-4.0.9
Browse files Browse the repository at this point in the history
Update 4.0.9
  • Loading branch information
jprodrigues70 authored Jan 26, 2021
2 parents 5733761 + a37c21d commit 6cbdd00
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/Model/SentEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 33 additions & 16 deletions tests/MailTrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
]);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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),
]);

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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'));
}

Expand All @@ -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),
]);
Expand Down Expand Up @@ -837,21 +838,37 @@ 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)
]);
$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);
}
}

0 comments on commit 6cbdd00

Please sign in to comment.