protected function inject_link_callback($matches) { if (empty($matches[2])) { $url = app()->make('url')->to('/'); } else { $url = $matches[2]; } return $matches[1] . route('mailTracker_l', [MailTracker::hash_url($url), $this->hash]); }
public function testLink() { $track = \jdavidbakr\MailTracker\Model\SentEmail::orderBy('id', 'desc')->first(); Event::fake(); $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 = action('\\jdavidbakr\\MailTracker\\MailTrackerController@getL', [\jdavidbakr\MailTracker\MailTracker::hash_url($redirect), $track->hash]); $this->call('GET', $url); $this->assertRedirectedTo($redirect); Event::assertFired(jdavidbakr\MailTracker\Events\LinkClickedEvent::class); $this->seeInDatabase('sent_emails_url_clicked', ['url' => $redirect, 'clicks' => 1]); $track = $track->fresh(); $this->assertEquals($clicks, $track->clicks); // Do it with an invalid hash $url = action('\\jdavidbakr\\MailTracker\\MailTrackerController@getL', [\jdavidbakr\MailTracker\MailTracker::hash_url($redirect), 'bad-hash']); $this->call('GET', $url); $this->assertRedirectedTo($redirect); }