Skip to content

Commit

Permalink
Fix alternate article urls meta tags
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed May 25, 2021
1 parent c7016d0 commit bc7e305
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Document/Serializer/WebsiteArticleUrlsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ public function addUrlsOnPostSerialize(ObjectEvent $event): void
$locale = $localization->getLocale();
$route = $this->routeRepository->findByEntity(get_class($article), $article->getUuid(), $locale);
$path = $route ? $route->getPath() : '/';
$alternate = (bool) $route;

$urls[$locale] = $path;
$localizations[$locale] = [
'locale' => $locale,
'url' => $this->webspaceManager->findUrlByResourceLocator($path, null, $locale),
'alternate' => $alternate,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,67 @@ public function testAddUrlsOnPostSerialize()
$this->webspaceManager->findUrlByResourceLocator('/page', null, 'en')->willReturn('http://sulu.io/page');

$visitor->visitProperty(
Argument::that(function (StaticPropertyMetadata $metadata) {
Argument::that(function(StaticPropertyMetadata $metadata) {
return 'urls' === $metadata->name;
}),
['de' => '/seite', 'en' => '/page']
)->shouldBeCalled();

$visitor->visitProperty(
Argument::that(function (StaticPropertyMetadata $metadata) {
Argument::that(function(StaticPropertyMetadata $metadata) {
return 'localizations' === $metadata->name;
}),
[
'de' => ['locale' => 'de', 'url' => 'http://sulu.io/de/seite'],
'en' => ['locale' => 'en', 'url' => 'http://sulu.io/page'],
'de' => ['locale' => 'de', 'url' => 'http://sulu.io/de/seite', 'alternate' => true],
'en' => ['locale' => 'en', 'url' => 'http://sulu.io/page', 'alternate' => true],
]
)->shouldBeCalled();

$this->urlsSubscriber->addUrlsOnPostSerialize($event->reveal());
}

public function testAddUrlsOnPostSerializeNonExistLocale()
{
$article = $this->prophesize(ArticleDocument::class);
$visitor = $this->prophesize(SerializationVisitorInterface::class);

$context = $this->prophesize(SerializationContext::class);
$context->hasAttribute('urls')->willReturn(true);

$entityId = '123-123-123';
$article->getUuid()->willReturn($entityId);

$event = $this->prophesize(ObjectEvent::class);
$event->getObject()->willReturn($article->reveal());
$event->getVisitor()->willReturn($visitor->reveal());
$event->getContext()->willReturn($context->reveal());

$entityClass = get_class($article->reveal());

$deRoute = $this->prophesize(RouteInterface::class);
$deRoute->getPath()->willReturn('/seite');
$this->routeRepository->findByEntity($entityClass, $entityId, 'de')->willReturn($deRoute->reveal());
$this->webspaceManager->findUrlByResourceLocator('/seite', null, 'de')->willReturn('http://sulu.io/de/seite');

$enRoute = $this->prophesize(RouteInterface::class);
$enRoute->getPath()->willReturn('/page');
$this->routeRepository->findByEntity($entityClass, $entityId, 'en')->willReturn(null);
$this->webspaceManager->findUrlByResourceLocator('/', null, 'en')->willReturn('http://sulu.io/');

$visitor->visitProperty(
Argument::that(function(StaticPropertyMetadata $metadata) {
return 'urls' === $metadata->name;
}),
['de' => '/seite', 'en' => '/']
)->shouldBeCalled();

$visitor->visitProperty(
Argument::that(function(StaticPropertyMetadata $metadata) {
return 'localizations' === $metadata->name;
}),
[
'de' => ['locale' => 'de', 'url' => 'http://sulu.io/de/seite', 'alternate' => true],
'en' => ['locale' => 'en', 'url' => 'http://sulu.io/', 'alternate' => false],
]
)->shouldBeCalled();

Expand Down

0 comments on commit bc7e305

Please sign in to comment.