2026-06-29 20:07:03 +03:30
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Unit;
|
|
|
|
|
|
|
|
|
|
use App\Models\File;
|
2026-06-29 20:11:39 +03:30
|
|
|
use App\Models\Setting;
|
2026-07-06 22:01:16 +03:30
|
|
|
use Illuminate\Http\Request;
|
2026-06-29 20:07:03 +03:30
|
|
|
use Illuminate\Support\Facades\Config;
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
class FilePublicUrlTest extends TestCase
|
|
|
|
|
{
|
2026-07-06 22:01:16 +03:30
|
|
|
private function bindRequest(string $host, string $path = '/server/admin/file'): Request
|
|
|
|
|
{
|
|
|
|
|
$request = Request::create('https://'.$host.$path, 'POST');
|
|
|
|
|
$this->app->instance('request', $request);
|
|
|
|
|
|
|
|
|
|
return $request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function expectedServerBase(string $host): string
|
|
|
|
|
{
|
|
|
|
|
return 'https://'.$host.'/server';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function seedServerUrlSetting(string $host): void
|
2026-06-29 20:07:03 +03:30
|
|
|
{
|
2026-06-29 20:11:39 +03:30
|
|
|
Setting::query()->updateOrCreate(
|
2026-07-06 22:01:16 +03:30
|
|
|
['key' => 'server_url'],
|
|
|
|
|
['value' => $this->expectedServerBase($host), 'is_active' => 1, 'title_fa' => 'server']
|
2026-06-29 20:11:39 +03:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 22:01:16 +03:30
|
|
|
private function seedAdminUrlSetting(string $host): void
|
2026-06-29 20:11:39 +03:30
|
|
|
{
|
2026-07-06 22:01:16 +03:30
|
|
|
Setting::query()->updateOrCreate(
|
|
|
|
|
['key' => 'admin_url'],
|
|
|
|
|
['value' => 'https://'.$host.'/admin/', 'is_active' => 1, 'title_fa' => 'admin']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_public_url_uses_request_host_for_storage_path(): void
|
|
|
|
|
{
|
|
|
|
|
$host = 'tenant-a.test';
|
|
|
|
|
$this->bindRequest($host);
|
2026-06-29 20:07:03 +03:30
|
|
|
|
|
|
|
|
$url = File::publicUrl('storage/1405/02/27/sample.jpg');
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
2026-07-06 22:01:16 +03:30
|
|
|
$this->expectedServerBase($host).'/storage/1405/02/27/sample.jpg',
|
2026-06-29 20:07:03 +03:30
|
|
|
$url
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 22:01:16 +03:30
|
|
|
public function test_public_url_rewrites_legacy_wrong_absolute_url_using_request_host(): void
|
2026-06-29 20:11:39 +03:30
|
|
|
{
|
2026-07-06 22:01:16 +03:30
|
|
|
$host = 'tenant-a.test';
|
|
|
|
|
$this->bindRequest($host);
|
2026-06-29 20:11:39 +03:30
|
|
|
|
2026-07-06 22:01:16 +03:30
|
|
|
$url = File::publicUrl('https://'.$host.'/storage/1405/02/27/sample.jpg');
|
2026-06-29 20:11:39 +03:30
|
|
|
|
|
|
|
|
$this->assertSame(
|
2026-07-06 22:01:16 +03:30
|
|
|
$this->expectedServerBase($host).'/storage/1405/02/27/sample.jpg',
|
2026-06-29 20:11:39 +03:30
|
|
|
$url
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 22:01:16 +03:30
|
|
|
public function test_resolve_base_from_admin_url_setting_when_no_http_request(): void
|
2026-06-29 20:07:03 +03:30
|
|
|
{
|
|
|
|
|
Config::set('app.domain', null);
|
|
|
|
|
Config::set('app.url', 'http://localhost');
|
|
|
|
|
|
2026-07-06 22:01:16 +03:30
|
|
|
$host = 'tenant-b.test';
|
|
|
|
|
$this->seedAdminUrlSetting($host);
|
|
|
|
|
|
|
|
|
|
$this->assertSame($this->expectedServerBase($host), File::resolvePublicBaseUrl());
|
2026-06-29 20:07:03 +03:30
|
|
|
}
|
2026-07-06 21:04:37 +03:30
|
|
|
|
|
|
|
|
public function test_public_url_falls_back_to_relative_path_when_base_unknown(): void
|
|
|
|
|
{
|
|
|
|
|
Config::set('app.domain', null);
|
|
|
|
|
Config::set('app.url', 'http://localhost');
|
|
|
|
|
|
|
|
|
|
$url = File::publicUrl('storage/1405/02/27/sample.jpg');
|
|
|
|
|
|
|
|
|
|
$this->assertSame('storage/1405/02/27/sample.jpg', $url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_resolve_base_from_current_request_host(): void
|
|
|
|
|
{
|
|
|
|
|
Config::set('app.domain', null);
|
|
|
|
|
Config::set('app.url', 'http://localhost');
|
|
|
|
|
|
2026-07-06 22:01:16 +03:30
|
|
|
$host = 'tenant-c.test';
|
|
|
|
|
$this->bindRequest($host);
|
2026-07-06 21:04:37 +03:30
|
|
|
|
2026-07-06 22:01:16 +03:30
|
|
|
$this->assertSame($this->expectedServerBase($host), File::resolvePublicBaseUrl());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_request_host_wins_over_stale_settings_domain(): void
|
|
|
|
|
{
|
|
|
|
|
$staleHost = 'stale-tenant.test';
|
|
|
|
|
$requestHost = 'current-tenant.test';
|
|
|
|
|
|
|
|
|
|
$this->seedServerUrlSetting($staleHost);
|
|
|
|
|
$this->bindRequest($requestHost);
|
|
|
|
|
|
|
|
|
|
$url = File::publicUrl('storage/1405/04/15/sample.png');
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
$this->expectedServerBase($requestHost).'/storage/1405/04/15/sample.png',
|
|
|
|
|
$url
|
|
|
|
|
);
|
2026-07-06 21:04:37 +03:30
|
|
|
}
|
2026-06-29 20:07:03 +03:30
|
|
|
}
|