diff --git a/src/App.php b/src/App.php index 4177c336d1..c6a251110f 100644 --- a/src/App.php +++ b/src/App.php @@ -673,11 +673,9 @@ public function url($page = [], array $extraRequestUrlArgs = []): string if ($pagePath === '') { // TODO path must always start with '/' $pagePath = '/'; } - if (str_ends_with($pagePath, '/')) { - $pagePath = $this->urlBuildingIndexPage; - } else { - $pagePath = basename($pagePath, $this->urlBuildingExt); - } + } + if (str_ends_with($pagePath, '/')) { + $pagePath .= $this->urlBuildingIndexPage; } if (!str_ends_with($pagePath, '/') && !str_contains(basename($pagePath), '.')) { $pagePath .= $this->urlBuildingExt; diff --git a/tests/AppTest.php b/tests/AppTest.php index 56b1bd2410..900b0d5f3e 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -82,6 +82,12 @@ public function provideUrlCases(): iterable yield [$requestPage . '?u=U', [], ['x'], [], 'x.php']; yield [$requestPage . '?index.php', [], ['x'], [], 'x.php']; + // /w page autoindex + yield [$requestPage, [], ['/'], [], '/index.php']; + yield [$requestPage, [], ['/0/0/'], [], '/0/0/index.php']; + yield [$requestPage, [], ['a.b c/'], [], 'a.b c/index.php']; + yield [$requestPage . '?u=U', [], ['x/'], [], 'x/index.php']; + // /w page args yield [$requestPage, [], ['x', 'foo' => 'a'], [], 'x.php?foo=a']; yield [$requestPage, [], ['x', 'foo' => 'a', 'bar' => '0'], [], 'x.php?foo=a&bar=0']; @@ -106,6 +112,11 @@ public function provideUrlCases(): iterable yield ['/', ['v' => false], ['x', 'v' => 'page'], [], 'x.php?v=page']; yield ['/', ['v' => false], ['x'], ['v' => 'extra'], 'x.php']; + // /wo page path + yield ['/x', [], [], [], '/x.php']; + yield ['/d/x.html', [], ['foo' => 'a'], [], '/d/x.html?foo=a']; + yield ['/?u=U&v=V', ['v' => true], [], [], '/index.php?v=V']; + // args priority yield ['/', [], ['x', 'foo' => 'page'], ['foo' => 'extra'], 'x.php?foo=page']; yield ['/?foo=sticky', ['foo' => true], ['x', 'foo' => 'page'], ['foo' => 'extra'], 'x.php?foo=page']; @@ -133,23 +144,35 @@ public function testUrl(string $requestUrl, array $appStickyGetArguments, array self::assertSame($expectedUrl, $app->url(($page[0] ?? '') . (count($pageAssocOnly) > 0 ? '?' . implode('&', array_map(static fn ($k) => $k . '=' . $pageAssocOnly[$k], array_keys($pageAssocOnly))) : ''), $extraRequestUrlArgs)); self::assertSame($expectedUrl, $app->jsUrl($page, array_merge(['__atk_json' => null], $extraRequestUrlArgs))); - $makeExpectedUrlFx = static function (string $ext) use ($page, $expectedUrl) { - return preg_replace_callback('~^[^?]*?\K(\.php)(?=\?|$)~', static function ($matches) use ($page, $ext) { - if ($matches[1] !== '' && !preg_match('~\.php(?=\?|$)~', $page[0] ?? '')) { - $matches[1] = $ext; + $makeExpectedUrlFx = static function (string $indexPage, string $ext) use ($page, $expectedUrl) { + return preg_replace_callback('~^[^?]*?\K([^/?]*)(\.php)(?=\?|$)~', static function ($matches) use ($page, $indexPage, $ext) { + if ($matches[1] === 'index' && !preg_match('~^([^?]*?/)?index(\.php)?(?=\?|$)~', $page[0] ?? '')) { + $matches[1] = $indexPage; + } + if (!preg_match('~^[^?]*?\.php(?=\?|$)~', $page[0] ?? '')) { + $matches[2] = $matches[1] !== '' ? $ext : ''; } - return $matches[1]; - }, $expectedUrl, 1); + return $matches[1] . $matches[2]; + }, $expectedUrl); }; + $app = $this->createApp([ + 'request' => $request, + 'stickyGetArguments' => $appStickyGetArguments, + 'urlBuildingIndexPage' => 'default', + 'urlBuildingExt' => '.php8', + ]); + $expectedUrlCustom = $makeExpectedUrlFx('default', '.php8'); + self::assertSame($expectedUrlCustom, $app->url($page, $extraRequestUrlArgs)); + $app = $this->createApp([ 'request' => $request, 'stickyGetArguments' => $appStickyGetArguments, 'urlBuildingIndexPage' => '', 'urlBuildingExt' => '', ]); - $expectedUrlAutoindex = $makeExpectedUrlFx(''); + $expectedUrlAutoindex = $makeExpectedUrlFx('', ''); self::assertSame($expectedUrlAutoindex, $app->url($page, $extraRequestUrlArgs)); $app = $this->createApp([ @@ -158,7 +181,7 @@ public function testUrl(string $requestUrl, array $appStickyGetArguments, array 'urlBuildingIndexPage' => '', 'urlBuildingExt' => '.html', ]); - $expectedUrlAutoindex2 = $makeExpectedUrlFx('.html'); + $expectedUrlAutoindex2 = $makeExpectedUrlFx('', '.html'); self::assertSame($expectedUrlAutoindex2, $app->url($page, $extraRequestUrlArgs)); } } diff --git a/tests/CallbackTest.php b/tests/CallbackTest.php index b4eaacf158..9f8d75a30c 100644 --- a/tests/CallbackTest.php +++ b/tests/CallbackTest.php @@ -89,8 +89,8 @@ public function testViewUrlCallback(): void $this->simulateCallbackTriggering($cbApp); $this->simulateCallbackTriggering($cb); - $expectedUrlCbApp = 'index.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'aa=callback&' . Callback::URL_QUERY_TARGET . '=aa'; - $expectedUrlCb = 'index.php?' . /* Callback::URL_QUERY_TRIGGER_PREFIX . 'aa=1&' . */ Callback::URL_QUERY_TRIGGER_PREFIX . 'bb=callback&' . Callback::URL_QUERY_TARGET . '=bb'; + $expectedUrlCbApp = '/index.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'aa=callback&' . Callback::URL_QUERY_TARGET . '=aa'; + $expectedUrlCb = '/index.php?' . /* Callback::URL_QUERY_TRIGGER_PREFIX . 'aa=1&' . */ Callback::URL_QUERY_TRIGGER_PREFIX . 'bb=callback&' . Callback::URL_QUERY_TARGET . '=bb'; self::assertSame($expectedUrlCbApp, $cbApp->getUrl()); self::assertSame($expectedUrlCb, $cb->getUrl());