From 78fac4029a3ac52c8a739b242f4722a3261c7b51 Mon Sep 17 00:00:00 2001 From: Stefan Doorn Date: Fri, 24 Aug 2018 17:49:03 +0200 Subject: [PATCH] Add sitemap provider for sections --- .../config/services/sitemap_provider.yml | 13 +- src/SitemapProvider/SectionUrlProvider.php | 133 ++++++++++++++++++ .../Provider/SitemapPageControllerApiTest.php | 1 + ...SitemapSectionControllerApiLocalesTest.php | 51 +++++++ .../SitemapSectionControllerApiTest.php | 47 +++++++ .../Expected/show_sitemap_sections.xml | 13 ++ .../Expected/show_sitemap_sections_locale.xml | 31 ++++ 7 files changed, 288 insertions(+), 1 deletion(-) create mode 100644 src/SitemapProvider/SectionUrlProvider.php create mode 100644 tests/Api/Sitemap/Provider/SitemapSectionControllerApiLocalesTest.php create mode 100644 tests/Api/Sitemap/Provider/SitemapSectionControllerApiTest.php create mode 100644 tests/Responses/Expected/show_sitemap_sections.xml create mode 100644 tests/Responses/Expected/show_sitemap_sections_locale.xml diff --git a/src/Resources/config/services/sitemap_provider.yml b/src/Resources/config/services/sitemap_provider.yml index 63296d8b3..ba3ce5605 100644 --- a/src/Resources/config/services/sitemap_provider.yml +++ b/src/Resources/config/services/sitemap_provider.yml @@ -8,4 +8,15 @@ services: - "@sylius.context.locale" - "@sylius.context.channel" tags: - - "sylius.sitemap_provider" \ No newline at end of file + - "sylius.sitemap_provider" + + bitbag_sylius_cms_plugin.sitemap_provider.section: + class: BitBag\SyliusCmsPlugin\SitemapProvider\SectionUrlProvider + arguments: + - "@bitbag_sylius_cms_plugin.repository.section" + - "@router" + - "@sylius.sitemap_url_factory" + - "@sylius.context.locale" + - "@sylius.context.channel" + tags: + - { name: sylius.sitemap_provider } diff --git a/src/SitemapProvider/SectionUrlProvider.php b/src/SitemapProvider/SectionUrlProvider.php new file mode 100644 index 000000000..bf9bcae8d --- /dev/null +++ b/src/SitemapProvider/SectionUrlProvider.php @@ -0,0 +1,133 @@ +sectionRepository = $sectionRepository; + $this->router = $router; + $this->sitemapUrlFactory = $sitemapUrlFactory; + $this->localeContext = $localeContext; + $this->channelContext = $channelContext; + } + + public function getName(): string + { + return 'cms_sections'; + } + + public function generate(): iterable + { + $urls = []; + + foreach ($this->getSections() as $section) { + $urls[] = $this->createSectionUrl($section); + } + + return $urls; + } + + private function getTranslations(SectionInterface $section): Collection + { + return $section->getTranslations()->filter(function (TranslationInterface $translation) { + return $this->localeInLocaleCodes($translation); + }); + } + + private function localeInLocaleCodes(TranslationInterface $translation): bool + { + return in_array($translation->getLocale(), $this->getLocaleCodes()); + } + + private function getSections(): iterable + { + return $this->sectionRepository->findAll(); + } + + private function getLocaleCodes(): array + { + /** @var ChannelInterface $channel */ + $channel = $this->channelContext->getChannel(); + + return $channel->getLocales()->map(function (LocaleInterface $locale) { + return $locale->getCode(); + })->toArray(); + } + + private function createSectionUrl(SectionInterface $section): SitemapUrlInterface + { + $url = $this->sitemapUrlFactory->createNew(); + + $url->setChangeFrequency(ChangeFrequency::daily()); + $url->setPriority(0.7); + + /** @var SectionTranslationInterface $translation */ + foreach ($this->getTranslations($section) as $translation) { + if (!$translation->getLocale() || !$this->localeInLocaleCodes($translation)) { + continue; + } + + $location = $this->router->generate('bitbag_sylius_cms_plugin_shop_section_show', [ + 'code' => $section->getCode(), + '_locale' => $translation->getLocale(), + ]); + + if ($translation->getLocale() === $this->localeContext->getLocaleCode()) { + $url->setLocalization($location); + + continue; + } + + $url->addAlternative($location, $translation->getLocale()); + } + + return $url; + } +} diff --git a/tests/Api/Sitemap/Provider/SitemapPageControllerApiTest.php b/tests/Api/Sitemap/Provider/SitemapPageControllerApiTest.php index c87864b38..6bf4404bf 100644 --- a/tests/Api/Sitemap/Provider/SitemapPageControllerApiTest.php +++ b/tests/Api/Sitemap/Provider/SitemapPageControllerApiTest.php @@ -46,6 +46,7 @@ public function setUpDatabase(): void $page->setSlug('test 2'); $page->setEnabled(false); $this->getEntityManager()->persist($page); + $this->getEntityManager()->flush(); } diff --git a/tests/Api/Sitemap/Provider/SitemapSectionControllerApiLocalesTest.php b/tests/Api/Sitemap/Provider/SitemapSectionControllerApiLocalesTest.php new file mode 100644 index 000000000..1b2e592f1 --- /dev/null +++ b/tests/Api/Sitemap/Provider/SitemapSectionControllerApiLocalesTest.php @@ -0,0 +1,51 @@ +setCurrentLocale('en_US'); + $section->setName('Test'); + $section->setCode('test'); + $section->setCurrentLocale('nl_NL'); + $section->setName('Test'); + $this->getEntityManager()->persist($section); + + $section = new Section(); + $section->setCurrentLocale('en_US'); + $section->setName('Mock'); + $section->setCode('mock'); + $section->setCurrentLocale('nl_NL'); + $section->setName('Mock'); + $this->getEntityManager()->persist($section); + + $this->getEntityManager()->flush(); + } + + public function testShowActionResponse(): void + { + $this->client->request('GET', '/sitemap/cms_sections.xml'); + $response = $this->client->getResponse(); + $this->assertResponse($response, 'show_sitemap_sections_locale'); + } +} diff --git a/tests/Api/Sitemap/Provider/SitemapSectionControllerApiTest.php b/tests/Api/Sitemap/Provider/SitemapSectionControllerApiTest.php new file mode 100644 index 000000000..45e982c2d --- /dev/null +++ b/tests/Api/Sitemap/Provider/SitemapSectionControllerApiTest.php @@ -0,0 +1,47 @@ +setCurrentLocale('en_US'); + $section->setName('Test'); + $section->setCode('test'); + $this->getEntityManager()->persist($section); + + $section = new Section(); + $section->setCurrentLocale('en_US'); + $section->setName('Mock'); + $section->setCode('mock'); + $this->getEntityManager()->persist($section); + + $this->getEntityManager()->flush(); + } + + public function testShowActionResponse(): void + { + $this->client->request('GET', '/sitemap/cms_sections.xml'); + $response = $this->client->getResponse(); + $this->assertResponse($response, 'show_sitemap_sections'); + } +} diff --git a/tests/Responses/Expected/show_sitemap_sections.xml b/tests/Responses/Expected/show_sitemap_sections.xml new file mode 100644 index 000000000..9bffc1cbc --- /dev/null +++ b/tests/Responses/Expected/show_sitemap_sections.xml @@ -0,0 +1,13 @@ + + + + http://localhost/en_US/section/test + daily + 0.7 + + + http://localhost/en_US/section/mock + daily + 0.7 + + diff --git a/tests/Responses/Expected/show_sitemap_sections_locale.xml b/tests/Responses/Expected/show_sitemap_sections_locale.xml new file mode 100644 index 000000000..ba9c3e7d8 --- /dev/null +++ b/tests/Responses/Expected/show_sitemap_sections_locale.xml @@ -0,0 +1,31 @@ + + + + http://localhost/en_US/section/test + + + daily + 0.7 + + + http://localhost/nl_NL/section/test + + + daily + 0.7 + + + http://localhost/en_US/section/mock + + + daily + 0.7 + + + http://localhost/nl_NL/section/mock + + + daily + 0.7 + +