123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sitemap\Test\Unit\Block;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class RobotsTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Framework\View\Element\Context|\PHPUnit_Framework_MockObject_MockObject
- */
- private $context;
- /**
- * @var \Magento\Store\Model\StoreResolver|\PHPUnit_Framework_MockObject_MockObject
- */
- private $storeResolver;
- /**
- * @var \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $sitemapCollectionFactory;
- /**
- * @var \Magento\Sitemap\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
- */
- private $sitemapHelper;
- /**
- * @var \Magento\Sitemap\Block\Robots
- */
- private $block;
- /**
- * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $eventManagerMock;
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $scopeConfigMock;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $storeManager;
- protected function setUp()
- {
- $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
- ->getMockForAbstractClass();
- $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
- ->getMockForAbstractClass();
- $this->context = $this->getMockBuilder(\Magento\Framework\View\Element\Context::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->context->expects($this->any())
- ->method('getEventManager')
- ->willReturn($this->eventManagerMock);
- $this->context->expects($this->any())
- ->method('getScopeConfig')
- ->willReturn($this->scopeConfigMock);
- $this->storeResolver = $this->getMockBuilder(\Magento\Store\Model\StoreResolver::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->sitemapCollectionFactory = $this->getMockBuilder(
- \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory::class
- )
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->sitemapHelper = $this->getMockBuilder(\Magento\Sitemap\Helper\Data::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
- ->getMockForAbstractClass();
- $this->block = new \Magento\Sitemap\Block\Robots(
- $this->context,
- $this->storeResolver,
- $this->sitemapCollectionFactory,
- $this->sitemapHelper,
- $this->storeManager
- );
- }
- /**
- * Check toHtml() method in case when robots submission is disabled
- */
- public function testToHtmlRobotsSubmissionIsDisabled()
- {
- $defaultStoreId = 1;
- $defaultWebsiteId = 1;
- $expected = '';
- $this->initEventManagerMock($expected);
- $this->scopeConfigMock->expects($this->once())->method('getValue')->willReturn(false);
- $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
- ->getMockForAbstractClass();
- $storeMock->expects($this->once())
- ->method('getWebsiteId')
- ->willReturn($defaultWebsiteId);
- $this->storeManager->expects($this->once())
- ->method('getDefaultStoreView')
- ->willReturn($storeMock);
- $storeMock->expects($this->any())
- ->method('getWebsiteId')
- ->willReturn($defaultWebsiteId);
- $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
- ->disableOriginalConstructor()
- ->getMock();
- $websiteMock->expects($this->any())
- ->method('getStoreIds')
- ->willReturn([$defaultStoreId]);
- $this->storeManager->expects($this->once())
- ->method('getWebsite')
- ->with($defaultWebsiteId)
- ->willReturn($websiteMock);
- $this->sitemapHelper->expects($this->once())
- ->method('getEnableSubmissionRobots')
- ->with($defaultStoreId)
- ->willReturn(false);
- $this->assertEquals($expected, $this->block->toHtml());
- }
- /**
- * Check toHtml() method in case when robots submission is enabled
- */
- public function testAfterGetDataRobotsSubmissionIsEnabled()
- {
- $defaultStoreId = 1;
- $secondStoreId = 2;
- $defaultWebsiteId = 1;
- $sitemapPath = '/';
- $sitemapFilenameOne = 'sitemap.xml';
- $sitemapFilenameTwo = 'sitemap_custom.xml';
- $sitemapFilenameThree = 'sitemap.xml';
- $expected = 'Sitemap: ' . $sitemapFilenameOne
- . PHP_EOL
- . 'Sitemap: ' . $sitemapFilenameTwo
- . PHP_EOL;
- $this->initEventManagerMock($expected);
- $this->scopeConfigMock->expects($this->once())->method('getValue')->willReturn(false);
- $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
- ->getMockForAbstractClass();
- $this->storeManager->expects($this->once())
- ->method('getDefaultStoreView')
- ->willReturn($storeMock);
- $storeMock->expects($this->any())
- ->method('getWebsiteId')
- ->willReturn($defaultWebsiteId);
- $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
- ->disableOriginalConstructor()
- ->getMock();
- $websiteMock->expects($this->any())
- ->method('getStoreIds')
- ->willReturn([$defaultStoreId, $secondStoreId]);
- $this->storeManager->expects($this->once())
- ->method('getWebsite')
- ->with($defaultWebsiteId)
- ->willReturn($websiteMock);
- $this->sitemapHelper->expects($this->any())
- ->method('getEnableSubmissionRobots')
- ->willReturnMap([
- [$defaultStoreId, true],
- [$secondStoreId, false],
- ]);
- $sitemapMockOne = $this->getSitemapMock($sitemapPath, $sitemapFilenameOne);
- $sitemapMockTwo = $this->getSitemapMock($sitemapPath, $sitemapFilenameTwo);
- $sitemapMockThree = $this->getSitemapMock($sitemapPath, $sitemapFilenameThree);
- $sitemapCollectionMock = $this->getMockBuilder(\Magento\Sitemap\Model\ResourceModel\Sitemap\Collection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $sitemapCollectionMock->expects($this->any())
- ->method('addStoreFilter')
- ->with([$defaultStoreId])
- ->willReturnSelf();
- $sitemapCollectionMock->expects($this->any())
- ->method('getIterator')
- ->willReturn(new \ArrayIterator([$sitemapMockOne, $sitemapMockTwo, $sitemapMockThree]));
- $this->sitemapCollectionFactory->expects($this->once())
- ->method('create')
- ->willReturn($sitemapCollectionMock);
- $this->assertEquals($expected, $this->block->toHtml());
- }
- /**
- * Check that getIdentities() method returns specified cache tag
- */
- public function testGetIdentities()
- {
- $storeId = 1;
- $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->getMockForAbstractClass();
- $this->storeManager->expects($this->once())
- ->method('getDefaultStoreView')
- ->willReturn($storeMock);
- $storeMock->expects($this->once())
- ->method('getId')
- ->willReturn($storeId);
- $expected = [
- \Magento\Robots\Model\Config\Value::CACHE_TAG . '_' . $storeId,
- ];
- $this->assertEquals($expected, $this->block->getIdentities());
- }
- /**
- * Initialize mock object of Event Manager
- *
- * @param string $data
- * @return void
- */
- protected function initEventManagerMock($data)
- {
- $this->eventManagerMock->expects($this->any())
- ->method('dispatch')
- ->willReturnMap([
- [
- 'view_block_abstract_to_html_before',
- [
- 'block' => $this->block,
- ],
- ],
- [
- 'view_block_abstract_to_html_after',
- [
- 'block' => $this->block,
- 'transport' => new \Magento\Framework\DataObject(['html' => $data]),
- ],
- ],
- ]);
- }
- /**
- * Create and return mock object of \Magento\Sitemap\Model\Sitemap class
- *
- * @param string $sitemapPath
- * @param string $sitemapFilename
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function getSitemapMock($sitemapPath, $sitemapFilename)
- {
- $sitemapMock = $this->getMockBuilder(\Magento\Sitemap\Model\Sitemap::class)
- ->disableOriginalConstructor()
- ->setMethods([
- 'getSitemapFilename',
- 'getSitemapPath',
- 'getSitemapUrl',
- ])
- ->getMock();
- $sitemapMock->expects($this->any())
- ->method('getSitemapFilename')
- ->willReturn($sitemapFilename);
- $sitemapMock->expects($this->any())
- ->method('getSitemapPath')
- ->willReturn($sitemapPath);
- $sitemapMock->expects($this->any())
- ->method('getSitemapUrl')
- ->with($sitemapPath, $sitemapFilename)
- ->willReturn($sitemapFilename);
- return $sitemapMock;
- }
- }
|