diff --git a/app/code/Magento/Rss/Model/Rss.php b/app/code/Magento/Rss/Model/Rss.php index e37ee263b8301..eb24db28b9572 100644 --- a/app/code/Magento/Rss/Model/Rss.php +++ b/app/code/Magento/Rss/Model/Rss.php @@ -8,10 +8,13 @@ namespace Magento\Rss\Model; +use Magento\Framework\App\CacheInterface; +use Magento\Framework\App\FeedFactoryInterface; 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; /** * Provides functionality to work with RSS feeds @@ -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; @@ -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 ) { @@ -59,6 +62,8 @@ public function __construct( } /** + * Returns feeds + * * @return array */ public function getFeeds() @@ -66,47 +71,48 @@ 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(); + // serializing data to make sure all Phrase objects converted to a string + $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(); } } diff --git a/app/code/Magento/Rss/Test/Mftf/Page/StorefrontRssPage.xml b/app/code/Magento/Rss/Test/Mftf/Page/StorefrontRssPage.xml new file mode 100644 index 0000000000000..d0559af3e5370 --- /dev/null +++ b/app/code/Magento/Rss/Test/Mftf/Page/StorefrontRssPage.xml @@ -0,0 +1,14 @@ + + + + + +
+ + diff --git a/app/code/Magento/Rss/Test/Mftf/Section/StorefrontRssListSection.xml b/app/code/Magento/Rss/Test/Mftf/Section/StorefrontRssListSection.xml new file mode 100644 index 0000000000000..839e937887ec1 --- /dev/null +++ b/app/code/Magento/Rss/Test/Mftf/Section/StorefrontRssListSection.xml @@ -0,0 +1,15 @@ + + + + +
+ + +
+
diff --git a/app/code/Magento/Rss/Test/Mftf/Test/RssListTest.xml b/app/code/Magento/Rss/Test/Mftf/Test/RssListTest.xml new file mode 100644 index 0000000000000..a9f8e96c0bc1c --- /dev/null +++ b/app/code/Magento/Rss/Test/Mftf/Test/RssListTest.xml @@ -0,0 +1,41 @@ + + + + + + + + + + <description value="View selected RSS feed by link."/> + <testCaseId value="MC-36686"/> + <severity value="AVERAGE"/> + </annotations> + <before> + <createData entity="SimpleProductWithNewFromDate" stepKey="createProduct"/> + <magentoCLI command="config:set rss/config/active 1" stepKey="enableRss"/> + <magentoCLI command="config:set rss/catalog/new 1" stepKey="enableRssForCatalogNewProducts"/> + <magentoCLI command="cache:clean" stepKey="cleanCache"/> + </before> + <after> + <deleteData createDataKey="createProduct" stepKey="deleteProduct"/> + <magentoCLI command="config:set rss/config/active 0" stepKey="disableRss"/> + <magentoCLI command="config:set rss/catalog/new 0" stepKey="disableRssForCatalogNewProducts"/> + <magentoCLI command="cache:clean" stepKey="cleanCache"/> + </after> + + <amOnPage url="{{StorefrontRssPage.url}}" stepKey="goToRssPage"/> + <seeElement selector="{{StorefrontRssListSection.rssTable}}" stepKey="seeRssList"/> + <click selector="{{StorefrontRssListSection.rssLink}}" stepKey="clickRssLink"/> + <seeInCurrentUrl url="rss/feed/index/type/new_products/" stepKey="seeInUrl"/> + <waitForPageLoad stepKey="waitForPageLoad"/> + <see userInput="New Products from Main Website Store" stepKey="seeText" /> + + </test> +</tests> diff --git a/app/code/Magento/Rss/Test/Unit/Model/RssTest.php b/app/code/Magento/Rss/Test/Unit/Model/RssTest.php index f2694fc81dab4..4e245f8b440be 100644 --- a/app/code/Magento/Rss/Test/Unit/Model/RssTest.php +++ b/app/code/Magento/Rss/Test/Unit/Model/RssTest.php @@ -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 @@ -62,11 +67,6 @@ class RssTest extends TestCase </channel> </rss>'; - /** - * @var ObjectManagerHelper - */ - protected $objectManagerHelper; - /** * @var CacheInterface|MockObject */ @@ -87,6 +87,9 @@ class RssTest extends TestCase */ private $serializerMock; + /** + * @inheritDoc + */ protected function setUp(): void { $this->cacheMock = $this->getMockForAbstractClass(CacheInterface::class); @@ -94,8 +97,8 @@ protected function setUp(): void $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, @@ -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); @@ -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()); } @@ -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()); } diff --git a/dev/tests/integration/testsuite/Magento/Rss/Controller/Feed/IndexTest.php b/dev/tests/integration/testsuite/Magento/Rss/Controller/Feed/IndexTest.php index 991914460c61b..f0d61bc618054 100644 --- a/dev/tests/integration/testsuite/Magento/Rss/Controller/Feed/IndexTest.php +++ b/dev/tests/integration/testsuite/Magento/Rss/Controller/Feed/IndexTest.php @@ -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); } /** @@ -60,6 +65,21 @@ public function testRssResponse() $this->assertStringContainsString('<title>John Smith\'s Wishlist', $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('New Products from Main Website Store', $body); + } + /** * Check Rss with incorrect wishlist id. * @@ -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;