From 11659ecb24c9d17bb00c9372fd125d8aeeb9e871 Mon Sep 17 00:00:00 2001 From: Anton Karunarathne Date: Tue, 28 Mar 2023 17:15:24 +0530 Subject: [PATCH 1/4] ALG-125 | Integration testing changes for Aligent\Sitemap\Model\ItemProvider\PwaUrl class --- Model/Config/ConfigReaderInterface.php | 85 ++++++++++++++++ Model/Config/Data.php | 55 +++-------- Model/ItemProvider/PwaUrl.php | 10 +- Model/Sitemap.php | 6 +- .../GetValidCategoryPagesForSitemap.php | 6 +- .../GetValidProductPagesForSitemap.php | 6 +- .../Cms/Page/GetValidCmsPagesForSitemap.php | 6 +- Test/Unit/Model/ItemProvider/PwaUrlTest.php | 96 +++++++++++++++++++ etc/di.xml | 3 + 9 files changed, 212 insertions(+), 61 deletions(-) create mode 100644 Model/Config/ConfigReaderInterface.php create mode 100644 Test/Unit/Model/ItemProvider/PwaUrlTest.php diff --git a/Model/Config/ConfigReaderInterface.php b/Model/Config/ConfigReaderInterface.php new file mode 100644 index 0000000..228d966 --- /dev/null +++ b/Model/Config/ConfigReaderInterface.php @@ -0,0 +1,85 @@ +aligentSitemapConfig->isPwaPagesInclude((int)$storeId)) { - $pwaPagesUrlKeys = $this->aligentSitemapConfig->getPwaPagesUrlKey((int)$storeId); + if ($this->aligentSitemapConfigReaderInterface->isPwaPagesInclude((int)$storeId)) { + $pwaPagesUrlKeys = $this->aligentSitemapConfigReaderInterface->getPwaPagesUrlKey((int)$storeId); foreach ($pwaPagesUrlKeys as $pwaPagesUrlKey) { $items[] = $this->itemFactory->create([ 'url' => $pwaPagesUrlKey, diff --git a/Model/Sitemap.php b/Model/Sitemap.php index 36c586f..eaab816 100644 --- a/Model/Sitemap.php +++ b/Model/Sitemap.php @@ -29,7 +29,7 @@ use Magento\Sitemap\Model\SitemapItemInterfaceFactory; use Magento\Store\Model\StoreManagerInterface; use Magento\Sitemap\Helper\Data as SitemapHelper; -use Aligent\Sitemap\Model\Config\Data as AligentSitemapConfig; +use Aligent\Sitemap\Model\Config\ConfigReaderInterface as AligentSitemapConfigReaderInterface; class Sitemap extends MagentoSitemap { @@ -46,7 +46,7 @@ class Sitemap extends MagentoSitemap * @param StoreManagerInterface $storeManager * @param RequestInterface $request * @param DateTime $dateTime - * @param AligentSitemapConfig $aligentSitemapConfig + * @param AligentSitemapConfigReaderInterface $aligentSitemapConfig * @param AbstractResource|null $resource * @param AbstractDb|null $resourceCollection * @param array $data @@ -68,7 +68,7 @@ public function __construct( StoreManagerInterface $storeManager, RequestInterface $request, DateTime $dateTime, - private readonly AligentSitemapConfig $aligentSitemapConfig, + private readonly AligentSitemapConfigReaderInterface $aligentSitemapConfig, AbstractResource $resource = null, AbstractDb $resourceCollection = null, array $data = [], diff --git a/Plugin/Model/ResourceModel/Catalog/GetValidCategoryPagesForSitemap.php b/Plugin/Model/ResourceModel/Catalog/GetValidCategoryPagesForSitemap.php index fd57b2f..02f24cc 100644 --- a/Plugin/Model/ResourceModel/Catalog/GetValidCategoryPagesForSitemap.php +++ b/Plugin/Model/ResourceModel/Catalog/GetValidCategoryPagesForSitemap.php @@ -17,7 +17,7 @@ use Magento\Store\Model\Store; use Magento\Store\Model\StoreManagerInterface; use Magento\Catalog\Model\ResourceModel\Category as CategoryResource; -use Aligent\Sitemap\Model\Config\Data as AligentSitemapConfig; +use Aligent\Sitemap\Model\Config\ConfigReaderInterface as AligentSitemapConfigReaderInterface; use Magento\Framework\EntityManager\MetadataPool; use Magento\Framework\DataObject; @@ -31,13 +31,13 @@ class GetValidCategoryPagesForSitemap private $_attributesCache = []; /** - * @param AligentSitemapConfig $aligentSitemapConfig + * @param AligentSitemapConfigReaderInterface $aligentSitemapConfig * @param StoreManagerInterface $storeManager * @param CategoryResource $categoryResource * @param MetadataPool $metadataPool */ public function __construct( - private readonly AligentSitemapConfig $aligentSitemapConfig, + private readonly AligentSitemapConfigReaderInterface $aligentSitemapConfig, private readonly StoreManagerInterface $storeManager, private readonly CategoryResource $categoryResource, private readonly MetadataPool $metadataPool diff --git a/Plugin/Model/ResourceModel/Catalog/GetValidProductPagesForSitemap.php b/Plugin/Model/ResourceModel/Catalog/GetValidProductPagesForSitemap.php index 21801a4..472ef05 100644 --- a/Plugin/Model/ResourceModel/Catalog/GetValidProductPagesForSitemap.php +++ b/Plugin/Model/ResourceModel/Catalog/GetValidProductPagesForSitemap.php @@ -24,7 +24,7 @@ use Magento\Store\Model\Store; use Magento\Store\Model\StoreManagerInterface; use Zend_Db_Statement_Exception; -use Aligent\Sitemap\Model\Config\Data as AligentSitemapConfig; +use Aligent\Sitemap\Model\Config\ConfigReaderInterface as AligentSitemapConfigReaderInterface; use Magento\Sitemap\Model\ResourceModel\Catalog\Product; use Magento\Catalog\Model\Product\Visibility; use Magento\Catalog\Model\Product\Attribute\Source\Status; @@ -35,7 +35,7 @@ class GetValidProductPagesForSitemap /** * Product constructor. * - * @param AligentSitemapConfig $aligentSitemapConfig + * @param AligentSitemapConfigReaderInterface $aligentSitemapConfig * @param ScopeConfigInterface $scopeConfig * @param StoreManagerInterface $storeManager * @param ProductResource $productResource @@ -47,7 +47,7 @@ class GetValidProductPagesForSitemap * @param ReadHandler $mediaGalleryReadHandler */ public function __construct( - private readonly AligentSitemapConfig $aligentSitemapConfig, + private readonly AligentSitemapConfigReaderInterface $aligentSitemapConfig, private readonly ScopeConfigInterface $scopeConfig, private readonly StoreManagerInterface $storeManager, private readonly ProductResource $productResource, diff --git a/Plugin/Model/ResourceModel/Cms/Page/GetValidCmsPagesForSitemap.php b/Plugin/Model/ResourceModel/Cms/Page/GetValidCmsPagesForSitemap.php index c4de636..cd21cbc 100644 --- a/Plugin/Model/ResourceModel/Cms/Page/GetValidCmsPagesForSitemap.php +++ b/Plugin/Model/ResourceModel/Cms/Page/GetValidCmsPagesForSitemap.php @@ -12,7 +12,7 @@ use Magento\Cms\Api\GetUtilityPageIdentifiersInterface; use Magento\Framework\Exception\LocalizedException; use Zend_Db_Statement_Exception; -use Aligent\Sitemap\Model\Config\Data as AligentSitemapConfig; +use Aligent\Sitemap\Model\Config\ConfigReaderInterface as AligentSitemapConfigReaderInterface; use Magento\Sitemap\Model\ResourceModel\Cms\Page; use Magento\Framework\EntityManager\MetadataPool; use Magento\Framework\DataObject; @@ -22,13 +22,13 @@ class GetValidCmsPagesForSitemap /** * @param GetUtilityPageIdentifiersInterface $getUtilityPageIdentifiers * @param MetadataPool $metadataPool - * @param AligentSitemapConfig $aligentSitemapConfig + * @param AligentSitemapConfigReaderInterface $aligentSitemapConfig * @param Page $page */ public function __construct( private readonly GetUtilityPageIdentifiersInterface $getUtilityPageIdentifiers, private readonly MetadataPool $metadataPool, - private readonly AligentSitemapConfig $aligentSitemapConfig, + private readonly AligentSitemapConfigReaderInterface $aligentSitemapConfig, private readonly Page $page ) { } diff --git a/Test/Unit/Model/ItemProvider/PwaUrlTest.php b/Test/Unit/Model/ItemProvider/PwaUrlTest.php new file mode 100644 index 0000000..a50dc89 --- /dev/null +++ b/Test/Unit/Model/ItemProvider/PwaUrlTest.php @@ -0,0 +1,96 @@ +getConfigReaderMock(); + $itemFactoryMock = $this->getItemFactoryMock(); + $aligentSitemapConfigMock = $this->getAligentSitemapConfigMock(); + $resolver = new PwaUrl($configReaderMock, $itemFactoryMock, $aligentSitemapConfigMock); + $items = $resolver->getItems(1); + + $this->assertCount(2, $items); + foreach ($items as $item) { + $this->assertSame('daily', $item->getChangeFrequency()); + $this->assertSame('1.0', $item->getPriority()); + } + } + + /** + * @return SitemapItemInterfaceFactory|MockObject + */ + private function getItemFactoryMock(): SitemapItemInterfaceFactory|MockObject + { + $itemFactoryMock = $this->getMockBuilder(SitemapItemInterfaceFactory::class) + ->setMethods(['create']) + ->disableOriginalConstructor() + ->getMock(); + + $itemFactoryMock->expects($this->any()) + ->method('create') + ->willReturnCallback(function ($data) { + $helper = new ObjectManager($this); + + return $helper->getObject(SitemapItem::class, $data); + }); + + return $itemFactoryMock; + } + + /** + * @return ConfigReaderInterface|MockObject + */ + private function getConfigReaderMock(): MockObject|ConfigReaderInterface + { + $configReaderMock = $this->getMockForAbstractClass(ConfigReaderInterface::class); + $configReaderMock->expects($this->any()) + ->method('getPriority') + ->willReturn('1.0'); + $configReaderMock->expects($this->any()) + ->method('getChangeFrequency') + ->willReturn('daily'); + + return $configReaderMock; + } + + /** + * @return AligentSitemapConfigReaderInterface|MockObject + */ + private function getAligentSitemapConfigMock(): MockObject|AligentSitemapConfigReaderInterface + { + $aligentSitemapConfigMock = $this->getMockForAbstractClass( + AligentSitemapConfigReaderInterface::class + ); + $aligentSitemapConfigMock->expects($this->any()) + ->method('isPwaPagesInclude') + ->with(1) + ->willReturn(true); + $aligentSitemapConfigMock->expects($this->any()) + ->method('getPwaPagesUrlKey') + ->with(1) + ->willReturn(['test-url-key','test-url-key-2']); + + return $aligentSitemapConfigMock; + } +} diff --git a/etc/di.xml b/etc/di.xml index 6f7d792..a684b1d 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -19,6 +19,9 @@ + + From 991cb05bd7fb3403fb4d85e5a4384122e3f78997 Mon Sep 17 00:00:00 2001 From: Anton Karunarathne Date: Tue, 28 Mar 2023 17:27:18 +0530 Subject: [PATCH 2/4] ALG-125 | code formatting changes --- Model/Config/Data.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Model/Config/Data.php b/Model/Config/Data.php index 174ea10..0ff5e4d 100644 --- a/Model/Config/Data.php +++ b/Model/Config/Data.php @@ -19,8 +19,8 @@ class Data implements ConfigReaderInterface * @param Json $serializer */ public function __construct( - private ScopeConfigInterface $scopeConfig, - private Json $serializer + private readonly ScopeConfigInterface $scopeConfig, + private readonly Json $serializer ) { } From d8d30cc158ee9ef3876966e08caeca0e1bbfd645 Mon Sep 17 00:00:00 2001 From: Anton Karunarathne Date: Tue, 28 Mar 2023 17:28:23 +0530 Subject: [PATCH 3/4] ALG-125 | code formatting changes --- Model/Config/ConfigReaderInterface.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Model/Config/ConfigReaderInterface.php b/Model/Config/ConfigReaderInterface.php index 228d966..e156eb7 100644 --- a/Model/Config/ConfigReaderInterface.php +++ b/Model/Config/ConfigReaderInterface.php @@ -11,13 +11,13 @@ interface ConfigReaderInterface { public const CONFIG_XML_PATH_SITEMAP_ENABLED = 'aligent_sitemap/general/enabled'; - public const CONFIG_XML_PATH_SITEMAP_EXCLUDE_CMS_PAGE = 'aligent_sitemap/general/exclude_cms_page'; - public const CONFIG_XML_PATH_SITEMAP_EXCLUDE_CATEGORY = 'aligent_sitemap/general/exclude_category'; - public const CONFIG_XML_PATH_SITEMAP_EXCLUDE_PRODUCT = 'aligent_sitemap/general/exclude_product'; - public const CONFIG_XML_PATH_SITEMAP_EXCLUDE_PRODUCT_IMAGES = 'aligent_sitemap/general/exclude_product_images'; - public const CONFIG_XML_PATH_SITEMAP_INCLUDE_PWA_PAGES = 'aligent_sitemap/general/include_pwa_pages'; - public const CONFIG_XML_PATH_SITEMAP_BASE_URL = 'aligent_sitemap/general/sitemap_base_url'; - public const CONFIG_XML_PATH_SITEMAP_PWA_PAGES_URL_KEY = 'aligent_sitemap/general/pwa_pages_url_key'; + public const CONFIG_XML_PATH_SITEMAP_EXCLUDE_CMS_PAGE = 'aligent_sitemap/general/exclude_cms_page'; + public const CONFIG_XML_PATH_SITEMAP_EXCLUDE_CATEGORY = 'aligent_sitemap/general/exclude_category'; + public const CONFIG_XML_PATH_SITEMAP_EXCLUDE_PRODUCT = 'aligent_sitemap/general/exclude_product'; + public const CONFIG_XML_PATH_SITEMAP_EXCLUDE_PRODUCT_IMAGES = 'aligent_sitemap/general/exclude_product_images'; + public const CONFIG_XML_PATH_SITEMAP_INCLUDE_PWA_PAGES = 'aligent_sitemap/general/include_pwa_pages'; + public const CONFIG_XML_PATH_SITEMAP_BASE_URL = 'aligent_sitemap/general/sitemap_base_url'; + public const CONFIG_XML_PATH_SITEMAP_PWA_PAGES_URL_KEY = 'aligent_sitemap/general/pwa_pages_url_key'; /** * Sitemap customization for the default sitemap enabled check From 8b1e45f7beb366668349cec3b2d7e7b58f56697e Mon Sep 17 00:00:00 2001 From: Anton Karunarathne Date: Tue, 28 Mar 2023 17:30:16 +0530 Subject: [PATCH 4/4] ALG-125 | code formatting changes --- Model/Config/Data.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Model/Config/Data.php b/Model/Config/Data.php index 0ff5e4d..e98c8d8 100644 --- a/Model/Config/Data.php +++ b/Model/Config/Data.php @@ -25,7 +25,7 @@ public function __construct( } /** - * {@inheritdoc} + * @inheritDoc */ public function isSitemapCustomizationEnabled(?int $storeId): bool { @@ -37,7 +37,7 @@ public function isSitemapCustomizationEnabled(?int $storeId): bool } /** - * {@inheritdoc} + * @inheritDoc */ public function isCmsPageExcludeEnabled(?int $storeId): bool { @@ -52,7 +52,7 @@ public function isCmsPageExcludeEnabled(?int $storeId): bool } /** - * {@inheritdoc} + * @inheritDoc */ public function isCategoryExcludeEnabled(?int $storeId): bool { @@ -67,7 +67,7 @@ public function isCategoryExcludeEnabled(?int $storeId): bool } /** - * {@inheritdoc} + * @inheritDoc */ public function isProductExcludeEnabled(?int $storeId): bool { @@ -82,7 +82,7 @@ public function isProductExcludeEnabled(?int $storeId): bool } /** - * {@inheritdoc} + * @inheritDoc */ public function isProductImagesExcludeEnabled(?int $storeId): bool { @@ -97,7 +97,7 @@ public function isProductImagesExcludeEnabled(?int $storeId): bool } /** - * {@inheritdoc} + * @inheritDoc */ public function isPwaPagesInclude(?int $storeId): bool { @@ -112,7 +112,7 @@ public function isPwaPagesInclude(?int $storeId): bool } /** - * {@inheritdoc} + * @inheritDoc */ public function getSitemapBaseUrl(?int $storeId): string { @@ -127,7 +127,7 @@ public function getSitemapBaseUrl(?int $storeId): string } /** - * {@inheritdoc} + * @inheritDoc */ public function getPwaPagesUrlKey(?int $storeId): array {