diff --git a/packages/admin/src/Http/Middleware/EnterSiteFolder.php b/packages/admin/src/Http/Middleware/EnterSiteFolder.php index 8650b6e4..aa792abd 100644 --- a/packages/admin/src/Http/Middleware/EnterSiteFolder.php +++ b/packages/admin/src/Http/Middleware/EnterSiteFolder.php @@ -44,8 +44,6 @@ public function handle(Request $request, Closure $next): Response $site = $this->sites->forRequest($request->getHost(), $path); if ($site->folder === null) { - $request->attributes->set(self::SITE, $site->id); - return $next($request); } @@ -99,7 +97,7 @@ private function isPanel(string $rest): bool $rest = trim($rest, '/').'/'; foreach ([config('atrium.prefix', 'admin'), config('atrium.api.prefix', 'api/v1'), config('atrium.mcp.path', 'mcp')] as $prefix) { - $prefix = trim((string) $prefix, '/'); + $prefix = trim($prefix, '/'); if ($prefix !== '' && str_starts_with($rest, $prefix.'/')) { return true; diff --git a/packages/admin/src/Models/Site.php b/packages/admin/src/Models/Site.php index d721e69a..bf5516c5 100644 --- a/packages/admin/src/Models/Site.php +++ b/packages/admin/src/Models/Site.php @@ -390,7 +390,7 @@ public function url(string $path = ''): string $root = $request->getScheme().'://'.$domain.($standard || $port === null ? '' : ':'.$port); } - $path = trim(implode('/', [(string) $this->folder, trim($path, '/')]), '/'); + $path = trim(implode('/', [$this->folder, trim($path, '/')]), '/'); return $path === '' ? $root : $root.'/'.$path; } @@ -524,11 +524,11 @@ private function guardFolder(): void } $first = strtolower(explode('/', $this->folder)[0]); - $reserved = array_filter([ - strtolower(explode('/', trim((string) config('atrium.prefix', 'admin'), '/'))[0]), - strtolower(explode('/', trim((string) config('atrium.api.prefix', 'api/v1'), '/'))[0]), - strtolower(explode('/', trim((string) config('atrium.mcp.path', 'mcp'), '/'))[0]), - ]); + $reserved = [ + strtolower(explode('/', trim(config('atrium.prefix', 'admin'), '/'))[0]), + strtolower(explode('/', trim(config('atrium.api.prefix', 'api/v1'), '/'))[0]), + strtolower(explode('/', trim(config('atrium.mcp.path', 'mcp'), '/'))[0]), + ]; if (in_array($first, $reserved, true)) { throw ValidationException::withMessages(['folder' => __('atrium::sites.errors.folder_reserved', ['folder' => $first])]); diff --git a/packages/admin/src/Support/Sites.php b/packages/admin/src/Support/Sites.php index d9ae72a8..3fecc050 100644 --- a/packages/admin/src/Support/Sites.php +++ b/packages/admin/src/Support/Sites.php @@ -98,8 +98,8 @@ public function folderSites(string $host): Collection return $this->all() ->filter(fn (Site $site): bool => $site->active && $site->folder !== null && ($site->hasDomain($host) || (array) $site->domains === [])) ->sortBy([ - fn (Site $a, Site $b): int => strlen((string) $b->folder) <=> strlen((string) $a->folder), - fn (Site $a, Site $b): int => (int) $b->hasDomain($host) <=> (int) $a->hasDomain($host), + fn (Site $a, Site $b): int => strlen($b->folder) <=> strlen($a->folder), + fn (Site $a, Site $b): int => $b->hasDomain($host) <=> $a->hasDomain($host), ]) ->unique('folder') ->values(); @@ -112,9 +112,9 @@ public function folderSites(string $host): Collection public static function rootUrl(Request $request): string { $root = $request->root(); - $folder = $request->attributes->get(EnterSiteFolder::FOLDER); + $folder = $request->attributes->getString(EnterSiteFolder::FOLDER); - return is_string($folder) && str_ends_with($root, '/'.$folder) + return str_ends_with($root, '/'.$folder) ? substr($root, 0, -strlen('/'.$folder)) : $root; } diff --git a/packages/admin/tests/Feature/FolderSiteRulesTest.php b/packages/admin/tests/Feature/FolderSiteRulesTest.php new file mode 100644 index 00000000..1dc6ee75 --- /dev/null +++ b/packages/admin/tests/Feature/FolderSiteRulesTest.php @@ -0,0 +1,256 @@ +set('app.url', 'http://main.test'); + } + + protected function defineRoutes($router): void + { + Route::middleware('web')->get('assets', fn () => asset('app.css').' '.url('page')); + } + + /** + * @param array $attributes + */ + private function site(string $code, array $attributes = []): Site + { + return Site::query()->create(['code' => $code, 'name' => ucfirst($code), ...$attributes]); + } + + private function main(): Site + { + return Site::query()->where('code', 'main')->sole(); + } + + /** + * The message the save stops with, on the field it names. + * + * @param array $attributes + * @return array> + */ + private function refusal(array $attributes, ?Site $site = null): array + { + try { + $site === null ? $this->site('other', $attributes) : $site->update($attributes); + } catch (ValidationException $e) { + return $e->errors(); + } + + $this->fail('Saved: '.json_encode($attributes)); + } + + /** + * @return array + */ + public static function shapes(): array + { + return [ + 'letters' => ['site', true], + 'digits' => ['2026', true], + 'mixed case' => ['Site2', true], + 'dash and underscore inside' => ['my-site_2', true], + 'nested' => ['a/b/c', true], + 'one character' => ['a', true], + 'longest allowed' => [str_repeat('a', 255), true], + 'one too long' => [str_repeat('a', 256), false], + 'nested, one too long' => [str_repeat('a', 127).'/'.str_repeat('b', 128), false], + 'a space' => ['my site', false], + 'starts with a dash' => ['-site', false], + 'starts with an underscore' => ['_site', false], + 'a part starts with a dash' => ['a/-b', false], + 'a dot' => ['site.html', false], + 'dots up' => ['../site', false], + 'Cyrillic' => ['сайт', false], + 'Chinese' => ['站点', false], + 'emoji' => ['site🪑', false], + 'HTML' => ['x', false], + 'a percent sign' => ['a%20b', false], + 'a trailing letter after a newline' => ["site\nx", false], + ]; + } + + #[DataProvider('shapes')] + public function test_a_folder_is_latin_letters_digits_dashes_and_underscores(string $folder, bool $valid): void + { + if ($valid) { + $this->assertSame($folder, $this->site('second', ['folder' => $folder])->fresh()->folder); + + return; + } + + $this->assertSame( + ['folder' => [__('atrium::sites.errors.folder_invalid', ['folder' => $folder])]], + $this->refusal(['folder' => $folder]), + ); + $this->assertFalse(Site::query()->where('code', 'other')->exists()); + } + + /** + * @return array + */ + public static function reserved(): array + { + return [ + 'the admin' => ['admin', 'admin'], + 'the admin, in capitals' => ['Admin', 'admin'], + 'inside the admin' => ['admin/x', 'admin'], + 'the API' => ['api', 'api'], + 'a version of the API' => ['api/v2', 'api'], + 'MCP' => ['mcp', 'mcp'], + 'MCP in capitals' => ['MCP/x', 'mcp'], + 'a longer word' => ['administration', null], + 'the word later' => ['x/admin', null], + 'apis' => ['apis', null], + ]; + } + + #[DataProvider('reserved')] + public function test_the_panels_own_addresses_are_not_folders(string $folder, ?string $taken): void + { + if ($taken === null) { + $this->assertSame($folder, $this->site('second', ['folder' => $folder])->folder); + + return; + } + + $this->assertSame( + ['folder' => [__('atrium::sites.errors.folder_reserved', ['folder' => $taken])]], + $this->refusal(['folder' => $folder]), + ); + } + + /** + * @return array + */ + public static function prefixes(): array + { + return [ + 'the admin elsewhere' => ['atrium.prefix', '/Panel/x/', 'panel'], + 'the API elsewhere' => ['atrium.api.prefix', '/REST/v2/', 'rest'], + 'MCP elsewhere' => ['atrium.mcp.path', '/AI/', 'ai'], + ]; + } + + #[DataProvider('prefixes')] + public function test_the_reserved_folders_follow_the_configuration(string $key, string $prefix, string $taken): void + { + config([$key => $prefix]); + + $this->assertSame( + ['folder' => [__('atrium::sites.errors.folder_reserved', ['folder' => $taken])]], + $this->refusal(['folder' => $taken.'/x']), + ); + } + + public function test_the_default_site_stays_at_the_root(): void + { + $this->assertSame( + ['folder' => [__('atrium::sites.errors.folder_default')]], + $this->refusal(['folder' => 'main'], $this->main()), + ); + $this->assertNull($this->main()->fresh()->folder); + + // Clearing the folder of the default site is fine. + $this->main()->update(['folder' => '']); + $this->assertNull($this->main()->fresh()->folder); + } + + public function test_one_address_belongs_to_one_site(): void + { + $this->main()->update(['domains' => ['example.com']]); + $this->site('a', ['domains' => ['example.com', 'example.org'], 'folder' => 'a']); + $this->site('anywhere', ['folder' => 'c']); + + // The same folder on a domain of that site. + $this->assertSame( + ['folder' => [__('atrium::sites.errors.folder_taken', ['address' => 'example.org/a/', 'site' => 'A'])]], + $this->refusal(['domains' => ['example.net', 'example.org'], 'folder' => 'a']), + ); + + // Two sites in one folder on every domain. + $this->assertSame( + ['folder' => [__('atrium::sites.errors.folder_taken', ['address' => '/c/', 'site' => 'Anywhere'])]], + $this->refusal(['folder' => 'c']), + ); + + // The root of a domain. + $this->assertSame( + ['domains' => [__('atrium::sites.errors.domain_taken', ['domain' => 'example.com', 'site' => $this->main()->name])]], + $this->refusal(['domains' => ['example.com']]), + ); + + // Fine: another folder, the same folder elsewhere, a folder next to + // a site on every domain, the root of another domain, a site saved again. + $this->site('b', ['domains' => ['example.com'], 'folder' => 'b']); + $this->site('a2', ['domains' => ['example.net'], 'folder' => 'a']); + $this->site('c2', ['domains' => ['example.com'], 'folder' => 'c']); + $this->site('root', ['domains' => ['example.net']]); + $this->site('a3', ['folder' => 'a']); + Site::query()->where('code', 'a')->sole()->update(['name' => 'A again']); + + $this->assertSame(8, Site::query()->count()); + } + + public function test_the_address_of_a_site_is_inside_its_folder(): void + { + $this->get('http://example.com/'); + $site = new Site(['domains' => ['example.com'], 'folder' => 'site2']); + + $this->assertSame('http://example.com/site2', $site->url()); + $this->assertSame('http://example.com/site2/news', $site->url('news')); + $this->assertSame('http://example.com/site2/news', $site->url('/news/')); + $this->assertSame('http://example.com/news', (new Site(['domains' => ['example.com']]))->url('/news/')); + $this->assertSame('http://example.com', (new Site(['domains' => ['example.com']]))->url('/')); + } + + public function test_the_panel_stays_out_of_a_folder_whatever_slashes_its_prefix_has(): void + { + $this->site('second', ['folder' => 'site2']); + config(['atrium.prefix' => '/admin/']); + + $this->get('http://main.test/site2/admin/login')->assertNotFound(); + $this->get('http://main.test/admin/login')->assertOk(); + } + + public function test_a_folder_request_leaves_nothing_behind(): void + { + $this->site('second', ['folder' => 'site2']); + + $this->get('http://main.test/site2/assets')->assertSeeText('http://main.test/app.css http://main.test/site2/page'); + // Another host next: its own root, not the one of the folder request. + $this->get('http://other.test/assets')->assertSeeText('http://other.test/app.css http://other.test/page'); + } + + public function test_assets_keep_their_own_origin_inside_a_folder(): void + { + $this->site('second', ['folder' => 'site2']); + $this->app['url']->useAssetOrigin('https://static.example.com'); + + $this->get('http://main.test/site2/assets')->assertSeeText('https://static.example.com/app.css http://main.test/site2/page'); + $this->get('http://main.test/assets')->assertSeeText('https://static.example.com/app.css http://main.test/page'); + } +} diff --git a/packages/admin/tests/Unit/SiteFolderTest.php b/packages/admin/tests/Unit/SiteFolderTest.php new file mode 100644 index 00000000..fedead61 --- /dev/null +++ b/packages/admin/tests/Unit/SiteFolderTest.php @@ -0,0 +1,140 @@ + + */ + public static function folders(): array + { + return [ + 'plain' => ['site2', 'site2'], + 'slashes around' => ['/site2/', 'site2'], + 'spaces around' => [' site2 ', 'site2'], + 'nested' => ['/a/b/', 'a/b'], + 'doubled slashes' => ['//a///b//', 'a/b'], + 'a whole address' => ['https://Example.com/site2/', 'site2'], + 'an address with a port' => ['http://example.com:8080/site2', 'site2'], + 'another scheme' => ['ftp://example.com/files', 'files'], + 'a query' => ['site2?page=2', 'site2'], + 'a fragment' => ['site2#top', 'site2'], + 'a query after a slash' => ['/site2/?a=1#b', 'site2'], + 'a colon inside, not a scheme' => ['site2/a:b', 'site2/a:b'], + 'a scheme later in the path' => ['site2/http://x', 'site2/http:/x'], + 'a domain only' => ['https://example.com', null], + 'a domain and a slash' => ['https://example.com/', null], + 'empty' => ['', null], + 'null' => [null, null], + 'only spaces' => [' ', null], + 'only a slash' => ['/', null], + 'only slashes' => ['///', null], + 'only a query' => ['?a=1', null], + 'Cyrillic is kept for the validator' => ['/сайт/', 'сайт'], + 'HTML is kept for the validator' => ['x', 'x'], + ]; + } + + #[DataProvider('folders')] + public function test_a_folder_is_normalized(mixed $typed, ?string $folder): void + { + $this->assertSame($folder, Site::normalizeFolder($typed)); + } + + /** + * @return array + */ + public static function paths(): array + { + return [ + 'the folder itself' => ['site2', '/site2', true], + 'the folder with a slash' => ['site2', '/site2/', true], + 'a page inside' => ['site2', '/site2/news/one', true], + 'without a leading slash' => ['site2', 'site2/news', true], + 'a longer name' => ['site2', '/site22', false], + 'a longer name inside' => ['site2', '/site22/news', false], + 'a shorter name' => ['site2', '/site', false], + 'the root' => ['site2', '/', false], + 'an empty path' => ['site2', '', false], + 'the folder deeper' => ['site2', '/de/site2', false], + 'another case' => ['site2', '/Site2', false], + 'a nested folder' => ['a/b', '/a/b/c', true], + 'half of a nested folder' => ['a/b', '/a', false], + 'no folder, the root' => [null, '/', false], + 'an empty folder, the root' => ['', '/', false], + 'an empty folder, a page' => ['', '/news', false], + 'no folder, a page' => [null, '/news', false], + ]; + } + + #[DataProvider('paths')] + public function test_a_site_holds_the_paths_inside_its_folder(?string $folder, string $path, bool $holds): void + { + $this->assertSame($holds, (new Site(['folder' => $folder]))->holdsPath($path)); + } + + /** + * @return array + */ + public static function roots(): array + { + return [ + 'no folder' => [null, null, 'http://example.com'], + 'moved into the folder' => ['/site2', 'site2', 'http://example.com'], + 'a nested folder' => ['/a/b', 'a/b', 'http://example.com'], + 'installed in a subfolder itself' => ['/app/site2', 'site2', 'http://example.com/app'], + 'a folder, not moved' => [null, 'site2', 'http://example.com'], + 'the root only ends like the folder' => ['/mysite2', 'site2', 'http://example.com/mysite2'], + 'another folder in the root' => ['/site3', 'site2', 'http://example.com/site3'], + ]; + } + + #[DataProvider('roots')] + public function test_the_root_url_leaves_the_folder_out(?string $baseUrl, ?string $folder, string $root): void + { + $request = Request::create('http://example.com/news'); + + if ($baseUrl !== null) { + // What EnterSiteFolder leaves: the folder in the base URL. + (function (string $baseUrl): void { + $this->baseUrl = $baseUrl; + })->call($request, $baseUrl); + } + + if ($folder !== null) { + $request->attributes->set(EnterSiteFolder::FOLDER, $folder); + } + + $this->assertSame($root, Sites::rootUrl($request)); + } + + public function test_the_root_url_keeps_its_port(): void + { + $request = Request::create('http://example.com:8000/site2/news'); + (function (): void { + $this->baseUrl = '/site2'; + })->call($request); + $request->attributes->set(EnterSiteFolder::FOLDER, 'site2'); + + $this->assertSame('http://example.com:8000', Sites::rootUrl($request)); + } +} diff --git a/packages/bitrix/src/Commands/MigrateSitesCommand.php b/packages/bitrix/src/Commands/MigrateSitesCommand.php index 60571df2..90a841ff 100644 --- a/packages/bitrix/src/Commands/MigrateSitesCommand.php +++ b/packages/bitrix/src/Commands/MigrateSitesCommand.php @@ -49,7 +49,7 @@ private function import(): int // The default site first: a site that loses the default flag to it // may then be put into a folder. - usort($sites, static fn (array $a, array $b): int => (int) $b['default'] <=> (int) $a['default']); + usort($sites, static fn (array $a, array $b): int => $b['default'] <=> $a['default']); $failed = false; $rows = []; diff --git a/packages/bitrix/src/Support/BitrixSites.php b/packages/bitrix/src/Support/BitrixSites.php index a45d6ba4..959fca0a 100644 --- a/packages/bitrix/src/Support/BitrixSites.php +++ b/packages/bitrix/src/Support/BitrixSites.php @@ -39,7 +39,7 @@ public function all(): array if ($this->bitrix->tableExists('b_lang_domain')) { foreach ($db->table('b_lang_domain')->orderBy('LID')->get() as $row) { - $domain = self::domain((string) $row->DOMAIN); + $domain = self::domain($row->DOMAIN); if ($domain !== '') { $domains[$row->LID][] = $domain; @@ -51,11 +51,11 @@ public function all(): array foreach ($db->table('b_lang')->orderBy('SORT')->orderBy('LID')->get() as $row) { $lid = $row->LID; - $language = strtolower((string) ($row->LANGUAGE_ID ?? '')); + $language = strtolower($row->LANGUAGE_ID); // The server name is the main domain in Bitrix: Atrium's first one. $own = array_unique($domains[$lid] ?? []); - $main = self::domain((string) ($row->SERVER_NAME ?? '')); - usort($own, static fn (string $a, string $b): int => (int) ($b === $main) <=> (int) ($a === $main)); + $main = self::domain($row->SERVER_NAME ?? ''); + usort($own, static fn (string $a, string $b): int => ($b === $main) <=> ($a === $main)); $sites[] = [ 'lid' => $lid, @@ -68,7 +68,7 @@ public function all(): array 'domains' => $own, 'active' => $row->ACTIVE === 'Y', 'sort' => $row->SORT, - 'languages' => $language !== '' && Language::query()->where('code', $language)->exists() ? [$language] : null, + 'languages' => Language::query()->where('code', $language)->exists() ? [$language] : null, ], ]; } @@ -91,6 +91,6 @@ public static function target(array $site): ?Site */ private static function domain(string $domain): string { - return rtrim((string) preg_replace('#:\d+$#', '', strtolower(trim($domain))), '.'); + return rtrim(preg_replace('#:\d+$#', '', strtolower(trim($domain))), '.'); } } diff --git a/packages/bitrix/tests/MigrateSitesTest.php b/packages/bitrix/tests/MigrateSitesTest.php index 33ce859d..eb4e9e14 100644 --- a/packages/bitrix/tests/MigrateSitesTest.php +++ b/packages/bitrix/tests/MigrateSitesTest.php @@ -102,6 +102,19 @@ public function test_dir_becomes_the_folder_and_the_domains_come_along(): void ], $this->siteStatuses()); } + public function test_the_default_site_goes_first_whatever_its_sort_order(): void + { + DB::connection('bitrix')->table('b_lang')->where('LID', 's1')->update(['SORT' => 900]); + + $this->artisan('atrium:bitrix:migrate:sites') + ->expectsTable(['Bitrix LID', 'Code', 'DIR', 'Folder', 'Domains'], [ + ['s1', 'main', '/', '/', 'example.com, www.example.com'], + ['s2', 's2', '/en/', '/en/', 'example.com, www.example.com'], + ['s3', 's3', '/site2/', '/site2/', '—'], + ]) + ->assertSuccessful(); + } + public function test_a_default_site_in_a_folder_stays_at_the_root(): void { DB::connection('bitrix')->table('b_lang')->where('LID', 's1')->update(['DIR' => '/ru/']);