Skip to content

Commit

Permalink
index correct ghost content when deleting the locale of an article
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Dec 14, 2023
1 parent 8ccbbf7 commit e4b7a2f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Admin/ArticleAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ function(Localization $localization) {
}
}

/**
* @return array<string, array<string, array<string, string[]>>>
*/
public function getSecurityContexts()
{
$securityContext = [];
Expand Down
12 changes: 12 additions & 0 deletions Document/Index/ArticleIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<array{locale: string}> $searchResult */
$searchResult = $repository->findArray($search);
foreach ($searchResult as $result) {
$this->replaceWithGhostData($document, $result['locale']);
}

$this->manager->persist($article);
}

Expand Down
11 changes: 11 additions & 0 deletions Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/Unit/Document/Subscriber/ArticleSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2147,7 +2147,7 @@ parameters:

-
message: "#^Cannot access offset 'pageNumber' on mixed\\.$#"
count: 2
count: 1
path: Preview/ArticleObjectProvider.php

-
Expand Down

0 comments on commit e4b7a2f

Please sign in to comment.