From d7e41d39e2e99e3f0db419c92fc7b81b0365e457 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Wed, 19 Jun 2024 13:29:05 +0200 Subject: [PATCH 1/3] Disable ghost content --- Reference/Refresh/ArticleReferenceRefresher.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Reference/Refresh/ArticleReferenceRefresher.php b/Reference/Refresh/ArticleReferenceRefresher.php index 434cc741..b2266c1e 100644 --- a/Reference/Refresh/ArticleReferenceRefresher.php +++ b/Reference/Refresh/ArticleReferenceRefresher.php @@ -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; From 2b9e83fbd84811fb6aadd7eb63afaaa517a5d203 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Thu, 20 Jun 2024 13:07:42 +0200 Subject: [PATCH 2/3] Add test for ArticleReferenceProvider --- Document/ArticleDocument.php | 2 +- Document/ArticlePageDocument.php | 2 +- Document/Subscriber/RoutableSubscriber.php | 4 +- Tests/Application/.env | 2 +- .../Provider/ArticleReferenceProviderTest.php | 139 ++++++++++++++++++ phpstan-baseline.neon | 5 - 6 files changed, 144 insertions(+), 10 deletions(-) create mode 100644 Tests/Functional/Reference/Provider/ArticleReferenceProviderTest.php diff --git a/Document/ArticleDocument.php b/Document/ArticleDocument.php index 068091ec..a6658df4 100644 --- a/Document/ArticleDocument.php +++ b/Document/ArticleDocument.php @@ -119,7 +119,7 @@ class ArticleDocument implements UuidBehavior, protected $originalLocale; /** - * @var string + * @var string|null */ protected $structureType; diff --git a/Document/ArticlePageDocument.php b/Document/ArticlePageDocument.php index d923aaa1..78f035e9 100644 --- a/Document/ArticlePageDocument.php +++ b/Document/ArticlePageDocument.php @@ -76,7 +76,7 @@ class ArticlePageDocument implements UuidBehavior, protected $originalLocale; /** - * @var string + * @var string|null */ protected $structureType; diff --git a/Document/Subscriber/RoutableSubscriber.php b/Document/Subscriber/RoutableSubscriber.php index 0ee552e5..36cde909 100644 --- a/Document/Subscriber/RoutableSubscriber.php +++ b/Document/Subscriber/RoutableSubscriber.php @@ -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()); @@ -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(); diff --git a/Tests/Application/.env b/Tests/Application/.env index 25b858f4..e0465258 100644 --- a/Tests/Application/.env +++ b/Tests/Application/.env @@ -1,5 +1,5 @@ APP_ENV=test -DATABASE_URL=mysql://root:@127.0.0.1:3306/sulu_test?serverVersion=5.7 +DATABASE_URL=mysql://root:root@127.0.0.1:3306/sulu_article_test?serverVersion=5.7 DATABASE_CHARSET=utf8mb4 DATABASE_COLLATE=utf8mb4_unicode_ci ELASTICSEARCH_HOST=127.0.0.1:9200 diff --git a/Tests/Functional/Reference/Provider/ArticleReferenceProviderTest.php b/Tests/Functional/Reference/Provider/ArticleReferenceProviderTest.php new file mode 100644 index 00000000..844b04f9 --- /dev/null +++ b/Tests/Functional/Reference/Provider/ArticleReferenceProviderTest.php @@ -0,0 +1,139 @@ + + */ + private EntityRepository $referenceRepository; + + public function setUp(): void + { + $this->purgeDatabase(); + $this->initPhpcr(); + + $this->articleReferenceProvider = $this->getContainer()->get('sulu_article.reference_provider'); + $this->documentManager = $this->getContainer()->get('sulu_document_manager.document_manager'); + $this->sessionManager = $this->getContainer()->get('sulu.phpcr.session'); + $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->sessionManager = $this->getContainer()->get('sulu.phpcr.session'); + $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; + } +} diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 515a4424..7c51ed89 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 From 8e28b06bd77fa183be4bd5ca19e8a3aa800fec41 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Thu, 20 Jun 2024 13:12:09 +0200 Subject: [PATCH 3/3] Fix backward compatibility --- Tests/Application/.env | 2 +- .../Provider/ArticleReferenceProviderTest.php | 21 ++++++++++++------- .../Refresh/ArticleReferenceRefresherTest.php | 1 + 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Tests/Application/.env b/Tests/Application/.env index e0465258..25b858f4 100644 --- a/Tests/Application/.env +++ b/Tests/Application/.env @@ -1,5 +1,5 @@ APP_ENV=test -DATABASE_URL=mysql://root:root@127.0.0.1:3306/sulu_article_test?serverVersion=5.7 +DATABASE_URL=mysql://root:@127.0.0.1:3306/sulu_test?serverVersion=5.7 DATABASE_CHARSET=utf8mb4 DATABASE_COLLATE=utf8mb4_unicode_ci ELASTICSEARCH_HOST=127.0.0.1:9200 diff --git a/Tests/Functional/Reference/Provider/ArticleReferenceProviderTest.php b/Tests/Functional/Reference/Provider/ArticleReferenceProviderTest.php index 844b04f9..7cc523b0 100644 --- a/Tests/Functional/Reference/Provider/ArticleReferenceProviderTest.php +++ b/Tests/Functional/Reference/Provider/ArticleReferenceProviderTest.php @@ -23,27 +23,35 @@ use Sulu\Component\DocumentManager\DocumentManagerInterface; use Sulu\Component\HttpKernel\SuluKernel; use Sulu\Component\Persistence\Repository\ORM\EntityRepository; -use Sulu\Component\PHPCR\SessionManager\SessionManager; class ArticleReferenceProviderTest extends SuluTestCase { - private ArticleReferenceProvider $articleReferenceProvider; - private DocumentManagerInterface $documentManager; - private SessionManager $sessionManager; + /** + * @var ArticleReferenceProvider + */ + private $articleReferenceProvider; + + /** + * @var DocumentManagerInterface + */ + private $documentManager; /** * @var EntityRepository */ - private EntityRepository $referenceRepository; + 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->sessionManager = $this->getContainer()->get('sulu.phpcr.session'); $this->referenceRepository = $this->getContainer()->get('sulu.repository.reference'); } @@ -97,7 +105,6 @@ public function testUpdateUnpublishedReferences(): void // 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->sessionManager = $this->getContainer()->get('sulu.phpcr.session'); $this->referenceRepository = $this->getContainer()->get('sulu.repository.reference'); /** @var ArticleDocument $article */ diff --git a/Tests/Functional/Reference/Refresh/ArticleReferenceRefresherTest.php b/Tests/Functional/Reference/Refresh/ArticleReferenceRefresherTest.php index 3291fcb9..a674ae77 100644 --- a/Tests/Functional/Reference/Refresh/ArticleReferenceRefresherTest.php +++ b/Tests/Functional/Reference/Refresh/ArticleReferenceRefresherTest.php @@ -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');