Skip to content

Commit

Permalink
Merge pull request #1 from Potelo/update-4.0.7
Browse files Browse the repository at this point in the history
Update 4.0.7
  • Loading branch information
jprodrigues70 authored Jan 26, 2021
2 parents a0d0a58 + 4f24821 commit bf28b8e
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 default queue in order to prevent the database from being overwhelmed is an email blast situation.
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.

You may want to do additional processing on these events, so an event is fired in these cases:

Expand Down
7 changes: 6 additions & 1 deletion config/mail-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
/**
* Determines whether or not the body of the email is logged in the sent_emails table
*/
'log-content' => true
'log-content' => true,

/**
* What queue should we dispatch our tracking jobs to? Null will use the default queue.
*/
'tracker-queue' => null,

];
6 changes: 3 additions & 3 deletions src/MailTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function injectTrackingPixel($html, $hash)
// Append the tracking url
$tracking_pixel = '<img border=0 width=1 alt="" height=1 src="'.route('mailTracker_t', [$hash]).'" />';

$linebreak = Str::random(32);
$linebreak = app(Str::class)->random(32);
$html = str_replace("\n", $linebreak, $html);

if (preg_match("/^(.*<body[^>]*>)(.*)$/", $html, $matches)) {
Expand All @@ -85,7 +85,7 @@ protected function injectLinkTracker($html, $hash)
$this->hash = $hash;

$html = preg_replace_callback(
"/(<a[^>]*href=['\"])([^'\"]*)/",
"/(<a[^>]*href=[\"])([^\"]*)/",
[$this, 'inject_link_callback'],
$html
);
Expand Down Expand Up @@ -175,7 +175,7 @@ protected function createTrackers($message)
continue;
}
do {
$hash = Str::random(32);
$hash = app(Str::class)->random(32);
$used = SentEmail::where('hash', $hash)->count();
} while ($used > 0);
$headers->addTextHeader('X-Mailer-Hash', $hash);
Expand Down
6 changes: 4 additions & 2 deletions src/MailTrackerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function getT($hash)
$tracker = Model\SentEmail::where('hash', $hash)
->first();
if ($tracker) {
RecordTrackingJob::dispatch($tracker);
RecordTrackingJob::dispatch($tracker)
->onQueue(config('mail-tracker.tracker-queue'));
}

return $response;
Expand All @@ -58,7 +59,8 @@ protected function linkClicked($url, $hash)
$tracker = Model\SentEmail::where('hash', $hash)
->first();
if ($tracker) {
RecordLinkClickJob::dispatch($tracker, $url);
RecordLinkClickJob::dispatch($tracker, $url)
->onQueue(config('mail-tracker.tracker-queue'));
return redirect($url);
}

Expand Down
9 changes: 6 additions & 3 deletions src/SNSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,19 @@ protected function process_notification($message)

protected function process_delivery($message)
{
RecordDeliveryJob::dispatch($message);
RecordDeliveryJob::dispatch($message)
->onQueue(config('mail-tracker.tracker-queue'));
}

public function process_bounce($message)
{
RecordBounceJob::dispatch($message);
RecordBounceJob::dispatch($message)
->onQueue(config('mail-tracker.tracker-queue'));
}

public function process_complaint($message)
{
RecordComplaintJob::dispatch($message);
RecordComplaintJob::dispatch($message)
->onQueue(config('mail-tracker.tracker-queue'));
}
}
113 changes: 99 additions & 14 deletions tests/MailTrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function testSendMessage()
]);
// Go into the future to make sure that the old email gets removed
\Carbon\Carbon::setTestNow(\Carbon\Carbon::now()->addWeek());
$str = Mockery::mock(Str::class);
app()->instance(Str::class, $str);
$str->shouldReceive('random')
->once()
->andReturn('random-hash');

Event::fake();

Expand Down Expand Up @@ -99,6 +104,7 @@ public function testSendMessage()
Event::assertDispatched(EmailSentEvent::class);

$this->assertDatabaseHas('sent_emails', [
'hash' => 'random-hash',
'recipient' => $name.' <'.$email.'>',
'subject' => $subject,
'sender' => 'From Name <[email protected]>',
Expand All @@ -115,6 +121,11 @@ public function testSendMessageWithMailRaw()
$name = $faker->firstName . ' ' .$faker->lastName;
$content = 'Text to e-mail';
View::addLocation(__DIR__);
$str = Mockery::mock(Str::class);
app()->instance(Str::class, $str);
$str->shouldReceive('random')
->once()
->andReturn('random-hash');

try {
Mail::raw($content, function ($message) use ($email, $name) {
Expand All @@ -126,6 +137,7 @@ public function testSendMessageWithMailRaw()
}

$this->assertDatabaseHas('sent_emails', [
'hash' => 'random-hash',
'recipient' => $name.' <'.$email.'>',
'sender' => 'From Name <[email protected]>',
'recipient' => "{$name} <{$email}>",
Expand Down Expand Up @@ -180,7 +192,7 @@ public function it_doesnt_track_if_told_not_to()
*/
public function testPing()
{
$this->disableExceptionHandling();
Config::set('mail-tracker.tracker-queue', 'alt-queue');
Bus::fake();
$track = \jdavidbakr\MailTracker\Model\SentEmail::create([
'hash' => Str::random(32),
Expand All @@ -193,12 +205,14 @@ public function testPing()

$response->assertSuccessful();
Bus::assertDispatched(RecordTrackingJob::class, function ($e) use ($track) {
return $e->sentEmail->id == $track->id;
return $e->sentEmail->id == $track->id &&
$e->queue == 'alt-queue';
});
}

public function testLegacyLink()
{
Config::set('mail-tracker.tracker-queue', 'alt-queue');
Bus::fake();
$track = \jdavidbakr\MailTracker\Model\SentEmail::create([
'hash' => Str::random(32),
Expand All @@ -215,12 +229,14 @@ public function testLegacyLink()
$response->assertRedirect($redirect);
Bus::assertDispatched(RecordLinkClickJob::class, function ($job) use ($track, $redirect) {
return $job->sentEmail->id == $track->id &&
$job->url == $redirect;
$job->url == $redirect &&
$job->queue == 'alt-queue';
});
}

public function testLink()
{
Config::set('mail-tracker.tracker-queue', 'alt-queue');
Bus::fake();
$track = \jdavidbakr\MailTracker\Model\SentEmail::create([
'hash' => Str::random(32),
Expand All @@ -236,7 +252,8 @@ public function testLink()
$response->assertRedirect($redirect);
Bus::assertDispatched(RecordLinkClickJob::class, function ($job) use ($track, $redirect) {
return $job->sentEmail->id == $track->id &&
$job->url == $redirect;
$job->url == $redirect &&
$job->queue == 'alt-queue';
});
}

Expand Down Expand Up @@ -357,24 +374,27 @@ public function it_retrieves_the_mesage_id_from_ses_mail_default()
*/
public function it_retrieves_the_mesage_id_from_ses_mail_driver()
{
$str = Mockery::mock(Str::class);
app()->instance(Str::class, $str);
$str->shouldReceive('random')
->with(32)
->once()
->andReturn('random-hash');
Config::set('mail.driver', 'ses');
Config::set('mail.default', null);
$headers = Mockery::mock();
$headers->shouldReceive('get')
->with('X-No-Track')
->once()
->andReturn(null);
$this->mailer_hash = '';
$headers->shouldReceive('addTextHeader')
->once()
->andReturnUsing(function ($key, $value) {
$this->mailer_hash = $value;
});
->with('X-Mailer-Hash', 'random-hash');
$mailer_hash_header = Mockery::mock();
$mailer_hash_header->shouldReceive('getFieldBody')
->once()
->andReturnUsing(function () {
return $this->mailer_hash;
return 'random-hash';
});
$headers->shouldReceive('get')
->with('X-Mailer-Hash')
Expand Down Expand Up @@ -502,7 +522,7 @@ public function it_ignores_invalid_topic()
*/
public function it_processes_a_delivery()
{
$this->disableExceptionHandling();
Config::set('mail-tracker.tracker-queue', 'alt-queue');
Bus::fake();
$message = [
'notificationType' => 'Delivery',
Expand All @@ -523,7 +543,8 @@ public function it_processes_a_delivery()

$response->assertSee('notification processed');
Bus::assertDispatched(RecordDeliveryJob::class, function ($job) use ($message) {
return $job->message == (object)$message;
return $job->message == (object)$message &&
$job->queue == 'alt-queue';
});
}

Expand All @@ -532,6 +553,7 @@ public function it_processes_a_delivery()
*/
public function it_processes_a_bounce()
{
Config::set('mail-tracker.tracker-queue', 'alt-queue');
Bus::fake();
$message = [
'notificationType' => 'Bounce',
Expand All @@ -552,7 +574,8 @@ public function it_processes_a_bounce()

$response->assertSee('notification processed');
Bus::assertDispatched(RecordBounceJob::class, function ($job) use ($message) {
return $job->message == (object)$message;
return $job->message == (object)$message &&
$job->queue == 'alt-queue';
});
}

Expand All @@ -561,7 +584,7 @@ public function it_processes_a_bounce()
*/
public function it_processes_a_complaint()
{
$this->disableExceptionHandling();
Config::set('mail-tracker.tracker-queue', 'alt-queue');
Bus::fake();
$message = [
'notificationType' => 'Complaint',
Expand All @@ -582,7 +605,8 @@ public function it_processes_a_complaint()

$response->assertSee('notification processed');
Bus::assertDispatched(RecordComplaintJob::class, function ($job) use ($message) {
return $job->message == (object)$message;
return $job->message == (object)$message &&
$job->queue == 'alt-queue';
});
}

Expand Down Expand Up @@ -651,6 +675,67 @@ public function it_handles_ampersands_in_links()
$this->assertEquals(1, $track->clicks);
}

/**
* @test
*/
public function it_handles_apostrophes_in_links()
{
Event::fake();
Config::set('mail-tracker.track-links', true);
Config::set('mail-tracker.inject-pixel', true);
Config::set('mail.driver', 'array');
(new MailServiceProvider(app()))->register();
// Must re-register the MailTracker to get the test to work
$this->app['mailer']->getSwiftMailer()->registerPlugin(new MailTracker());

$faker = Factory::create();
$email = $faker->email;
$subject = $faker->sentence;
$name = $faker->firstName . ' ' . $faker->lastName;
View::addLocation(__DIR__);

Mail::send('email.testApostrophe', [], function ($message) use ($email, $subject, $name) {
$message->from('[email protected]', 'From Name');
$message->sender('[email protected]', 'Sender Name');
$message->to($email, $name);
$message->cc('[email protected]', 'CC Name');
$message->bcc('[email protected]', 'BCC Name');
$message->replyTo('[email protected]', 'Reply-To Name');
$message->subject($subject);
$message->priority(3);
});
$driver = app('mailer')->getSwiftMailer()->getTransport();
$this->assertEquals(1, count($driver->messages()));

$mes = $driver->messages()[0];
$body = $mes->getBody();
$hash = $mes->getHeaders()->get('X-Mailer-Hash')->getValue();

$matches = null;
preg_match_all('/(<a[^>]*href=[\"])([^\"]*)/', $body, $matches);
$links = $matches[2];
$aLink = $links[0];

$expected_url = "http://www.google.com?q=foo'bar";
$this->assertNotNull($aLink);
$this->assertNotEquals($expected_url, $aLink);

$response = $this->call('GET', $aLink);
$response->assertRedirect($expected_url);

Event::assertDispatched(LinkClickedEvent::class);

$this->assertDatabaseHas('sent_emails_url_clicked', [
'url' => $expected_url,
'clicks' => 1,
]);

$track = \jdavidbakr\MailTracker\Model\SentEmail::whereHash($hash)->first();
$this->assertNotNull($track);
$this->assertEquals(1, $track->clicks);
}


/**
* @test
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/email/testApostrophe.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<head>
This is the head
</head>
<body>
<h1>
This is a test!
</h1>
<p>
<a class="test" href="http://www.google.com?q=foo'bar">
Click here
</a>
</p>
</body>

0 comments on commit bf28b8e

Please sign in to comment.