Skip to content

Commit

Permalink
Disable loading ghost content in the ArticleReferenceRefresher (#681)
Browse files Browse the repository at this point in the history
  • Loading branch information
Prokyonn authored Jun 20, 2024
1 parent 797253f commit 43ea998
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Document/ArticleDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ArticleDocument implements UuidBehavior,
protected $originalLocale;

/**
* @var string
* @var string|null
*/
protected $structureType;

Expand Down
2 changes: 1 addition & 1 deletion Document/ArticlePageDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ArticlePageDocument implements UuidBehavior,
protected $originalLocale;

/**
* @var string
* @var string|null
*/
protected $structureType;

Expand Down
4 changes: 2 additions & 2 deletions Document/Subscriber/RoutableSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private function reallocateExistingRoute(RoutablePageBehavior $document, string
private function updateRoute(RoutablePageBehavior $document): void
{
$locale = $this->documentInspector->getLocale($document);
$propertyName = $this->getRoutePathPropertyName($document->getStructureType(), $locale);
$propertyName = $this->getRoutePathPropertyName((string) $document->getStructureType(), $locale);

$route = $this->chainRouteGenerator->generate($document);
$document->setRoutePath($route->getPath());
Expand Down Expand Up @@ -340,7 +340,7 @@ private function generateChildRoutes(ChildrenBehavior $document, string $locale)
$child->setRoutePath($childRoute->getPath());
$childNode = $this->documentInspector->getNode($child);

$propertyName = $this->getRoutePathPropertyName($child->getStructureType(), $locale);
$propertyName = $this->getRoutePathPropertyName((string) $child->getStructureType(), $locale);
$childNode->setProperty($propertyName, $childRoute->getPath());

$routes[] = $childRoute->getPath();
Expand Down
8 changes: 7 additions & 1 deletion Reference/Refresh/ArticleReferenceRefresher.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ public function refresh(): \Generator
/** @var string $uuid */
$uuid = $row->getValue('jcr:uuid');
/** @var (UuidBehavior&TitleBehavior&StructureBehavior)|null $document */
$document = $this->documentManager->find($uuid, $locale);
$document = $this->documentManager->find(
$uuid,
$locale,
[
'load_ghost_content' => false,
]
);

if (!$document) {
continue;
Expand Down
146 changes: 146 additions & 0 deletions Tests/Functional/Reference/Provider/ArticleReferenceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Tests\Functional\Reference\Provider;

use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Reference\Provider\ArticleReferenceProvider;
use Sulu\Bundle\MediaBundle\Entity\Collection;
use Sulu\Bundle\MediaBundle\Entity\CollectionType;
use Sulu\Bundle\MediaBundle\Entity\Media;
use Sulu\Bundle\MediaBundle\Entity\MediaType;
use Sulu\Bundle\ReferenceBundle\Application\Refresh\ReferenceRefresherInterface;
use Sulu\Bundle\ReferenceBundle\Domain\Model\Reference;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\HttpKernel\SuluKernel;
use Sulu\Component\Persistence\Repository\ORM\EntityRepository;

class ArticleReferenceProviderTest extends SuluTestCase
{
/**
* @var ArticleReferenceProvider
*/
private $articleReferenceProvider;

/**
* @var DocumentManagerInterface
*/
private $documentManager;

/**
* @var EntityRepository<Reference>
*/
private $referenceRepository;

public function setUp(): void
{
$this->purgeDatabase();
$this->initPhpcr();

if (!\interface_exists(ReferenceRefresherInterface::class)) {
return;
}

$this->articleReferenceProvider = $this->getContainer()->get('sulu_article.reference_provider');
$this->documentManager = $this->getContainer()->get('sulu_document_manager.document_manager');
$this->referenceRepository = $this->getContainer()->get('sulu.repository.reference');
}

public function testUpdateReferences(): void
{
if (!\interface_exists(ReferenceRefresherInterface::class)) {
$this->markTestSkipped('References did not exist in Sulu <2.6.');
}

$media = $this->createMedia();
/** @var \Sulu\Bundle\ArticleBundle\Tests\TestExtendBundle\Document\ArticleDocument $article */
$article = $this->documentManager->create('article');
$article->setTitle('Example article');
$article->setStructureType('default_image');
$article->getStructure()->bind(['image' => ['id' => $media->getId()]]);
$this->documentManager->persist($article, 'en');
$this->documentManager->publish($article, 'en');
$this->documentManager->flush();

$this->articleReferenceProvider->updateReferences($article, 'en', 'test');
$this->getEntityManager()->flush();

/** @var Reference[] $references */
$references = $this->referenceRepository->findBy(['referenceContext' => 'test']);
$this->assertCount(1, $references);
self::assertSame((string) $media->getId(), $references[0]->getResourceId());
}

public function testUpdateUnpublishedReferences(): void
{
if (!\interface_exists(ReferenceRefresherInterface::class)) {
$this->markTestSkipped('References did not exist in Sulu <2.6.');
}

$media = $this->createMedia();
/** @var \Sulu\Bundle\ArticleBundle\Tests\TestExtendBundle\Document\ArticleDocument $article */
$article = $this->documentManager->create('article');
$article->setTitle('Example article');
$article->setStructureType('default_image');
$article->getStructure()->bind(['image' => ['id' => $media->getId()]]);
$this->documentManager->persist($article, 'en');
$this->documentManager->publish($article, 'en');
$this->documentManager->flush();

$this->documentManager->unpublish($article, 'en');
$this->documentManager->flush();
$this->documentManager->clear();

static::ensureKernelShutdown();
static::bootKernel(['sulu.context' => SuluKernel::CONTEXT_WEBSITE]);
// refresh services from new kernel
$this->articleReferenceProvider = $this->getContainer()->get('sulu_article.reference_provider');
$this->documentManager = $this->getContainer()->get('sulu_document_manager.document_manager');
$this->referenceRepository = $this->getContainer()->get('sulu.repository.reference');

/** @var ArticleDocument $article */
$article = $this->documentManager->find($article->getUuid(), 'en', [
'load_ghost_content' => false,
]);

$this->articleReferenceProvider->updateReferences($article, 'en', 'test');
$this->getEntityManager()->flush();

$references = $this->referenceRepository->findBy(['referenceContext' => 'test']);
$this->assertCount(0, $references);
}

private function createMedia(): Media
{
$collectionType = new CollectionType();
$collectionType->setName('Default Collection Type');
$collectionType->setDescription('Default Collection Type');

$mediaType = new MediaType();
$mediaType->setName('Default Media Type');

$collection = new Collection();
$collection->setType($collectionType);

$media = new Media();
$media->setType($mediaType);
$media->setCollection($collection);

$this->getEntityManager()->persist($collection);
$this->getEntityManager()->persist($collectionType);
$this->getEntityManager()->persist($mediaType);
$this->getEntityManager()->persist($media);
$this->getEntityManager()->flush();

return $media;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function setUp(): void
if (!\interface_exists(ReferenceRefresherInterface::class)) {
return;
}

$this->articleReferenceRefresher = $this->getContainer()->get('sulu_article.article_reference_refresher');
$this->documentManager = $this->getContainer()->get('sulu_document_manager.document_manager');
$this->referenceRepository = $this->getContainer()->get('sulu.repository.reference');
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1660,11 +1660,6 @@ parameters:
count: 1
path: Document/Subscriber/ArticleSubscriber.php

-
message: "#^Parameter \\#1 \\$structureType of method Sulu\\\\Bundle\\\\ArticleBundle\\\\Document\\\\ArticlePageDocument\\:\\:setStructureType\\(\\) expects string, string\\|null given\\.$#"
count: 1
path: Document/Subscriber/ArticleSubscriber.php

-
message: "#^Parameter \\#3 \\$originalPages of method Sulu\\\\Bundle\\\\ArticleBundle\\\\Document\\\\Subscriber\\\\ArticleSubscriber\\:\\:loadPageDataForShadow\\(\\) expects array, mixed given\\.$#"
count: 1
Expand Down

0 comments on commit 43ea998

Please sign in to comment.