Skip to content

Commit

Permalink
- **Renamed domains_in_content to domains_in_context:** The attribute…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
Mirko authored and jdavidbakr committed May 11, 2024
1 parent 8c706bf commit 227b2d2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/MailTrackerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') ?: '/');
}

Expand Down
4 changes: 2 additions & 2 deletions src/Model/SentEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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("/(<a[^>]*href=[\"])([^\"]*)/", $this->content, $matches);
if ( ! isset($matches[2]) ) return [];
$domains = [];
Expand Down
33 changes: 29 additions & 4 deletions tests/MailTrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="'.$redirect.'">'.$redirect.'</a>',
]);
$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
Expand All @@ -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 <a href="'.$redirect.'">'.$redirect.'</a>',
]);
$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
Expand All @@ -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),
Expand All @@ -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
Expand All @@ -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 <a href="https://goodwebsite.com">Good website</a>',
]);

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');
}

/**
Expand Down

0 comments on commit 227b2d2

Please sign in to comment.