Skip to content

Commit

Permalink
ENGCOM-4000: Remove sku from operators to validate condition #20412
Browse files Browse the repository at this point in the history
  • Loading branch information
sidolov authored Mar 18, 2019
2 parents 3b9b259 + abe4071 commit d002ee2
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 25 deletions.
103 changes: 103 additions & 0 deletions app/code/Magento/CatalogWidget/Setup/UpgradeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogWidget\Setup;

use Magento\CatalogWidget\Block\Product\ProductsList;
use Magento\CatalogWidget\Model\Rule\Condition\Product as ConditionProduct;
use Magento\Framework\Serialize\Serializer\Json as Serializer;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

/**
* Upgrade data for CatalogWidget module.
*/
class UpgradeData implements UpgradeDataInterface
{
/**
* @var Serializer
*/
private $serializer;

/**
* @param Serializer $serializer
*/
public function __construct(
Serializer $serializer
) {
$this->serializer = $serializer;
}

/**
* @inheritdoc
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '2.0.1', '<')) {
$this->replaceIsWithIsOneOf($setup);
}
}

/**
* Replace 'is' condition with 'is one of' in database.
*
* If 'is' product list condition is used with multiple skus it should be replaced by 'is one of' condition.
*
* @param ModuleDataSetupInterface $setup
*/
private function replaceIsWithIsOneOf(ModuleDataSetupInterface $setup)
{
$tableName = $setup->getTable('widget_instance');
$connection = $setup->getConnection();
$select = $connection->select()
->from(
$tableName,
[
'instance_id',
'widget_parameters',
]
)->where('instance_type = ? ', ProductsList::class);

$result = $setup->getConnection()->fetchAll($select);

if ($result) {
$updatedData = $this->updateWidgetData($result);

$connection->insertOnDuplicate(
$tableName,
$updatedData
);
}
}

/**
* Replace 'is' condition with 'is one of' in widget parameters.
*
* @param array $result
* @return array
*/
private function updateWidgetData(array $result): array
{
return array_map(
function ($widgetData) {
$widgetParameters = $this->serializer->unserialize($widgetData['widget_parameters']);
foreach ($widgetParameters['conditions'] as &$condition) {
if (ConditionProduct::class === $condition['type'] &&
'sku' === $condition['attribute'] &&
'==' === $condition['operator']) {
$condition['operator'] = '()';
}
}
$widgetData['widget_parameters'] = $this->serializer->serialize($widgetParameters);

return $widgetData;
},
$result
);
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/CatalogWidget/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_CatalogWidget" setup_version="2.0.0">
<module name="Magento_CatalogWidget" setup_version="2.0.1">
<sequence>
<module name="Magento_Catalog"/>
<module name="Magento_Widget"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public function getDefaultOperatorInputByType()
*/
$this->_defaultOperatorInputByType['category'] = ['==', '!=', '{}', '!{}', '()', '!()'];
$this->_arrayInputTypes[] = 'category';
$this->_defaultOperatorInputByType['sku'] = ['==', '!=', '{}', '!{}', '()', '!()'];
}
return $this->_defaultOperatorInputByType;
}
Expand Down Expand Up @@ -383,9 +382,6 @@ public function getInputType()
if ($this->getAttributeObject()->getAttributeCode() == 'category_ids') {
return 'category';
}
if ($this->getAttributeObject()->getAttributeCode() == 'sku') {
return 'sku';
}
switch ($this->getAttributeObject()->getFrontendInput()) {
case 'select':
return 'select';
Expand Down Expand Up @@ -614,10 +610,6 @@ public function getBindArgumentValue()
$this->getValueParsed()
)->__toString()
);
} elseif ($this->getAttribute() === 'sku') {
$value = $this->getData('value');
$value = preg_split('#\s*[,;]\s*#', $value, null, PREG_SPLIT_NO_EMPTY);
$this->setValueParsed($value);
}

return parent::getBindArgumentValue();
Expand Down Expand Up @@ -718,7 +710,7 @@ protected function _getAttributeSetId($productId)
public function getOperatorForValidate()
{
$operator = $this->getOperator();
if (in_array($this->getInputType(), ['category', 'sku'])) {
if ('category' === $this->getInputType()) {
if ($operator == '==') {
$operator = '{}';
} elseif ($operator == '!=') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
<click selector="{{AdminNewWidgetSection.addNewCondition}}" stepKey="clickAddNewCondition"/>
<selectOption selector="{{AdminNewWidgetSection.selectCondition}}" userInput="{{widget.condition}}" stepKey="selectCondition"/>
<waitForElement selector="{{AdminNewWidgetSection.ruleParameter}}" stepKey="waitRuleParameter"/>
<click selector="{{AdminNewWidgetSection.ruleParameterEdit}}" stepKey="clickRuleParameterEdit"/>
<selectOption selector="{{AdminNewWidgetSection.ruleParameterEditSelect}}" userInput="{{widget.ruleIsOneOf}}" stepKey="selectRuleParameterEdit"/>
<click selector="{{AdminNewWidgetSection.ruleParameter}}" stepKey="clickRuleParameter"/>
<click selector="{{AdminNewWidgetSection.openChooser}}" stepKey="clickChooser"/>
<waitForPageLoad stepKey="waitForAjaxLoad"/>
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Widget/Test/Mftf/Data/WidgetsData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<data key="condition">SKU</data>
<data key="display_on">All Pages</data>
<data key="container">Main Content Area</data>
<data key="ruleIsOneOf">is one of</data>
</entity>
<entity name="DynamicBlocksRotatorWidget" type="widget">
<data key="type">Banner Rotator</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<element name="widgetOptions" type="select" selector="#widget_instace_tabs_properties_section"/>
<element name="addNewCondition" type="select" selector=".rule-param.rule-param-new-child"/>
<element name="selectCondition" type="input" selector="#conditions__1__new_child"/>
<element name="ruleParameterEdit" type="select" selector="#conditions__1__children>li:nth-child(1)>span:nth-child(3)>a"/>
<element name="ruleParameterEditSelect" type="select" selector="#conditions__1__children>li:nth-child(1)>span:nth-child(3)>span>select"/>
<element name="ruleParameter" type="select" selector="#conditions__1__children>li:nth-child(1)>span:nth-child(4)>a"/>
<element name="setRuleParameter" type="input" selector="#conditions__1--1__value"/>
<element name="applyParameter" type="button" selector=".rule-param-apply"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,6 @@ public function testCreateCollection()
$this->performAssertions(2);
}

/**
* Test product list widget can process condition with multiple product sku.
*
* @magentoDbIsolation disabled
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
*/
public function testCreateCollectionWithMultipleSkuCondition()
{
$encodedConditions = '^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,' .
'`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule|' .
'|Condition||Product`,`attribute`:`sku`,`operator`:`==`,`value`:`simple1, simple2`^]^]';
$this->block->setData('conditions_encoded', $encodedConditions);
$this->performAssertions(2);
}

/**
* Test product list widget can process condition with dropdown type of attribute
*
Expand Down

0 comments on commit d002ee2

Please sign in to comment.