Skip to content

Commit eebd8a8

Browse files
author
Sergey Semenov
committed
MAGETWO-47607: [Github] Sitemap generation in wrong folder when vhost is connected to pub folder
1 parent 21995e2 commit eebd8a8

File tree

9 files changed

+303
-40
lines changed

9 files changed

+303
-40
lines changed

app/code/Magento/Robots/Block/Data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct(
5555
*/
5656
protected function _toHtml()
5757
{
58-
return $this->robots->getData();
58+
return $this->robots->getData() . PHP_EOL;
5959
}
6060

6161
/**

app/code/Magento/Robots/Model/Config/Value.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
/**
1919
* Backend model for design/search_engine_robots/custom_instructions configuration value.
2020
* Required to implement Page Cache functionality.
21+
*
22+
* @api
2123
*/
2224
class Value extends ConfigValue implements IdentityInterface
2325
{

app/code/Magento/Robots/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
The Robots module contains router to match application action class for robots.txt requests and realizes the possibility of obtaining content of robots.txt file depending on the current website.
1+
The Robots module provides the following functionalities:
2+
* contains a router to match application action class for requests to the `robots.txt` file;
3+
* allows obtaining the content of the `robots.txt` file depending on the settings of the current website.

app/code/Magento/Robots/Test/Unit/Block/DataTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ protected function setUp()
6565
*/
6666
public function testToHtml()
6767
{
68-
$expected = 'test';
68+
$data = 'test';
6969

70-
$this->initEventManagerMock($expected);
70+
$this->initEventManagerMock($data);
7171

7272
$this->robots->expects($this->once())
7373
->method('getData')
74-
->willReturn($expected);
74+
->willReturn($data);
7575

76-
$this->assertEquals($expected, $this->block->toHtml());
76+
$this->assertEquals($data . PHP_EOL, $this->block->toHtml());
7777
}
7878

7979
/**

app/code/Magento/Sitemap/Block/Robots.php

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Robots\Model\Config\Value;
1212
use Magento\Sitemap\Helper\Data as SitemapHelper;
1313
use Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory;
14+
use Magento\Store\Model\StoreManagerInterface;
1415
use Magento\Store\Model\StoreResolver;
1516

1617
/**
@@ -35,60 +36,94 @@ class Robots extends AbstractBlock implements IdentityInterface
3536
*/
3637
private $sitemapHelper;
3738

39+
/**
40+
* @var StoreManagerInterface
41+
*/
42+
private $storeManager;
43+
3844
/**
3945
* @param Context $context
4046
* @param StoreResolver $storeResolver
4147
* @param CollectionFactory $sitemapCollectionFactory
4248
* @param SitemapHelper $sitemapHelper
49+
* @param StoreManagerInterface $storeManager
4350
* @param array $data
4451
*/
4552
public function __construct(
4653
Context $context,
4754
StoreResolver $storeResolver,
4855
CollectionFactory $sitemapCollectionFactory,
4956
SitemapHelper $sitemapHelper,
57+
StoreManagerInterface $storeManager,
5058
array $data = []
5159
) {
5260
$this->storeResolver = $storeResolver;
5361
$this->sitemapCollectionFactory = $sitemapCollectionFactory;
5462
$this->sitemapHelper = $sitemapHelper;
63+
$this->storeManager = $storeManager;
5564

5665
parent::__construct($context, $data);
5766
}
5867

5968
/**
6069
* Prepare sitemap links to add to the robots.txt file
6170
*
62-
* Detects if sitemap file information is required to be added to robots.txt,
63-
* then gets the name of sitemap files that linked with current store,
64-
* and adds record for this sitemap files into result data.
71+
* Collects sitemap links for all stores of given website.
72+
* Detects if sitemap file information is required to be added to robots.txt
73+
* and adds links for this sitemap files into result data.
6574
*
6675
* @return string
6776
*/
6877
protected function _toHtml()
6978
{
70-
$result = '';
79+
$defaultStoreId = $this->storeResolver->getCurrentStoreId();
80+
$defalutStore = $this->storeManager->getStore($defaultStoreId);
7181

72-
$storeId = $this->storeResolver->getCurrentStoreId();
82+
/** @var \Magento\Store\Model\Website $website */
83+
$website = $this->storeManager->getWebsite($defalutStore->getWebsiteId());
7384

74-
if ((bool)$this->sitemapHelper->getEnableSubmissionRobots($storeId)) {
75-
/** @var \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection $collection */
76-
$collection = $this->sitemapCollectionFactory->create();
77-
$collection->addStoreFilter([$storeId]);
85+
$storeIds = [];
86+
foreach ($website->getStoreIds() as $storeId) {
87+
if ((bool)$this->sitemapHelper->getEnableSubmissionRobots($storeId)) {
88+
$storeIds[] = (int)$storeId;
89+
}
90+
}
7891

79-
foreach ($collection as $sitemap) {
80-
/** @var \Magento\Sitemap\Model\Sitemap $sitemap */
81-
$sitemapFilename = $sitemap->getSitemapFilename();
82-
$sitemapPath = $sitemap->getSitemapPath();
92+
$links = [];
93+
if ($storeIds) {
94+
$links = array_merge($links, $this->getSitemapLinks($storeIds));
95+
}
8396

84-
$robotsSitemapLine = 'Sitemap: ' . $sitemap->getSitemapUrl($sitemapPath, $sitemapFilename);
85-
if (strpos($result, $robotsSitemapLine) === false) {
86-
$result .= PHP_EOL . $robotsSitemapLine;
87-
}
88-
}
97+
return $links ? implode(PHP_EOL, $links) . PHP_EOL : '';
98+
}
99+
100+
/**
101+
* Retrieve sitemap links for given store
102+
*
103+
* Gets the names of sitemap files that linked with given store,
104+
* and adds links for this sitemap files into result array.
105+
*
106+
* @param int[] $storeIds
107+
* @return array
108+
*/
109+
protected function getSitemapLinks(array $storeIds)
110+
{
111+
$sitemapLinks = [];
112+
113+
/** @var \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection $collection */
114+
$collection = $this->sitemapCollectionFactory->create();
115+
$collection->addStoreFilter($storeIds);
116+
117+
foreach ($collection as $sitemap) {
118+
/** @var \Magento\Sitemap\Model\Sitemap $sitemap */
119+
$sitemapFilename = $sitemap->getSitemapFilename();
120+
$sitemapPath = $sitemap->getSitemapPath();
121+
122+
$sitemapUrl = $sitemap->getSitemapUrl($sitemapPath, $sitemapFilename);
123+
$sitemapLinks[$sitemapUrl] = 'Sitemap: ' . $sitemapUrl;
89124
}
90125

91-
return $result;
126+
return $sitemapLinks;
92127
}
93128

94129
/**
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sitemap\Model\Config\Backend;
7+
8+
use Magento\Framework\App\Cache\TypeListInterface;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\App\Config\Value;
11+
use Magento\Framework\Data\Collection\AbstractDb;
12+
use Magento\Framework\DataObject\IdentityInterface;
13+
use Magento\Framework\Model\Context;
14+
use Magento\Framework\Model\ResourceModel\AbstractResource;
15+
use Magento\Framework\Registry;
16+
use Magento\Robots\Model\Config\Value as RobotsValue;
17+
use Magento\Store\Model\StoreResolver;
18+
19+
/**
20+
* Backend model for sitemap/search_engines/submission_robots configuration value.
21+
* Required to implement Page Cache functionality.
22+
*/
23+
class Robots extends Value implements IdentityInterface
24+
{
25+
/**
26+
* Model cache tag for clear cache in after save and after delete
27+
*
28+
* @var string
29+
*/
30+
protected $_cacheTag = true;
31+
32+
/**
33+
* @var StoreResolver
34+
*/
35+
private $storeResolver;
36+
37+
/**
38+
* @param Context $context
39+
* @param Registry $registry
40+
* @param ScopeConfigInterface $config
41+
* @param TypeListInterface $cacheTypeList
42+
* @param StoreResolver $storeResolver
43+
* @param AbstractResource|null $resource
44+
* @param AbstractDb|null $resourceCollection
45+
* @param array $data
46+
*/
47+
public function __construct(
48+
Context $context,
49+
Registry $registry,
50+
ScopeConfigInterface $config,
51+
TypeListInterface $cacheTypeList,
52+
StoreResolver $storeResolver,
53+
AbstractResource $resource = null,
54+
AbstractDb $resourceCollection = null,
55+
array $data = []
56+
) {
57+
$this->storeResolver = $storeResolver;
58+
59+
parent::__construct(
60+
$context,
61+
$registry,
62+
$config,
63+
$cacheTypeList,
64+
$resource,
65+
$resourceCollection,
66+
$data
67+
);
68+
}
69+
70+
/**
71+
* Get unique page cache identities
72+
*
73+
* @return array
74+
*/
75+
public function getIdentities()
76+
{
77+
return [
78+
RobotsValue::CACHE_TAG . '_' . $this->storeResolver->getCurrentStoreId(),
79+
];
80+
}
81+
}

app/code/Magento/Sitemap/Test/Unit/Block/RobotsTest.php

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class RobotsTest extends \PHPUnit_Framework_TestCase
3737
*/
3838
private $eventManagerMock;
3939

40+
/**
41+
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $storeManager;
44+
4045
protected function setUp()
4146
{
4247
$this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
@@ -64,11 +69,15 @@ protected function setUp()
6469
->disableOriginalConstructor()
6570
->getMock();
6671

72+
$this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
73+
->getMockForAbstractClass();
74+
6775
$this->block = new \Magento\Sitemap\Block\Robots(
6876
$this->context,
6977
$this->storeResolver,
7078
$this->sitemapCollectionFactory,
71-
$this->sitemapHelper
79+
$this->sitemapHelper,
80+
$this->storeManager
7281
);
7382
}
7483

@@ -77,19 +86,42 @@ protected function setUp()
7786
*/
7887
public function testToHtmlRobotsSubmissionIsDisabled()
7988
{
80-
$storeId = 1;
89+
$defaultStoreId = 1;
90+
$defaultWebsiteId = 1;
8191

8292
$expected = '';
8393

8494
$this->initEventManagerMock($expected);
8595

8696
$this->storeResolver->expects($this->once())
8797
->method('getCurrentStoreId')
88-
->willReturn($storeId);
98+
->willReturn($defaultStoreId);
99+
100+
$storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
101+
->getMockForAbstractClass();
102+
$storeMock->expects($this->any())
103+
->method('getWebsiteId')
104+
->willReturn($defaultWebsiteId);
105+
106+
$websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
107+
->disableOriginalConstructor()
108+
->getMock();
109+
$websiteMock->expects($this->any())
110+
->method('getStoreIds')
111+
->willReturn([$defaultStoreId]);
112+
113+
$this->storeManager->expects($this->once())
114+
->method('getStore')
115+
->with($defaultStoreId)
116+
->willReturn($storeMock);
117+
$this->storeManager->expects($this->once())
118+
->method('getWebsite')
119+
->with($defaultWebsiteId)
120+
->willReturn($websiteMock);
89121

90122
$this->sitemapHelper->expects($this->once())
91123
->method('getEnableSubmissionRobots')
92-
->with($storeId)
124+
->with($defaultStoreId)
93125
->willReturn(false);
94126

95127
$this->assertEquals($expected, $this->block->toHtml());
@@ -100,29 +132,54 @@ public function testToHtmlRobotsSubmissionIsDisabled()
100132
*/
101133
public function testAfterGetDataRobotsSubmissionIsEnabled()
102134
{
103-
$storeId = 1;
135+
$defaultStoreId = 1;
136+
$secondStoreId = 2;
137+
$defaultWebsiteId = 1;
104138

105139
$sitemapPath = '/';
106140
$sitemapFilenameOne = 'sitemap.xml';
107141
$sitemapFilenameTwo = 'sitemap_custom.xml';
108142
$sitemapFilenameThree = 'sitemap.xml';
109143

110-
$expected = ''
111-
. PHP_EOL
112-
. 'Sitemap: ' . $sitemapFilenameOne
144+
$expected = 'Sitemap: ' . $sitemapFilenameOne
113145
. PHP_EOL
114-
. 'Sitemap: ' . $sitemapFilenameTwo;
146+
. 'Sitemap: ' . $sitemapFilenameTwo
147+
. PHP_EOL;
115148

116149
$this->initEventManagerMock($expected);
117150

118151
$this->storeResolver->expects($this->once())
119152
->method('getCurrentStoreId')
120-
->willReturn($storeId);
153+
->willReturn($defaultStoreId);
121154

122-
$this->sitemapHelper->expects($this->once())
155+
$storeMock = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
156+
->getMockForAbstractClass();
157+
$storeMock->expects($this->any())
158+
->method('getWebsiteId')
159+
->willReturn($defaultWebsiteId);
160+
161+
$websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
162+
->disableOriginalConstructor()
163+
->getMock();
164+
$websiteMock->expects($this->any())
165+
->method('getStoreIds')
166+
->willReturn([$defaultStoreId, $secondStoreId]);
167+
168+
$this->storeManager->expects($this->once())
169+
->method('getStore')
170+
->with($defaultStoreId)
171+
->willReturn($storeMock);
172+
$this->storeManager->expects($this->once())
173+
->method('getWebsite')
174+
->with($defaultWebsiteId)
175+
->willReturn($websiteMock);
176+
177+
$this->sitemapHelper->expects($this->any())
123178
->method('getEnableSubmissionRobots')
124-
->with($storeId)
125-
->willReturn(true);
179+
->willReturnMap([
180+
[$defaultStoreId, true],
181+
[$secondStoreId, false],
182+
]);
126183

127184
$sitemapMockOne = $this->getSitemapMock($sitemapPath, $sitemapFilenameOne);
128185
$sitemapMockTwo = $this->getSitemapMock($sitemapPath, $sitemapFilenameTwo);
@@ -131,9 +188,9 @@ public function testAfterGetDataRobotsSubmissionIsEnabled()
131188
$sitemapCollectionMock = $this->getMockBuilder(\Magento\Sitemap\Model\ResourceModel\Sitemap\Collection::class)
132189
->disableOriginalConstructor()
133190
->getMock();
134-
$sitemapCollectionMock->expects($this->once())
191+
$sitemapCollectionMock->expects($this->any())
135192
->method('addStoreFilter')
136-
->with([$storeId])
193+
->with([$defaultStoreId])
137194
->willReturnSelf();
138195

139196
$sitemapCollectionMock->expects($this->any())

0 commit comments

Comments
 (0)