/**
  * @param $parentsIds
  * @param $validationResult
  * @param $runValidateAmount
  * @param $result
  * @dataProvider dataProviderForValidateWithValidConfigurableProduct
  * @return void
  */
 public function testAroundValidateWithValidConfigurableProduct($parentsIds, $validationResult, $runValidateAmount, $result)
 {
     $closureMock = function () {
         return false;
     };
     $this->productMock->expects($this->once())->method('getId')->willReturn('product_id');
     $this->configurableMock->expects($this->once())->method('getParentIdsByChild')->with('product_id')->willReturn($parentsIds);
     $this->ruleMock->expects($this->exactly($runValidateAmount))->method('getConditions')->willReturn($this->ruleConditionsMock);
     $this->ruleConditionsMock->expects($this->exactly($runValidateAmount))->method('validateByEntityId')->willReturnMap($validationResult);
     $this->assertEquals($result, $this->validation->aroundValidate($this->ruleMock, $closureMock, $this->productMock));
 }
 /**
  * Remove catalog attribute condition by attribute code from rule conditions
  *
  * @param \Magento\Rule\Model\Condition\Combine $combine
  * @param string $attributeCode
  * @return void
  */
 protected function _removeAttributeFromConditions($combine, $attributeCode)
 {
     $conditions = $combine->getConditions();
     foreach ($conditions as $conditionId => $condition) {
         if ($condition instanceof \Magento\Rule\Model\Condition\Combine) {
             $this->_removeAttributeFromConditions($condition, $attributeCode);
         }
         if ($condition instanceof \Magento\SalesRule\Model\Rule\Condition\Product) {
             if ($condition->getAttribute() == $attributeCode) {
                 unset($conditions[$conditionId]);
             }
         }
     }
     $combine->setConditions($conditions);
 }
Example #3
0
 /**
  * @return array
  */
 public function getNewChildSelectOptions()
 {
     $productAttributes = $this->productFactory->create()->loadAttributeOptions()->getAttributeOption();
     $attributes = [];
     foreach ($productAttributes as $code => $label) {
         $attributes[] = ['value' => 'Magento\\CatalogWidget\\Model\\Rule\\Condition\\Product|' . $code, 'label' => $label];
     }
     $conditions = parent::getNewChildSelectOptions();
     $conditions = array_merge_recursive($conditions, [['value' => 'Magento\\CatalogWidget\\Model\\Rule\\Condition\\Combine', 'label' => __('Conditions Combination')], ['label' => __('Product Attribute'), 'value' => $attributes]]);
     return $conditions;
 }
Example #4
0
 /**
  * {@inheritDoc}
  */
 public function getNewChildSelectOptions()
 {
     $productAttributes = $this->productConditionFactory->create()->loadAttributeOptions()->getAttributeOption();
     $attributes = [];
     $productConditionType = get_class($this->productConditionFactory->create());
     foreach ($productAttributes as $code => $label) {
         $attributes[] = ['value' => $productConditionType . '|' . $code, 'label' => $label];
     }
     $conditions = parent::getNewChildSelectOptions();
     $conditions = array_merge_recursive($conditions, [['value' => $this->getType(), 'label' => __('Conditions Combination')], ['label' => __('Product Attribute'), 'value' => $attributes]]);
     return $conditions;
 }
Example #5
0
 /**
  * @dataProvider dataProviderCallbackValidateProduct
  * @param bool $validate
  *
  * @return void
  */
 public function testCallbackValidateProduct($validate)
 {
     $args['product'] = $this->productModel;
     $args['attributes'] = [];
     $args['idx'] = 0;
     $args['row'] = ['entity_id' => '1', 'entity_type_id' => '4', 'attribute_set_id' => '4', 'type_id' => 'simple', 'sku' => 'Product', 'has_options' => '0', 'required_options' => '0', 'created_at' => '2014-06-25 13:14:30', 'updated_at' => '2014-06-25 14:37:15'];
     $this->storeManager->expects($this->any())->method('getWebsites')->with(false)->will($this->returnValue([$this->websiteModel, $this->websiteModel]));
     $this->websiteModel->expects($this->at(0))->method('getId')->will($this->returnValue('1'));
     $this->websiteModel->expects($this->at(2))->method('getId')->will($this->returnValue('2'));
     $this->websiteModel->expects($this->any())->method('getDefaultStore')->will($this->returnValue($this->storeModel));
     $this->storeModel->expects($this->at(0))->method('getId')->will($this->returnValue('1'));
     $this->storeModel->expects($this->at(1))->method('getId')->will($this->returnValue('2'));
     $this->combineFactory->expects($this->any())->method('create')->will($this->returnValue($this->condition));
     $this->condition->expects($this->any())->method('validate')->will($this->returnValue($validate));
     $this->condition->expects($this->any())->method('setRule')->will($this->returnSelf());
     $this->productModel->expects($this->any())->method('getId')->will($this->returnValue(1));
     $this->rule->callbackValidateProduct($args);
     $matchingProducts = $this->rule->getMatchingProductIds();
     foreach ($matchingProducts['1'] as $matchingRules) {
         $this->assertEquals($validate, $matchingRules);
     }
 }
Example #6
0
 /**
  * Get new child select options
  *
  * @return array
  */
 public function getNewChildSelectOptions()
 {
     $productAttributes = $this->_ruleConditionProd->loadAttributeOptions()->getAttributeOption();
     $pAttributes = array();
     $iAttributes = array();
     foreach ($productAttributes as $code => $label) {
         if (strpos($code, 'quote_item_') === 0) {
             $iAttributes[] = array('value' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Product|' . $code, 'label' => $label);
         } else {
             $pAttributes[] = array('value' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Product|' . $code, 'label' => $label);
         }
     }
     $conditions = parent::getNewChildSelectOptions();
     $conditions = array_merge_recursive($conditions, array(array('value' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Product\\Combine', 'label' => __('Conditions Combination')), array('label' => __('Cart Item Attribute'), 'value' => $iAttributes), array('label' => __('Product Attribute'), 'value' => $pAttributes)));
     return $conditions;
 }
Example #7
0
 /**
  * Get new child select options
  *
  * @return array
  */
 public function getNewChildSelectOptions()
 {
     $addressAttributes = $this->_conditionAddress->loadAttributeOptions()->getAttributeOption();
     $attributes = [];
     foreach ($addressAttributes as $code => $label) {
         $attributes[] = ['value' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Address|' . $code, 'label' => $label];
     }
     $conditions = parent::getNewChildSelectOptions();
     $conditions = array_merge_recursive($conditions, [['value' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Product\\Found', 'label' => __('Product attribute combination')], ['value' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Product\\Subselect', 'label' => __('Products subselection')], ['value' => 'Magento\\SalesRule\\Model\\Rule\\Condition\\Combine', 'label' => __('Conditions combination')], ['label' => __('Cart Attribute'), 'value' => $attributes]]);
     $additional = new \Magento\Framework\Object();
     $this->_eventManager->dispatch('salesrule_rule_condition_combine', ['additional' => $additional]);
     $additionalConditions = $additional->getConditions();
     if ($additionalConditions) {
         $conditions = array_merge_recursive($conditions, $additionalConditions);
     }
     return $conditions;
 }
 /**
  * Set up test
  *
  * @return void
  */
 protected function setUp()
 {
     $this->resource = $this->getMock('Magento\\Framework\\App\\Resource', ['getConnection', 'getTableName'], [], '', false);
     $this->ruleCollectionFactory = $this->getMock('Magento\\CatalogRule\\Model\\Resource\\Rule\\CollectionFactory', ['create', 'addFieldToFilter'], [], '', false);
     $this->backend = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\AbstractBackend', [], [], '', false);
     $this->select = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false);
     $this->connection = $this->getMock('Magento\\Framework\\DB\\Adapter\\AdapterInterface');
     $this->db = $this->getMock('Zend_Db_Statement_Interface', [], [], '', false);
     $this->website = $this->getMock('Magento\\Store\\Model\\Website', [], [], '', false);
     $this->storeManager = $this->getMock('Magento\\Store\\Model\\StoreManagerInterface', [], [], '', false);
     $this->combine = $this->getMock('Magento\\Rule\\Model\\Condition\\Combine', [], [], '', false);
     $this->rules = $this->getMock('Magento\\CatalogRule\\Model\\Rule', [], [], '', false);
     $this->logger = $this->getMock('Psr\\Log\\LoggerInterface', [], [], '', false);
     $this->attribute = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute', [], [], '', false);
     $this->priceCurrency = $this->getMock('Magento\\Framework\\Pricing\\PriceCurrencyInterface');
     $this->dateFormat = $this->getMock('Magento\\Framework\\Stdlib\\DateTime', [], [], '', false);
     $this->dateTime = $this->getMock('Magento\\Framework\\Stdlib\\DateTime\\DateTime', [], [], '', false);
     $this->eavConfig = $this->getMock('Magento\\Eav\\Model\\Config', [], [], '', false);
     $this->product = $this->getMock('Magento\\Catalog\\Model\\Product', [], [], '', false);
     $this->productFactory = $this->getMock('Magento\\Catalog\\Model\\ProductFactory', ['create'], [], '', false);
     $this->connection->expects($this->any())->method('select')->will($this->returnValue($this->select));
     $this->connection->expects($this->any())->method('query')->will($this->returnValue($this->db));
     $this->select->expects($this->any())->method('distinct')->will($this->returnSelf());
     $this->select->expects($this->any())->method('where')->will($this->returnSelf());
     $this->select->expects($this->any())->method('from')->will($this->returnSelf());
     $this->select->expects($this->any())->method('order')->will($this->returnSelf());
     $this->resource->expects($this->any())->method('getConnection')->will($this->returnValue($this->connection));
     $this->resource->expects($this->any())->method('getTableName')->will($this->returnArgument(0));
     $this->storeManager->expects($this->any())->method('getWebsites')->will($this->returnValue([$this->website]));
     $this->storeManager->expects($this->any())->method('getWebsite')->will($this->returnValue($this->website));
     $this->rules->expects($this->any())->method('getId')->will($this->returnValue(1));
     $this->rules->expects($this->any())->method('getWebsiteIds')->will($this->returnValue([1]));
     $this->rules->expects($this->any())->method('getConditions')->will($this->returnValue($this->combine));
     $this->rules->expects($this->any())->method('getCustomerGroupIds')->will($this->returnValue([1]));
     $this->ruleCollectionFactory->expects($this->any())->method('create')->will($this->returnSelf());
     $this->ruleCollectionFactory->expects($this->any())->method('addFieldToFilter')->will($this->returnValue([$this->rules]));
     $this->product->expects($this->any())->method('load')->will($this->returnSelf());
     $this->product->expects($this->any())->method('getId')->will($this->returnValue(1));
     $this->product->expects($this->any())->method('getWebsiteIds')->will($this->returnValue([1]));
     $this->combine->expects($this->any())->method('validate')->will($this->returnValue(true));
     $this->attribute->expects($this->any())->method('getBackend')->will($this->returnValue($this->backend));
     $this->eavConfig->expects($this->any())->method('getAttribute')->will($this->returnValue($this->attribute));
     $this->productFactory->expects($this->any())->method('create')->will($this->returnValue($this->product));
     $this->indexBuilder = new \Magento\CatalogRule\Model\Indexer\IndexBuilder($this->ruleCollectionFactory, $this->priceCurrency, $this->resource, $this->storeManager, $this->logger, $this->eavConfig, $this->dateFormat, $this->dateTime, $this->productFactory);
 }
Example #9
0
 /**
  * Retrieve rule combine conditions model
  *
  * @return \Magento\Rule\Model\Condition\Combine
  */
 public function getConditions()
 {
     if (empty($this->_conditions)) {
         $this->_resetConditions();
     }
     // Load rule conditions if it is applicable
     if ($this->hasConditionsSerialized()) {
         $conditions = $this->getConditionsSerialized();
         if (!empty($conditions)) {
             $conditions = unserialize($conditions);
             if (is_array($conditions) && !empty($conditions)) {
                 $this->_conditions->loadArray($conditions);
             }
         }
         $this->unsConditionsSerialized();
     }
     return $this->_conditions;
 }
Example #10
0
 /**
  * @param Combine $combine
  * @param string $value
  * @return string
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function _getMappedSqlCombination(Combine $combine, $value = '')
 {
     $out = !empty($value) ? $value : '';
     $value = $combine->getValue() ? '' : ' NOT ';
     $getAggregator = $combine->getAggregator();
     $conditions = $combine->getConditions();
     foreach ($conditions as $key => $condition) {
         /** @var $condition AbstractCondition|Combine */
         $con = $getAggregator == 'any' ? \Zend_Db_Select::SQL_OR : \Zend_Db_Select::SQL_AND;
         $con = isset($conditions[$key + 1]) ? $con : '';
         if ($condition instanceof Combine) {
             $out .= $this->_getMappedSqlCombination($condition, $value);
         } else {
             $out .= $this->_getMappedSqlCondition($condition, $value);
         }
         $out .= $out ? ' ' . $con : '';
     }
     return $this->_expressionFactory->create(['expression' => $out]);
 }
Example #11
0
 /**
  * @covers \Magento\Rule\Model\Condition\AbstractCondition::getValueName
  * @dataProvider optionValuesData
  * @param string|array $value
  * @param string $expectingData
  */
 public function testGetValueName($value, $expectingData)
 {
     $this->_combine->setValueOption(array('option_key' => 'option_value'))->setValue($value);
     $this->assertEquals($expectingData, $this->_combine->getValueName());
 }