Example #1
0
 /**
  * @magentoAppIsolation enabled
  * @covers \Magento\CatalogRule\Model\Rule::calcProductPriceRule
  */
 public function testCalcProductPriceRule()
 {
     $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product');
     $this->assertEquals($this->_object->calcProductPriceRule($product, 100), 45);
     $product->setParentId(true);
     $this->assertEquals($this->_object->calcProductPriceRule($product, 50), 5);
 }
 /**
  * @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));
 }
 /**
  * @param \Magento\CatalogRule\Model\Rule $rule
  * @param array $productIds
  * @return array
  */
 public function afterGetMatchingProductIds(\Magento\CatalogRule\Model\Rule $rule, array $productIds)
 {
     $configurableProductIds = $this->configurableProductsProvider->getIds(array_keys($productIds));
     foreach ($configurableProductIds as $productId) {
         $subProductsIds = $this->configurable->getChildrenIds($productId)[0];
         $parentValidationResult = $productIds[$productId];
         foreach ($subProductsIds as $subProductsId) {
             $productIds[$subProductsId] = $this->getSubProductValidationResult($rule->getId(), $subProductsId, $parentValidationResult);
         }
         unset($productIds[$productId]);
     }
     return $productIds;
 }
 /**
  * @param \Magento\CatalogRule\Model\Rule $rule
  * @param \Closure $proceed
  * @param \Magento\Framework\DataObject|\Magento\Catalog\Model\Product $product
  * @return bool
  */
 public function aroundValidate(\Magento\CatalogRule\Model\Rule $rule, \Closure $proceed, \Magento\Framework\DataObject $product)
 {
     $validateResult = $proceed($product);
     if (!$validateResult && ($configurableProducts = $this->configurable->getParentIdsByChild($product->getId()))) {
         foreach ($configurableProducts as $configurableProductId) {
             $validateResult = $rule->getConditions()->validateByEntityId($configurableProductId);
             // If any of configurable product is valid for current rule, then their sub-product must be valid too
             if ($validateResult) {
                 break;
             }
         }
     }
     return $validateResult;
 }
Example #5
0
 public function testGetConditionsFieldSetId()
 {
     $formName = 'form_name';
     $this->rule->setId(100);
     $expectedResult = 'form_namerule_conditions_fieldset_100';
     $this->assertEquals($expectedResult, $this->rule->getConditionsFieldSetId($formName));
 }
Example #6
0
 /**
  * Test after update action
  *
  * @return void
  */
 public function testAfterUpdate()
 {
     $this->rule->isObjectNew(false);
     $indexer = $this->getMock('\\Magento\\Indexer\\Model\\IndexerInterface');
     $indexer->expects($this->once())->method('invalidate');
     $this->_ruleProductProcessor->expects($this->once())->method('getIndexer')->will($this->returnValue($indexer));
     $this->rule->afterSave();
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function validate(\Magento\Framework\DataObject $object)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'validate');
     if (!$pluginInfo) {
         return parent::validate($object);
     } else {
         return $this->___callPlugins('validate', func_get_args(), $pluginInfo);
     }
 }
Example #8
0
 /**
  * @dataProvider dataProviderCallbackValidateProduct
  * @param bool $validate
  */
 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(true)->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 #9
0
 /**
  * Test IsRuleBehaviorChanged action
  *
  * @dataProvider ruleData
  * @param array $dataArray
  * @param array $originDataArray
  * @param bool $isObjectNew
  * @param bool $result
  *
  * @return void
  */
 public function testIsRuleBehaviorChanged($dataArray, $originDataArray, $isObjectNew, $result)
 {
     $this->rule->setData('website_ids', []);
     $this->rule->isObjectNew($isObjectNew);
     $indexer = $this->getMock('\\Magento\\Indexer\\Model\\IndexerInterface');
     $indexer->expects($this->any())->method('invalidate');
     $this->_ruleProductProcessor->expects($this->any())->method('getIndexer')->will($this->returnValue($indexer));
     foreach ($dataArray as $data) {
         $this->rule->setData($data);
     }
     $this->rule->afterSave();
     foreach ($originDataArray as $data) {
         $this->rule->setOrigData($data);
     }
     $this->assertEquals($result, $this->rule->isRuleBehaviorChanged());
 }
 /**
  * Set up test
  *
  * @return void
  */
 protected function setUp()
 {
     $this->resource = $this->getMock('Magento\\Framework\\App\\ResourceConnection', ['getConnection', 'getTableName'], [], '', false);
     $this->ruleCollectionFactory = $this->getMock('Magento\\CatalogRule\\Model\\ResourceModel\\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->metadataPool = $this->getMock('Magento\\Framework\\EntityManager\\MetadataPool', [], [], '', false);
     $metadata = $this->getMockBuilder('Magento\\Framework\\EntityManager\\EntityMetadata')->disableOriginalConstructor()->getMock();
     $this->metadataPool->expects($this->any())->method('getMetadata')->willReturn($metadata);
     $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', ['getAttribute'], [], '', 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('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->rules->expects($this->any())->method('validate')->with($this->product)->willReturn(true);
     $this->attribute->expects($this->any())->method('getBackend')->will($this->returnValue($this->backend));
     $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);
     $this->setProperties($this->indexBuilder, ['metadataPool' => $this->metadataPool]);
 }
Example #11
0
 /**
  * @param Rule $rule
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function updateRuleProductData(Rule $rule)
 {
     $ruleId = $rule->getId();
     if ($rule->getProductsFilter()) {
         $this->connection->delete($this->getTable('catalogrule_product'), ['rule_id=?' => $ruleId, 'product_id IN (?)' => $rule->getProductsFilter()]);
     } else {
         $this->connection->delete($this->getTable('catalogrule_product'), $this->connection->quoteInto('rule_id=?', $ruleId));
     }
     if (!$rule->getIsActive()) {
         return $this;
     }
     $websiteIds = $rule->getWebsiteIds();
     if (!is_array($websiteIds)) {
         $websiteIds = explode(',', $websiteIds);
     }
     if (empty($websiteIds)) {
         return $this;
     }
     \Magento\Framework\Profiler::start('__MATCH_PRODUCTS__');
     $productIds = $rule->getMatchingProductIds();
     \Magento\Framework\Profiler::stop('__MATCH_PRODUCTS__');
     $customerGroupIds = $rule->getCustomerGroupIds();
     $fromTime = strtotime($rule->getFromDate());
     $toTime = strtotime($rule->getToDate());
     $toTime = $toTime ? $toTime + self::SECONDS_IN_DAY - 1 : 0;
     $sortOrder = (int) $rule->getSortOrder();
     $actionOperator = $rule->getSimpleAction();
     $actionAmount = $rule->getDiscountAmount();
     $subActionOperator = $rule->getSubIsEnable() ? $rule->getSubSimpleAction() : '';
     $subActionAmount = $rule->getSubDiscountAmount();
     $actionStop = $rule->getStopRulesProcessing();
     $rows = [];
     foreach ($productIds as $productId => $validationByWebsite) {
         foreach ($websiteIds as $websiteId) {
             if (empty($validationByWebsite[$websiteId])) {
                 continue;
             }
             foreach ($customerGroupIds as $customerGroupId) {
                 $rows[] = ['rule_id' => $ruleId, 'from_time' => $fromTime, 'to_time' => $toTime, 'website_id' => $websiteId, 'customer_group_id' => $customerGroupId, 'product_id' => $productId, 'action_operator' => $actionOperator, 'action_amount' => $actionAmount, 'action_stop' => $actionStop, 'sort_order' => $sortOrder, 'sub_simple_action' => $subActionOperator, 'sub_discount_amount' => $subActionAmount];
                 if (count($rows) == $this->batchCount) {
                     $this->connection->insertMultiple($this->getTable('catalogrule_product'), $rows);
                     $rows = [];
                 }
             }
         }
     }
     if (!empty($rows)) {
         $this->connection->insertMultiple($this->getTable('catalogrule_product'), $rows);
     }
     return $this;
 }
Example #12
0
 /**
  * Apply catalog rule to product
  *
  * @param ModelRule $rule
  * @param Product $product
  * @param array $websiteIds
  * @return $this
  * @throws \Exception
  */
 public function applyToProduct($rule, $product, $websiteIds)
 {
     if (!$rule->getIsActive()) {
         return $this;
     }
     $ruleId = $rule->getId();
     $productId = $product->getId();
     $write = $this->_getWriteAdapter();
     $write->beginTransaction();
     $write->delete($this->getTable('catalogrule_product'), array($write->quoteInto('rule_id=?', $ruleId), $write->quoteInto('product_id=?', $productId)));
     if (!$rule->getConditions()->validate($product)) {
         $write->delete($this->getTable('catalogrule_product_price'), array($write->quoteInto('product_id=?', $productId)));
         $write->commit();
         return $this;
     }
     $customerGroupIds = $rule->getCustomerGroupIds();
     $fromTime = strtotime($rule->getFromDate());
     $toTime = strtotime($rule->getToDate());
     $toTime = $toTime ? $toTime + self::SECONDS_IN_DAY - 1 : 0;
     $sortOrder = (int) $rule->getSortOrder();
     $actionOperator = $rule->getSimpleAction();
     $actionAmount = $rule->getDiscountAmount();
     $actionStop = $rule->getStopRulesProcessing();
     $subActionOperator = $rule->getSubIsEnable() ? $rule->getSubSimpleAction() : '';
     $subActionAmount = $rule->getSubDiscountAmount();
     $rows = array();
     try {
         foreach ($websiteIds as $websiteId) {
             foreach ($customerGroupIds as $customerGroupId) {
                 $rows[] = array('rule_id' => $ruleId, 'from_time' => $fromTime, 'to_time' => $toTime, 'website_id' => $websiteId, 'customer_group_id' => $customerGroupId, 'product_id' => $productId, 'action_operator' => $actionOperator, 'action_amount' => $actionAmount, 'action_stop' => $actionStop, 'sort_order' => $sortOrder, 'sub_simple_action' => $subActionOperator, 'sub_discount_amount' => $subActionAmount);
                 if (count($rows) == 1000) {
                     $write->insertMultiple($this->getTable('catalogrule_product'), $rows);
                     $rows = array();
                 }
             }
         }
         if (!empty($rows)) {
             $write->insertMultiple($this->getTable('catalogrule_product'), $rows);
         }
     } catch (\Exception $e) {
         $write->rollback();
         throw $e;
     }
     $this->applyAllRulesForDateRange(null, null, $product);
     $write->commit();
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 public function offsetGet($offset)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'offsetGet');
     if (!$pluginInfo) {
         return parent::offsetGet($offset);
     } else {
         return $this->___callPlugins('offsetGet', func_get_args(), $pluginInfo);
     }
 }