Skip to content

Commit

Permalink
Add reference store integration for StructureResolver (#78)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>

* Fix merge conflicts

* Update Content/StructureResolver.php

Co-authored-by: nnatter <[email protected]>
  • Loading branch information
alexander-schranz and niklasnatter authored Mar 24, 2021
1 parent 259c735 commit cf92090
Show file tree
Hide file tree
Showing 6 changed files with 415 additions and 178 deletions.
1 change: 1 addition & 0 deletions Content/ContentTypeResolver/PageSelectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
31 changes: 30 additions & 1 deletion Content/StructureResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -202,6 +211,8 @@ private function getStructureData(StructureInterface $structure): array
}
}

$this->addToReferenceStore($structure->getUuid(), $type);

return [
'id' => $structure->getUuid(),
'type' => $type,
Expand Down Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<argument type="service" id="sulu_headless.content_resolver"/>
<argument type="service" id="sulu.content.structure_manager"/>
<argument type="service" id="sulu_document_manager.document_inspector"/>
<argument type="service" id="sulu_website.reference_store_pool"/>
</service>
<service id="Sulu\Bundle\HeadlessBundle\Content\StructureResolverInterface" alias="sulu_headless.structure_resolver"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -158,6 +160,7 @@ public function testResolve(): void
],
])->shouldBeCalledOnce();

// call test function
$result = $this->pageSelectionResolver->resolve(
['page-id-1', 'page-id-2'],
$property->reveal(),
Expand Down Expand Up @@ -206,6 +209,7 @@ public function testResolve(): void
],
$result->getContent()
);

$this->assertSame(
['ids' => ['page-id-1', 'page-id-2']],
$result->getView()
Expand All @@ -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());
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class PageDataProviderResolverTest extends TestCase
/**
* @var PageDataProviderResolver
*/
private $pageResolver;
private $pageDataProviderResolver;

protected function setUp(): void
{
Expand All @@ -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(),
Expand All @@ -71,23 +71,23 @@ protected function setUp(): void

public function testGetDataProvider(): void
{
self::assertSame('pages', $this->pageResolver::getDataProvider());
self::assertSame('pages', $this->pageDataProviderResolver::getDataProvider());
}

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
{
$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
Expand All @@ -109,6 +109,7 @@ public function testResolve(): void
]),
];

// expected and unexpected service calls
$this->pageDataProvider->resolveResourceItems(
['filter-key' => 'filter-value'],
$propertyParameters,
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -250,16 +252,19 @@ public function testResolveEmptyProviderResult(): void
]),
];

// expected and unexpected service calls
$this->pageDataProvider->resolveResourceItems(
['filter-key' => 'filter-value'],
$propertyParameters,
['webspaceKey' => 'webspace-key', 'locale' => 'en'],
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'],
Expand Down
Loading

0 comments on commit cf92090

Please sign in to comment.