fix(backurl): avoid duplicating back query when already present
This commit is contained in:
@@ -114,6 +114,11 @@ class BackUrl
|
||||
return $base . '?' . $backQuery;
|
||||
}
|
||||
|
||||
// 若 path 自身已包含 back=(调用方误用),则不再追加,避免重复 back 造成 URL 膨胀/绕过。
|
||||
if (preg_match('/(?:^|&)back=/', $qs)) {
|
||||
return $base . '?' . $qs;
|
||||
}
|
||||
|
||||
if ($preferFirst) {
|
||||
return $base . '?' . $backQuery . '&' . $qs;
|
||||
}
|
||||
|
||||
27
tests/Unit/BackUrlWithBackNoDuplicateTest.php
Normal file
27
tests/Unit/BackUrlWithBackNoDuplicateTest.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Support\BackUrl;
|
||||
use Illuminate\Support\Arr;
|
||||
use Tests\TestCase;
|
||||
|
||||
class BackUrlWithBackNoDuplicateTest extends TestCase
|
||||
{
|
||||
public function test_with_back_should_not_duplicate_when_path_already_has_back_query(): void
|
||||
{
|
||||
$back = '/admin/platform-orders/2';
|
||||
|
||||
$path = '/admin/site-subscriptions/2?' . Arr::query([
|
||||
'back' => $back,
|
||||
'order_sync_status' => 'syncable',
|
||||
]);
|
||||
|
||||
$url = BackUrl::withBack($path, $back);
|
||||
|
||||
$this->assertSame($path, $url);
|
||||
|
||||
// 防止重复 back= 出现两次
|
||||
$this->assertSame(1, substr_count($url, 'back='));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user