Files
boursecu_server/tests/Unit/FilePublicUrlTest.php
T

76 lines
2.3 KiB
PHP
Raw Normal View History

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-06-29 20:07:03 +03:30
use Illuminate\Support\Facades\Config;
use Tests\TestCase;
class FilePublicUrlTest extends TestCase
{
2026-06-29 20:11:39 +03:30
private function seedGaroosiAdminUrl(): void
2026-06-29 20:07:03 +03:30
{
2026-06-29 20:11:39 +03:30
Setting::query()->updateOrCreate(
['key' => 'admin_url'],
['value' => 'https://garoosigholaktala.ir/admin/', 'is_active' => 1, 'title_fa' => 'admin']
);
}
public function test_public_url_uses_server_storage_path_from_site_domain(): void
{
$this->seedGaroosiAdminUrl();
Config::set('app.domain', 'https://garoosigholaktala.ir');
2026-06-29 20:07:03 +03:30
$url = File::publicUrl('storage/1405/02/27/sample.jpg');
$this->assertSame(
'https://garoosigholaktala.ir/server/storage/1405/02/27/sample.jpg',
$url
);
}
2026-06-29 20:11:39 +03:30
public function test_public_url_rewrites_legacy_wrong_absolute_url(): void
{
$this->seedGaroosiAdminUrl();
Config::set('app.domain', 'https://garoosigholaktala.ir');
$url = File::publicUrl('https://garoosigholaktala.ir/storage/1405/02/27/sample.jpg');
$this->assertSame(
'https://garoosigholaktala.ir/server/storage/1405/02/27/sample.jpg',
$url
);
}
public function test_resolve_base_from_admin_url_setting(): void
2026-06-29 20:07:03 +03:30
{
Config::set('app.domain', null);
Config::set('app.url', 'http://localhost');
2026-06-29 20:11:39 +03:30
$this->seedGaroosiAdminUrl();
2026-06-29 20:07:03 +03:30
2026-06-29 20:11:39 +03:30
$this->assertSame('https://garoosigholaktala.ir/server', 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');
$request = \Illuminate\Http\Request::create('https://niazitala.ir/server/admin/file', 'POST');
$this->app->instance('request', $request);
$this->assertSame('https://niazitala.ir/server', File::resolvePublicBaseUrl());
}
2026-06-29 20:07:03 +03:30
}