Skip to content

Commit

Permalink
fix: [IPET-407] Return correct item urls in ajax filter request
Browse files Browse the repository at this point in the history
  • Loading branch information
Leone committed Nov 3, 2021
1 parent 817bcd5 commit 4ae1cf5
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 20 deletions.
28 changes: 27 additions & 1 deletion Helper/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class Filter extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @var \MageSuite\SeoLinkMasking\Helper\Configuration
*/
protected $configuration;

/**
* @var \Magento\Framework\App\RequestInterface
*/
Expand All @@ -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;
}
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class IsLinkMaskingEnabled
{
/**
* @var \Magento\Framework\App\RequestInterface
*/
protected $request;

/**
* @var \Magento\Framework\Registry
*/
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
30 changes: 29 additions & 1 deletion Plugin/Catalog/Model/Layer/Filter/Item/AdjustFilterItemUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,44 @@ 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
*/
protected $filterItemUrlProcessor;

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;
}

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Service/FilterItemUrlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions Test/Integration/Service/FilterItemUrlProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 4ae1cf5

Please sign in to comment.