Skip to content

Commit b2a8ff4

Browse files
fix rss first load
1 parent 237c2b1 commit b2a8ff4

File tree

3 files changed

+97
-47
lines changed

3 files changed

+97
-47
lines changed

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
namespace Magento\Rss\Model;
1010

11+
use Magento\Framework\App\CacheInterface;
1112
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\App\Rss\DataProviderInterface;
14+
use Magento\Framework\Exception\InputException;
15+
use Magento\Framework\Exception\RuntimeException;
1316
use Magento\Framework\Serialize\SerializerInterface;
1417
use Magento\Framework\App\FeedFactoryInterface;
1518

@@ -27,12 +30,12 @@ class Rss
2730
protected $dataProvider;
2831

2932
/**
30-
* @var \Magento\Framework\App\CacheInterface
33+
* @var CacheInterface
3134
*/
3235
protected $cache;
3336

3437
/**
35-
* @var \Magento\Framework\App\FeedFactoryInterface
38+
* @var FeedFactoryInterface
3639
*/
3740
private $feedFactory;
3841

@@ -44,12 +47,12 @@ class Rss
4447
/**
4548
* Rss constructor
4649
*
47-
* @param \Magento\Framework\App\CacheInterface $cache
50+
* @param CacheInterface $cache
4851
* @param SerializerInterface|null $serializer
4952
* @param FeedFactoryInterface|null $feedFactory
5053
*/
5154
public function __construct(
52-
\Magento\Framework\App\CacheInterface $cache,
55+
CacheInterface $cache,
5356
SerializerInterface $serializer = null,
5457
FeedFactoryInterface $feedFactory = null
5558
) {
@@ -59,54 +62,56 @@ public function __construct(
5962
}
6063

6164
/**
65+
* Returns feeds
66+
*
6267
* @return array
6368
*/
6469
public function getFeeds()
6570
{
6671
if ($this->dataProvider === null) {
6772
return [];
6873
}
69-
$cache = false;
70-
if ($this->dataProvider->getCacheKey() && $this->dataProvider->getCacheLifetime()) {
71-
$cache = $this->cache->load($this->dataProvider->getCacheKey());
72-
}
74+
$cacheKey = $this->dataProvider->getCacheKey();
75+
$cacheLifeTime = $this->dataProvider->getCacheLifetime();
7376

77+
$cache = $cacheKey && $cacheLifeTime ? $this->cache->load($cacheKey) : false;
7478
if ($cache) {
7579
return $this->serializer->unserialize($cache);
7680
}
7781

78-
$data = $this->dataProvider->getRssData();
82+
$serializedData = $this->serializer->serialize($this->dataProvider->getRssData());
7983

80-
if ($this->dataProvider->getCacheKey() && $this->dataProvider->getCacheLifetime()) {
81-
$this->cache->save(
82-
$this->serializer->serialize($data),
83-
$this->dataProvider->getCacheKey(),
84-
['rss'],
85-
$this->dataProvider->getCacheLifetime()
86-
);
84+
if ($cacheKey && $cacheLifeTime) {
85+
$this->cache->save($serializedData, $cacheKey, ['rss'], $cacheLifeTime);
8786
}
8887

89-
return $data;
88+
return $this->serializer->unserialize($serializedData);
9089
}
9190

9291
/**
92+
* Sets data provider
93+
*
9394
* @param DataProviderInterface $dataProvider
9495
* @return $this
9596
*/
9697
public function setDataProvider(DataProviderInterface $dataProvider)
9798
{
9899
$this->dataProvider = $dataProvider;
100+
99101
return $this;
100102
}
101103

102104
/**
105+
* Returns rss xml
106+
*
103107
* @return string
104-
* @throws \Magento\Framework\Exception\InputException
105-
* @throws \Magento\Framework\Exception\RuntimeException
108+
* @throws InputException
109+
* @throws RuntimeException
106110
*/
107111
public function createRssXml()
108112
{
109113
$feed = $this->feedFactory->create($this->getFeeds(), FeedFactoryInterface::FORMAT_RSS);
114+
110115
return $feed->getFormattedContent();
111116
}
112117
}

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
use PHPUnit\Framework\MockObject\MockObject;
1818
use PHPUnit\Framework\TestCase;
1919

20+
/**
21+
* Test for \Magento\Rss\Model\Rss.
22+
*/
2023
class RssTest extends TestCase
2124
{
25+
private const STUB_SERIALIZED_DATA = 'serializedData';
26+
2227
/**
2328
* @var Rss
2429
*/
25-
protected $rss;
30+
private $rss;
2631

2732
/**
2833
* @var array
@@ -62,11 +67,6 @@ class RssTest extends TestCase
6267
</channel>
6368
</rss>';
6469

65-
/**
66-
* @var ObjectManagerHelper
67-
*/
68-
protected $objectManagerHelper;
69-
7070
/**
7171
* @var CacheInterface|MockObject
7272
*/
@@ -87,15 +87,18 @@ class RssTest extends TestCase
8787
*/
8888
private $serializerMock;
8989

90+
/**
91+
* @inheritDoc
92+
*/
9093
protected function setUp(): void
9194
{
9295
$this->cacheMock = $this->getMockForAbstractClass(CacheInterface::class);
9396
$this->serializerMock = $this->getMockForAbstractClass(SerializerInterface::class);
9497
$this->feedFactoryMock = $this->getMockForAbstractClass(FeedFactoryInterface::class);
9598
$this->feedMock = $this->getMockForAbstractClass(FeedInterface::class);
9699

97-
$this->objectManagerHelper = new ObjectManagerHelper($this);
98-
$this->rss = $this->objectManagerHelper->getObject(
100+
$objectManagerHelper = new ObjectManagerHelper($this);
101+
$this->rss = $objectManagerHelper->getObject(
99102
Rss::class,
100103
[
101104
'cache' => $this->cacheMock,
@@ -105,12 +108,23 @@ protected function setUp(): void
105108
);
106109
}
107110

108-
public function testGetFeeds()
111+
/**
112+
* Get feeds test
113+
*
114+
* @return void
115+
*/
116+
public function testGetFeeds(): void
109117
{
110118
$dataProvider = $this->getMockForAbstractClass(DataProviderInterface::class);
111-
$dataProvider->expects($this->any())->method('getCacheKey')->willReturn('cache_key');
112-
$dataProvider->expects($this->any())->method('getCacheLifetime')->willReturn(100);
113-
$dataProvider->expects($this->any())->method('getRssData')->willReturn($this->feedData);
119+
$dataProvider->expects($this->atLeastOnce())
120+
->method('getCacheKey')
121+
->willReturn('cache_key');
122+
$dataProvider->expects($this->atLeastOnce())
123+
->method('getCacheLifetime')
124+
->willReturn(100);
125+
$dataProvider->expects($this->once())
126+
->method('getRssData')
127+
->willReturn($this->feedData);
114128

115129
$this->rss->setDataProvider($dataProvider);
116130

@@ -125,7 +139,11 @@ public function testGetFeeds()
125139
$this->serializerMock->expects($this->once())
126140
->method('serialize')
127141
->with($this->feedData)
128-
->willReturn('serializedData');
142+
->willReturn(self::STUB_SERIALIZED_DATA);
143+
$this->serializerMock->expects($this->once())
144+
->method('unserialize')
145+
->with(self::STUB_SERIALIZED_DATA)
146+
->willReturn($this->feedData);
129147

130148
$this->assertEquals($this->feedData, $this->rss->getFeeds());
131149
}
@@ -168,6 +186,14 @@ public function testCreateRssXml()
168186
->with($this->feedData, FeedFactoryInterface::FORMAT_RSS)
169187
->willReturn($this->feedMock);
170188

189+
$this->serializerMock->expects($this->once())
190+
->method('serialize')
191+
->willReturn(self::STUB_SERIALIZED_DATA);
192+
$this->serializerMock->expects($this->once())
193+
->method('unserialize')
194+
->with(self::STUB_SERIALIZED_DATA)
195+
->willReturn($this->feedData);
196+
171197
$this->rss->setDataProvider($dataProvider);
172198
$this->assertNotNull($this->rss->createRssXml());
173199
}

dev/tests/integration/testsuite/Magento/Rss/Controller/Feed/IndexTest.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,42 @@
77

88
namespace Magento\Rss\Controller\Feed;
99

10-
class IndexTest extends \Magento\TestFramework\TestCase\AbstractBackendController
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Model\Session;
12+
use Magento\TestFramework\TestCase\AbstractBackendController;
13+
use Magento\Wishlist\Model\Wishlist;
14+
15+
/**
16+
* Test for \Magento\Rss\Controller\Feed\Index.
17+
*/
18+
class IndexTest extends AbstractBackendController
1119
{
12-
/**
13-
* @var \Magento\Rss\Model\UrlBuilder
14-
*/
15-
private $urlBuilder;
20+
private const RSS_NEW_PRODUCTS_PATH = 'rss/feed/index/type/new_products/';
1621

1722
/**
18-
* @var \Magento\Customer\Api\CustomerRepositoryInterface
23+
* @var CustomerRepositoryInterface
1924
*/
2025
private $customerRepository;
2126

2227
/**
23-
* @var \Magento\Wishlist\Model\Wishlist
28+
* @var Wishlist
2429
*/
2530
private $wishlist;
2631

2732
/**
28-
* @var
33+
* @var Session
2934
*/
3035
private $customerSession;
3136

37+
/**
38+
* @inheritDoc
39+
*/
3240
protected function setUp(): void
3341
{
3442
parent::setUp();
35-
$this->urlBuilder = $this->_objectManager->get(\Magento\Rss\Model\UrlBuilder::class);
36-
$this->customerRepository = $this->_objectManager->get(
37-
\Magento\Customer\Api\CustomerRepositoryInterface::class
38-
);
39-
$this->wishlist = $this->_objectManager->get(\Magento\Wishlist\Model\Wishlist::class);
40-
$this->customerSession = $this->_objectManager->get(\Magento\Customer\Model\Session::class);
43+
$this->customerRepository = $this->_objectManager->get(CustomerRepositoryInterface::class);
44+
$this->wishlist = $this->_objectManager->get(Wishlist::class);
45+
$this->customerSession = $this->_objectManager->get(Session::class);
4146
}
4247

4348
/**
@@ -60,6 +65,21 @@ public function testRssResponse()
6065
$this->assertStringContainsString('<title>John Smith\'s Wishlist</title>', $body);
6166
}
6267

68+
/**
69+
* Check Rss response from `New Products`.
70+
*
71+
* @magentoConfigFixture current_store rss/catalog/new 1
72+
* @magentoConfigFixture current_store rss/config/active 1
73+
*
74+
* @return void
75+
*/
76+
public function testRssResponseNewProducts(): void
77+
{
78+
$this->dispatch(self::RSS_NEW_PRODUCTS_PATH);
79+
$body = $this->getResponse()->getBody();
80+
$this->assertStringContainsString('<title>New Products from Main Website Store</title>', $body);
81+
}
82+
6383
/**
6484
* Check Rss with incorrect wishlist id.
6585
*
@@ -83,7 +103,6 @@ public function testRssResponseWithIncorrectWishlistId()
83103

84104
private function getLink($customerId, $customerEmail, $wishlistId)
85105
{
86-
87106
return 'rss/feed/index/type/wishlist/data/'
88107
. base64_encode($customerId . ',' . $customerEmail)
89108
. '/wishlist_id/' . $wishlistId;

0 commit comments

Comments
 (0)