diff --git a/composer.json b/composer.json index 9adeb53d..533b20a5 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "laminas/laminas-stdlib": "^3.18", "pagerfanta/doctrine-orm-adapter": "^4.6", "pagerfanta/twig": "^4.6", - "sylius/grid-bundle": "^1.13@alpha", + "sylius/grid-bundle": "^1.13@beta", "sylius/resource-bundle": "^1.11 || ^1.12@alpha", "symfony/asset": "^6.4 || ^7.0", "symfony/config": "^6.4 || ^7.0", diff --git a/src/AdminUi/composer.json b/src/AdminUi/composer.json index 75134ab1..f417785d 100644 --- a/src/AdminUi/composer.json +++ b/src/AdminUi/composer.json @@ -13,8 +13,11 @@ "twig/twig": "^2.15 || ^3.0" }, "require-dev": { + "doctrine/doctrine-bundle": "^2.12", + "doctrine/orm": "^2.0", "matthiasnoback/symfony-dependency-injection-test": "^5.1", "phpunit/phpunit": "^9.6", + "sylius/grid-bundle": "^1.13@beta", "symfony/browser-kit": "^6.4 || ^7.0", "symfony/config": "^6.4 || ^7.0", "symfony/console": "^6.4 || ^7.0", diff --git a/src/AdminUi/config/services/twig/extension.php b/src/AdminUi/config/services/twig/extension.php new file mode 100644 index 00000000..c6521d09 --- /dev/null +++ b/src/AdminUi/config/services/twig/extension.php @@ -0,0 +1,28 @@ +services(); + + $services->set('sylius_twig_extra.twig.extension.redirect_path', RedirectPathExtension::class) + ->args([ + service('router'), + service('sylius.grid.filter_storage')->nullOnInvalid(), + ]) + ->tag(name: 'twig.extension') + ; +}; diff --git a/src/AdminUi/src/Twig/Extension/RedirectPathExtension.php b/src/AdminUi/src/Twig/Extension/RedirectPathExtension.php new file mode 100644 index 00000000..42f8f789 --- /dev/null +++ b/src/AdminUi/src/Twig/Extension/RedirectPathExtension.php @@ -0,0 +1,69 @@ +router->match($request->getPathInfo()); + } catch (\Throwable) { + return $path; + } + + if ([] !== $request->query->all() || $this->hasAdditionalParameters($routeInfo)) { + return $path; + } + + $route = $routeInfo['_route']; + + return $this->router->generate($route, $this->filterStorage?->all() ?? []); + } + + /** + * @param mixed[] $routeInfo + */ + private function hasAdditionalParameters(array $routeInfo): bool + { + return count($routeInfo) > self::NUMBER_OF_ROUTE_PROPERTIES; + } +} diff --git a/src/AdminUi/tests/Functional/.application/.env b/src/AdminUi/tests/Functional/.application/.env index 4a5b1be7..d84e5bf0 100644 --- a/src/AdminUi/tests/Functional/.application/.env +++ b/src/AdminUi/tests/Functional/.application/.env @@ -19,3 +19,6 @@ APP_ENV=dev APP_SECRET=c9570eaa5cbfadf33ad1a93f95c2e42a ###< symfony/framework-bundle ### +###> doctrine/doctrine-bundle ### +DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db" +###< doctrine/doctrine-bundle ### diff --git a/src/AdminUi/tests/Functional/.application/config/bundles.php b/src/AdminUi/tests/Functional/.application/config/bundles.php index 46d7ddbb..2ac82377 100644 --- a/src/AdminUi/tests/Functional/.application/config/bundles.php +++ b/src/AdminUi/tests/Functional/.application/config/bundles.php @@ -19,4 +19,6 @@ Sylius\TwigHooks\SyliusTwigHooksBundle::class => ['all' => true], Sylius\Bundle\ResourceBundle\SyliusResourceBundle::class => ['all' => true], Sylius\AdminUi\Symfony\SyliusAdminUiBundle::class => ['all' => true], + Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true], + Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true], ]; diff --git a/src/AdminUi/tests/Functional/.application/config/packages/doctrine.yaml b/src/AdminUi/tests/Functional/.application/config/packages/doctrine.yaml new file mode 100644 index 00000000..d42c52d6 --- /dev/null +++ b/src/AdminUi/tests/Functional/.application/config/packages/doctrine.yaml @@ -0,0 +1,50 @@ +doctrine: + dbal: + url: '%env(resolve:DATABASE_URL)%' + + # IMPORTANT: You MUST configure your server version, + # either here or in the DATABASE_URL env var (see .env file) + #server_version: '16' + + profiling_collect_backtrace: '%kernel.debug%' + use_savepoints: true + orm: + auto_generate_proxy_classes: true + enable_lazy_ghost_objects: true + report_fields_where_declared: true + validate_xml_mapping: true + naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware + auto_mapping: true + mappings: + App: + type: attribute + is_bundle: false + dir: '%kernel.project_dir%/src/Entity' + prefix: 'App\Entity' + alias: App + +when@test: + doctrine: + dbal: + # "TEST_TOKEN" is typically set by ParaTest + dbname_suffix: '_test%env(default::TEST_TOKEN)%' + +when@prod: + doctrine: + orm: + auto_generate_proxy_classes: false + proxy_dir: '%kernel.build_dir%/doctrine/orm/Proxies' + query_cache_driver: + type: pool + pool: doctrine.system_cache_pool + result_cache_driver: + type: pool + pool: doctrine.result_cache_pool + + framework: + cache: + pools: + doctrine.result_cache_pool: + adapter: cache.app + doctrine.system_cache_pool: + adapter: cache.system diff --git a/src/AdminUi/tests/Functional/Twig/Extension/RedirectPathExtensionTest.php b/src/AdminUi/tests/Functional/Twig/Extension/RedirectPathExtensionTest.php new file mode 100644 index 00000000..a87ef6f7 --- /dev/null +++ b/src/AdminUi/tests/Functional/Twig/Extension/RedirectPathExtensionTest.php @@ -0,0 +1,66 @@ +get('session.factory')->createSession(); + $request = new Request(); + $request->setSession($session); + $container->get('request_stack')->push($request); + + $this->redirectPathExtension = $container->get('sylius_twig_extra.twig.extension.redirect_path'); + $container->get('sylius.grid.filter_storage')->set(['criteria' => ['enabled' => true]]); + } + + public function testItReturnsRedirectPathWithFiltersFromStorageApplied(): void + { + // Note, it requires an existing route path. + $redirectPath = $this->redirectPathExtension->generateRedirectPath('/books'); + + $this->assertSame('/books?criteria%5Benabled%5D=1', $redirectPath); + } + + public function testItReturnsGivenPathIfRouteHasSomeMoreConfiguration(): void + { + // TODO ask internal core team member to reproduce this route with "some more configuration" + $redirectPath = $this->redirectPathExtension->generateRedirectPath('/admin/ajax/products/search'); + + $this->assertSame('/admin/ajax/products/search', $redirectPath); + } + + public function testItReturnsGivenPathIfRouteIsNotMatched(): void + { + $redirectPath = $this->redirectPathExtension->generateRedirectPath('/invalid-path'); + + $this->assertSame('/invalid-path', $redirectPath); + } + + public function testItReturnsNullIfThePathIsNullAsWell(): void + { + $redirectPath = $this->redirectPathExtension->generateRedirectPath(null); + + $this->assertNull($redirectPath); + } +} diff --git a/src/BootstrapAdminUi/composer.json b/src/BootstrapAdminUi/composer.json index 8f0b01e4..5bb02dbd 100644 --- a/src/BootstrapAdminUi/composer.json +++ b/src/BootstrapAdminUi/composer.json @@ -5,7 +5,7 @@ "php": "^8.1", "pagerfanta/twig": "^4.6", "sylius/admin-ui": "dev-main", - "sylius/grid-bundle": "^1.13@alpha", + "sylius/grid-bundle": "^1.13@beta", "sylius/resource-bundle": "^1.11 || ^1.12@alpha", "sylius/twig-extra": "dev-main", "sylius/twig-hooks": "^0.3 || dev-main", diff --git a/src/BootstrapAdminUi/templates/shared/crud/common/content/header/title_block/actions/cancel.html.twig b/src/BootstrapAdminUi/templates/shared/crud/common/content/header/title_block/actions/cancel.html.twig index 73694427..0e212628 100644 --- a/src/BootstrapAdminUi/templates/shared/crud/common/content/header/title_block/actions/cancel.html.twig +++ b/src/BootstrapAdminUi/templates/shared/crud/common/content/header/title_block/actions/cancel.html.twig @@ -2,14 +2,9 @@ {% set configuration = hookable_metadata.context.configuration %} -{#{% set index_url = sylius_generate_redirect_path(app.request.headers.get('referer')|default(path(#} -{# configuration.vars.index.route.name|default(configuration.getRouteName('index')),#} -{# configuration.vars.index.route.parameters|default(configuration.vars.route.parameters|default({}))#} -{#))) %}#} - -{% set index_url = path( +{% set index_url = sylius_generate_redirect_path(app.request.headers.get('referer')|default(path( configuration.vars.index.route.name|default(configuration.getRouteName('index')), configuration.vars.index.route.parameters|default(configuration.vars.route.parameters|default({})) -) %} +))) %} {{ button.cancel(sylius_test_form_attribute('cancel-changes-button')|merge({ text: 'sylius.ui.cancel'|trans, url: index_url, class: 'btn' })) }}