From 47a1acd2a86551c35d76e890d46ae0bef75916d9 Mon Sep 17 00:00:00 2001 From: Jon Baker Date: Wed, 15 Jul 2020 09:20:06 -0500 Subject: [PATCH 1/4] Updating tests to use dependency injection for Str::[] functions --- src/MailTracker.php | 4 ++-- tests/MailTrackerTest.php | 26 ++++++++++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/MailTracker.php b/src/MailTracker.php index f94f26a..5a230f0 100644 --- a/src/MailTracker.php +++ b/src/MailTracker.php @@ -66,7 +66,7 @@ protected function injectTrackingPixel($html, $hash) // Append the tracking url $tracking_pixel = ''; - $linebreak = Str::random(32); + $linebreak = app(Str::class)->random(32); $html = str_replace("\n", $linebreak, $html); if (preg_match("/^(.*]*>)(.*)$/", $html, $matches)) { @@ -139,7 +139,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); diff --git a/tests/MailTrackerTest.php b/tests/MailTrackerTest.php index be0bc6b..9e6e446 100644 --- a/tests/MailTrackerTest.php +++ b/tests/MailTrackerTest.php @@ -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(); @@ -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 ', @@ -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) { @@ -126,6 +137,7 @@ public function testSendMessageWithMailRaw() } $this->assertDatabaseHas('sent_emails', [ + 'hash' => 'random-hash', 'recipient' => $name.' <'.$email.'>', 'sender' => 'From Name ', 'recipient' => "{$name} <{$email}>", @@ -179,7 +191,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), @@ -355,6 +366,12 @@ 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(); @@ -362,17 +379,14 @@ public function it_retrieves_the_mesage_id_from_ses_mail_driver() ->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') From 1a1e6d62e685a0b6a2ad73324b3b69f1e3965f38 Mon Sep 17 00:00:00 2001 From: Ivan Date: Thu, 2 Jul 2020 22:32:48 +0200 Subject: [PATCH 2/4] fix for inject links with urls having a ' in the path --- src/MailTracker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MailTracker.php b/src/MailTracker.php index 5a230f0..37c007a 100644 --- a/src/MailTracker.php +++ b/src/MailTracker.php @@ -84,7 +84,7 @@ protected function injectLinkTracker($html, $hash) $this->hash = $hash; $html = preg_replace_callback( - "/(]*href=['\"])([^'\"]*)/", + "/(]*href=[\"])([^\"]*)/", [$this, 'inject_link_callback'], $html ); From f5ffc9638a04c41b8fcc1e1837355075dc0adb6f Mon Sep 17 00:00:00 2001 From: Ivan Date: Thu, 6 Aug 2020 20:54:40 +0200 Subject: [PATCH 3/4] added phpunit test --- tests/MailTrackerTest.php | 61 ++++++++++++++++++++++++++++ tests/email/testApostrophe.blade.php | 13 ++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/email/testApostrophe.blade.php diff --git a/tests/MailTrackerTest.php b/tests/MailTrackerTest.php index 9e6e446..5579afd 100644 --- a/tests/MailTrackerTest.php +++ b/tests/MailTrackerTest.php @@ -663,6 +663,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('from@johndoe.com', 'From Name'); + $message->sender('sender@johndoe.com', 'Sender Name'); + $message->to($email, $name); + $message->cc('cc@johndoe.com', 'CC Name'); + $message->bcc('bcc@johndoe.com', 'BCC Name'); + $message->replyTo('reply-to@johndoe.com', '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('/(]*href=[\"])([^\"]*)/', $body, $matches); + $links = $matches[2]; + $aLink = $links[1]; + + $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 */ diff --git a/tests/email/testApostrophe.blade.php b/tests/email/testApostrophe.blade.php new file mode 100644 index 0000000..551d5fd --- /dev/null +++ b/tests/email/testApostrophe.blade.php @@ -0,0 +1,13 @@ + + This is the head + + +

+ This is a test! +

+

+ + Click here + +

+ From e00b77b790f3ce19529f0474f9efb992f88e8fc7 Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 7 Aug 2020 16:47:32 +0200 Subject: [PATCH 4/4] fixed phptest typo --- tests/MailTrackerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/MailTrackerTest.php b/tests/MailTrackerTest.php index 5579afd..b0874f9 100644 --- a/tests/MailTrackerTest.php +++ b/tests/MailTrackerTest.php @@ -702,7 +702,7 @@ public function it_handles_apostrophes_in_links() $matches = null; preg_match_all('/(]*href=[\"])([^\"]*)/', $body, $matches); $links = $matches[2]; - $aLink = $links[1]; + $aLink = $links[0]; $expected_url = "http://www.google.com?q=foo'bar"; $this->assertNotNull($aLink);