From 227b2d24a9d3deffceda11d82fec2581c7aed6f5 Mon Sep 17 00:00:00 2001 From: Mirko Date: Wed, 17 Apr 2024 16:35:55 +0200 Subject: [PATCH] - **Renamed domains_in_content to domains_in_context:** The attribute name was updated to better reflect its purpose and align with Laravel conventions. - **Added `empty()` for `domains_in_context` check:** A check was added to the `linkClicked` function to handle cases where the `domains_in_context` attribute is empty. This ensures backward compatibility and prevents potential errors. - **Updated tests:** The test suite was updated to include a new test verifying redirection behavior for links with invalid domains. --- src/MailTrackerController.php | 2 +- src/Model/SentEmail.php | 4 ++-- tests/MailTrackerTest.php | 33 +++++++++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/MailTrackerController.php b/src/MailTrackerController.php index fa2876c..12a2e53 100644 --- a/src/MailTrackerController.php +++ b/src/MailTrackerController.php @@ -92,7 +92,7 @@ protected function linkClicked($url, $hash) } } - if ( ! $tracker || ! in_array($url_host, $tracker->domains_in_content) ){ + if ( ! $tracker || empty($tracker->domains_in_context) || ! in_array($url_host, $tracker->domains_in_context) ){ return redirect(config('mail-tracker.redirect-missing-links-to') ?: '/'); } diff --git a/src/Model/SentEmail.php b/src/Model/SentEmail.php index 60abc50..3f162ec 100644 --- a/src/Model/SentEmail.php +++ b/src/Model/SentEmail.php @@ -47,10 +47,10 @@ class SentEmail extends Model implements SentEmailModel ]; protected $appends = [ - 'domains_in_content' + 'domains_in_context' ]; - public function getDomainsInContentAttribute(){ + public function getDomainsInContextAttribute(){ preg_match_all("/(]*href=[\"])([^\"]*)/", $this->content, $matches); if ( ! isset($matches[2]) ) return []; $domains = []; diff --git a/tests/MailTrackerTest.php b/tests/MailTrackerTest.php index 393b6c5..4caffc0 100644 --- a/tests/MailTrackerTest.php +++ b/tests/MailTrackerTest.php @@ -405,12 +405,14 @@ public function testLegacyLink() Carbon::setTestNow(now()); Config::set('mail-tracker.tracker-queue', 'alt-queue'); Bus::fake(); + $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); + $track = MailTracker::sentEmailModel()->newQuery()->create([ 'hash' => Str::random(32), + 'content' => 'Hello, visit my website '.$redirect.'', ]); $clicks = $track->clicks; $clicks++; - $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); $url = route('mailTracker_l', [ MailTracker::hash_url($redirect), // Replace slash with dollar sign $track->hash @@ -436,10 +438,11 @@ public function testLink() Config::set('mail-tracker.inject-pixel', true); Config::set('mail-tracker.tracker-queue', 'alt-queue'); Bus::fake(); + $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); $track = MailTracker::sentEmailModel()->newQuery()->create([ 'hash' => Str::random(32), + 'content' => 'Hello, visit my website '.$redirect.'', ]); - $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); $url = route('mailTracker_n', [ 'l' => $redirect, 'h' => $track->hash @@ -464,7 +467,7 @@ public function testLink() /** * @test */ - public function it_redirects_even_if_no_sent_email_exists() + public function it_redirects_to_fallback_if_the_sent_email_does_not_exists() { $track = MailTracker::sentEmailModel()->newQuery()->create([ 'hash' => Str::random(32), @@ -473,6 +476,7 @@ public function it_redirects_even_if_no_sent_email_exists() $clicks = $track->clicks; $clicks++; + Config::set('mail-tracker.redirect-missing-links-to', '/home'); $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); // Do it with an invalid hash @@ -482,7 +486,28 @@ public function it_redirects_even_if_no_sent_email_exists() ]); $response = $this->get($url); - $response->assertRedirect($redirect); + $response->assertRedirect('/home'); + } + + + /** + * @test + */ + public function it_redirects_to_fallback_for_invalid_domain() + { + Event::fake(); + $track = MailTracker::sentEmailModel()->newQuery()->create([ + 'hash' => Str::random(32), + 'content' => 'This is some content with a link to Good website', + ]); + + Config::set('mail-tracker.redirect-missing-links-to', '/home'); + + $invalidUrl = 'http://evil.com'; // Domain not present in email content + + $response = $this->get(route('mailTracker_l', [MailTracker::hash_url($invalidUrl), $track->hash])); + + $response->assertRedirect('/home'); } /**