Skip to content

Commit 80e9b17

Browse files
committed
ACP2E-3103: New Products RSS feed is not updated with new products due to cache
1 parent 1b99b6d commit 80e9b17

File tree

6 files changed

+107
-16
lines changed

6 files changed

+107
-16
lines changed

app/code/Magento/Catalog/Block/Rss/Product/NewProducts.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
use Magento\Framework\App\Rss\DataProviderInterface;
99

10-
/**
11-
* Class NewProducts
12-
* @package Magento\Catalog\Block\Rss\Product
13-
*/
14-
class NewProducts extends \Magento\Framework\View\Element\AbstractBlock implements DataProviderInterface
10+
class NewProducts extends \Magento\Framework\View\Element\AbstractBlock implements DataProviderInterface,
11+
\Magento\Framework\DataObject\IdentityInterface
1512
{
13+
public const CACHE_TAG = 'rss_p_new';
14+
1615
/**
1716
* @var \Magento\Catalog\Helper\Image
1817
*/
@@ -55,6 +54,8 @@ public function __construct(
5554
}
5655

5756
/**
57+
* Configure class
58+
*
5859
* @return void
5960
*/
6061
protected function _construct()
@@ -64,15 +65,15 @@ protected function _construct()
6465
}
6566

6667
/**
67-
* {@inheritdoc}
68+
* @inheritdoc
6869
*/
6970
public function isAllowed()
7071
{
7172
return $this->_scopeConfig->isSetFlag('rss/catalog/new', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
7273
}
7374

7475
/**
75-
* {@inheritdoc}
76+
* @inheritdoc
7677
*/
7778
public function getRssData()
7879
{
@@ -132,6 +133,8 @@ public function getRssData()
132133
}
133134

134135
/**
136+
* Get current store id
137+
*
135138
* @return int
136139
*/
137140
protected function getStoreId()
@@ -177,14 +180,16 @@ protected function renderPriceHtml(\Magento\Catalog\Model\Product $product)
177180
}
178181

179182
/**
180-
* {@inheritdoc}
183+
* @inheritdoc
181184
*/
182185
public function getCacheLifetime()
183186
{
184187
return 600;
185188
}
186189

187190
/**
191+
* Generate rss feed
192+
*
188193
* @return array
189194
*/
190195
public function getFeeds()
@@ -199,10 +204,18 @@ public function getFeeds()
199204
}
200205

201206
/**
202-
* {@inheritdoc}
207+
* @inheritdoc
203208
*/
204209
public function isAuthRequired()
205210
{
206211
return false;
207212
}
213+
214+
/**
215+
* @inheritdoc
216+
*/
217+
public function getIdentities()
218+
{
219+
return [self::CACHE_TAG];
220+
}
208221
}

app/code/Magento/Catalog/Model/Product.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,6 +2413,7 @@ public function getIdentities()
24132413
|| $this->isObjectNew();
24142414
if ($isProductNew && ($isStatusChanged || $this->getStatus() == Status::STATUS_ENABLED)) {
24152415
$identities[] = \Magento\Catalog\Block\Product\NewProduct::CACHE_TAG;
2416+
$identities[] = \Magento\Catalog\Block\Rss\Product\NewProducts::CACHE_TAG;
24162417
}
24172418

24182419
return array_unique($identities);

app/code/Magento/Rss/Controller/Feed/Index.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
*/
77
namespace Magento\Rss\Controller\Feed;
88

9+
use Magento\Framework\DataObject\IdentityInterface;
910
use Magento\Framework\Exception\NotFoundException;
1011

11-
/**
12-
* Class Index
13-
* @package Magento\Rss\Controller\Feed
14-
*/
1512
class Index extends \Magento\Rss\Controller\Feed
1613
{
1714
/**
@@ -46,6 +43,11 @@ public function execute()
4643
$rss->setDataProvider($provider);
4744

4845
$this->getResponse()->setHeader('Content-type', 'text/xml; charset=UTF-8');
46+
$tags = ['rss'];
47+
if ($provider instanceof IdentityInterface) {
48+
$tags = array_merge($tags, $provider->getIdentities());
49+
}
50+
$this->getResponse()->setHeader('X-Magento-Tags', implode(',', $tags));
4951
$this->getResponse()->setBody($rss->createRssXml());
5052
}
5153
}

app/code/Magento/Rss/Model/Rss.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\App\FeedFactoryInterface;
1313
use Magento\Framework\App\ObjectManager;
1414
use Magento\Framework\App\Rss\DataProviderInterface;
15+
use Magento\Framework\DataObject\IdentityInterface;
1516
use Magento\Framework\Exception\InputException;
1617
use Magento\Framework\Exception\RuntimeException;
1718
use Magento\Framework\Serialize\SerializerInterface;
@@ -83,12 +84,26 @@ public function getFeeds()
8384
$serializedData = $this->serializer->serialize($this->dataProvider->getRssData());
8485

8586
if ($cacheKey && $cacheLifeTime) {
86-
$this->cache->save($serializedData, $cacheKey, ['rss'], $cacheLifeTime);
87+
$this->cache->save($serializedData, $cacheKey, $this->getCacheTags(), $cacheLifeTime);
8788
}
8889

8990
return $this->serializer->unserialize($serializedData);
9091
}
9192

93+
/**
94+
* Returns cache tags
95+
*
96+
* @return array|string[]
97+
*/
98+
private function getCacheTags()
99+
{
100+
$tags = ['rss'];
101+
if ($this->dataProvider instanceof IdentityInterface) {
102+
$tags = array_merge($tags, $this->dataProvider->getIdentities());
103+
}
104+
return $tags;
105+
}
106+
92107
/**
93108
* Sets data provider
94109
*

app/code/Magento/Rss/Test/Unit/Controller/Feed/IndexTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,16 @@ public function testExecute()
9292
$rssModel->expects($this->once())->method('setDataProvider')->willReturnSelf();
9393
$rssModel->expects($this->once())->method('createRssXml')->willReturn('');
9494

95-
$this->response->expects($this->once())->method('setHeader')->willReturnSelf();
95+
$matcher = $this->exactly(2);
96+
$this->response->expects($matcher)
97+
->method('setHeader')
98+
->willReturnCallback(function (string $param) use ($matcher) {
99+
match ($matcher->numberOfInvocations()) {
100+
1 => $this->assertEquals($param, 'Content-type'),
101+
2 => $this->assertEquals($param, 'X-Magento-Tags'),
102+
};
103+
})
104+
->willReturnSelf();
96105
$this->response->expects($this->once())->method('setBody')->willReturnSelf();
97106

98107
$this->rssFactory->expects($this->once())->method('create')->willReturn($rssModel);
@@ -118,7 +127,16 @@ public function testExecuteWithException()
118127
$exceptionMock
119128
);
120129

121-
$this->response->expects($this->once())->method('setHeader')->willReturnSelf();
130+
$matcher = $this->exactly(2);
131+
$this->response->expects($matcher)
132+
->method('setHeader')
133+
->willReturnCallback(function (string $param) use ($matcher) {
134+
match ($matcher->numberOfInvocations()) {
135+
1 => $this->assertEquals($param, 'Content-type'),
136+
2 => $this->assertEquals($param, 'X-Magento-Tags'),
137+
};
138+
})
139+
->willReturnSelf();
122140
$this->rssFactory->expects($this->once())->method('create')->willReturn($rssModel);
123141
$this->rssManager->expects($this->once())->method('getProvider')->willReturn($dataProvider);
124142

app/code/Magento/Rss/Test/Unit/Model/RssTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Rss\Test\Unit\Model;
99

10+
use Magento\Catalog\Block\Rss\Product\NewProducts;
1011
use Magento\Framework\App\CacheInterface;
1112
use Magento\Framework\App\FeedFactoryInterface;
1213
use Magento\Framework\App\FeedInterface;
@@ -148,6 +149,47 @@ public function testGetFeeds(): void
148149
$this->assertEquals($this->feedData, $this->rss->getFeeds());
149150
}
150151

152+
/**
153+
* Get new products feed test
154+
*
155+
* @return void
156+
*/
157+
public function testGetNewProductsFeed(): void
158+
{
159+
$dataProvider = $this->createMock(NewProducts::class);
160+
$dataProvider->expects($this->atLeastOnce())
161+
->method('getCacheKey')
162+
->willReturn('cache_key');
163+
$dataProvider->expects($this->atLeastOnce())
164+
->method('getCacheLifetime')
165+
->willReturn(100);
166+
$dataProvider->expects($this->once())
167+
->method('getRssData')
168+
->willReturn($this->feedData);
169+
$dataProvider->expects($this->once())->method('getIdentities')->willReturn(['identity']);
170+
171+
$this->rss->setDataProvider($dataProvider);
172+
173+
$this->cacheMock->expects($this->once())
174+
->method('load')
175+
->with('cache_key')
176+
->willReturn(false);
177+
$this->cacheMock->expects($this->once())
178+
->method('save')
179+
->with('serializedData')
180+
->willReturn(true);
181+
$this->serializerMock->expects($this->once())
182+
->method('serialize')
183+
->with($this->feedData)
184+
->willReturn(self::STUB_SERIALIZED_DATA);
185+
$this->serializerMock->expects($this->once())
186+
->method('unserialize')
187+
->with(self::STUB_SERIALIZED_DATA)
188+
->willReturn($this->feedData);
189+
190+
$this->assertEquals($this->feedData, $this->rss->getFeeds());
191+
}
192+
151193
public function testGetFeedsWithCache()
152194
{
153195
$dataProvider = $this->getMockForAbstractClass(DataProviderInterface::class);

0 commit comments

Comments
 (0)