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/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; diff --git a/Tests/Functional/Reference/Provider/ArticleReferenceProviderTest.php b/Tests/Functional/Reference/Provider/ArticleReferenceProviderTest.php new file mode 100644 index 00000000..7cc523b0 --- /dev/null +++ b/Tests/Functional/Reference/Provider/ArticleReferenceProviderTest.php @@ -0,0 +1,146 @@ + + */ + 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; + } +} 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'); 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