From e4b7a2ff1e9887d2f81f71db6bc8413b60c17e12 Mon Sep 17 00:00:00 2001 From: Johannes Wachter Date: Thu, 14 Dec 2023 16:43:57 +0100 Subject: [PATCH] index correct ghost content when deleting the locale of an article --- Admin/ArticleAdmin.php | 3 +++ Document/Index/ArticleIndexer.php | 12 ++++++++++++ Document/Subscriber/ArticleSubscriber.php | 11 +++++++++++ .../Document/Subscriber/ArticleSubscriberTest.php | 2 ++ phpstan-baseline.neon | 2 +- 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Admin/ArticleAdmin.php b/Admin/ArticleAdmin.php index 8e0df371..fde7b233 100644 --- a/Admin/ArticleAdmin.php +++ b/Admin/ArticleAdmin.php @@ -416,6 +416,9 @@ function(Localization $localization) { } } + /** + * @return array>> + */ public function getSecurityContexts() { $securityContext = []; diff --git a/Document/Index/ArticleIndexer.php b/Document/Index/ArticleIndexer.php index 2fd4eecf..f17e72ec 100644 --- a/Document/Index/ArticleIndexer.php +++ b/Document/Index/ArticleIndexer.php @@ -13,6 +13,7 @@ use ONGR\ElasticsearchBundle\Collection\Collection; use ONGR\ElasticsearchBundle\Service\Manager; +use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; use Sulu\Bundle\ArticleBundle\Document\ArticleDocument; @@ -453,6 +454,17 @@ public function replaceWithGhostData(ArticleDocument $document, string $locale): $article = $this->createOrUpdateArticle($document, $locale); $article->setLocalizationState(new LocalizationStateViewObject(LocalizationState::GHOST, $document->getOriginalLocale())); + $repository = $this->manager->getRepository($this->documentFactory->getClass('article')); + $search = $repository->createSearch(); + $search->addQuery(new TermQuery('localization_state.state', 'ghost'), BoolQuery::MUST); + $search->addQuery(new TermQuery('localization_state.locale', $locale), BoolQuery::MUST); + + /** @var array $searchResult */ + $searchResult = $repository->findArray($search); + foreach ($searchResult as $result) { + $this->replaceWithGhostData($document, $result['locale']); + } + $this->manager->persist($article); } diff --git a/Document/Subscriber/ArticleSubscriber.php b/Document/Subscriber/ArticleSubscriber.php index 1aec28ab..9aee0695 100644 --- a/Document/Subscriber/ArticleSubscriber.php +++ b/Document/Subscriber/ArticleSubscriber.php @@ -465,6 +465,17 @@ public function handleRemoveLocale(RemoveLocaleEvent $event) return; } + $concreteLocales = $this->documentInspector->getConcreteLocales($document); + $ghostLocale = $document->getOriginalLocale(); + if (!\in_array($ghostLocale, $concreteLocales, true)) { + $ghostLocale = $concreteLocales[0] ?? null; + } + + if (null !== $ghostLocale) { + /** @var ArticleDocument $document */ + $document = $this->documentManager->find($document->getUuid(), $ghostLocale); + } + $this->indexer->replaceWithGhostData($document, $event->getLocale()); $this->indexer->flush(); } diff --git a/Tests/Unit/Document/Subscriber/ArticleSubscriberTest.php b/Tests/Unit/Document/Subscriber/ArticleSubscriberTest.php index f6509564..0b94b21f 100644 --- a/Tests/Unit/Document/Subscriber/ArticleSubscriberTest.php +++ b/Tests/Unit/Document/Subscriber/ArticleSubscriberTest.php @@ -677,6 +677,8 @@ public function testHandleRemoveLocale() $event->getDocument()->willReturn($document->reveal()); $event->getLocale()->willReturn('en'); + // TODO adapt tests + // Ensure the indexer methods are called $this->indexer->replaceWithGhostData($document->reveal(), 'en')->shouldBeCalled(); $this->indexer->flush()->shouldBeCalled(); diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 7b70dbe0..b403b38c 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -2147,7 +2147,7 @@ parameters: - message: "#^Cannot access offset 'pageNumber' on mixed\\.$#" - count: 2 + count: 1 path: Preview/ArticleObjectProvider.php -