From 4ae1cf5755ea06e34ab9555dbf263242ef500f50 Mon Sep 17 00:00:00 2001 From: Grzegorz Bogusz Date: Wed, 3 Nov 2021 12:55:52 +0100 Subject: [PATCH] fix: [IPET-407] Return correct item urls in ajax filter request --- Helper/Filter.php | 28 +++++++++++++++- .../AbstractFilter/IsLinkMaskingEnabled.php | 31 ++++++++---------- .../Layer/Filter/Item/AdjustFilterItemUrl.php | 30 ++++++++++++++++- Service/FilterItemUrlProcessor.php | 2 +- .../Service/FilterItemUrlProcessorTest.php | 32 +++++++++++++++++++ 5 files changed, 103 insertions(+), 20 deletions(-) diff --git a/Helper/Filter.php b/Helper/Filter.php index ea1237a..7f84fb1 100644 --- a/Helper/Filter.php +++ b/Helper/Filter.php @@ -4,6 +4,11 @@ class Filter extends \Magento\Framework\App\Helper\AbstractHelper { + /** + * @var \MageSuite\SeoLinkMasking\Helper\Configuration + */ + protected $configuration; + /** * @var \Magento\Framework\App\RequestInterface */ @@ -16,11 +21,13 @@ class Filter extends \Magento\Framework\App\Helper\AbstractHelper public function __construct( \Magento\Framework\App\Helper\Context $context, + \MageSuite\SeoLinkMasking\Helper\Configuration $configuration, \Magento\Framework\App\RequestInterface $request, \MageSuite\SeoLinkMasking\Service\FilterableAttributesProvider $filterableAttributesProvider ) { parent::__construct($context); + $this->configuration = $configuration; $this->request = $request; $this->filterableAttributesProvider = $filterableAttributesProvider; } @@ -41,6 +48,25 @@ public function isFilterSelected($category) $difference = array_diff_key($filters, $filterableAttributes); - return count($difference) < count($filters) ? true : false; + return count($difference) < count($filters); + } + + public function isFilterMasked($category, $attributeId) + { + if ($this->configuration->onlyOneFilterDemasked() && $this->isFilterSelected($category)) { + return true; + } + + $seoLinkMasking = $category->getSeoLinkMasking(); + + if (empty($seoLinkMasking)) { + return $this->configuration->getDefaultMaskingState(); + } + + if (isset($seoLinkMasking[$attributeId])) { + return $seoLinkMasking[$attributeId]; + } + + return $this->configuration->getDefaultMaskingState(); } } diff --git a/Plugin/Catalog/Model/Layer/Filter/AbstractFilter/IsLinkMaskingEnabled.php b/Plugin/Catalog/Model/Layer/Filter/AbstractFilter/IsLinkMaskingEnabled.php index 8bdc667..c25e9ab 100644 --- a/Plugin/Catalog/Model/Layer/Filter/AbstractFilter/IsLinkMaskingEnabled.php +++ b/Plugin/Catalog/Model/Layer/Filter/AbstractFilter/IsLinkMaskingEnabled.php @@ -4,6 +4,11 @@ class IsLinkMaskingEnabled { + /** + * @var \Magento\Framework\App\RequestInterface + */ + protected $request; + /** * @var \Magento\Framework\Registry */ @@ -25,11 +30,13 @@ class IsLinkMaskingEnabled protected $categoryHelper; public function __construct( + \Magento\Framework\App\RequestInterface $request, \Magento\Framework\Registry $registry, \MageSuite\SeoLinkMasking\Helper\Configuration $configuration, \MageSuite\SeoLinkMasking\Helper\Filter $filterHelper, \MageSuite\SeoLinkMasking\Helper\Category $categoryHelper ) { + $this->request = $request; $this->registry = $registry; $this->configuration = $configuration; $this->filterHelper = $filterHelper; @@ -42,33 +49,23 @@ public function aroundGetData(\Magento\Catalog\Model\Layer\Filter\AbstractFilter return $proceed($key, $index); } - $category = $this->getCategory(); + $category = $this->getCategory($subject); if (empty($category)) { return $proceed($key, $index); } - if ($this->configuration->onlyOneFilterDemasked() && $this->filterHelper->isFilterSelected($category)) { - return true; - } - - $seoLinkMasking = $category->getSeoLinkMasking(); - - if (empty($seoLinkMasking)) { - return $this->configuration->getDefaultMaskingState(); - } - $attributeId = $subject->getAttributeModel()->getId(); - if (isset($seoLinkMasking[$attributeId])) { - return $seoLinkMasking[$attributeId]; - } - - return $this->configuration->getDefaultMaskingState(); + return $this->filterHelper->isFilterMasked($category, $attributeId); } - protected function getCategory() + protected function getCategory($subject) { + if ($this->request->getFullActionName() == \MageSuite\SeoLinkMasking\Helper\Configuration::AJAX_FILTER_FULL_ACTION_NAME) { + return $subject->getLayer()->getCurrentCategory(); + } + $category = $this->registry->registry('current_category'); return $this->categoryHelper->getCategoryEntityForSearchResultPage($category); } diff --git a/Plugin/Catalog/Model/Layer/Filter/Item/AdjustFilterItemUrl.php b/Plugin/Catalog/Model/Layer/Filter/Item/AdjustFilterItemUrl.php index 43c4909..23c03de 100644 --- a/Plugin/Catalog/Model/Layer/Filter/Item/AdjustFilterItemUrl.php +++ b/Plugin/Catalog/Model/Layer/Filter/Item/AdjustFilterItemUrl.php @@ -11,11 +11,26 @@ class AdjustFilterItemUrl */ protected $configuration; + /** + * @var \Magento\Framework\App\RequestInterface + */ + protected $request; + /** * @var \Magento\Framework\Registry */ protected $registry; + /** + * @var \Magento\Framework\UrlInterface + */ + protected $url; + + /** + * @var \Magento\Framework\Data\Helper\PostHelper + */ + protected $postHelper; + /** * @var \MageSuite\SeoLinkMasking\Service\FilterItemUrlProcessor */ @@ -23,11 +38,17 @@ class AdjustFilterItemUrl public function __construct( \MageSuite\SeoLinkMasking\Helper\Configuration $configuration, + \Magento\Framework\App\RequestInterface $request, \Magento\Framework\Registry $registry, + \Magento\Framework\UrlInterface $url, + \Magento\Framework\Data\Helper\PostHelper $postHelper, \MageSuite\SeoLinkMasking\Service\FilterItemUrlProcessor $filterItemUrlProcessor ) { $this->configuration = $configuration; + $this->request = $request; $this->registry = $registry; + $this->url = $url; + $this->postHelper = $postHelper; $this->filterItemUrlProcessor = $filterItemUrlProcessor; } @@ -40,7 +61,14 @@ public function aroundGetUrl(\Magento\Catalog\Model\Layer\Filter\Item $subject, return $proceed(); } - return $this->filterItemUrlProcessor->prepareItemUrl($filter, $category, $subject->getValue()); + $url = $this->filterItemUrlProcessor->prepareItemUrl($filter, $category, $subject->getValue()); + + if ($this->request->getFullActionName() != \MageSuite\SeoLinkMasking\Helper\Configuration::AJAX_FILTER_FULL_ACTION_NAME || !$filter->getIsLinkMaskingEnabled()) { + return $url; + } + + $linkMaskingUrl = $this->url->getUrl(\MageSuite\SeoLinkMasking\Plugin\Smile\ElasticsuiteCatalog\Block\Navigation\Renderer\Attribute\AddLinkMaskingToFilterData::LINK_MASKING_ENDPOINT); + return $this->postHelper->getPostData($linkMaskingUrl, ['url' => $url]); } public function aroundGetRemoveUrl(\Magento\Catalog\Model\Layer\Filter\Item $subject, \Closure $proceed) diff --git a/Service/FilterItemUrlProcessor.php b/Service/FilterItemUrlProcessor.php index 750fe45..b195840 100644 --- a/Service/FilterItemUrlProcessor.php +++ b/Service/FilterItemUrlProcessor.php @@ -293,7 +293,7 @@ public function getCategoryUrlCacheKey($categoryId) public function getUrl($category, $requestParameters) { - if ($this->request->getFullActionName() == 'catalog_navigation_filter_ajax') { + if ($this->request->getFullActionName() == \MageSuite\SeoLinkMasking\Helper\Configuration::AJAX_FILTER_FULL_ACTION_NAME) { if ($this->request->getParam('cat')) { return $this->getCategoryUrl($category); } else { diff --git a/Test/Integration/Service/FilterItemUrlProcessorTest.php b/Test/Integration/Service/FilterItemUrlProcessorTest.php index 51f9943..216c786 100644 --- a/Test/Integration/Service/FilterItemUrlProcessorTest.php +++ b/Test/Integration/Service/FilterItemUrlProcessorTest.php @@ -20,11 +20,22 @@ class FilterItemUrlProcessorTest extends \Magento\TestFramework\TestCase\Abstrac */ protected $registry; + /** + * @var \MageSuite\SeoLinkMasking\Helper\Filter + */ + protected $filterHelper; + protected function setUp(): void { parent::setUp(); $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $this->registry = $this->objectManager->get(\Magento\Framework\Registry::class); + + $this->filterHelper = $this->createStub( + \MageSuite\SeoLinkMasking\Helper\Filter::class + ); + + $this->objectManager->addSharedInstance($this->filterHelper, \MageSuite\SeoLinkMasking\Helper\Filter::class); } /** @@ -65,6 +76,27 @@ public function testLinkMaskingGetCorrectCategoryUrlSearchResult() $this->assertTrue($urlContainPath); } + /** + * @magentoAppArea frontend + * @magentoDbIsolation enabled + * @magentoAppIsolation enabled + * @magentoConfigFixture current_store seo/link_masking/is_enabled 1 + * @magentoConfigFixture current_store seo/link_masking/is_short_filter_url_enabled 1 + * @magentoDataFixture loadFilterableProducts + */ + public function testItReturnsCorrectUrlForMaskedFilterInAjaxRequest() + { + $this->filterHelper->method('isFilterMasked')->willReturn(true); + + $this->dispatch('catalog/navigation_filter/ajax/?filterName=multiselect_attribute'); + + $response = json_decode($this->getResponse()->getBody(), true); + $decodedUrl = json_decode($response[0]['url'], true); + + $this->assertEquals('http://localhost/index.php/linkmasking/filter/redirect/', $decodedUrl['action']); + $this->assertEquals('http://localhost/index.php/catalogsearch/result/index/option+1', $decodedUrl['data']['url']); + } + public static function loadFilterableProducts() { require __DIR__.'/../_files/filterable_products.php';