Skip to content

fix RSS Feed - first load not working #29455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions app/code/Magento/Rss/Model/Rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

namespace Magento\Rss\Model;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Rss\DataProviderInterface;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\RuntimeException;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\App\FeedFactoryInterface;

Expand All @@ -27,12 +30,12 @@ class Rss
protected $dataProvider;

/**
* @var \Magento\Framework\App\CacheInterface
* @var CacheInterface
*/
protected $cache;

/**
* @var \Magento\Framework\App\FeedFactoryInterface
* @var FeedFactoryInterface
*/
private $feedFactory;

Expand All @@ -44,12 +47,12 @@ class Rss
/**
* Rss constructor
*
* @param \Magento\Framework\App\CacheInterface $cache
* @param CacheInterface $cache
* @param SerializerInterface|null $serializer
* @param FeedFactoryInterface|null $feedFactory
*/
public function __construct(
\Magento\Framework\App\CacheInterface $cache,
CacheInterface $cache,
SerializerInterface $serializer = null,
FeedFactoryInterface $feedFactory = null
) {
Expand All @@ -59,54 +62,56 @@ public function __construct(
}

/**
* Returns feeds
*
* @return array
*/
public function getFeeds()
{
if ($this->dataProvider === null) {
return [];
}
$cache = false;
if ($this->dataProvider->getCacheKey() && $this->dataProvider->getCacheLifetime()) {
$cache = $this->cache->load($this->dataProvider->getCacheKey());
}
$cacheKey = $this->dataProvider->getCacheKey();
$cacheLifeTime = $this->dataProvider->getCacheLifetime();

$cache = $cacheKey && $cacheLifeTime ? $this->cache->load($cacheKey) : false;
if ($cache) {
return $this->serializer->unserialize($cache);
}

$data = $this->dataProvider->getRssData();
$serializedData = $this->serializer->serialize($this->dataProvider->getRssData());

if ($this->dataProvider->getCacheKey() && $this->dataProvider->getCacheLifetime()) {
$this->cache->save(
$this->serializer->serialize($data),
$this->dataProvider->getCacheKey(),
['rss'],
$this->dataProvider->getCacheLifetime()
);
if ($cacheKey && $cacheLifeTime) {
$this->cache->save($serializedData, $cacheKey, ['rss'], $cacheLifeTime);
}

return $data;
return $this->serializer->unserialize($serializedData);
}

/**
* Sets data provider
*
* @param DataProviderInterface $dataProvider
* @return $this
*/
public function setDataProvider(DataProviderInterface $dataProvider)
{
$this->dataProvider = $dataProvider;

return $this;
}

/**
* Returns rss xml
*
* @return string
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\RuntimeException
* @throws InputException
* @throws RuntimeException
*/
public function createRssXml()
{
$feed = $this->feedFactory->create($this->getFeeds(), FeedFactoryInterface::FORMAT_RSS);

return $feed->getFormattedContent();
}
}
52 changes: 39 additions & 13 deletions app/code/Magento/Rss/Test/Unit/Model/RssTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Test for \Magento\Rss\Model\Rss.
*/
class RssTest extends TestCase
{
private const STUB_SERIALIZED_DATA = 'serializedData';

/**
* @var Rss
*/
protected $rss;
private $rss;

/**
* @var array
Expand Down Expand Up @@ -62,11 +67,6 @@ class RssTest extends TestCase
</channel>
</rss>';

/**
* @var ObjectManagerHelper
*/
protected $objectManagerHelper;

/**
* @var CacheInterface|MockObject
*/
Expand All @@ -87,15 +87,18 @@ class RssTest extends TestCase
*/
private $serializerMock;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->cacheMock = $this->getMockForAbstractClass(CacheInterface::class);
$this->serializerMock = $this->getMockForAbstractClass(SerializerInterface::class);
$this->feedFactoryMock = $this->getMockForAbstractClass(FeedFactoryInterface::class);
$this->feedMock = $this->getMockForAbstractClass(FeedInterface::class);

$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->rss = $this->objectManagerHelper->getObject(
$objectManagerHelper = new ObjectManagerHelper($this);
$this->rss = $objectManagerHelper->getObject(
Rss::class,
[
'cache' => $this->cacheMock,
Expand All @@ -105,12 +108,23 @@ protected function setUp(): void
);
}

public function testGetFeeds()
/**
* Get feeds test
*
* @return void
*/
public function testGetFeeds(): void
{
$dataProvider = $this->getMockForAbstractClass(DataProviderInterface::class);
$dataProvider->expects($this->any())->method('getCacheKey')->willReturn('cache_key');
$dataProvider->expects($this->any())->method('getCacheLifetime')->willReturn(100);
$dataProvider->expects($this->any())->method('getRssData')->willReturn($this->feedData);
$dataProvider->expects($this->atLeastOnce())
->method('getCacheKey')
->willReturn('cache_key');
$dataProvider->expects($this->atLeastOnce())
->method('getCacheLifetime')
->willReturn(100);
$dataProvider->expects($this->once())
->method('getRssData')
->willReturn($this->feedData);

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

Expand All @@ -125,7 +139,11 @@ public function testGetFeeds()
$this->serializerMock->expects($this->once())
->method('serialize')
->with($this->feedData)
->willReturn('serializedData');
->willReturn(self::STUB_SERIALIZED_DATA);
$this->serializerMock->expects($this->once())
->method('unserialize')
->with(self::STUB_SERIALIZED_DATA)
->willReturn($this->feedData);

$this->assertEquals($this->feedData, $this->rss->getFeeds());
}
Expand Down Expand Up @@ -168,6 +186,14 @@ public function testCreateRssXml()
->with($this->feedData, FeedFactoryInterface::FORMAT_RSS)
->willReturn($this->feedMock);

$this->serializerMock->expects($this->once())
->method('serialize')
->willReturn(self::STUB_SERIALIZED_DATA);
$this->serializerMock->expects($this->once())
->method('unserialize')
->with(self::STUB_SERIALIZED_DATA)
->willReturn($this->feedData);

$this->rss->setDataProvider($dataProvider);
$this->assertNotNull($this->rss->createRssXml());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,42 @@

namespace Magento\Rss\Controller\Feed;

class IndexTest extends \Magento\TestFramework\TestCase\AbstractBackendController
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Session;
use Magento\TestFramework\TestCase\AbstractBackendController;
use Magento\Wishlist\Model\Wishlist;

/**
* Test for \Magento\Rss\Controller\Feed\Index.
*/
class IndexTest extends AbstractBackendController
{
/**
* @var \Magento\Rss\Model\UrlBuilder
*/
private $urlBuilder;
private const RSS_NEW_PRODUCTS_PATH = 'rss/feed/index/type/new_products/';

/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface
* @var CustomerRepositoryInterface
*/
private $customerRepository;

/**
* @var \Magento\Wishlist\Model\Wishlist
* @var Wishlist
*/
private $wishlist;

/**
* @var
* @var Session
*/
private $customerSession;

/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$this->urlBuilder = $this->_objectManager->get(\Magento\Rss\Model\UrlBuilder::class);
$this->customerRepository = $this->_objectManager->get(
\Magento\Customer\Api\CustomerRepositoryInterface::class
);
$this->wishlist = $this->_objectManager->get(\Magento\Wishlist\Model\Wishlist::class);
$this->customerSession = $this->_objectManager->get(\Magento\Customer\Model\Session::class);
$this->customerRepository = $this->_objectManager->get(CustomerRepositoryInterface::class);
$this->wishlist = $this->_objectManager->get(Wishlist::class);
$this->customerSession = $this->_objectManager->get(Session::class);
}

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

/**
* Check Rss response from `New Products`.
*
* @magentoConfigFixture current_store rss/catalog/new 1
* @magentoConfigFixture current_store rss/config/active 1
*
* @return void
*/
public function testRssResponseNewProducts(): void
{
$this->dispatch(self::RSS_NEW_PRODUCTS_PATH);
$body = $this->getResponse()->getBody();
$this->assertStringContainsString('<title>New Products from Main Website Store</title>', $body);
}

/**
* Check Rss with incorrect wishlist id.
*
Expand All @@ -83,7 +103,6 @@ public function testRssResponseWithIncorrectWishlistId()

private function getLink($customerId, $customerEmail, $wishlistId)
{

return 'rss/feed/index/type/wishlist/data/'
. base64_encode($customerId . ',' . $customerEmail)
. '/wishlist_id/' . $wishlistId;
Expand Down