Skip to content

Commit

Permalink
Merge tag '4.0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
jprodrigues70 committed Jan 26, 2021
2 parents a0d0a58 + e00b77b commit ff0ba07
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 9 deletions.
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
87 changes: 81 additions & 6 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,6 @@ public function it_doesnt_track_if_told_not_to()
*/
public function testPing()
{
$this->disableExceptionHandling();
Bus::fake();
$track = \jdavidbakr\MailTracker\Model\SentEmail::create([
'hash' => Str::random(32),
Expand Down Expand Up @@ -357,24 +368,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 @@ -651,6 +665,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 ff0ba07

Please sign in to comment.