File: /home/xedaptot/ai.naniguide.com/tests/Unit/UrlServiceTest.php
<?php
namespace Tests\Unit;
use App\Services\UrlService;
use App\Library\StringHelper;
use App\Model\TrackingDomain;
use Illuminate\Support\Facades\Route;
use Tests\TestCase;
class UrlServiceTest extends TestCase
{
protected UrlService $service;
protected function setUp(): void
{
parent::setUp();
$this->service = new UrlService();
// Set base URL
config()->set('app.url', 'https://example.com');
// @important: route("named route") does not respect config(app.url)
// it uses the actual request url or localhost (in console)
// So, force it in test
$this->app['url']->forceRootUrl(config('app.url'));
}
public function test_it_prepends_app_url_for_relative_img_src_case_insensitive()
{
$html = '<html><body><iMg SrC="/images/logo.png"></body></html>';
$result = $this->service->transformHtmlUrls($html, 123);
$this->assertStringContainsString('https://example.com/images/logo.png', $result);
}
public function test_it_does_not_modify_absolute_img_src()
{
$html = '<html><body><img src="https://cdn.example.org/image.png"></body></html>';
$result = $this->service->transformHtmlUrls($html, 123);
$this->assertStringContainsString('https://cdn.example.org/image.png', $result);
}
public function test_it_does_not_throw_exception_for_empty_src()
{
$html = '<html><body><img src=""></body></html>';
$this->service->transformHtmlUrls($html, 123);
$this->assertTrue(true);
}
public function test_it_throws_exception_for_invalid_app_url()
{
config()->set('app.url', 'invalid-url');
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Invalid app.url setting');
$this->service->getAppUrl();
}
public function test_it_makes_links_trackable_no_matter_capitalize_A_or_lowercase_a_insensitive()
{
$html = '<html><body><A href="https://target.com/page">Click</A></body></html>';
$result = $this->service->transformHtmlUrls($html, 123);
// The final link should now be a Laravel route URL with encoded params
// Notice that & shall be converted to & after DOM processing
$this->assertStringContainsString(
'http://example.com/p/click?url=aHR0cHM6Ly90YXJnZXQuY29tL3BhZ2U&message_id=MTIz',
$result
);
$html = '<html><body><a href="https://target.com/page">Click</a></body></html>';
$result = $this->service->transformHtmlUrls($html, 123);
$this->assertStringContainsString(
'http://example.com/p/click?url=aHR0cHM6Ly90YXJnZXQuY29tL3BhZ2U&message_id=MTIz',
$result
);
}
public function test_it_ignores_non_web_links()
{
$html = '<html><body><a href="mailto:[email protected]">Mail</a><a href="tel:+123456789">Call</a></body></html>';
$result = $this->service->transformHtmlUrls($html, 123);
$this->assertStringContainsString('mailto:[email protected]', $result);
$this->assertStringContainsString('tel:+123456789', $result);
}
public function test_it_throws_for_invalid_href()
{
$this->expectException(\Exception::class);
$html = '<html><body><a href="http:///broken-url">Click</a></body></html>';
$this->service->transformHtmlUrls($html, 123);
}
public function test_it_generates_tracking_url()
{
$url = '/relative/path/example.jpg';
$domain = $this->createMock(TrackingDomain::class);
$domain->method('generateTrackingUrl')
->willReturnCallback(fn($url) => "https://track.example.com/t=".StringHelper::base64UrlEncode($url));
$html = "<html><body><img src='{$url}'>Click</a></body></html>";
$result = $this->service->transformHtmlUrls($html, 123, $domain);
$this->assertStringContainsString('https://track.example.com/t=aHR0cHM6Ly9leGFtcGxlLmNvbS9yZWxhdGl2ZS9wYXRoL2V4YW1wbGUuanBn', $result);
}
}