From 5599cddc15796c284df75384882952068f1a29af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Szubert?= Date: Sat, 19 Sep 2020 11:34:51 +0200 Subject: [PATCH 1/2] Fix #29971 - Layered navigation swatches ignore show tooltip setting --- .../Swatches/Model/Plugin/FilterRenderer.php | 77 ++++++++----- .../templates/product/layered/renderer.phtml | 107 +++++++++--------- .../web/js/layered/swatch-renderer.js | 30 +++++ 3 files changed, 134 insertions(+), 80 deletions(-) create mode 100644 app/code/Magento/Swatches/view/frontend/web/js/layered/swatch-renderer.js diff --git a/app/code/Magento/Swatches/Model/Plugin/FilterRenderer.php b/app/code/Magento/Swatches/Model/Plugin/FilterRenderer.php index e2d452dd9ddaa..ec7030903c6cc 100644 --- a/app/code/Magento/Swatches/Model/Plugin/FilterRenderer.php +++ b/app/code/Magento/Swatches/Model/Plugin/FilterRenderer.php @@ -3,63 +3,82 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Swatches\Model\Plugin; -/** - * Class FilterRenderer - */ +use Closure; +use Magento\Catalog\Model\Layer\Filter\FilterInterface; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\View\LayoutInterface; +use Magento\LayeredNavigation\Block\Navigation\FilterRenderer as Subject; +use Magento\Swatches\Block\LayeredNavigation\RenderLayered; +use Magento\Swatches\Helper\Data; +use Magento\Swatches\ViewModel\Product\Renderer\Configurable as ConfigurableViewModel; + class FilterRenderer { /** - * @var \Magento\Framework\View\LayoutInterface + * @var LayoutInterface */ protected $layout; /** - * Path to RenderLayered Block - * - * @var string + * @var Data */ - protected $block = \Magento\Swatches\Block\LayeredNavigation\RenderLayered::class; + protected $swatchHelper; /** - * @var \Magento\Swatches\Helper\Data + * @var ConfigurableViewModel|null */ - protected $swatchHelper; + private $configurableViewModel; /** - * @param \Magento\Framework\View\LayoutInterface $layout - * @param \Magento\Swatches\Helper\Data $swatchHelper + * @var string + */ + protected $block = RenderLayered::class; + + /** + * @param LayoutInterface $layout + * @param Data $swatchHelper + * @param ConfigurableViewModel|null $configurableViewModel */ public function __construct( - \Magento\Framework\View\LayoutInterface $layout, - \Magento\Swatches\Helper\Data $swatchHelper + LayoutInterface $layout, + Data $swatchHelper, + ?ConfigurableViewModel $configurableViewModel = null ) { $this->layout = $layout; $this->swatchHelper = $swatchHelper; + $this->configurableViewModel = $configurableViewModel + ?? ObjectManager::getInstance()->get(ConfigurableViewModel::class); } /** - * @SuppressWarnings(PHPMD.UnusedFormalParameter) - * @param \Magento\LayeredNavigation\Block\Navigation\FilterRenderer $subject - * @param \Closure $proceed - * @param \Magento\Catalog\Model\Layer\Filter\FilterInterface $filter + * Override block rendered for swatch attribute in layered navigation + * + * @param Subject $subject + * @param Closure $proceed + * @param FilterInterface $filter + * * @return mixed - * @throws \Magento\Framework\Exception\LocalizedException + * @throws LocalizedException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundRender( - \Magento\LayeredNavigation\Block\Navigation\FilterRenderer $subject, - \Closure $proceed, - \Magento\Catalog\Model\Layer\Filter\FilterInterface $filter + Subject $subject, + Closure $proceed, + FilterInterface $filter ) { - if ($filter->hasAttributeModel()) { - if ($this->swatchHelper->isSwatchAttribute($filter->getAttributeModel())) { - return $this->layout - ->createBlock($this->block) - ->setSwatchFilter($filter) - ->toHtml(); - } + if ($filter->hasAttributeModel() && $this->swatchHelper->isSwatchAttribute($filter->getAttributeModel())) { + return $this->layout + ->createBlock($this->block) + ->setSwatchFilter($filter) + ->setData('configurable_view_model', $this->configurableViewModel) + ->toHtml(); } + return $proceed($filter); } } diff --git a/app/code/Magento/Swatches/view/frontend/templates/product/layered/renderer.phtml b/app/code/Magento/Swatches/view/frontend/templates/product/layered/renderer.phtml index bae3820042de0..1194d3ffc00e6 100644 --- a/app/code/Magento/Swatches/view/frontend/templates/product/layered/renderer.phtml +++ b/app/code/Magento/Swatches/view/frontend/templates/product/layered/renderer.phtml @@ -4,31 +4,38 @@ * See COPYING.txt for license details. */ +use Magento\Framework\Escaper; +use Magento\Framework\View\Helper\SecureHtmlRenderer; +use Magento\Swatches\Block\LayeredNavigation\RenderLayered; +use Magento\Swatches\ViewModel\Product\Renderer\Configurable as ConfigurableViewModel; + // phpcs:disable PSR2.ControlStructures.SwitchDeclaration // phpcs:disable Generic.WhiteSpace.ScopeIndent -/** @var $block \Magento\Swatches\Block\LayeredNavigation\RenderLayered */ -/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */ -/** @var \Magento\Framework\Escaper $escaper */ +/** @var RenderLayered $block */ +/** @var SecureHtmlRenderer $secureRenderer */ +/** @var Escaper $escaper */ ?> getSwatchData(); ?> -
+ +getConfigurableViewModel() ?> +
$label): ?> - -
@@ -43,64 +50,64 @@ 'swatch_image', $swatchData['swatches'][$option]['value'] ); - $escapedUrl = $block->escapeUrl($swatchImagePath); + $escapedUrl = $escaper->escapeUrl($swatchImagePath); ?> -
escapeJs($option); - $script = 'var ' .$element - .' = document.querySelector(\'div[data-option-id="' .$escaper->escapeJs($option) - .'"]\');' .PHP_EOL; - $script .= $element .'.style.background = "url(\'' - .$escapedUrl .'\') no-repeat center";' .PHP_EOL; - $script .= $element .'.style.backgroundSize = "initial";'; + $element = 'swatchImageOption' . $escaper->escapeJs($option); + $script = 'var ' . $element + . ' = document.querySelector(\'div[data-option-id="' . $escaper->escapeJs($option) + . '"]\');' . PHP_EOL; + $script .= $element . '.style.background = "url(\'' + . $escapedUrl . '\') no-repeat center";' . PHP_EOL; + $script .= $element . '.style.backgroundSize = "initial";'; ?> - renderTag('script', [], $script, false); ?> + renderTag('script', [], $script, false) ?> -
escapeHtmlAttr( $swatchData['swatches'][$option]['value'] ) ?>">
escapeJs($option); + $element = 'swatchImageOption' . $escaper->escapeJs($option); $backgroundValue = $escaper->escapeJs( str_replace('\'', '\\\'', $swatchData['swatches'][$option]['value']) ); - $script = 'var ' .$element - .' = document.querySelector(\'div[data-option-id="' .$escaper->escapeJs($option) - .'"]\');' .PHP_EOL; - $script .= $element .'.style.background = "' .$backgroundValue - .' no-repeat center";' .PHP_EOL; - $script .= $element .'.style.backgroundSize = "initial";'; + $script = 'var ' . $element + . ' = document.querySelector(\'div[data-option-id="' . $escaper->escapeJs($option) + . '"]\');' . PHP_EOL; + $script .= $element . '.style.background = "' . $backgroundValue + . ' no-repeat center";' . PHP_EOL; + $script .= $element . '.style.backgroundSize = "initial";'; ?> - renderTag('script', [], $script, false); ?> + renderTag('script', [], $script, false) ?> -
escapeHtml($swatchData['swatches'][$option]['value']) ?>
+ >escapeHtml($swatchData['swatches'][$option]['value']) ?>
@@ -108,14 +115,12 @@
-escapeJs($swatchData['attribute_code'])}') - .find('[data-option-type="1"], [data-option-type="2"], [data-option-type="0"], [data-option-type="3"]') - .SwatchRendererTooltip(); - }); - -script; -?> -renderTag('script', [], $scriptString, false) ?> + diff --git a/app/code/Magento/Swatches/view/frontend/web/js/layered/swatch-renderer.js b/app/code/Magento/Swatches/view/frontend/web/js/layered/swatch-renderer.js new file mode 100644 index 0000000000000..de96dc15b4ff7 --- /dev/null +++ b/app/code/Magento/Swatches/view/frontend/web/js/layered/swatch-renderer.js @@ -0,0 +1,30 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +define([ + 'jquery', + 'Magento_Swatches/js/swatch-renderer' +], function ($) { + 'use strict'; + + $.widget('mage.layeredSwatchRenderer', { + options: { + showTooltip: 1, + swatchElementSelector: '[data-option-type="1"], [data-option-type="2"], ' + + '[data-option-type="0"], [data-option-type="3"]' + }, + + /** + * @private + */ + _create: function () { + if (this.options.showTooltip === 1) { + $(this.element).find(this.options.swatchElementSelector).SwatchRendererTooltip(); + } + } + }); + + return $.mage.layeredSwatchRenderer; +}); From 354ec22b3153048eb8b751c5366364316ec90022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Szubert?= Date: Tue, 6 Oct 2020 14:35:30 +0200 Subject: [PATCH 2/2] Fix #29971 - cover changes with mftf test --- ...dminAssignCategoryToProductActionGroup.xml | 21 +++ ...ignCategoryToProductAndSaveActionGroup.xml | 12 +- ...SaveProductWithNewAttributeActionGroup.xml | 26 +++ .../StorefrontCategorySidebarSection.xml | 1 + ...nfigurationsByAttributeCodeActionGroup.xml | 2 + ...ndFilterInLayeredNavigationActionGroup.xml | 21 +++ .../StorefrontLayeredNavigationSection.xml | 1 + .../Swatches/Model/Plugin/FilterRenderer.php | 14 +- ...vigationSwatchTooltipHiddenActionGroup.xml | 19 +++ ...igationSwatchTooltipVisibleActionGroup.xml | 23 +++ ...ductPageSwatchTooltipHiddenActionGroup.xml | 19 +++ ...uctPageSwatchTooltipVisibleActionGroup.xml | 24 +++ .../Test/AdminDisablingSwatchTooltipsTest.xml | 160 ++++++------------ 13 files changed, 213 insertions(+), 130 deletions(-) create mode 100644 app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminAssignCategoryToProductActionGroup.xml create mode 100644 app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminSaveProductWithNewAttributeActionGroup.xml create mode 100644 app/code/Magento/LayeredNavigation/Test/Mftf/ActionGroup/StorefrontExpandFilterInLayeredNavigationActionGroup.xml create mode 100644 app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertLayeredNavigationSwatchTooltipHiddenActionGroup.xml create mode 100644 app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertLayeredNavigationSwatchTooltipVisibleActionGroup.xml create mode 100644 app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertProductPageSwatchTooltipHiddenActionGroup.xml create mode 100644 app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertProductPageSwatchTooltipVisibleActionGroup.xml diff --git a/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminAssignCategoryToProductActionGroup.xml b/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminAssignCategoryToProductActionGroup.xml new file mode 100644 index 0000000000000..a8cafd4433161 --- /dev/null +++ b/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminAssignCategoryToProductActionGroup.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + diff --git a/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminAssignCategoryToProductAndSaveActionGroup.xml b/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminAssignCategoryToProductAndSaveActionGroup.xml index 0cb7c4885ac77..2d0396f6a0b0c 100644 --- a/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminAssignCategoryToProductAndSaveActionGroup.xml +++ b/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminAssignCategoryToProductAndSaveActionGroup.xml @@ -8,17 +8,9 @@ - - - - - - - - - + - \ No newline at end of file + diff --git a/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminSaveProductWithNewAttributeActionGroup.xml b/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminSaveProductWithNewAttributeActionGroup.xml new file mode 100644 index 0000000000000..1a991f963569c --- /dev/null +++ b/app/code/Magento/Catalog/Test/Mftf/ActionGroup/AdminSaveProductWithNewAttributeActionGroup.xml @@ -0,0 +1,26 @@ + + + + + + + Clicks on the Save button. If popup related to new attribute appears, clicks on the Confirm button to close it. Validates that the Success Message is present and correct. + + + + + + + + + + + diff --git a/app/code/Magento/Catalog/Test/Mftf/Section/StorefrontCategorySidebarSection/StorefrontCategorySidebarSection.xml b/app/code/Magento/Catalog/Test/Mftf/Section/StorefrontCategorySidebarSection/StorefrontCategorySidebarSection.xml index 5ec493aef0cea..64cc35bdd3419 100644 --- a/app/code/Magento/Catalog/Test/Mftf/Section/StorefrontCategorySidebarSection/StorefrontCategorySidebarSection.xml +++ b/app/code/Magento/Catalog/Test/Mftf/Section/StorefrontCategorySidebarSection/StorefrontCategorySidebarSection.xml @@ -16,6 +16,7 @@ + diff --git a/app/code/Magento/ConfigurableProduct/Test/Mftf/ActionGroup/GenerateConfigurationsByAttributeCodeActionGroup.xml b/app/code/Magento/ConfigurableProduct/Test/Mftf/ActionGroup/GenerateConfigurationsByAttributeCodeActionGroup.xml index 80248cf5e00f8..3baab15bb573b 100644 --- a/app/code/Magento/ConfigurableProduct/Test/Mftf/ActionGroup/GenerateConfigurationsByAttributeCodeActionGroup.xml +++ b/app/code/Magento/ConfigurableProduct/Test/Mftf/ActionGroup/GenerateConfigurationsByAttributeCodeActionGroup.xml @@ -17,6 +17,7 @@ + @@ -28,5 +29,6 @@ + diff --git a/app/code/Magento/LayeredNavigation/Test/Mftf/ActionGroup/StorefrontExpandFilterInLayeredNavigationActionGroup.xml b/app/code/Magento/LayeredNavigation/Test/Mftf/ActionGroup/StorefrontExpandFilterInLayeredNavigationActionGroup.xml new file mode 100644 index 0000000000000..dfe771cb053df --- /dev/null +++ b/app/code/Magento/LayeredNavigation/Test/Mftf/ActionGroup/StorefrontExpandFilterInLayeredNavigationActionGroup.xml @@ -0,0 +1,21 @@ + + + + + + + Expands the filter on a Storefront layered navigation if it is not expanded. + + + + + + + + diff --git a/app/code/Magento/LayeredNavigation/Test/Mftf/Section/LayeredNavigationSection/StorefrontLayeredNavigationSection.xml b/app/code/Magento/LayeredNavigation/Test/Mftf/Section/LayeredNavigationSection/StorefrontLayeredNavigationSection.xml index d3a3005c296b2..5bf2ed43347dd 100644 --- a/app/code/Magento/LayeredNavigation/Test/Mftf/Section/LayeredNavigationSection/StorefrontLayeredNavigationSection.xml +++ b/app/code/Magento/LayeredNavigation/Test/Mftf/Section/LayeredNavigationSection/StorefrontLayeredNavigationSection.xml @@ -9,5 +9,6 @@ xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
+
diff --git a/app/code/Magento/Swatches/Model/Plugin/FilterRenderer.php b/app/code/Magento/Swatches/Model/Plugin/FilterRenderer.php index ec7030903c6cc..961d664d36db3 100644 --- a/app/code/Magento/Swatches/Model/Plugin/FilterRenderer.php +++ b/app/code/Magento/Swatches/Model/Plugin/FilterRenderer.php @@ -9,7 +9,6 @@ use Closure; use Magento\Catalog\Model\Layer\Filter\FilterInterface; -use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\View\LayoutInterface; use Magento\LayeredNavigation\Block\Navigation\FilterRenderer as Subject; @@ -22,12 +21,12 @@ class FilterRenderer /** * @var LayoutInterface */ - protected $layout; + private $layout; /** * @var Data */ - protected $swatchHelper; + private $swatchHelper; /** * @var ConfigurableViewModel|null @@ -37,22 +36,21 @@ class FilterRenderer /** * @var string */ - protected $block = RenderLayered::class; + private $block = RenderLayered::class; /** * @param LayoutInterface $layout * @param Data $swatchHelper - * @param ConfigurableViewModel|null $configurableViewModel + * @param ConfigurableViewModel $configurableViewModel */ public function __construct( LayoutInterface $layout, Data $swatchHelper, - ?ConfigurableViewModel $configurableViewModel = null + ConfigurableViewModel $configurableViewModel ) { $this->layout = $layout; $this->swatchHelper = $swatchHelper; - $this->configurableViewModel = $configurableViewModel - ?? ObjectManager::getInstance()->get(ConfigurableViewModel::class); + $this->configurableViewModel = $configurableViewModel; } /** diff --git a/app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertLayeredNavigationSwatchTooltipHiddenActionGroup.xml b/app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertLayeredNavigationSwatchTooltipHiddenActionGroup.xml new file mode 100644 index 0000000000000..30aec8a5c5aab --- /dev/null +++ b/app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertLayeredNavigationSwatchTooltipHiddenActionGroup.xml @@ -0,0 +1,19 @@ + + + + + + + Validates that the swatch tooltip doesn't appear on layered navigation after hovering swatch. + + + + + + diff --git a/app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertLayeredNavigationSwatchTooltipVisibleActionGroup.xml b/app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertLayeredNavigationSwatchTooltipVisibleActionGroup.xml new file mode 100644 index 0000000000000..6eb159db51af1 --- /dev/null +++ b/app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertLayeredNavigationSwatchTooltipVisibleActionGroup.xml @@ -0,0 +1,23 @@ + + + + + + + Validates that the swatch tooltip appears on layered navigation after hovering swatch. + + + + + + + + + + diff --git a/app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertProductPageSwatchTooltipHiddenActionGroup.xml b/app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertProductPageSwatchTooltipHiddenActionGroup.xml new file mode 100644 index 0000000000000..8c813a950e76f --- /dev/null +++ b/app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertProductPageSwatchTooltipHiddenActionGroup.xml @@ -0,0 +1,19 @@ + + + + + + + Validates that the swatch tooltip doesn't appear on product page after hovering swatch. + + + + + + diff --git a/app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertProductPageSwatchTooltipVisibleActionGroup.xml b/app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertProductPageSwatchTooltipVisibleActionGroup.xml new file mode 100644 index 0000000000000..c067471bf9295 --- /dev/null +++ b/app/code/Magento/Swatches/Test/Mftf/ActionGroup/AssertProductPageSwatchTooltipVisibleActionGroup.xml @@ -0,0 +1,24 @@ + + + + + + + Validates that the swatch tooltip appears on product page after hovering swatch. + + + + + + + + + + + diff --git a/app/code/Magento/Swatches/Test/Mftf/Test/AdminDisablingSwatchTooltipsTest.xml b/app/code/Magento/Swatches/Test/Mftf/Test/AdminDisablingSwatchTooltipsTest.xml index b48f181c8d199..077428a30b19b 100644 --- a/app/code/Magento/Swatches/Test/Mftf/Test/AdminDisablingSwatchTooltipsTest.xml +++ b/app/code/Magento/Swatches/Test/Mftf/Test/AdminDisablingSwatchTooltipsTest.xml @@ -18,145 +18,81 @@ - - + + + + + + + + + - - - - - - - - - - - + - - + + + - - - - - - - - - - - - - + + - - - - + + + - - - - - + - - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + - - - - + + + + + + + - - - - - - - + + + + + + + + + + + +