RobotsTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sitemap\Test\Unit\Block;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class RobotsTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\View\Element\Context|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $context;
  17. /**
  18. * @var \Magento\Store\Model\StoreResolver|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $storeResolver;
  21. /**
  22. * @var \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $sitemapCollectionFactory;
  25. /**
  26. * @var \Magento\Sitemap\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $sitemapHelper;
  29. /**
  30. * @var \Magento\Sitemap\Block\Robots
  31. */
  32. private $block;
  33. /**
  34. * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $eventManagerMock;
  37. /**
  38. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $scopeConfigMock;
  41. /**
  42. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $storeManager;
  45. protected function setUp()
  46. {
  47. $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
  48. ->getMockForAbstractClass();
  49. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  50. ->getMockForAbstractClass();
  51. $this->context = $this->getMockBuilder(\Magento\Framework\View\Element\Context::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->context->expects($this->any())
  55. ->method('getEventManager')
  56. ->willReturn($this->eventManagerMock);
  57. $this->context->expects($this->any())
  58. ->method('getScopeConfig')
  59. ->willReturn($this->scopeConfigMock);
  60. $this->storeResolver = $this->getMockBuilder(\Magento\Store\Model\StoreResolver::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->sitemapCollectionFactory = $this->getMockBuilder(
  64. \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory::class
  65. )
  66. ->setMethods(['create'])
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->sitemapHelper = $this->getMockBuilder(\Magento\Sitemap\Helper\Data::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
  73. ->getMockForAbstractClass();
  74. $this->block = new \Magento\Sitemap\Block\Robots(
  75. $this->context,
  76. $this->storeResolver,
  77. $this->sitemapCollectionFactory,
  78. $this->sitemapHelper,
  79. $this->storeManager
  80. );
  81. }
  82. /**
  83. * Check toHtml() method in case when robots submission is disabled
  84. */
  85. public function testToHtmlRobotsSubmissionIsDisabled()
  86. {
  87. $defaultStoreId = 1;
  88. $defaultWebsiteId = 1;
  89. $expected = '';
  90. $this->initEventManagerMock($expected);
  91. $this->scopeConfigMock->expects($this->once())->method('getValue')->willReturn(false);
  92. $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  93. ->getMockForAbstractClass();
  94. $storeMock->expects($this->once())
  95. ->method('getWebsiteId')
  96. ->willReturn($defaultWebsiteId);
  97. $this->storeManager->expects($this->once())
  98. ->method('getDefaultStoreView')
  99. ->willReturn($storeMock);
  100. $storeMock->expects($this->any())
  101. ->method('getWebsiteId')
  102. ->willReturn($defaultWebsiteId);
  103. $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
  104. ->disableOriginalConstructor()
  105. ->getMock();
  106. $websiteMock->expects($this->any())
  107. ->method('getStoreIds')
  108. ->willReturn([$defaultStoreId]);
  109. $this->storeManager->expects($this->once())
  110. ->method('getWebsite')
  111. ->with($defaultWebsiteId)
  112. ->willReturn($websiteMock);
  113. $this->sitemapHelper->expects($this->once())
  114. ->method('getEnableSubmissionRobots')
  115. ->with($defaultStoreId)
  116. ->willReturn(false);
  117. $this->assertEquals($expected, $this->block->toHtml());
  118. }
  119. /**
  120. * Check toHtml() method in case when robots submission is enabled
  121. */
  122. public function testAfterGetDataRobotsSubmissionIsEnabled()
  123. {
  124. $defaultStoreId = 1;
  125. $secondStoreId = 2;
  126. $defaultWebsiteId = 1;
  127. $sitemapPath = '/';
  128. $sitemapFilenameOne = 'sitemap.xml';
  129. $sitemapFilenameTwo = 'sitemap_custom.xml';
  130. $sitemapFilenameThree = 'sitemap.xml';
  131. $expected = 'Sitemap: ' . $sitemapFilenameOne
  132. . PHP_EOL
  133. . 'Sitemap: ' . $sitemapFilenameTwo
  134. . PHP_EOL;
  135. $this->initEventManagerMock($expected);
  136. $this->scopeConfigMock->expects($this->once())->method('getValue')->willReturn(false);
  137. $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
  138. ->getMockForAbstractClass();
  139. $this->storeManager->expects($this->once())
  140. ->method('getDefaultStoreView')
  141. ->willReturn($storeMock);
  142. $storeMock->expects($this->any())
  143. ->method('getWebsiteId')
  144. ->willReturn($defaultWebsiteId);
  145. $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
  146. ->disableOriginalConstructor()
  147. ->getMock();
  148. $websiteMock->expects($this->any())
  149. ->method('getStoreIds')
  150. ->willReturn([$defaultStoreId, $secondStoreId]);
  151. $this->storeManager->expects($this->once())
  152. ->method('getWebsite')
  153. ->with($defaultWebsiteId)
  154. ->willReturn($websiteMock);
  155. $this->sitemapHelper->expects($this->any())
  156. ->method('getEnableSubmissionRobots')
  157. ->willReturnMap([
  158. [$defaultStoreId, true],
  159. [$secondStoreId, false],
  160. ]);
  161. $sitemapMockOne = $this->getSitemapMock($sitemapPath, $sitemapFilenameOne);
  162. $sitemapMockTwo = $this->getSitemapMock($sitemapPath, $sitemapFilenameTwo);
  163. $sitemapMockThree = $this->getSitemapMock($sitemapPath, $sitemapFilenameThree);
  164. $sitemapCollectionMock = $this->getMockBuilder(\Magento\Sitemap\Model\ResourceModel\Sitemap\Collection::class)
  165. ->disableOriginalConstructor()
  166. ->getMock();
  167. $sitemapCollectionMock->expects($this->any())
  168. ->method('addStoreFilter')
  169. ->with([$defaultStoreId])
  170. ->willReturnSelf();
  171. $sitemapCollectionMock->expects($this->any())
  172. ->method('getIterator')
  173. ->willReturn(new \ArrayIterator([$sitemapMockOne, $sitemapMockTwo, $sitemapMockThree]));
  174. $this->sitemapCollectionFactory->expects($this->once())
  175. ->method('create')
  176. ->willReturn($sitemapCollectionMock);
  177. $this->assertEquals($expected, $this->block->toHtml());
  178. }
  179. /**
  180. * Check that getIdentities() method returns specified cache tag
  181. */
  182. public function testGetIdentities()
  183. {
  184. $storeId = 1;
  185. $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->getMockForAbstractClass();
  186. $this->storeManager->expects($this->once())
  187. ->method('getDefaultStoreView')
  188. ->willReturn($storeMock);
  189. $storeMock->expects($this->once())
  190. ->method('getId')
  191. ->willReturn($storeId);
  192. $expected = [
  193. \Magento\Robots\Model\Config\Value::CACHE_TAG . '_' . $storeId,
  194. ];
  195. $this->assertEquals($expected, $this->block->getIdentities());
  196. }
  197. /**
  198. * Initialize mock object of Event Manager
  199. *
  200. * @param string $data
  201. * @return void
  202. */
  203. protected function initEventManagerMock($data)
  204. {
  205. $this->eventManagerMock->expects($this->any())
  206. ->method('dispatch')
  207. ->willReturnMap([
  208. [
  209. 'view_block_abstract_to_html_before',
  210. [
  211. 'block' => $this->block,
  212. ],
  213. ],
  214. [
  215. 'view_block_abstract_to_html_after',
  216. [
  217. 'block' => $this->block,
  218. 'transport' => new \Magento\Framework\DataObject(['html' => $data]),
  219. ],
  220. ],
  221. ]);
  222. }
  223. /**
  224. * Create and return mock object of \Magento\Sitemap\Model\Sitemap class
  225. *
  226. * @param string $sitemapPath
  227. * @param string $sitemapFilename
  228. * @return \PHPUnit_Framework_MockObject_MockObject
  229. */
  230. protected function getSitemapMock($sitemapPath, $sitemapFilename)
  231. {
  232. $sitemapMock = $this->getMockBuilder(\Magento\Sitemap\Model\Sitemap::class)
  233. ->disableOriginalConstructor()
  234. ->setMethods([
  235. 'getSitemapFilename',
  236. 'getSitemapPath',
  237. 'getSitemapUrl',
  238. ])
  239. ->getMock();
  240. $sitemapMock->expects($this->any())
  241. ->method('getSitemapFilename')
  242. ->willReturn($sitemapFilename);
  243. $sitemapMock->expects($this->any())
  244. ->method('getSitemapPath')
  245. ->willReturn($sitemapPath);
  246. $sitemapMock->expects($this->any())
  247. ->method('getSitemapUrl')
  248. ->with($sitemapPath, $sitemapFilename)
  249. ->willReturn($sitemapFilename);
  250. return $sitemapMock;
  251. }
  252. }