|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Sitemap\Block; |
| 9 | + |
| 10 | +use Magento\Framework\View\LayoutInterface; |
| 11 | +use Magento\Sitemap\Model\SitemapFactory; |
| 12 | +use Magento\Store\Model\StoreManagerInterface; |
| 13 | +use Magento\TestFramework\Helper\Bootstrap; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test for \Magento\Sitemap\Block\Robots. |
| 18 | + */ |
| 19 | +class RobotsTest extends TestCase |
| 20 | +{ |
| 21 | + private const STUB_SITEMAP_FILENAME = 'sitemap_file.xml'; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var LayoutInterface |
| 25 | + */ |
| 26 | + private $layout; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var mixed |
| 30 | + */ |
| 31 | + private $sitemapFactory; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var StoreManagerInterface |
| 35 | + */ |
| 36 | + private $storeManager; |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheridoc |
| 40 | + */ |
| 41 | + protected function setUp(): void |
| 42 | + { |
| 43 | + $this->layout = Bootstrap::getObjectManager()->get(LayoutInterface::class); |
| 44 | + $this->sitemapFactory = Bootstrap::getObjectManager()->get(SitemapFactory::class); |
| 45 | + $this->storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Test toHtml with few websites |
| 50 | + * |
| 51 | + * @magentoDataFixture Magento/Store/_files/multiple_websites_with_store_groups_stores.php |
| 52 | + * @magentoConfigFixture default_store sitemap/search_engines/submission_robots 1 |
| 53 | + * @magentoConfigFixture second_store_view_store sitemap/search_engines/submission_robots 1 |
| 54 | + * |
| 55 | + * @return void |
| 56 | + */ |
| 57 | + public function testToHtml(): void |
| 58 | + { |
| 59 | + $secondSitemapFile = 'second_' . self::STUB_SITEMAP_FILENAME; |
| 60 | + |
| 61 | + $this->createSitemap(self::STUB_SITEMAP_FILENAME, 1); |
| 62 | + $this->createSitemap($secondSitemapFile, 2); |
| 63 | + |
| 64 | + $this->assertStringContainsString(self::STUB_SITEMAP_FILENAME, $this->getToHtmlOutput(1)); |
| 65 | + $this->assertStringContainsString($secondSitemapFile, $this->getToHtmlOutput(2)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns toHtml output per store |
| 70 | + * |
| 71 | + * @param int $storeId |
| 72 | + * @return string |
| 73 | + */ |
| 74 | + private function getToHtmlOutput(int $storeId): string |
| 75 | + { |
| 76 | + $this->storeManager->setCurrentStore($storeId); |
| 77 | + $block = $this->layout->createBlock(Robots::class); |
| 78 | + |
| 79 | + return $block->toHtml(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Create Sitemap |
| 84 | + * |
| 85 | + * @param string $fileName |
| 86 | + * @param int $storeId |
| 87 | + * @param string $siteMath |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + private function createSitemap(string $fileName, int $storeId, string $siteMath = '/'): void |
| 91 | + { |
| 92 | + $model = $this->sitemapFactory->create(); |
| 93 | + $model->setData(['sitemap_filename' => $fileName, 'store_id' => $storeId, 'sitemap_path' => $siteMath]); |
| 94 | + $model->save(); |
| 95 | + } |
| 96 | +} |
0 commit comments