هپشلث عحمخشی
This commit is contained in:
@@ -4,52 +4,76 @@ namespace Tests\Unit;
|
||||
|
||||
use App\Models\File;
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Tests\TestCase;
|
||||
|
||||
class FilePublicUrlTest extends TestCase
|
||||
{
|
||||
private function seedGaroosiAdminUrl(): void
|
||||
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
|
||||
{
|
||||
Setting::query()->updateOrCreate(
|
||||
['key' => 'admin_url'],
|
||||
['value' => 'https://garoosigholaktala.ir/admin/', 'is_active' => 1, 'title_fa' => 'admin']
|
||||
['key' => 'server_url'],
|
||||
['value' => $this->expectedServerBase($host), 'is_active' => 1, 'title_fa' => 'server']
|
||||
);
|
||||
}
|
||||
|
||||
public function test_public_url_uses_server_storage_path_from_site_domain(): void
|
||||
private function seedAdminUrlSetting(string $host): void
|
||||
{
|
||||
$this->seedGaroosiAdminUrl();
|
||||
Config::set('app.domain', 'https://garoosigholaktala.ir');
|
||||
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);
|
||||
|
||||
$url = File::publicUrl('storage/1405/02/27/sample.jpg');
|
||||
|
||||
$this->assertSame(
|
||||
'https://garoosigholaktala.ir/server/storage/1405/02/27/sample.jpg',
|
||||
$this->expectedServerBase($host).'/storage/1405/02/27/sample.jpg',
|
||||
$url
|
||||
);
|
||||
}
|
||||
|
||||
public function test_public_url_rewrites_legacy_wrong_absolute_url(): void
|
||||
public function test_public_url_rewrites_legacy_wrong_absolute_url_using_request_host(): void
|
||||
{
|
||||
$this->seedGaroosiAdminUrl();
|
||||
Config::set('app.domain', 'https://garoosigholaktala.ir');
|
||||
$host = 'tenant-a.test';
|
||||
$this->bindRequest($host);
|
||||
|
||||
$url = File::publicUrl('https://garoosigholaktala.ir/storage/1405/02/27/sample.jpg');
|
||||
$url = File::publicUrl('https://'.$host.'/storage/1405/02/27/sample.jpg');
|
||||
|
||||
$this->assertSame(
|
||||
'https://garoosigholaktala.ir/server/storage/1405/02/27/sample.jpg',
|
||||
$this->expectedServerBase($host).'/storage/1405/02/27/sample.jpg',
|
||||
$url
|
||||
);
|
||||
}
|
||||
|
||||
public function test_resolve_base_from_admin_url_setting(): void
|
||||
public function test_resolve_base_from_admin_url_setting_when_no_http_request(): void
|
||||
{
|
||||
Config::set('app.domain', null);
|
||||
Config::set('app.url', 'http://localhost');
|
||||
$this->seedGaroosiAdminUrl();
|
||||
|
||||
$this->assertSame('https://garoosigholaktala.ir/server', File::resolvePublicBaseUrl());
|
||||
$host = 'tenant-b.test';
|
||||
$this->seedAdminUrlSetting($host);
|
||||
|
||||
$this->assertSame($this->expectedServerBase($host), File::resolvePublicBaseUrl());
|
||||
}
|
||||
|
||||
public function test_public_url_falls_back_to_relative_path_when_base_unknown(): void
|
||||
@@ -67,9 +91,25 @@ class FilePublicUrlTest extends TestCase
|
||||
Config::set('app.domain', null);
|
||||
Config::set('app.url', 'http://localhost');
|
||||
|
||||
$request = \Illuminate\Http\Request::create('https://niazitala.ir/server/admin/file', 'POST');
|
||||
$this->app->instance('request', $request);
|
||||
$host = 'tenant-c.test';
|
||||
$this->bindRequest($host);
|
||||
|
||||
$this->assertSame('https://niazitala.ir/server', File::resolvePublicBaseUrl());
|
||||
$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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user