Skip to content

Commit

Permalink
Fix redundant URLs in sitemap index in multichannel contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephanie committed Jan 31, 2022
1 parent 7af1799 commit e9c0f0e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/Builder/SitemapIndexBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ public function addProvider(UrlProviderInterface $provider): void
$this->providers[] = $provider;
}

public function addIndexProvider(IndexUrlProviderInterface $provider): void
public function addIndexProvider(IndexUrlProviderInterface $indexProvider): void
{
$this->indexProviders[] = $provider;
foreach ($this->providers as $provider) {
$indexProvider->addProvider($provider);
}

$this->indexProviders[] = $indexProvider;
}

public function build(): SitemapInterface
Expand All @@ -40,10 +44,6 @@ public function build(): SitemapInterface
$urls = [];

foreach ($this->indexProviders as $indexProvider) {
foreach ($this->providers as $provider) {
$indexProvider->addProvider($provider);
}

$urls[] = $indexProvider->generate();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Builder/SitemapIndexBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

interface SitemapIndexBuilderInterface extends BuilderInterface
{
public function addIndexProvider(IndexUrlProviderInterface $provider): void;
public function addIndexProvider(IndexUrlProviderInterface $indexProvider): void;

public function build(): SitemapInterface;
}
10 changes: 3 additions & 7 deletions src/Provider/IndexUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace SitemapPlugin\Provider;

use SitemapPlugin\Factory\IndexUrlFactoryInterface;
use SitemapPlugin\Model\IndexUrlInterface;
use Symfony\Component\Routing\RouterInterface;

final class IndexUrlProvider implements IndexUrlProviderInterface
Expand All @@ -17,9 +16,6 @@ final class IndexUrlProvider implements IndexUrlProviderInterface

private IndexUrlFactoryInterface $sitemapIndexUrlFactory;

/** @var IndexUrlInterface[] */
private array $urls = [];

public function __construct(
RouterInterface $router,
IndexUrlFactoryInterface $sitemapIndexUrlFactory
Expand All @@ -35,12 +31,12 @@ public function addProvider(UrlProviderInterface $provider): void

public function generate(): iterable
{
$urls = [];
foreach ($this->providers as $provider) {
$location = $this->router->generate('sylius_sitemap_' . $provider->getName());

$this->urls[] = $this->sitemapIndexUrlFactory->createNew($location);
$urls[] = $this->sitemapIndexUrlFactory->createNew($location);
}

return $this->urls;
return $urls;
}
}
22 changes: 22 additions & 0 deletions tests/Controller/MultiChannelSitemapIndexControllerApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Tests\SitemapPlugin\Controller;

final class MultiChannelSitemapIndexControllerApiTest extends XmlApiTestCase
{
public function testShowActionResponse()
{
$this->loadFixturesFromFiles(['multi_channel.yaml']);
$this->generateSitemaps();

$response = $this->getBufferedResponse('http://localhost/sitemap_index.xml');
$this->assertResponse($response, 'show_sitemap_index');

$response = $this->getBufferedResponse('http://store.fr/sitemap_index.xml');
$this->assertResponse($response, 'show_second_sitemap_index');

$this->deleteSitemaps();
}
}
12 changes: 12 additions & 0 deletions tests/Responses/show_second_sitemap_index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>http://store.fr/sitemap/products.xml</loc>
</sitemap>
<sitemap>
<loc>http://store.fr/sitemap/taxons.xml</loc>
</sitemap>
<sitemap>
<loc>http://store.fr/sitemap/static.xml</loc>
</sitemap>
</sitemapindex>

0 comments on commit e9c0f0e

Please sign in to comment.