diff --git a/src/Sulu/Bundle/CategoryBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/CategorySelectionPropertyResolverTest.php b/src/Sulu/Bundle/CategoryBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/CategorySelectionPropertyResolverTest.php index d8c58b70277..589d0e647cf 100644 --- a/src/Sulu/Bundle/CategoryBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/CategorySelectionPropertyResolverTest.php +++ b/src/Sulu/Bundle/CategoryBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/CategorySelectionPropertyResolverTest.php @@ -9,22 +9,22 @@ * with this source code in the file LICENSE. */ -namespace Sulu\Bundle\CategoryBundle\Tests\Unit\Infrastructure\Sulu\Content\PropertyResolver; +namespace Sulu\Bundle\MedaiBundle\Tests\Unit\Infrastructure\Sulu\Content\PropertyResolver; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -use Sulu\Bundle\CategoryBundle\Infrastructure\Sulu\Content\PropertyResolver\CategorySelectionPropertyResolver; +use Sulu\Bundle\MedaiBundle\Infrastructure\Sulu\Content\PropertyResolver\CollectionSelectionPropertyResolver; use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource; -#[CoversClass(CategorySelectionPropertyResolver::class)] -class CategorySelectionPropertyResolverTest extends TestCase +#[CoversClass(CollectionSelectionPropertyResolver::class)] +class CollectionSelectionPropertyResolverTest extends TestCase { - private CategorySelectionPropertyResolver $resolver; + private CollectionSelectionPropertyResolver $resolver; public function setUp(): void { - $this->resolver = new CategorySelectionPropertyResolver(); + $this->resolver = new CollectionSelectionPropertyResolver(); } public function testResolveEmpty(): void diff --git a/src/Sulu/Bundle/MediaBundle/Infrastructure/Sulu/Content/PropertyResolver/CollectionSelectionPropertyResolver.php b/src/Sulu/Bundle/MediaBundle/Infrastructure/Sulu/Content/PropertyResolver/CollectionSelectionPropertyResolver.php new file mode 100644 index 00000000000..0e53347c263 --- /dev/null +++ b/src/Sulu/Bundle/MediaBundle/Infrastructure/Sulu/Content/PropertyResolver/CollectionSelectionPropertyResolver.php @@ -0,0 +1,50 @@ + [], ...$params]); + } + + /** @var string $resourceLoaderKey */ + $resourceLoaderKey = $params['resourceLoader'] ?? CollectionResourceLoader::getKey(); + + return ContentView::createResolvables( + $data, + $resourceLoaderKey, + ['ids' => $data, ...$params], + ); + } + + public static function getType(): string + { + return 'collection_selection'; + } +} diff --git a/src/Sulu/Bundle/MediaBundle/Infrastructure/Sulu/Content/PropertyResolver/SingleCollectionSelectionPropertyResolver.php b/src/Sulu/Bundle/MediaBundle/Infrastructure/Sulu/Content/PropertyResolver/SingleCollectionSelectionPropertyResolver.php new file mode 100644 index 00000000000..20c832ea19c --- /dev/null +++ b/src/Sulu/Bundle/MediaBundle/Infrastructure/Sulu/Content/PropertyResolver/SingleCollectionSelectionPropertyResolver.php @@ -0,0 +1,50 @@ + null, ...$params]); + } + + /** @var string $resourceLoaderKey */ + $resourceLoaderKey = $params['resourceLoader'] ?? CollectionResourceLoader::getKey(); + + return ContentView::createResolvable( + (int) $data, + $resourceLoaderKey, + [ + 'id' => $data, + ...$params, + ], + ); + } + + public static function getType(): string + { + return 'single_collection_selection'; + } +} diff --git a/src/Sulu/Bundle/MediaBundle/Infrastructure/Sulu/Content/ResourceLoader/CollectionResourceLoader.php b/src/Sulu/Bundle/MediaBundle/Infrastructure/Sulu/Content/ResourceLoader/CollectionResourceLoader.php new file mode 100644 index 00000000000..62471f3545c --- /dev/null +++ b/src/Sulu/Bundle/MediaBundle/Infrastructure/Sulu/Content/ResourceLoader/CollectionResourceLoader.php @@ -0,0 +1,53 @@ +collectionManager->getById($id, $locale); // TODO load all over one query + $mappedResult[$collection->getId()] = $collection; + } catch (CollectionNotFoundException $e) { + // @ignoreException: do not crash page if selected collection is deleted + } + } + + return $mappedResult; + } + + public static function getKey(): string + { + return self::RESOURCE_LOADER_KEY; + } +} diff --git a/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/CollectionSelectionPropertyResolverTest.php b/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/CollectionSelectionPropertyResolverTest.php new file mode 100644 index 00000000000..caa756b734f --- /dev/null +++ b/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/CollectionSelectionPropertyResolverTest.php @@ -0,0 +1,114 @@ +resolver = new CollectionSelectionPropertyResolver(); + } + + public function testResolveEmpty(): void + { + $contentView = $this->resolver->resolve([], 'en'); + + $this->assertSame([], $contentView->getContent()); + $this->assertSame(['ids' => []], $contentView->getView()); + } + + public function testResolveParams(): void + { + $contentView = $this->resolver->resolve([], 'en', ['custom' => 'params']); + + $this->assertSame([], $contentView->getContent()); + $this->assertSame([ + 'ids' => [], + 'custom' => 'params', + ], $contentView->getView()); + } + + #[DataProvider('provideUnresolvableData')] + public function testResolveUnresolvableData(mixed $data): void + { + $contentView = $this->resolver->resolve($data, 'en'); + + $this->assertSame([], $contentView->getContent()); + $this->assertSame(['ids' => []], $contentView->getView()); + } + + /** + * @return iterable + */ + public static function provideUnresolvableData(): iterable + { + yield 'null' => [null]; + yield 'smart_content' => [['source' => '123']]; + yield 'single_value' => [1]; + yield 'object' => [(object) [1, 2]]; + } + + /** + * @param array $data + */ + #[DataProvider('provideResolvableData')] + public function testResolveResolvableData(array $data): void + { + $contentView = $this->resolver->resolve($data, 'en'); + + $content = $contentView->getContent(); + $this->assertIsArray($content); + foreach ($data as $key => $value) { + $resolvable = $content[$key] ?? null; + $this->assertInstanceOf(ResolvableResource::class, $resolvable); + $this->assertSame($value, $resolvable->getId()); + $this->assertSame('collection', $resolvable->getResourceLoaderKey()); + } + + $this->assertSame(['ids' => $data], $contentView->getView()); + } + + /** + * @return iterable, + * }> + */ + public static function provideResolvableData(): iterable + { + yield 'empty' => [[]]; + yield 'int_list' => [[1, 2]]; + yield 'string_list' => [['1', '2']]; + } + + public function testCustomResourceLoader(): void + { + $contentView = $this->resolver->resolve([1], 'en', ['resourceLoader' => 'custom_collection']); + + $content = $contentView->getContent(); + $this->assertIsArray($content); + $resolvable = $content[0] ?? null; + $this->assertInstanceOf(ResolvableResource::class, $resolvable); + $this->assertSame(1, $resolvable->getId()); + $this->assertSame('custom_collection', $resolvable->getResourceLoaderKey()); + } +} diff --git a/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/ImageMapPropertyResolverTest.php b/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/ImageMapPropertyResolverTest.php index e5df02f4493..8f8d3b06429 100644 --- a/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/ImageMapPropertyResolverTest.php +++ b/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/ImageMapPropertyResolverTest.php @@ -150,7 +150,9 @@ public static function provideResolvableData(): iterable { yield 'empty' => [[]]; yield 'int_id' => [['imageId' => 1]]; - yield 'int_id_with_hotspots' => [['imageId' => 1, 'hotspots' => [['type' => 'text', 'hotspot' => ['type' => 'circle'], 'title' => 'Title 1'], ['type' => 'text', 'hotspot' => ['type' => 'circle'], 'title' => 'Title 2']]]]; + yield 'int_id_with_hotspots' => [ + ['imageId' => 1, 'hotspots' => [['type' => 'text', 'hotspot' => ['type' => 'circle'], 'title' => 'Title 1'], ['type' => 'text', 'hotspot' => ['type' => 'circle'], 'title' => 'Title 2']]], + ]; yield 'string_id' => [['imageId' => '1']]; yield 'string_id_with_hotspots' => [['imageId' => '1', 'hotspots' => [['type' => 'text', 'hotspot' => ['type' => 'circle'], 'title' => 'Title 1'], ['type' => 'text', 'hotspot' => ['type' => 'circle'], 'title' => 'Title 2']]]]; } diff --git a/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/SingleCollectionSelectionPropertyResolverTest.php b/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/SingleCollectionSelectionPropertyResolverTest.php new file mode 100644 index 00000000000..78f6dffb1a5 --- /dev/null +++ b/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/PropertyResolver/SingleCollectionSelectionPropertyResolverTest.php @@ -0,0 +1,105 @@ +resolver = new SingleCollectionSelectionPropertyResolver(); + } + + public function testResolveEmpty(): void + { + $contentView = $this->resolver->resolve(null, 'en'); + + $this->assertNull($contentView->getContent()); + $this->assertSame(['id' => null], $contentView->getView()); + } + + public function testResolveParams(): void + { + $contentView = $this->resolver->resolve(null, 'en', ['custom' => 'params']); + + $this->assertNull($contentView->getContent()); + $this->assertSame([ + 'id' => null, + 'custom' => 'params', + ], $contentView->getView()); + } + + #[DataProvider('provideUnresolvableData')] + public function testResolveUnresolvableData(mixed $data): void + { + $contentView = $this->resolver->resolve($data, 'en'); + + $this->assertNull($contentView->getContent()); + $this->assertSame(['id' => null], $contentView->getView()); + } + + /** + * @return iterable + */ + public static function provideUnresolvableData(): iterable + { + yield 'null' => [null]; + yield 'smart_content' => [['source' => '123']]; + yield 'multi_value' => [[1]]; + yield 'object' => [(object) [1]]; + } + + #[DataProvider('provideResolvableData')] + public function testResolveResolvableData(int|string $data): void + { + $contentView = $this->resolver->resolve($data, 'en'); + + $content = $contentView->getContent(); + $this->assertInstanceOf(ResolvableResource::class, $content); + $this->assertSame((int) $data, $content->getId()); + $this->assertSame('collection', $content->getResourceLoaderKey()); + + $this->assertSame(['id' => $data], $contentView->getView()); + } + + /** + * @return iterable + */ + public static function provideResolvableData(): iterable + { + yield 'int' => [1]; + yield 'string' => ['2']; + } + + public function testCustomResourceLoader(): void + { + $contentView = $this->resolver->resolve(1, 'en', ['resourceLoader' => 'custom_collection']); + + $content = $contentView->getContent(); + + $this->assertInstanceOf(ResolvableResource::class, $content); + $this->assertSame(1, $content->getId()); + $this->assertSame('custom_collection', $content->getResourceLoaderKey()); + } +} diff --git a/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/ResourceLoader/CollectionResourceLoaderTest.php b/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/ResourceLoader/CollectionResourceLoaderTest.php new file mode 100644 index 00000000000..d705d047479 --- /dev/null +++ b/src/Sulu/Bundle/MediaBundle/Tests/Unit/Infrastructure/Sulu/Content/ResourceLoader/CollectionResourceLoaderTest.php @@ -0,0 +1,72 @@ + + */ + private ObjectProphecy $collectionManager; + + private CollectionResourceLoader $loader; + + public function setUp(): void + { + $this->collectionManager = $this->prophesize(CollectionManagerInterface::class); + $this->loader = new CollectionResourceLoader($this->collectionManager->reveal()); + } + + public function testGetKey(): void + { + $this->assertSame('collection', $this->loader::getKey()); + } + + public function testLoad(): void + { + $collection1 = $this->createCollection(1); + $collection3 = $this->createCollection(3); + + $this->collectionManager->getById(1, 'en')->willReturn($collection1) + ->shouldBeCalled(); + + $this->collectionManager->getById(3, 'en')->willReturn($collection3) + ->shouldBeCalled(); + + $result = $this->loader->load([1, 3], 'en', []); + + $this->assertSame([ + 1 => $collection1, + 3 => $collection3, + ], $result); + } + + private static function createCollection(int $id): ApiCollection + { + $collection = new Collection(); + static::setPrivateProperty($collection, 'id', $id); + + return new ApiCollection($collection, 'en'); + } +}