Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
RobotsTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class RobotsTest extends \PHPUnit\Framework\TestCase
14 {
18  private $context;
19 
23  private $storeResolver;
24 
28  private $sitemapCollectionFactory;
29 
33  private $sitemapHelper;
34 
38  private $block;
39 
43  private $eventManagerMock;
44 
48  private $scopeConfigMock;
49 
53  private $storeManager;
54 
55  protected function setUp()
56  {
57  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
58  ->getMockForAbstractClass();
59 
60  $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
61  ->getMockForAbstractClass();
62 
63  $this->context = $this->getMockBuilder(\Magento\Framework\View\Element\Context::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66 
67  $this->context->expects($this->any())
68  ->method('getEventManager')
69  ->willReturn($this->eventManagerMock);
70 
71  $this->context->expects($this->any())
72  ->method('getScopeConfig')
73  ->willReturn($this->scopeConfigMock);
74 
75  $this->storeResolver = $this->getMockBuilder(\Magento\Store\Model\StoreResolver::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78 
79  $this->sitemapCollectionFactory = $this->getMockBuilder(
80  \Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory::class
81  )
82  ->setMethods(['create'])
83  ->disableOriginalConstructor()
84  ->getMock();
85 
86  $this->sitemapHelper = $this->getMockBuilder(\Magento\Sitemap\Helper\Data::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89 
90  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
91  ->getMockForAbstractClass();
92 
93  $this->block = new \Magento\Sitemap\Block\Robots(
94  $this->context,
95  $this->storeResolver,
96  $this->sitemapCollectionFactory,
97  $this->sitemapHelper,
98  $this->storeManager
99  );
100  }
101 
106  {
107  $defaultStoreId = 1;
108  $defaultWebsiteId = 1;
109 
110  $expected = '';
111 
112  $this->initEventManagerMock($expected);
113  $this->scopeConfigMock->expects($this->once())->method('getValue')->willReturn(false);
114 
115  $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
116  ->getMockForAbstractClass();
117 
118  $storeMock->expects($this->once())
119  ->method('getWebsiteId')
120  ->willReturn($defaultWebsiteId);
121 
122  $this->storeManager->expects($this->once())
123  ->method('getDefaultStoreView')
124  ->willReturn($storeMock);
125 
126  $storeMock->expects($this->any())
127  ->method('getWebsiteId')
128  ->willReturn($defaultWebsiteId);
129 
130  $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
131  ->disableOriginalConstructor()
132  ->getMock();
133  $websiteMock->expects($this->any())
134  ->method('getStoreIds')
135  ->willReturn([$defaultStoreId]);
136 
137  $this->storeManager->expects($this->once())
138  ->method('getWebsite')
139  ->with($defaultWebsiteId)
140  ->willReturn($websiteMock);
141 
142  $this->sitemapHelper->expects($this->once())
143  ->method('getEnableSubmissionRobots')
144  ->with($defaultStoreId)
145  ->willReturn(false);
146 
147  $this->assertEquals($expected, $this->block->toHtml());
148  }
149 
154  {
155  $defaultStoreId = 1;
156  $secondStoreId = 2;
157  $defaultWebsiteId = 1;
158 
159  $sitemapPath = '/';
160  $sitemapFilenameOne = 'sitemap.xml';
161  $sitemapFilenameTwo = 'sitemap_custom.xml';
162  $sitemapFilenameThree = 'sitemap.xml';
163 
164  $expected = 'Sitemap: ' . $sitemapFilenameOne
165  . PHP_EOL
166  . 'Sitemap: ' . $sitemapFilenameTwo
167  . PHP_EOL;
168 
169  $this->initEventManagerMock($expected);
170  $this->scopeConfigMock->expects($this->once())->method('getValue')->willReturn(false);
171 
172  $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
173  ->getMockForAbstractClass();
174 
175  $this->storeManager->expects($this->once())
176  ->method('getDefaultStoreView')
177  ->willReturn($storeMock);
178 
179  $storeMock->expects($this->any())
180  ->method('getWebsiteId')
181  ->willReturn($defaultWebsiteId);
182 
183  $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
184  ->disableOriginalConstructor()
185  ->getMock();
186  $websiteMock->expects($this->any())
187  ->method('getStoreIds')
188  ->willReturn([$defaultStoreId, $secondStoreId]);
189 
190  $this->storeManager->expects($this->once())
191  ->method('getWebsite')
192  ->with($defaultWebsiteId)
193  ->willReturn($websiteMock);
194 
195  $this->sitemapHelper->expects($this->any())
196  ->method('getEnableSubmissionRobots')
197  ->willReturnMap([
198  [$defaultStoreId, true],
199  [$secondStoreId, false],
200  ]);
201 
202  $sitemapMockOne = $this->getSitemapMock($sitemapPath, $sitemapFilenameOne);
203  $sitemapMockTwo = $this->getSitemapMock($sitemapPath, $sitemapFilenameTwo);
204  $sitemapMockThree = $this->getSitemapMock($sitemapPath, $sitemapFilenameThree);
205 
206  $sitemapCollectionMock = $this->getMockBuilder(\Magento\Sitemap\Model\ResourceModel\Sitemap\Collection::class)
207  ->disableOriginalConstructor()
208  ->getMock();
209  $sitemapCollectionMock->expects($this->any())
210  ->method('addStoreFilter')
211  ->with([$defaultStoreId])
212  ->willReturnSelf();
213 
214  $sitemapCollectionMock->expects($this->any())
215  ->method('getIterator')
216  ->willReturn(new \ArrayIterator([$sitemapMockOne, $sitemapMockTwo, $sitemapMockThree]));
217 
218  $this->sitemapCollectionFactory->expects($this->once())
219  ->method('create')
220  ->willReturn($sitemapCollectionMock);
221 
222  $this->assertEquals($expected, $this->block->toHtml());
223  }
224 
228  public function testGetIdentities()
229  {
230  $storeId = 1;
231 
232  $storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)->getMockForAbstractClass();
233 
234  $this->storeManager->expects($this->once())
235  ->method('getDefaultStoreView')
236  ->willReturn($storeMock);
237 
238  $storeMock->expects($this->once())
239  ->method('getId')
240  ->willReturn($storeId);
241 
242  $expected = [
244  ];
245  $this->assertEquals($expected, $this->block->getIdentities());
246  }
247 
254  protected function initEventManagerMock($data)
255  {
256  $this->eventManagerMock->expects($this->any())
257  ->method('dispatch')
258  ->willReturnMap([
259  [
260  'view_block_abstract_to_html_before',
261  [
262  'block' => $this->block,
263  ],
264  ],
265  [
266  'view_block_abstract_to_html_after',
267  [
268  'block' => $this->block,
269  'transport' => new \Magento\Framework\DataObject(['html' => $data]),
270  ],
271  ],
272  ]);
273  }
274 
282  protected function getSitemapMock($sitemapPath, $sitemapFilename)
283  {
284  $sitemapMock = $this->getMockBuilder(\Magento\Sitemap\Model\Sitemap::class)
285  ->disableOriginalConstructor()
286  ->setMethods([
287  'getSitemapFilename',
288  'getSitemapPath',
289  'getSitemapUrl',
290  ])
291  ->getMock();
292 
293  $sitemapMock->expects($this->any())
294  ->method('getSitemapFilename')
295  ->willReturn($sitemapFilename);
296  $sitemapMock->expects($this->any())
297  ->method('getSitemapPath')
298  ->willReturn($sitemapPath);
299  $sitemapMock->expects($this->any())
300  ->method('getSitemapUrl')
301  ->with($sitemapPath, $sitemapFilename)
302  ->willReturn($sitemapFilename);
303 
304  return $sitemapMock;
305  }
306 }
getSitemapMock($sitemapPath, $sitemapFilename)
Definition: RobotsTest.php:282