From 86b0ddda54c783b6ff5df189e4cee50582130f8f Mon Sep 17 00:00:00 2001 From: Luca Rath-Heel Date: Fri, 5 Jun 2020 08:46:55 +0200 Subject: [PATCH 1/3] Enhance route generation --- .../DataMapper/RoutableDataMapper.php | 16 +++- Resources/config/data-mapper.xml | 1 + .../DataMapper/RoutableDataMapperTest.php | 96 ++++++++++++++----- 3 files changed, 90 insertions(+), 23 deletions(-) diff --git a/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapper.php b/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapper.php index a6b250ea..978a25b8 100644 --- a/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapper.php +++ b/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapper.php @@ -13,6 +13,7 @@ namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper; +use Sulu\Bundle\ContentBundle\Content\Application\ContentNormalizer\ContentNormalizerInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\RoutableInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface; use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorInterface; @@ -38,6 +39,11 @@ class RoutableDataMapper implements DataMapperInterface */ private $routeManager; + /** + * @var ContentNormalizerInterface + */ + private $contentNormalizer; + /** * @var array */ @@ -56,12 +62,14 @@ public function __construct( StructureMetadataFactoryInterface $factory, RouteGeneratorInterface $routeGenerator, RouteManagerInterface $routeManager, + ContentNormalizerInterface $contentNormalizer, array $structureDefaultTypes, array $routeMappings ) { $this->factory = $factory; $this->routeGenerator = $routeGenerator; $this->routeManager = $routeManager; + $this->contentNormalizer = $contentNormalizer; $this->structureDefaultTypes = $structureDefaultTypes; $this->routeMappings = $routeMappings; } @@ -138,8 +146,14 @@ public function map( $routePath = $data[$name] ?? null; if (!$routePath) { + /** @var mixed $routeGenerationData */ + $routeGenerationData = array_merge( + $this->contentNormalizer->normalize($unlocalizedObject), + $this->contentNormalizer->normalize($localizedObject) + ); + $routePath = $this->routeGenerator->generate( - $localizedObject, + $routeGenerationData, $routeSchema ); diff --git a/Resources/config/data-mapper.xml b/Resources/config/data-mapper.xml index 25cd324a..bd016e85 100644 --- a/Resources/config/data-mapper.xml +++ b/Resources/config/data-mapper.xml @@ -30,6 +30,7 @@ + %sulu.content.structure.default_types% %sulu_route.mappings% diff --git a/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php b/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php index 69101d53..72567d7e 100644 --- a/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php +++ b/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php @@ -17,6 +17,7 @@ use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; use Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper\RoutableDataMapper; +use Sulu\Bundle\ContentBundle\Content\Application\ContentNormalizer\ContentNormalizerInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\RoutableInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface; @@ -39,6 +40,7 @@ protected function createRouteDataMapperInstance( StructureMetadataFactoryInterface $factory, RouteGeneratorInterface $routeGenerator, RouteManagerInterface $routeManager, + ContentNormalizerInterface $contentNormalizer, array $structureDefaultTypes = [], array $resourceKeyMappings = [] ): RoutableDataMapper { @@ -47,7 +49,7 @@ protected function createRouteDataMapperInstance( 'testKey' => [ 'generator' => 'schema', 'options' => [ - 'route_schema' => '/{object.getTitle()}', + 'route_schema' => '/{object["title"]}', ], 'resource_key' => 'testKey', 'entityClass' => 'mock-content-class', @@ -55,7 +57,7 @@ protected function createRouteDataMapperInstance( ]; } - return new RoutableDataMapper($factory, $routeGenerator, $routeManager, $structureDefaultTypes, $resourceKeyMappings); + return new RoutableDataMapper($factory, $routeGenerator, $routeManager, $contentNormalizer, $structureDefaultTypes, $resourceKeyMappings); } protected function wrapRoutableMock(ObjectProphecy $routableMock): RoutableInterface @@ -78,6 +80,7 @@ public function testMapNoRoutable(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $factory->getStructureMetadata(Argument::cetera())->shouldNotBeCalled(); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -85,7 +88,8 @@ public function testMapNoRoutable(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map($data, $dimensionContent->reveal(), $localizedDimensionContent->reveal()); @@ -100,6 +104,7 @@ public function testMapNoLocalizedDimension(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $factory->getStructureMetadata(Argument::cetera())->shouldNotBeCalled(); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -107,7 +112,8 @@ public function testMapNoLocalizedDimension(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map($data, $dimensionContent->reveal(), null); @@ -126,6 +132,7 @@ public function testMapNoTemplateInterface(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $factory->getStructureMetadata(Argument::cetera())->shouldNotBeCalled(); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -134,7 +141,8 @@ public function testMapNoTemplateInterface(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map($data, $dimensionContent->reveal(), $localizedDimensionContent->reveal()); @@ -154,6 +162,7 @@ public function testMapNoTemplate(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $factory->getStructureMetadata(Argument::cetera())->shouldNotBeCalled(); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -162,7 +171,8 @@ public function testMapNoTemplate(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map( @@ -186,6 +196,7 @@ public function testMapNoMetadata(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -194,7 +205,8 @@ public function testMapNoMetadata(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map( @@ -218,6 +230,7 @@ public function testMapNoRouteProperty(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -236,7 +249,8 @@ public function testMapNoRouteProperty(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map( @@ -260,6 +274,7 @@ public function testMapNoRoutePropertyData(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -278,7 +293,8 @@ public function testMapNoRoutePropertyData(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map( @@ -302,6 +318,7 @@ public function testMapNoRoutePropertyDataAndNoOldRoute(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $metadata = $this->prophesize(StructureMetadata::class); $property = $this->prophesize(PropertyMetadata::class); @@ -319,7 +336,12 @@ public function testMapNoRoutePropertyDataAndNoOldRoute(): void $factory->getStructureMetadata('mock-template-type', 'default')->willReturn($metadata->reveal())->shouldBeCalled(); - $routeGenerator->generate($localizedDimensionContentMock, ['route_schema' => '/{object.getTitle()}']) + $normalizedData = [ + 'title' => 'test', + ]; + $contentNormalizer->normalize($dimensionContent->reveal())->willReturn([]); + $contentNormalizer->normalize($localizedDimensionContentMock)->willReturn($normalizedData); + $routeGenerator->generate($normalizedData, ['route_schema' => '/{object["title"]}']) ->willReturn('/test'); $routeManager->createOrUpdateByAttributes( @@ -332,7 +354,8 @@ public function testMapNoRoutePropertyDataAndNoOldRoute(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map( @@ -356,6 +379,7 @@ public function testMapNoRoutePropertyDataAndNoOldRouteIgnoreSlash(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $metadata = $this->prophesize(StructureMetadata::class); $property = $this->prophesize(PropertyMetadata::class); @@ -373,7 +397,11 @@ public function testMapNoRoutePropertyDataAndNoOldRouteIgnoreSlash(): void $factory->getStructureMetadata('mock-template-type', 'default')->willReturn($metadata->reveal())->shouldBeCalled(); - $routeGenerator->generate($localizedDimensionContentMock, ['route_schema' => '/{object.getTitle()}']) + $normalizedData = [ + 'title' => '', + ]; + $contentNormalizer->normalize($localizedDimensionContentMock)->willReturn($normalizedData); + $routeGenerator->generate($normalizedData, ['route_schema' => '/{object["title"]}']) ->willReturn('/'); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -381,7 +409,8 @@ public function testMapNoRoutePropertyDataAndNoOldRouteIgnoreSlash(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map( @@ -405,6 +434,7 @@ public function testMapNoContentId(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -421,7 +451,8 @@ public function testMapNoContentId(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map( @@ -445,6 +476,7 @@ public function testMapNoLocale(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -462,7 +494,8 @@ public function testMapNoLocale(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map( @@ -487,6 +520,7 @@ public function testMapNoRoutePath(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $metadata = $this->prophesize(StructureMetadata::class); $property = $this->prophesize(PropertyMetadata::class); @@ -505,7 +539,12 @@ public function testMapNoRoutePath(): void $localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent); - $routeGenerator->generate($localizedDimensionContentMock, ['route_schema' => '/{object.getTitle()}']) + $normalizedData = [ + 'title' => 'test', + ]; + $contentNormalizer->normalize($dimensionContent->reveal())->willReturn([]); + $contentNormalizer->normalize($localizedDimensionContentMock)->willReturn($normalizedData); + $routeGenerator->generate($normalizedData, ['route_schema' => '/{object["title"]}']) ->willReturn('/test'); $routeManager->createOrUpdateByAttributes( @@ -518,7 +557,8 @@ public function testMapNoRoutePath(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map( @@ -543,6 +583,7 @@ public function testMap(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $metadata = $this->prophesize(StructureMetadata::class); $property = $this->prophesize(PropertyMetadata::class); @@ -559,7 +600,8 @@ public function testMap(): void $localizedDimensionContent->getResourceKey()->willReturn('testKey'); $localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent); - $routeGenerator->generate($localizedDimensionContentMock, ['schema' => '/{object.getTitle()}']) + $contentNormalizer->normalize($localizedDimensionContentMock)->shouldNotBeCalled(); + $routeGenerator->generate(Argument::any(), ['schema' => '/{object["title"]}']) ->shouldNotBeCalled(); $routeManager->createOrUpdateByAttributes( @@ -572,7 +614,8 @@ public function testMap(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal() + $routeManager->reveal(), + $contentNormalizer->reveal() ); $mapper->map( @@ -596,6 +639,7 @@ public function testMapNoTemplateWithDefaultTemplate(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $metadata = $this->prophesize(StructureMetadata::class); $property = $this->prophesize(PropertyMetadata::class); @@ -612,7 +656,8 @@ public function testMapNoTemplateWithDefaultTemplate(): void $localizedDimensionContent->getResourceKey()->willReturn('testKey'); $localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent); - $routeGenerator->generate($localizedDimensionContentMock, ['schema' => '/{object.getTitle()}']) + $contentNormalizer->normalize($localizedDimensionContentMock)->shouldNotBeCalled(); + $routeGenerator->generate(Argument::any(), ['schema' => '/{object["title"]}']) ->shouldNotBeCalled(); $routeManager->createOrUpdateByAttributes( @@ -651,6 +696,7 @@ public function testMapCustomRoute(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); + $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $metadata = $this->prophesize(StructureMetadata::class); $property = $this->prophesize(PropertyMetadata::class); @@ -668,7 +714,13 @@ public function testMapCustomRoute(): void $localizedDimensionContent->getResourceKey()->willReturn('testKey'); $localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent); - $routeGenerator->generate($localizedDimensionContentMock, ['route_schema' => 'custom/{object.getName()}-{object.getId()}']) + $normalizedData = [ + 'id' => '123', + 'name' => 'testEntity', + ]; + $contentNormalizer->normalize($dimensionContent->reveal())->willReturn([]); + $contentNormalizer->normalize($localizedDimensionContentMock)->willReturn($normalizedData); + $routeGenerator->generate($normalizedData, ['route_schema' => 'custom/{object["name"]}-{object["id"]}']) ->willReturn('/custom/testEntity-123'); $routeManager->createOrUpdateByAttributes( @@ -687,7 +739,7 @@ public function testMapCustomRoute(): void 'testKey' => [ 'generator' => 'schema', 'options' => [ - 'route_schema' => 'custom/{object.getName()}-{object.getId()}', + 'route_schema' => 'custom/{object["name"]}-{object["id"]}', ], 'resource_key' => 'testKey', 'entityClass' => 'Sulu/Test/TestEntity', From dd0e46fe802b0452be97eccbac1ef72ba3abdee2 Mon Sep 17 00:00:00 2001 From: Luca Rath-Heel Date: Fri, 5 Jun 2020 09:07:16 +0200 Subject: [PATCH 2/3] fix ci --- .../ContentDataMapper/DataMapper/RoutableDataMapperTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php b/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php index 72567d7e..2596e89f 100644 --- a/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php +++ b/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php @@ -400,6 +400,7 @@ public function testMapNoRoutePropertyDataAndNoOldRouteIgnoreSlash(): void $normalizedData = [ 'title' => '', ]; + $contentNormalizer->normalize($dimensionContent->reveal())->willReturn([]); $contentNormalizer->normalize($localizedDimensionContentMock)->willReturn($normalizedData); $routeGenerator->generate($normalizedData, ['route_schema' => '/{object["title"]}']) ->willReturn('/'); @@ -671,6 +672,7 @@ public function testMapNoTemplateWithDefaultTemplate(): void $factory->reveal(), $routeGenerator->reveal(), $routeManager->reveal(), + $contentNormalizer->reveal(), ['mock-template-type' => 'default'] ); @@ -734,6 +736,7 @@ public function testMapCustomRoute(): void $factory->reveal(), $routeGenerator->reveal(), $routeManager->reveal(), + $contentNormalizer->reveal(), [], [ 'testKey' => [ From 16009af8cdfdf278ecdb08e980a4a93a2a15e3a6 Mon Sep 17 00:00:00 2001 From: Luca Rath-Heel Date: Fri, 5 Jun 2020 11:00:09 +0200 Subject: [PATCH 3/3] fix review --- .../DataMapper/RoutableDataMapper.php | 15 +-- Resources/config/data-mapper.xml | 1 - .../DataMapper/RoutableDataMapperTest.php | 127 +++++++----------- 3 files changed, 50 insertions(+), 93 deletions(-) diff --git a/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapper.php b/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapper.php index 978a25b8..543658dd 100644 --- a/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapper.php +++ b/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapper.php @@ -13,7 +13,6 @@ namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper; -use Sulu\Bundle\ContentBundle\Content\Application\ContentNormalizer\ContentNormalizerInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\RoutableInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface; use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorInterface; @@ -39,11 +38,6 @@ class RoutableDataMapper implements DataMapperInterface */ private $routeManager; - /** - * @var ContentNormalizerInterface - */ - private $contentNormalizer; - /** * @var array */ @@ -62,14 +56,12 @@ public function __construct( StructureMetadataFactoryInterface $factory, RouteGeneratorInterface $routeGenerator, RouteManagerInterface $routeManager, - ContentNormalizerInterface $contentNormalizer, array $structureDefaultTypes, array $routeMappings ) { $this->factory = $factory; $this->routeGenerator = $routeGenerator; $this->routeManager = $routeManager; - $this->contentNormalizer = $contentNormalizer; $this->structureDefaultTypes = $structureDefaultTypes; $this->routeMappings = $routeMappings; } @@ -148,8 +140,11 @@ public function map( if (!$routePath) { /** @var mixed $routeGenerationData */ $routeGenerationData = array_merge( - $this->contentNormalizer->normalize($unlocalizedObject), - $this->contentNormalizer->normalize($localizedObject) + $data, + [ + '_unlocalizedObject' => $unlocalizedObject, + '_localizedObject' => $localizedObject, + ] ); $routePath = $this->routeGenerator->generate( diff --git a/Resources/config/data-mapper.xml b/Resources/config/data-mapper.xml index bd016e85..25cd324a 100644 --- a/Resources/config/data-mapper.xml +++ b/Resources/config/data-mapper.xml @@ -30,7 +30,6 @@ - %sulu.content.structure.default_types% %sulu_route.mappings% diff --git a/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php b/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php index 2596e89f..e8d3aa31 100644 --- a/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php +++ b/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/RoutableDataMapperTest.php @@ -17,7 +17,6 @@ use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; use Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper\RoutableDataMapper; -use Sulu\Bundle\ContentBundle\Content\Application\ContentNormalizer\ContentNormalizerInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\RoutableInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface; @@ -40,7 +39,6 @@ protected function createRouteDataMapperInstance( StructureMetadataFactoryInterface $factory, RouteGeneratorInterface $routeGenerator, RouteManagerInterface $routeManager, - ContentNormalizerInterface $contentNormalizer, array $structureDefaultTypes = [], array $resourceKeyMappings = [] ): RoutableDataMapper { @@ -57,7 +55,7 @@ protected function createRouteDataMapperInstance( ]; } - return new RoutableDataMapper($factory, $routeGenerator, $routeManager, $contentNormalizer, $structureDefaultTypes, $resourceKeyMappings); + return new RoutableDataMapper($factory, $routeGenerator, $routeManager, $structureDefaultTypes, $resourceKeyMappings); } protected function wrapRoutableMock(ObjectProphecy $routableMock): RoutableInterface @@ -80,7 +78,6 @@ public function testMapNoRoutable(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $factory->getStructureMetadata(Argument::cetera())->shouldNotBeCalled(); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -88,8 +85,7 @@ public function testMapNoRoutable(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map($data, $dimensionContent->reveal(), $localizedDimensionContent->reveal()); @@ -104,7 +100,6 @@ public function testMapNoLocalizedDimension(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $factory->getStructureMetadata(Argument::cetera())->shouldNotBeCalled(); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -112,8 +107,7 @@ public function testMapNoLocalizedDimension(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map($data, $dimensionContent->reveal(), null); @@ -132,7 +126,6 @@ public function testMapNoTemplateInterface(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $factory->getStructureMetadata(Argument::cetera())->shouldNotBeCalled(); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -141,8 +134,7 @@ public function testMapNoTemplateInterface(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map($data, $dimensionContent->reveal(), $localizedDimensionContent->reveal()); @@ -162,7 +154,6 @@ public function testMapNoTemplate(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $factory->getStructureMetadata(Argument::cetera())->shouldNotBeCalled(); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -171,8 +162,7 @@ public function testMapNoTemplate(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map( @@ -196,7 +186,6 @@ public function testMapNoMetadata(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -205,8 +194,7 @@ public function testMapNoMetadata(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map( @@ -230,7 +218,6 @@ public function testMapNoRouteProperty(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -249,8 +236,7 @@ public function testMapNoRouteProperty(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map( @@ -274,7 +260,6 @@ public function testMapNoRoutePropertyData(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -293,8 +278,7 @@ public function testMapNoRoutePropertyData(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map( @@ -318,7 +302,6 @@ public function testMapNoRoutePropertyDataAndNoOldRoute(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $metadata = $this->prophesize(StructureMetadata::class); $property = $this->prophesize(PropertyMetadata::class); @@ -336,13 +319,13 @@ public function testMapNoRoutePropertyDataAndNoOldRoute(): void $factory->getStructureMetadata('mock-template-type', 'default')->willReturn($metadata->reveal())->shouldBeCalled(); - $normalizedData = [ - 'title' => 'test', - ]; - $contentNormalizer->normalize($dimensionContent->reveal())->willReturn([]); - $contentNormalizer->normalize($localizedDimensionContentMock)->willReturn($normalizedData); - $routeGenerator->generate($normalizedData, ['route_schema' => '/{object["title"]}']) - ->willReturn('/test'); + $routeGenerator->generate( + array_merge($data, [ + '_unlocalizedObject' => $dimensionContent->reveal(), + '_localizedObject' => $localizedDimensionContentMock, + ]), + ['route_schema' => '/{object["title"]}'] + )->willReturn('/test'); $routeManager->createOrUpdateByAttributes( 'mock-content-class', @@ -354,8 +337,7 @@ public function testMapNoRoutePropertyDataAndNoOldRoute(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map( @@ -379,7 +361,6 @@ public function testMapNoRoutePropertyDataAndNoOldRouteIgnoreSlash(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $metadata = $this->prophesize(StructureMetadata::class); $property = $this->prophesize(PropertyMetadata::class); @@ -397,21 +378,20 @@ public function testMapNoRoutePropertyDataAndNoOldRouteIgnoreSlash(): void $factory->getStructureMetadata('mock-template-type', 'default')->willReturn($metadata->reveal())->shouldBeCalled(); - $normalizedData = [ - 'title' => '', - ]; - $contentNormalizer->normalize($dimensionContent->reveal())->willReturn([]); - $contentNormalizer->normalize($localizedDimensionContentMock)->willReturn($normalizedData); - $routeGenerator->generate($normalizedData, ['route_schema' => '/{object["title"]}']) - ->willReturn('/'); + $routeGenerator->generate( + array_merge($data, [ + '_unlocalizedObject' => $dimensionContent->reveal(), + '_localizedObject' => $localizedDimensionContentMock, + ]), + ['route_schema' => '/{object["title"]}'] + )->willReturn('/'); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map( @@ -435,7 +415,6 @@ public function testMapNoContentId(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -452,8 +431,7 @@ public function testMapNoContentId(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map( @@ -477,7 +455,6 @@ public function testMapNoLocale(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled(); @@ -495,8 +472,7 @@ public function testMapNoLocale(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map( @@ -521,7 +497,6 @@ public function testMapNoRoutePath(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $metadata = $this->prophesize(StructureMetadata::class); $property = $this->prophesize(PropertyMetadata::class); @@ -540,13 +515,13 @@ public function testMapNoRoutePath(): void $localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent); - $normalizedData = [ - 'title' => 'test', - ]; - $contentNormalizer->normalize($dimensionContent->reveal())->willReturn([]); - $contentNormalizer->normalize($localizedDimensionContentMock)->willReturn($normalizedData); - $routeGenerator->generate($normalizedData, ['route_schema' => '/{object["title"]}']) - ->willReturn('/test'); + $routeGenerator->generate( + array_merge($data, [ + '_unlocalizedObject' => $dimensionContent->reveal(), + '_localizedObject' => $localizedDimensionContentMock, + ]), + ['route_schema' => '/{object["title"]}'] + )->willReturn('/test'); $routeManager->createOrUpdateByAttributes( 'mock-content-class', @@ -558,8 +533,7 @@ public function testMapNoRoutePath(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map( @@ -584,7 +558,6 @@ public function testMap(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $metadata = $this->prophesize(StructureMetadata::class); $property = $this->prophesize(PropertyMetadata::class); @@ -601,9 +574,7 @@ public function testMap(): void $localizedDimensionContent->getResourceKey()->willReturn('testKey'); $localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent); - $contentNormalizer->normalize($localizedDimensionContentMock)->shouldNotBeCalled(); - $routeGenerator->generate(Argument::any(), ['schema' => '/{object["title"]}']) - ->shouldNotBeCalled(); + $routeGenerator->generate(Argument::any())->shouldNotBeCalled(); $routeManager->createOrUpdateByAttributes( 'mock-content-class', @@ -615,8 +586,7 @@ public function testMap(): void $mapper = $this->createRouteDataMapperInstance( $factory->reveal(), $routeGenerator->reveal(), - $routeManager->reveal(), - $contentNormalizer->reveal() + $routeManager->reveal() ); $mapper->map( @@ -640,7 +610,6 @@ public function testMapNoTemplateWithDefaultTemplate(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $metadata = $this->prophesize(StructureMetadata::class); $property = $this->prophesize(PropertyMetadata::class); @@ -657,9 +626,7 @@ public function testMapNoTemplateWithDefaultTemplate(): void $localizedDimensionContent->getResourceKey()->willReturn('testKey'); $localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent); - $contentNormalizer->normalize($localizedDimensionContentMock)->shouldNotBeCalled(); - $routeGenerator->generate(Argument::any(), ['schema' => '/{object["title"]}']) - ->shouldNotBeCalled(); + $routeGenerator->generate(Argument::any())->shouldNotBeCalled(); $routeManager->createOrUpdateByAttributes( 'mock-content-class', @@ -672,7 +639,6 @@ public function testMapNoTemplateWithDefaultTemplate(): void $factory->reveal(), $routeGenerator->reveal(), $routeManager->reveal(), - $contentNormalizer->reveal(), ['mock-template-type' => 'default'] ); @@ -698,7 +664,6 @@ public function testMapCustomRoute(): void $factory = $this->prophesize(StructureMetadataFactoryInterface::class); $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); $routeManager = $this->prophesize(RouteManagerInterface::class); - $contentNormalizer = $this->prophesize(ContentNormalizerInterface::class); $metadata = $this->prophesize(StructureMetadata::class); $property = $this->prophesize(PropertyMetadata::class); @@ -716,14 +681,13 @@ public function testMapCustomRoute(): void $localizedDimensionContent->getResourceKey()->willReturn('testKey'); $localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent); - $normalizedData = [ - 'id' => '123', - 'name' => 'testEntity', - ]; - $contentNormalizer->normalize($dimensionContent->reveal())->willReturn([]); - $contentNormalizer->normalize($localizedDimensionContentMock)->willReturn($normalizedData); - $routeGenerator->generate($normalizedData, ['route_schema' => 'custom/{object["name"]}-{object["id"]}']) - ->willReturn('/custom/testEntity-123'); + $routeGenerator->generate( + array_merge($data, [ + '_unlocalizedObject' => $dimensionContent->reveal(), + '_localizedObject' => $localizedDimensionContentMock, + ]), + ['route_schema' => 'custom/{object["_localizedObject"].getName()}-{object["_unlocalizedObject"].getRoutableId()}'] + )->willReturn('/custom/testEntity-123'); $routeManager->createOrUpdateByAttributes( 'Sulu/Test/TestEntity', @@ -736,13 +700,12 @@ public function testMapCustomRoute(): void $factory->reveal(), $routeGenerator->reveal(), $routeManager->reveal(), - $contentNormalizer->reveal(), [], [ 'testKey' => [ 'generator' => 'schema', 'options' => [ - 'route_schema' => 'custom/{object["name"]}-{object["id"]}', + 'route_schema' => 'custom/{object["_localizedObject"].getName()}-{object["_unlocalizedObject"].getRoutableId()}', ], 'resource_key' => 'testKey', 'entityClass' => 'Sulu/Test/TestEntity',