Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreateCatalogPriceRuleEntityTest.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\CatalogRule\Test\Fixture\CatalogRule;
13 
27 {
28  /* tags */
29  const TEST_TYPE = 'acceptance_test, extended_acceptance_test';
30  const MVP = 'yes';
31  /* end tags */
32 
42  public function test(
43  CatalogRule $catalogPriceRule,
44  $product = null,
45  $conditionEntity = null,
46  Customer $customer = null
47  ) {
48  // Prepare data
50  $productSimple = $this->fixtureFactory->createByCode('catalogProductSimple', ['dataset' => $product]);
51  $catalogPriceRule = $this->applyCustomerGroup($catalogPriceRule, $customer);
52  $replace = $this->prepareCondition($productSimple, $conditionEntity);
53 
54  // Steps
55  $this->catalogRuleIndex->open();
56  $this->catalogRuleIndex->getGridPageActions()->addNew();
57  $this->catalogRuleNew->getEditForm()->fill($catalogPriceRule, null, $replace);
58  $this->catalogRuleNew->getFormPageActions()->save();
59  }
60 
68  protected function applyCustomerGroup(CatalogRule $catalogPriceRule, Customer $customer = null)
69  {
70  if ($customer !== null) {
71  $customer->persist();
73  $customerGroup = $customer->getDataFieldConfig('group_id')['source']->getCustomerGroup();
74  $catalogPriceRule = $this->fixtureFactory->createByCode(
75  'catalogRule',
76  [
77  'data' => array_merge(
78  $catalogPriceRule->getData(),
79  ['customer_group_ids' => $customerGroup->getCustomerGroupCode()]
80  )
81  ]
82  );
83  }
84 
85  return $catalogPriceRule;
86  }
87 
95  private function prepareCondition(CatalogProductSimple $productSimple, $conditionEntity)
96  {
97  $result = [];
98 
99  switch ($conditionEntity) {
100  case 'category':
101  $result['%category_id%'] = $productSimple->getDataFieldConfig('category_ids')['source']->getIds()[0];
102  break;
103  case 'attribute':
105  $attributes = $productSimple->getDataFieldConfig('attribute_set_id')['source']
106  ->getAttributeSet()->getDataFieldConfig('assigned_attributes')['source']->getAttributes();
107 
108  $result['%attribute_name%'] = $attributes[0]->getFrontendLabel();
109  $result['%attribute_value%'] = $attributes[0]->getOptions()[0]['view'];
110  break;
111  }
112 
113  return [
114  'conditions' => [
115  'conditions' => $result,
116  ],
117  ];
118  }
119 }
$customer
Definition: customers.php:11
$attributes
Definition: matrix.phtml:13