From cf920904ee8b5242a40fbad97eb89131e942f648 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Wed, 24 Mar 2021 10:45:04 +0100 Subject: [PATCH] Add reference store integration for StructureResolver (#78) * Add reference store integration for page resolver and page data provider * Add reference store for serialized snippet and pages * Apply suggestions from code review Co-authored-by: nnatter * Add new structure resolver test * Add reference store integration for page resolver and page data provider * Add reference store for serialized snippet and pages * Apply suggestions from code review Co-authored-by: nnatter * Fix merge conflicts * Update Content/StructureResolver.php Co-authored-by: nnatter --- .../PageSelectionResolver.php | 1 + Content/StructureResolver.php | 31 +- Resources/config/services.xml | 1 + .../PageSelectionResolverTest.php | 20 + .../PageDataProviderResolverTest.php | 21 +- Tests/Unit/Content/StructureResolverTest.php | 519 ++++++++++++------ 6 files changed, 415 insertions(+), 178 deletions(-) diff --git a/Content/ContentTypeResolver/PageSelectionResolver.php b/Content/ContentTypeResolver/PageSelectionResolver.php index d02e448..207ad0a 100644 --- a/Content/ContentTypeResolver/PageSelectionResolver.php +++ b/Content/ContentTypeResolver/PageSelectionResolver.php @@ -75,6 +75,7 @@ public function resolve($data, PropertyInterface $property, string $locale, arra 'properties' => $propertiesParamValue, 'published' => !$this->showDrafts, ]); + list($pagesQuery) = $this->contentQueryBuilder->build($property->getStructure()->getWebspaceKey(), [$locale]); $pageStructures = $this->contentMapper->loadBySql2( diff --git a/Content/StructureResolver.php b/Content/StructureResolver.php index d87732e..4f7d7b7 100644 --- a/Content/StructureResolver.php +++ b/Content/StructureResolver.php @@ -15,6 +15,8 @@ use Sulu\Bundle\DocumentManagerBundle\Bridge\DocumentInspector; use Sulu\Bundle\PageBundle\Document\BasePageDocument; +use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreNotExistsException; +use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStorePoolInterface; use Sulu\Component\Content\Compat\Structure\StructureBridge; use Sulu\Component\Content\Compat\StructureInterface; use Sulu\Component\Content\Compat\StructureManagerInterface; @@ -38,14 +40,21 @@ class StructureResolver implements StructureResolverInterface */ private $documentInspector; + /** + * @var ReferenceStorePoolInterface + */ + private $referenceStorePool; + public function __construct( ContentResolverInterface $contentResolver, StructureManagerInterface $structureManager, - DocumentInspector $documentInspector + DocumentInspector $documentInspector, + ReferenceStorePoolInterface $referenceStorePool ) { $this->contentResolver = $contentResolver; $this->structureManager = $structureManager; $this->documentInspector = $documentInspector; + $this->referenceStorePool = $referenceStorePool; } /** @@ -202,6 +211,8 @@ private function getStructureData(StructureInterface $structure): array } } + $this->addToReferenceStore($structure->getUuid(), $type); + return [ 'id' => $structure->getUuid(), 'type' => $type, @@ -289,4 +300,22 @@ private function resolveProperty( $attributes ); } + + private function addToReferenceStore(string $uuid, string $alias): void + { + if ('page' === $alias) { + // unfortunately the reference store for pages was not adjusted and still uses content as alias + $alias = 'content'; + } + + try { + $referenceStore = $this->referenceStorePool->getStore($alias); + } catch (ReferenceStoreNotExistsException $e) { + // @ignoreException do nothing when reference store was not found + + return; + } + + $referenceStore->add($uuid); + } } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index c3a3337..33b1651 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -21,6 +21,7 @@ + diff --git a/Tests/Unit/Content/ContentTypeResolver/PageSelectionResolverTest.php b/Tests/Unit/Content/ContentTypeResolver/PageSelectionResolverTest.php index ae592c0..89710b3 100644 --- a/Tests/Unit/Content/ContentTypeResolver/PageSelectionResolverTest.php +++ b/Tests/Unit/Content/ContentTypeResolver/PageSelectionResolverTest.php @@ -14,6 +14,7 @@ namespace Sulu\Bundle\HeadlessBundle\Tests\Unit\Content\ContentTypeResolver; use PHPUnit\Framework\TestCase; +use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; use Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\PageSelectionResolver; use Sulu\Bundle\HeadlessBundle\Content\ContentView; @@ -83,6 +84,7 @@ public function testResolve(): void $property->getParams()->willReturn($params); $property->getStructure()->willReturn($structure->reveal()); + // expected and unexpected service calls $this->contentQueryBuilder->init([ 'ids' => ['page-id-1', 'page-id-2'], 'properties' => $params['properties']->getValue(), @@ -158,6 +160,7 @@ public function testResolve(): void ], ])->shouldBeCalledOnce(); + // call test function $result = $this->pageSelectionResolver->resolve( ['page-id-1', 'page-id-2'], $property->reveal(), @@ -206,6 +209,7 @@ public function testResolve(): void ], $result->getContent() ); + $this->assertSame( ['ids' => ['page-id-1', 'page-id-2']], $result->getView() @@ -217,6 +221,14 @@ public function testResolveDataIsNull(): void $locale = 'en'; $property = $this->prophesize(PropertyInterface::class); + // expected and unexpected service calls + $this->contentQueryBuilder->init(Argument::cetera()) + ->shouldNotBeCalled(); + + $this->structureResolver->resolve(Argument::cetera()) + ->shouldNotBeCalled(); + + // call test function $result = $this->pageSelectionResolver->resolve(null, $property->reveal(), $locale); $this->assertSame([], $result->getContent()); @@ -229,6 +241,14 @@ public function testResolveDataIsEmptyArray(): void $locale = 'en'; $property = $this->prophesize(PropertyInterface::class); + // expected and unexpected service calls + $this->contentQueryBuilder->init(Argument::any()) + ->shouldNotBeCalled(); + + $this->structureResolver->resolve(Argument::any()) + ->shouldNotBeCalled(); + + // call test function $result = $this->pageSelectionResolver->resolve([], $property->reveal(), $locale); $this->assertSame([], $result->getContent()); diff --git a/Tests/Unit/Content/DataProviderResolver/PageDataProviderResolverTest.php b/Tests/Unit/Content/DataProviderResolver/PageDataProviderResolverTest.php index 7f3f6fd..8817967 100644 --- a/Tests/Unit/Content/DataProviderResolver/PageDataProviderResolverTest.php +++ b/Tests/Unit/Content/DataProviderResolver/PageDataProviderResolverTest.php @@ -51,7 +51,7 @@ class PageDataProviderResolverTest extends TestCase /** * @var PageDataProviderResolver */ - private $pageResolver; + private $pageDataProviderResolver; protected function setUp(): void { @@ -60,7 +60,7 @@ protected function setUp(): void $this->contentQueryBuilder = $this->prophesize(ContentQueryBuilderInterface::class); $this->contentMapper = $this->prophesize(ContentMapperInterface::class); - $this->pageResolver = new PageDataProviderResolver( + $this->pageDataProviderResolver = new PageDataProviderResolver( $this->pageDataProvider->reveal(), $this->structureResolver->reveal(), $this->contentQueryBuilder->reveal(), @@ -71,7 +71,7 @@ protected function setUp(): void public function testGetDataProvider(): void { - self::assertSame('pages', $this->pageResolver::getDataProvider()); + self::assertSame('pages', $this->pageDataProviderResolver::getDataProvider()); } public function testGetProviderConfiguration(): void @@ -79,7 +79,7 @@ public function testGetProviderConfiguration(): void $configuration = $this->prophesize(ProviderConfigurationInterface::class); $this->pageDataProvider->getConfiguration()->willReturn($configuration->reveal()); - $this->assertSame($configuration->reveal(), $this->pageResolver->getProviderConfiguration()); + $this->assertSame($configuration->reveal(), $this->pageDataProviderResolver->getProviderConfiguration()); } public function testGetProviderDefaultParams(): void @@ -87,7 +87,7 @@ public function testGetProviderDefaultParams(): void $propertyParameter = $this->prophesize(PropertyParameter::class); $this->pageDataProvider->getDefaultPropertyParameter()->willReturn(['test' => $propertyParameter->reveal()]); - $this->assertSame(['test' => $propertyParameter->reveal()], $this->pageResolver->getProviderDefaultParams()); + $this->assertSame(['test' => $propertyParameter->reveal()], $this->pageDataProviderResolver->getProviderDefaultParams()); } public function testResolve(): void @@ -109,6 +109,7 @@ public function testResolve(): void ]), ]; + // expected and unexpected service calls $this->pageDataProvider->resolveResourceItems( ['filter-key' => 'filter-value'], $propertyParameters, @@ -188,7 +189,8 @@ public function testResolve(): void ], ])->shouldBeCalledOnce(); - $result = $this->pageResolver->resolve( + // call test function + $result = $this->pageDataProviderResolver->resolve( ['filter-key' => 'filter-value'], $propertyParameters, ['webspaceKey' => 'webspace-key', 'locale' => 'en'], @@ -250,6 +252,7 @@ public function testResolveEmptyProviderResult(): void ]), ]; + // expected and unexpected service calls $this->pageDataProvider->resolveResourceItems( ['filter-key' => 'filter-value'], $propertyParameters, @@ -257,9 +260,11 @@ public function testResolveEmptyProviderResult(): void 10, 1, 5 - )->willReturn($providerResult->reveal())->shouldBeCalledOnce(); + )->willReturn($providerResult->reveal()) + ->shouldBeCalledOnce(); - $result = $this->pageResolver->resolve( + // call test function + $result = $this->pageDataProviderResolver->resolve( ['filter-key' => 'filter-value'], $propertyParameters, ['webspaceKey' => 'webspace-key', 'locale' => 'en'], diff --git a/Tests/Unit/Content/StructureResolverTest.php b/Tests/Unit/Content/StructureResolverTest.php index d51c3da..75ace2c 100644 --- a/Tests/Unit/Content/StructureResolverTest.php +++ b/Tests/Unit/Content/StructureResolverTest.php @@ -22,6 +22,9 @@ use Sulu\Bundle\HeadlessBundle\Content\StructureResolver; use Sulu\Bundle\PageBundle\Document\HomeDocument; use Sulu\Bundle\PageBundle\Document\PageDocument; +use Sulu\Bundle\SnippetBundle\Document\SnippetDocument; +use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreInterface; +use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStorePoolInterface; use Sulu\Component\Content\Compat\PropertyInterface; use Sulu\Component\Content\Compat\Structure\StructureBridge; use Sulu\Component\Content\Compat\StructureManagerInterface; @@ -29,36 +32,11 @@ class StructureResolverTest extends TestCase { - /** - * @var StructureBridge|ObjectProphecy - */ - private $structure; - /** * @var StructureBridge|ObjectProphecy */ private $excerpt; - /** - * @var PageDocument|ObjectProphecy - */ - private $pageDocument; - - /** - * @var HomeDocument|ObjectProphecy - */ - private $homepageDocument; - - /** - * @var Metadata|ObjectProphecy - */ - private $homepageMetadata; - - /** - * @var Metadata|ObjectProphecy - */ - private $pageMetadata; - /** * @var ContentResolverInterface|ObjectProphecy */ @@ -74,6 +52,11 @@ class StructureResolverTest extends TestCase */ private $documentInspector; + /** + * @var ReferenceStorePoolInterface|ObjectProphecy + */ + private $referenceStorePool; + /** * @var StructureResolver */ @@ -81,10 +64,6 @@ class StructureResolverTest extends TestCase protected function setUp(): void { - $this->structure = $this->prophesize(StructureBridge::class); - $this->pageDocument = $this->prophesize(PageDocument::class); - $this->homepageDocument = $this->prophesize(HomeDocument::class); - $this->contentResolver = $this->prophesize(ContentResolverInterface::class); $this->structureManager = $this->prophesize(StructureManagerInterface::class); $this->documentInspector = $this->prophesize(DocumentInspector::class); @@ -92,20 +71,13 @@ protected function setUp(): void $this->excerpt = $this->prophesizeExcerpt(); $this->structureManager->getStructure('excerpt')->willReturn($this->excerpt->reveal()); - $this->homepageMetadata = $this->prophesize(Metadata::class); - $this->homepageMetadata->getAlias()->willReturn('home'); - $this->documentInspector->getMetadata($this->homepageDocument->reveal()) - ->willReturn($this->homepageMetadata->reveal()); - - $this->pageMetadata = $this->prophesize(Metadata::class); - $this->pageMetadata->getAlias()->willReturn('page'); - $this->documentInspector->getMetadata($this->pageDocument->reveal()) - ->willReturn($this->pageMetadata->reveal()); + $this->referenceStorePool = $this->prophesize(ReferenceStorePoolInterface::class); $this->structureResolver = new StructureResolver( $this->contentResolver->reveal(), $this->structureManager->reveal(), - $this->documentInspector->reveal() + $this->documentInspector->reveal(), + $this->referenceStorePool->reveal() ); } @@ -123,7 +95,7 @@ private function prophesizeExcerpt(): ObjectProphecy $excerpt->getProperties(true)->willReturn([$titleProperty->reveal()]); $excerpt->getProperty('title')->willReturn($titleProperty->reveal()); - $this->contentResolver->resolve(Argument::cetera())->will( + $this->contentResolver->resolve(Argument::any(), $titleProperty->reveal(), Argument::cetera())->will( function ($arguments) { return new ContentView($arguments[0]); } @@ -134,34 +106,46 @@ function ($arguments) { public function testResolvePage(): void { - $this->structure->getDocument()->willReturn($this->pageDocument->reveal()); + $structure = $this->prophesize(StructureBridge::class); + $pageDocument = $this->prophesize(PageDocument::class); + $pageMetadata = $this->prophesize(Metadata::class); + + // expected object calls + $structure->getUuid()->willReturn('123-123-123')->shouldBeCalled(); + $structure->getWebspaceKey()->willReturn('sulu_io')->shouldBeCalled(); $now = new \DateTimeImmutable(); - $this->structure->getUuid()->willReturn('123-123-123'); - $this->structure->getWebspaceKey()->willReturn('sulu_io'); - $this->structure->getLanguageCode()->willReturn('en'); - - $this->pageDocument->getStructureType()->willReturn('default'); - $this->pageDocument->getAuthored()->willReturn($now); - $this->pageDocument->getAuthor()->willReturn(1); - $this->pageDocument->getCreated()->willReturn($now); - $this->pageDocument->getCreator()->willReturn(2); - $this->pageDocument->getChanged()->willReturn($now); - $this->pageDocument->getChanger()->willReturn(3); - $this->pageDocument->getExtensionsData()->willReturn([ - 'seo' => [ - 'title' => 'seo-title', - 'noIndex' => false, - ], - 'excerpt' => [ - 'title' => 'excerpt-title', - 'categories' => [1, 2, 3], - 'tags' => [1, 2, 3], - 'icon' => [1, 2, 3], - 'images' => [1, 2, 3], - ], - ]); + $pageDocument->getStructureType()->willReturn('default')->shouldBeCalled(); + $pageDocument->getAuthored()->willReturn($now)->shouldBeCalled(); + $pageDocument->getAuthor()->willReturn(1)->shouldBeCalled(); + $pageDocument->getCreated()->willReturn($now)->shouldBeCalled(); + $pageDocument->getCreator()->willReturn(2)->shouldBeCalled(); + $pageDocument->getChanged()->willReturn($now)->shouldBeCalled(); + $pageDocument->getChanger()->willReturn(3)->shouldBeCalled(); + $pageDocument->getExtensionsData() + ->willReturn([ + 'seo' => [ + 'title' => 'seo-title', + 'noIndex' => false, + ], + 'excerpt' => [ + 'title' => 'excerpt-title', + 'categories' => [1, 2, 3], + 'tags' => [1, 2, 3], + 'icon' => [1, 2, 3], + 'images' => [1, 2, 3], + ], + ]) + ->shouldBeCalled(); + + $pageMetadata->getAlias() + ->willReturn('page') + ->shouldBeCalled(); + + $structure->getDocument() + ->willReturn($pageDocument->reveal()) + ->shouldBeCalled(); $titleProperty = $this->prophesize(PropertyInterface::class); $titleProperty->getName()->willReturn('title'); @@ -170,30 +154,48 @@ public function testResolvePage(): void $mediaProperty->getName()->willReturn('media'); $mediaProperty->getValue()->willReturn(['ids' => [1, 2, 3]]); - $this->structure->getProperties(true)->willReturn( + $structure->getProperties(true)->willReturn( [ $titleProperty->reveal(), $mediaProperty->reveal(), ] ); - $contentView1 = $this->prophesize(ContentView::class); - $contentView1->getContent()->willReturn('test-123'); - $contentView1->getView()->willReturn([]); + $titleContentView = $this->prophesize(ContentView::class); + $titleContentView->getContent()->willReturn('test-123'); + $titleContentView->getView()->willReturn([]); - $this->contentResolver->resolve('test-123', $titleProperty->reveal(), 'en', ['webspaceKey' => 'sulu_io']) - ->willReturn($contentView1->reveal()); + $mediaContentView = $this->prophesize(ContentView::class); + $mediaContentView->getContent()->willReturn(['media1', 'media2', 'media3']); + $mediaContentView->getView()->willReturn(['ids' => [1, 2, 3]]); + + // expected service calls + $this->documentInspector->getMetadata($pageDocument->reveal()) + ->willReturn($pageMetadata->reveal()) + ->shouldBeCalled(); - $contentView2 = $this->prophesize(ContentView::class); - $contentView2->getContent()->willReturn(['media1', 'media2', 'media3']); - $contentView2->getView()->willReturn(['ids' => [1, 2, 3]]); + $this->contentResolver->resolve('test-123', $titleProperty->reveal(), 'en', ['webspaceKey' => 'sulu_io']) + ->willReturn($titleContentView->reveal()) + ->shouldBeCalled(); $this->contentResolver->resolve( ['ids' => [1, 2, 3]], $mediaProperty->reveal(), 'en', ['webspaceKey' => 'sulu_io'] - )->willReturn($contentView2->reveal()); + )->willReturn($mediaContentView->reveal()) + ->shouldBeCalled(); + + $referenceStore = $this->prophesize(ReferenceStoreInterface::class); + $referenceStore->add('123-123-123') + ->shouldBeCalled(); + + $this->referenceStorePool->getStore('content') + ->willReturn($referenceStore->reveal()) + ->shouldBeCalled(); + + // call test function + $result = $this->structureResolver->resolve($structure->reveal(), 'en'); $this->assertSame( [ @@ -224,61 +226,85 @@ public function testResolvePage(): void ], ], ], - $this->structureResolver->resolve($this->structure->reveal(), 'en') + $result ); } public function testResolveHomepage(): void { - $this->structure->getDocument()->willReturn($this->homepageDocument->reveal()); + $structure = $this->prophesize(StructureBridge::class); + $homepageDocument = $this->prophesize(HomeDocument::class); + $homepageMetadata = $this->prophesize(Metadata::class); + + // expected object calls + $structure->getUuid()->willReturn('123-123-123')->shouldBeCalled(); + $structure->getWebspaceKey()->willReturn('sulu_io')->shouldBeCalled(); + + $structure->getDocument()->willReturn($homepageDocument->reveal())->shouldBeCalled(); $now = new \DateTimeImmutable(); - $this->structure->getUuid()->willReturn('123-123-123'); - $this->structure->getParent()->shouldNotBeCalled(); - $this->structure->getWebspaceKey()->willReturn('sulu_io'); - $this->structure->getLanguageCode()->willReturn('en'); + $homepageDocument->getStructureType()->willReturn('default')->shouldBeCalled(); + $homepageDocument->getAuthored()->willReturn($now)->shouldBeCalled(); + $homepageDocument->getAuthor()->willReturn(1)->shouldBeCalled(); + $homepageDocument->getCreated()->willReturn($now)->shouldBeCalled(); + $homepageDocument->getCreator()->willReturn(2)->shouldBeCalled(); + $homepageDocument->getChanged()->willReturn($now)->shouldBeCalled(); + $homepageDocument->getChanger()->willReturn(3)->shouldBeCalled(); + $homepageDocument->getExtensionsData()->willReturn([])->shouldBeCalled(); - $this->homepageDocument->getStructureType()->willReturn('default'); - $this->homepageDocument->getAuthored()->willReturn($now); - $this->homepageDocument->getAuthor()->willReturn(1); - $this->homepageDocument->getCreated()->willReturn($now); - $this->homepageDocument->getCreator()->willReturn(2); - $this->homepageDocument->getChanged()->willReturn($now); - $this->homepageDocument->getChanger()->willReturn(3); - $this->homepageDocument->getExtensionsData()->willReturn([]); + $homepageMetadata->getAlias()->willReturn('home')->shouldBeCalled(); $titleProperty = $this->prophesize(PropertyInterface::class); - $titleProperty->getName()->willReturn('title'); - $titleProperty->getValue()->willReturn('test-123'); + $titleProperty->getName()->willReturn('title')->shouldBeCalled(); + $titleProperty->getValue()->willReturn('test-123')->shouldBeCalled(); + $mediaProperty = $this->prophesize(PropertyInterface::class); - $mediaProperty->getName()->willReturn('media'); - $mediaProperty->getValue()->willReturn(['ids' => [1, 2, 3]]); + $mediaProperty->getName()->willReturn('media')->shouldBeCalled(); + $mediaProperty->getValue()->willReturn(['ids' => [1, 2, 3]])->shouldBeCalled(); - $this->structure->getProperties(true)->willReturn( + $structure->getProperties(true)->willReturn( [ $titleProperty->reveal(), $mediaProperty->reveal(), ] - ); + )->shouldBeCalled(); - $contentView1 = $this->prophesize(ContentView::class); - $contentView1->getContent()->willReturn('test-123'); - $contentView1->getView()->willReturn([]); + $titleContentView = $this->prophesize(ContentView::class); + $titleContentView->getContent()->willReturn('test-123')->shouldBeCalled(); + $titleContentView->getView()->willReturn([]); - $this->contentResolver->resolve('test-123', $titleProperty->reveal(), 'en', ['webspaceKey' => 'sulu_io']) - ->willReturn($contentView1->reveal()); + $mediaContentView = $this->prophesize(ContentView::class); + $mediaContentView->getContent()->willReturn(['media1', 'media2', 'media3']); + $mediaContentView->getView()->willReturn(['ids' => [1, 2, 3]]); - $contentView2 = $this->prophesize(ContentView::class); - $contentView2->getContent()->willReturn(['media1', 'media2', 'media3']); - $contentView2->getView()->willReturn(['ids' => [1, 2, 3]]); + // expected service calls + $this->documentInspector->getMetadata($homepageDocument->reveal()) + ->willReturn($homepageMetadata->reveal()) + ->shouldBeCalled(); + + $this->contentResolver->resolve('test-123', $titleProperty->reveal(), 'en', ['webspaceKey' => 'sulu_io']) + ->willReturn($titleContentView->reveal()) + ->shouldBeCalled(); $this->contentResolver->resolve( ['ids' => [1, 2, 3]], $mediaProperty->reveal(), 'en', ['webspaceKey' => 'sulu_io'] - )->willReturn($contentView2->reveal()); + )->willReturn($mediaContentView->reveal()) + ->shouldBeCalled(); + + $referenceStore = $this->prophesize(ReferenceStoreInterface::class); + $referenceStore->add('123-123-123') + ->shouldBeCalled(); + + $this->referenceStorePool->getStore('content') + ->willReturn($referenceStore->reveal()) + ->shouldBeCalled(); + + // call test function + $result = $this->structureResolver->resolve($structure->reveal(), 'en'); $this->assertSame( [ @@ -305,58 +331,187 @@ public function testResolveHomepage(): void ], ], ], - $this->structureResolver->resolve($this->structure->reveal(), 'en') + $result ); } - public function testResolveProperties(): void + public function testResolveSnippet(): void { - $this->structure->getDocument()->willReturn($this->pageDocument->reveal()); + $structure = $this->prophesize(StructureBridge::class); + $snippetDocument = $this->prophesize(SnippetDocument::class); + $snippetMetadata = $this->prophesize(Metadata::class); + + // expected object calls + $structure->getUuid()->willReturn('123-123-123')->shouldBeCalled(); + $structure->getWebspaceKey()->willReturn('sulu_io')->shouldBeCalled(); + + $structure->getDocument()->willReturn($snippetDocument->reveal())->shouldBeCalled(); $now = new \DateTimeImmutable(); - $this->structure->getUuid()->willReturn('123-123-123'); - $this->structure->getWebspaceKey()->willReturn('sulu_io'); - $this->structure->getLanguageCode()->willReturn('en'); - - $this->pageDocument->getStructureType()->willReturn('default'); - $this->pageDocument->getAuthored()->willReturn($now); - $this->pageDocument->getAuthor()->willReturn(1); - $this->pageDocument->getCreated()->willReturn($now); - $this->pageDocument->getCreator()->willReturn(2); - $this->pageDocument->getChanged()->willReturn($now); - $this->pageDocument->getChanger()->willReturn(3); - $this->pageDocument->getExtensionsData()->willReturn([ - 'seo' => [ - 'title' => 'seo-title', - 'description' => 'seo-description', - 'noIndex' => false, - ], - 'excerpt' => [ - 'title' => 'excerpt-title', - 'categories' => [1, 2, 3], - 'tags' => [1, 2, 3], - 'icon' => [1, 2, 3], - 'images' => [1, 2, 3], + $snippetDocument->getStructureType()->willReturn('default')->shouldBeCalled(); + $snippetDocument->getCreated()->willReturn($now)->shouldBeCalled(); + $snippetDocument->getCreator()->willReturn(2)->shouldBeCalled(); + $snippetDocument->getChanged()->willReturn($now)->shouldBeCalled(); + $snippetDocument->getChanger()->willReturn(3)->shouldBeCalled(); + $snippetDocument->getExtensionsData()->willReturn([])->shouldBeCalled(); + + $snippetMetadata->getAlias()->willReturn('snippet')->shouldBeCalled(); + + $titleProperty = $this->prophesize(PropertyInterface::class); + $titleProperty->getName()->willReturn('title')->shouldBeCalled(); + $titleProperty->getValue()->willReturn('test-123')->shouldBeCalled(); + + $mediaProperty = $this->prophesize(PropertyInterface::class); + $mediaProperty->getName()->willReturn('media')->shouldBeCalled(); + $mediaProperty->getValue()->willReturn(['ids' => [1, 2, 3]])->shouldBeCalled(); + + $structure->getProperties(true)->willReturn( + [ + $titleProperty->reveal(), + $mediaProperty->reveal(), + ] + )->shouldBeCalled(); + + $titleContentView = $this->prophesize(ContentView::class); + $titleContentView->getContent()->willReturn('test-123'); + $titleContentView->getView()->willReturn([]); + + $mediaContentView = $this->prophesize(ContentView::class); + $mediaContentView->getContent()->willReturn(['media1', 'media2', 'media3']); + $mediaContentView->getView()->willReturn(['ids' => [1, 2, 3]]); + + // expected service calls + $this->documentInspector->getMetadata($snippetDocument->reveal()) + ->willReturn($snippetMetadata->reveal()) + ->shouldBeCalled(); + + $this->contentResolver->resolve('test-123', $titleProperty->reveal(), 'en', ['webspaceKey' => 'sulu_io']) + ->willReturn($titleContentView->reveal()) + ->shouldBeCalled(); + + $this->contentResolver->resolve( + ['ids' => [1, 2, 3]], + $mediaProperty->reveal(), + 'en', + ['webspaceKey' => 'sulu_io'] + )->willReturn($mediaContentView->reveal()) + ->shouldBeCalled(); + + $referenceStore = $this->prophesize(ReferenceStoreInterface::class); + $referenceStore->add('123-123-123') + ->shouldBeCalled(); + + $this->referenceStorePool->getStore('snippet') + ->willReturn($referenceStore->reveal()) + ->shouldBeCalled(); + + // call test function + $result = $this->structureResolver->resolve($structure->reveal(), 'en'); + + $this->assertSame( + [ + 'id' => '123-123-123', + 'type' => 'snippet', + 'template' => 'default', + 'content' => [ + 'title' => 'test-123', + 'media' => ['media1', 'media2', 'media3'], + ], + 'view' => [ + 'title' => [], + 'media' => ['ids' => [1, 2, 3]], + ], + 'author' => null, + 'authored' => null, + 'changer' => 3, + 'changed' => $now->format(\DateTimeImmutable::ISO8601), + 'creator' => 2, + 'created' => $now->format(\DateTimeImmutable::ISO8601), + 'extension' => [ + 'excerpt' => [ + 'title' => null, + ], + ], ], - ]); + $result + ); + } + + public function testResolveProperties(): void + { + $structure = $this->prophesize(StructureBridge::class); + $pageDocument = $this->prophesize(PageDocument::class); + $pageMetadata = $this->prophesize(Metadata::class); + + // expected object calls + $structure->getUuid()->willReturn('123-123-123')->shouldBeCalled(); + $structure->getWebspaceKey()->willReturn('sulu_io')->shouldBeCalled(); + + $now = new \DateTimeImmutable(); + + $pageDocument->getStructureType()->willReturn('default')->shouldBeCalled(); + $pageDocument->getAuthored()->willReturn($now)->shouldBeCalled(); + $pageDocument->getAuthor()->willReturn(1)->shouldBeCalled(); + $pageDocument->getCreated()->willReturn($now)->shouldBeCalled(); + $pageDocument->getCreator()->willReturn(2)->shouldBeCalled(); + $pageDocument->getChanged()->willReturn($now)->shouldBeCalled(); + $pageDocument->getChanger()->willReturn(3)->shouldBeCalled(); + $pageDocument->getExtensionsData() + ->willReturn([ + 'seo' => [ + 'title' => 'seo-title', + 'description' => 'seo-description', + 'noIndex' => false, + ], + 'excerpt' => [ + 'title' => 'excerpt-title', + 'categories' => [1, 2, 3], + 'tags' => [1, 2, 3], + 'icon' => [1, 2, 3], + 'images' => [1, 2, 3], + ], + ]) + ->shouldBeCalled(); + + $pageMetadata->getAlias() + ->willReturn('page') + ->shouldBeCalled(); + + $structure->getDocument() + ->willReturn($pageDocument->reveal()) + ->shouldBeCalled(); $titleProperty = $this->prophesize(PropertyInterface::class); $titleProperty->getName()->willReturn('title'); $titleProperty->getValue()->willReturn('test-123'); $titleProperty->setValue('test-123')->shouldBeCalled(); - $this->structure->getProperty('title')->willReturn($titleProperty->reveal()); + $structure->getProperty('title')->willReturn($titleProperty->reveal()); - $contentView1 = $this->prophesize(ContentView::class); - $contentView1->getContent()->willReturn('test-123'); - $contentView1->getView()->willReturn([]); + $titleContentView = $this->prophesize(ContentView::class); + $titleContentView->getContent()->willReturn('test-123'); + $titleContentView->getView()->willReturn([]); + + // expected service calls + $this->documentInspector->getMetadata($pageDocument->reveal()) + ->willReturn($pageMetadata->reveal()) + ->shouldBeCalled(); $this->contentResolver->resolve('test-123', $titleProperty->reveal(), 'en', ['webspaceKey' => 'sulu_io']) - ->willReturn($contentView1->reveal()); + ->willReturn($titleContentView->reveal()); + + $referenceStore = $this->prophesize(ReferenceStoreInterface::class); + $referenceStore->add('123-123-123') + ->shouldBeCalled(); + $this->referenceStorePool->getStore('content') + ->willReturn($referenceStore->reveal()) + ->shouldBeCalled(); + + // call test function $result = $this->structureResolver->resolveProperties( - $this->structure->reveal(), + $structure->reveal(), ['myTitle' => 'title', 'seoDescription' => 'seo.description', 'excerptTitle' => 'excerpt.title'], 'en' ); @@ -389,52 +544,78 @@ public function testResolveProperties(): void public function testResolvePropertiesIncludeExtension(): void { - $this->structure->getDocument()->willReturn($this->pageDocument->reveal()); + $structure = $this->prophesize(StructureBridge::class); + $pageDocument = $this->prophesize(PageDocument::class); + $pageMetadata = $this->prophesize(Metadata::class); + + // expected object calls + $structure->getUuid()->willReturn('123-123-123')->shouldBeCalled(); + $structure->getWebspaceKey()->willReturn('sulu_io')->shouldBeCalled(); $now = new \DateTimeImmutable(); - $this->structure->getUuid()->willReturn('123-123-123'); - $this->structure->getWebspaceKey()->willReturn('sulu_io'); - $this->structure->getLanguageCode()->willReturn('en'); - - $this->pageDocument->getStructureType()->willReturn('default'); - $this->pageDocument->getAuthored()->willReturn($now); - $this->pageDocument->getAuthor()->willReturn(1); - $this->pageDocument->getCreated()->willReturn($now); - $this->pageDocument->getCreator()->willReturn(2); - $this->pageDocument->getChanged()->willReturn($now); - $this->pageDocument->getChanger()->willReturn(3); - $this->pageDocument->getExtensionsData()->willReturn([ - 'seo' => [ - 'title' => 'seo-title', - 'description' => 'seo-description', - 'noIndex' => false, - ], - 'excerpt' => [ - 'title' => 'excerpt-title', - 'categories' => [1, 2, 3], - 'tags' => [1, 2, 3], - 'icon' => [1, 2, 3], - 'images' => [1, 2, 3], - ], - ]); + $pageDocument->getStructureType()->willReturn('default')->shouldBeCalled(); + $pageDocument->getAuthored()->willReturn($now)->shouldBeCalled(); + $pageDocument->getAuthor()->willReturn(1)->shouldBeCalled(); + $pageDocument->getCreated()->willReturn($now)->shouldBeCalled(); + $pageDocument->getCreator()->willReturn(2)->shouldBeCalled(); + $pageDocument->getChanged()->willReturn($now)->shouldBeCalled(); + $pageDocument->getChanger()->willReturn(3)->shouldBeCalled(); + $pageDocument->getExtensionsData() + ->willReturn([ + 'seo' => [ + 'title' => 'seo-title', + 'description' => 'seo-description', + 'noIndex' => false, + ], + 'excerpt' => [ + 'title' => 'excerpt-title', + 'categories' => [1, 2, 3], + 'tags' => [1, 2, 3], + 'icon' => [1, 2, 3], + 'images' => [1, 2, 3], + ], + ]) + ->shouldBeCalled(); + + $pageMetadata->getAlias() + ->willReturn('page') + ->shouldBeCalled(); + + $structure->getDocument() + ->willReturn($pageDocument->reveal()) + ->shouldBeCalled(); $titleProperty = $this->prophesize(PropertyInterface::class); $titleProperty->getName()->willReturn('title'); $titleProperty->getValue()->willReturn('test-123'); $titleProperty->setValue('test-123')->shouldBeCalled(); - $this->structure->getProperty('title')->willReturn($titleProperty->reveal()); + $structure->getProperty('title')->willReturn($titleProperty->reveal()); - $contentView1 = $this->prophesize(ContentView::class); - $contentView1->getContent()->willReturn('test-123'); - $contentView1->getView()->willReturn([]); + $titleContentView = $this->prophesize(ContentView::class); + $titleContentView->getContent()->willReturn('test-123'); + $titleContentView->getView()->willReturn([]); + + // expected service calls + $this->documentInspector->getMetadata($pageDocument->reveal()) + ->willReturn($pageMetadata->reveal()) + ->shouldBeCalled(); $this->contentResolver->resolve('test-123', $titleProperty->reveal(), 'en', ['webspaceKey' => 'sulu_io']) - ->willReturn($contentView1->reveal()); + ->willReturn($titleContentView->reveal()); + + $referenceStore = $this->prophesize(ReferenceStoreInterface::class); + $referenceStore->add('123-123-123') + ->shouldBeCalled(); + + $this->referenceStorePool->getStore('content') + ->willReturn($referenceStore->reveal()) + ->shouldBeCalled(); + // call test function $result = $this->structureResolver->resolveProperties( - $this->structure->reveal(), + $structure->reveal(), ['myTitle' => 'title'], 'en', true