Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/Filter/FilterUrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This file is part of MetaModels/core.
*
* (c) 2012-2025 The MetaModels team.
* (c) 2012-2026 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -16,7 +16,7 @@
* @author Ingolf Steinhardt <info@e-spin.de>
* @author Stefan Heimes <stefan_heimes@hotmail.com>
* @author Andreas Fischer <anfischer@kaffee-partner.de>
* @copyright 2012-2025 The MetaModels team.
* @copyright 2012-2026 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
Expand Down Expand Up @@ -182,20 +182,23 @@ public function addFromCurrentRequest(FilterUrl $filterUrl, array $options = nul
$filterUrl->setPageValue('id', \substr($routeName, 8));
$requestUri = \rawurldecode(\substr($request->getPathInfo(), 1));

if (null === ($route = $request->attributes->get('_route_object'))) {
return;
}
assert($route instanceof Route);
if (null === ($pageModel = $request->attributes->get('pageModel'))) {
if (null === ($route = $request->attributes->get('_route_object'))) {
return;
}
assert($route instanceof Route);

$pageModel = $route->getDefault('pageModel');
assert($pageModel instanceof PageModel);
$pageModel = $route->getDefault('pageModel');
assert($pageModel instanceof PageModel);
}

$length = $pageModel->urlSuffix ? -\strlen($pageModel->urlSuffix) : null;
$start = ($pageModel->urlPrefix ? \strlen($pageModel->urlPrefix . '/') : 0)
+ \strlen($pageModel->alias . '/');
$fragments = \explode('/', \substr($requestUri, $start, $length));
$slicedUri = \substr($requestUri, $start, $length);
$fragments = '' !== $slicedUri ? \explode('/', $slicedUri) : [];

if (1 === \count($fragments) % 2) {
if (!empty($fragments) && 1 === \count($fragments) % 2) {
\array_unshift($fragments, 'auto_item');
}
\array_unshift($fragments, $pageModel->alias);
Expand Down
58 changes: 36 additions & 22 deletions tests/Filter/FilterUrlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* This file is part of MetaModels/core.
*
* (c) 2012-2025 The MetaModels team.
* (c) 2012-2026 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -13,13 +13,15 @@
* @package MetaModels/core
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Sven Baumann <baumann.sv@gmail.com>
* @copyright 2012-2025 The MetaModels team.
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2012-2026 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/

namespace MetaModels\Test\Filter;

use Contao\PageModel;
use MetaModels\Filter\FilterUrl;
use MetaModels\Filter\FilterUrlBuilder;
use PHPUnit\Framework\Attributes\CoversClass;
Expand Down Expand Up @@ -47,10 +49,10 @@ public static function generateProvider(): array
'get2' => 'value',
'parameters' => '/auto/slug/sluggy',
],
'page' => [
'alias' => 'page-alias',
'page' => fn ($test) => $test->mockPage([
'alias' => 'alias',
'id' => 42,
],
]),
'get' => [
'get2' => 'value',
],
Expand All @@ -70,9 +72,10 @@ public static function generateProvider(): array
'parameters' => '/auto/slug/sluggy',
'get-param' => 'get-value',
],
'page' => [
'id' => 42,
],
'page' => fn ($test) => $test->mockPage(
['id' => 42, 'alias' => 'alias', 'urlSuffix' => '.html'],
['id' => 42]
),
'get' => [
'get2' => 'value',
],
Expand All @@ -91,28 +94,28 @@ public static function generateProvider(): array
/**
* Test initialization.
*
* @param string $expectedUrl The expected URL.
* @param array $expectedParameters The expected parameters.
* @param array $page The page array.
* @param array $get The GET parameters.
* @param array $slug The slug parameters.
* @param array $requestGet The GET parameters of the current request.
* @param string $requestUrl The current URL.
*
* @return void
* @param string $expectedUrl The expected URL.
* @param array $expectedParameters The expected parameters.
* @param callable(FilterUrlBuilderTest): PageModel $page The page array.
* @param array $get The GET parameters.
* @param array $slug The slug parameters.
* @param array $requestGet The GET parameters of the current request.
* @param string $requestUrl The current URL.
*/
#[DataProvider('generateProvider')]
public function testGenerate(
string $expectedUrl,
array $expectedParameters,
array $page,
callable $page,
array $get,
array $slug,
array $requestGet,
string $requestUrl
): void {
$pageModel = $page($this);

$filterUrl = new FilterUrl(
$page,
$pageModel->row(),
$get,
$slug
);
Expand All @@ -127,7 +130,7 @@ public function testGenerate(
->with($expectedUrl, $expectedParameters)
->willReturn('success');

$requestStack = $this->mockRequestStack($requestGet, $requestUrl, $page['id']);
$requestStack = $this->mockRequestStack($requestGet, $requestUrl, $pageModel);

$builder = new FilterUrlBuilder($generator, $requestStack);

Expand Down Expand Up @@ -158,7 +161,7 @@ public function testGeneratesNonStandardPorts(): void
new Request(
['get-param' => 'get-value'],
[],
['pageModel' => 42],
['pageModel' => $this->mockPage(['alias' => 'folder/page', 'id' => 42, 'urlSuffix' => '.html'])],
[],
[],
[
Expand All @@ -179,7 +182,7 @@ public function testGeneratesNonStandardPorts(): void
* @param array $requestGet The current GET parameters.
* @param string $requestUrl The request URL.
*/
private function mockRequestStack(array $requestGet, string $requestUrl, int $pageModel): RequestStack
private function mockRequestStack(array $requestGet, string $requestUrl, PageModel $pageModel): RequestStack
{
$requestStack = $this->getMockBuilder(RequestStack::class)->getMock();
$requestStack->method('getCurrentRequest')->willReturn(
Expand All @@ -188,4 +191,15 @@ private function mockRequestStack(array $requestGet, string $requestUrl, int $pa

return $requestStack;
}

private function mockPage(array $data, array $rowData = null): PageModel
{
$mock = $this->getMockBuilder(PageModel::class)->disableOriginalConstructor()->getMock();
$mock->method('__get')->willReturnCallback(
fn (string $name) => $data[$name] ?? null
);
$mock->method('row')->willReturn($rowData ?? $data);

return $mock;
}
}
Loading