Skip to content

Commit b37999c

Browse files
author
Tang, Yu(ytang1)
committed
Merge pull request #262 from magento-fearless-kiwis/develop
[FearlessKiwis] FAT coverage for SalesRule and bug fix
2 parents e2e4d7e + fb45f19 commit b37999c

File tree

14 files changed

+587
-60
lines changed

14 files changed

+587
-60
lines changed

app/code/Magento/Rule/Model/AbstractModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ public function beforeSave()
118118
// Serialize conditions
119119
if ($this->getConditions()) {
120120
$this->setConditionsSerialized(serialize($this->getConditions()->asArray()));
121-
$this->unsConditions();
121+
$this->_conditions = null;
122122
}
123123

124124
// Serialize actions
125125
if ($this->getActions()) {
126126
$this->setActionsSerialized(serialize($this->getActions()->asArray()));
127-
$this->unsActions();
127+
$this->_actions = null;
128128
}
129129

130130
/**

app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ class RuleTest extends \PHPUnit_Framework_TestCase
1818
*/
1919
protected $coupon;
2020

21+
/**
22+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\SalesRule\Model\Rule\Condition\CombineFactory
23+
*/
24+
protected $conditionCombineFactoryMock;
25+
26+
/**
27+
* @var \Magento\SalesRule\Model\Rule\Condition\Product\CombineFactory|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
protected $condProdCombineFactoryMock;
30+
2131
public function setUp()
2232
{
2333
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -35,10 +45,22 @@ public function setUp()
3545
->method('create')
3646
->willReturn($this->coupon);
3747

48+
$this->conditionCombineFactoryMock = $this->getMockBuilder(
49+
'\Magento\SalesRule\Model\Rule\Condition\CombineFactory'
50+
)->disableOriginalConstructor()
51+
->getMock();
52+
53+
$this->condProdCombineFactoryMock = $this->getMockBuilder(
54+
'\Magento\SalesRule\Model\Rule\Condition\Product\CombineFactory'
55+
)->disableOriginalConstructor()
56+
->getMock();
57+
3858
$this->model = $objectManager->getObject(
3959
'Magento\SalesRule\Model\Rule',
4060
[
41-
'couponFactory' => $couponFactory
61+
'couponFactory' => $couponFactory,
62+
'condCombineFactory' => $this->conditionCombineFactoryMock,
63+
'condProdCombineF' => $this->condProdCombineFactoryMock,
4264
]
4365
);
4466
}
@@ -70,4 +92,62 @@ public function testLoadCouponCode()
7092
$this->model->loadCouponCode();
7193
$this->assertEquals(1, $this->model->getUsesPerCoupon());
7294
}
95+
96+
public function testBeforeSaveResetConditionToNull()
97+
{
98+
$conditionMock = $this->setupConditionMock();
99+
100+
//Make sure that we reset _condition in beforeSave method
101+
$this->conditionCombineFactoryMock->expects($this->exactly(2))
102+
->method('create')
103+
->willReturn($conditionMock);
104+
105+
$prodConditionMock = $this->setupProdConditionMock();
106+
$this->condProdCombineFactoryMock->expects($this->exactly(2))
107+
->method('create')
108+
->willReturn($prodConditionMock);
109+
110+
$this->model->beforeSave();
111+
$this->model->getConditions();
112+
$this->model->getActions();
113+
}
114+
115+
protected function setupProdConditionMock()
116+
{
117+
$prodConditionMock = $this->getMockBuilder('\Magento\SalesRule\Model\Rule\Condition\Product\Combine')
118+
->disableOriginalConstructor()
119+
->setMethods(['setRule', 'setId', 'loadArray', 'getConditions'])
120+
->getMock();
121+
122+
$prodConditionMock->expects($this->any())
123+
->method('setRule')
124+
->willReturnSelf();
125+
$prodConditionMock->expects($this->any())
126+
->method('setId')
127+
->willReturnSelf();
128+
$prodConditionMock->expects($this->any())
129+
->method('getConditions')
130+
->willReturn([]);
131+
132+
return $prodConditionMock;
133+
}
134+
135+
protected function setupConditionMock()
136+
{
137+
$conditionMock = $this->getMockBuilder('\Magento\SalesRule\Model\Rule\Condition\Combine')
138+
->disableOriginalConstructor()
139+
->setMethods(['setRule', 'setId', 'loadArray', 'getConditions'])
140+
->getMock();
141+
$conditionMock->expects($this->any())
142+
->method('setRule')
143+
->willReturnSelf();
144+
$conditionMock->expects($this->any())
145+
->method('setId')
146+
->willReturnSelf();
147+
$conditionMock->expects($this->any())
148+
->method('getConditions')
149+
->willReturn([]);
150+
151+
return $conditionMock;
152+
}
73153
}

dev/tests/functional/tests/app/Magento/Backend/Test/Handler/Conditions.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ abstract class Conditions extends Curl
5252
'is not' => '!=',
5353
'equal to' => '==',
5454
'matches' => '==',
55+
'greater than' => '>',
56+
'equals or greater than' => '>=',
5557
],
5658
'value_type' => [
5759
'same_as' => 'the Same as Matched Product Categories',
@@ -60,9 +62,15 @@ abstract class Conditions extends Curl
6062
'California' => '12',
6163
'United States' => 'US',
6264
'[flatrate] Fixed' => 'flatrate_flatrate',
65+
'FOUND' => '1',
66+
'TRUE' => '1',
6367
],
6468
'aggregator' => [
6569
'ALL' => 'all',
70+
'ANY' => 'any',
71+
],
72+
'attribute'=> [
73+
'total quantity' => 'qty',
6674
],
6775
];
6876

@@ -181,7 +189,7 @@ private function convertSingleCondition($condition)
181189
);
182190
}
183191

184-
return $typeParam + $ruleParam;
192+
return $ruleParam + $typeParam;
185193
}
186194

187195
/**

dev/tests/functional/tests/app/Magento/Catalog/Test/Repository/CatalogProductSimple.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@
402402

403403
<dataset name="simple_for_salesrule_2">
404404
<field name="attribute_set_id" xsi:type="array">
405-
<item name="dataset" xsi:type="string">default</item>
405+
<item name="dataset" xsi:type="string">custom_attribute_set_with_colors</item>
406406
</field>
407407
<field name="name" xsi:type="string">Simple Product %isolation%</field>
408408
<field name="sku" xsi:type="string">sku_simple_product_%isolation%</field>

dev/tests/functional/tests/app/Magento/SalesRule/Test/Block/Adminhtml/Promo/Quote/Edit/PromoQuoteForm.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Magento\SalesRule\Test\Block\Adminhtml\Promo\Quote\Edit;
77

88
use Magento\Backend\Test\Block\Widget\FormTabs;
9+
use Magento\Mtf\Client\Element\SimpleElement;
10+
use Magento\Mtf\Fixture\FixtureInterface;
911

1012
/**
1113
* Sales rule edit form.
@@ -25,4 +27,45 @@ class PromoQuoteForm extends FormTabs
2527
* @var boolean
2628
*/
2729
protected $waitForSelectorVisible = false;
30+
31+
/**
32+
* Fill form with tabs.
33+
*
34+
* @param FixtureInterface $fixture
35+
* @param SimpleElement $element
36+
* @param array $replace
37+
* @return $this|FormTabs
38+
*/
39+
public function fill(FixtureInterface $fixture, SimpleElement $element = null, array $replace = null)
40+
{
41+
$tabs = $this->getFieldsByTabs($fixture);
42+
if ($replace) {
43+
$tabs = $this->prepareData($tabs, $replace);
44+
}
45+
$this->fillTabs($tabs, $element);
46+
}
47+
48+
/**
49+
* Replace placeholders in each values of data.
50+
*
51+
* @param array $tabs
52+
* @param array $replace
53+
* @return array
54+
*/
55+
protected function prepareData(array $tabs, array $replace)
56+
{
57+
foreach ($replace as $tabName => $fields) {
58+
foreach ($fields as $key => $pairs) {
59+
if (isset($tabs[$tabName][$key])) {
60+
$tabs[$tabName][$key]['value'] = str_replace(
61+
array_keys($pairs),
62+
array_values($pairs),
63+
$tabs[$tabName][$key]['value']
64+
);
65+
}
66+
}
67+
}
68+
69+
return $tabs;
70+
}
2871
}

dev/tests/functional/tests/app/Magento/SalesRule/Test/Constraint/AssertCartPriceRuleApplying.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ abstract class AssertCartPriceRuleApplying extends AbstractConstraint
8888
*/
8989
protected $productForSalesRule2;
9090

91+
/**
92+
* Cart prices to compare.
93+
*
94+
* @array cartPrice
95+
*/
96+
protected $cartPrice;
97+
9198
/**
9299
* Implementation assert.
93100
*
@@ -120,15 +127,16 @@ abstract protected function assert();
120127
* @param CustomerAccountLogout $customerAccountLogout
121128
* @param CatalogCategoryView $catalogCategoryView
122129
* @param CatalogProductView $catalogProductView
123-
* @param Customer $customer
124130
* @param SalesRule $salesRule
125131
* @param SalesRule $salesRuleOrigin
126132
* @param array $productQuantity
127133
* @param CatalogProductSimple $productForSalesRule1
128134
* @param CatalogProductSimple $productForSalesRule2
135+
* @param Customer $customer
129136
* @param Address $address
130137
* @param int|null $isLoggedIn
131138
* @param array $shipping
139+
* @param array $cartPrice
132140
* @return void
133141
*
134142
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -140,25 +148,29 @@ public function processAssert(
140148
CustomerAccountLogout $customerAccountLogout,
141149
CatalogCategoryView $catalogCategoryView,
142150
CatalogProductView $catalogProductView,
143-
Customer $customer,
144151
SalesRule $salesRule,
145152
SalesRule $salesRuleOrigin,
146153
array $productQuantity,
147154
CatalogProductSimple $productForSalesRule1,
148155
CatalogProductSimple $productForSalesRule2 = null,
156+
Customer $customer = null,
149157
Address $address = null,
150158
$isLoggedIn = null,
151-
array $shipping = []
159+
array $shipping = [],
160+
array $cartPrice = []
152161
) {
153162
$this->checkoutCart = $checkoutCart;
154163
$this->cmsIndex = $cmsIndex;
155164
$this->customerAccountLogin = $customerAccountLogin;
156165
$this->customerAccountLogout = $customerAccountLogout;
157166
$this->catalogCategoryView = $catalogCategoryView;
158167
$this->catalogProductView = $catalogProductView;
159-
$this->customer = $customer;
160168
$this->productForSalesRule1 = $productForSalesRule1;
161169
$this->productForSalesRule2 = $productForSalesRule2;
170+
$this->cartPrice = $cartPrice;
171+
if ($customer !== null) {
172+
$this->customer = $customer;
173+
}
162174
$isLoggedIn ? $this->login() : $this->customerAccountLogout->open();
163175
$this->checkoutCart->open()->getCartBlock()->clearShoppingCart();
164176
$this->addProductsToCart($productQuantity);

dev/tests/functional/tests/app/Magento/SalesRule/Test/Constraint/AssertCartPriceRuleConditionIsApplied.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@
77
namespace Magento\SalesRule\Test\Constraint;
88

99
/**
10-
* Check that shopping cart subtotal not equals with grand total(excluding shipping price if exist).
10+
* Assert that Catalog Price Rule is applied in Shopping Cart.
1111
*/
1212
class AssertCartPriceRuleConditionIsApplied extends AssertCartPriceRuleApplying
1313
{
1414
/**
15-
* Assert that shopping cart subtotal not equals with grand total.
15+
* Assert that Catalog Price Rule is applied in Shopping Cart.
1616
*
1717
* @return void
1818
*/
1919
protected function assert()
2020
{
21-
$subTotal = $this->checkoutCart->getTotalsBlock()->getSubtotal();
22-
$grandTotal = $this->checkoutCart->getTotalsBlock()->getGrandTotal();
21+
$this->checkoutCart->getTotalsBlock()->waitForShippingPriceBlock();
22+
$this->checkoutCart->getTotalsBlock()->waitForUpdatedTotals();
23+
$actualPrices['sub_total'] = $this->checkoutCart->getTotalsBlock()->getSubtotal();
24+
$actualPrices['grand_total'] = $this->checkoutCart->getTotalsBlock()->getGrandTotal();
25+
$actualPrices['discount'] = $this->checkoutCart->getTotalsBlock()->getDiscount();
26+
$expectedPrices = $this->cartPrice;
2327

24-
if ($this->checkoutCart->getTotalsBlock()->isVisibleShippingPriceBlock()) {
25-
$shippingPrice = $this->checkoutCart->getTotalsBlock()->getShippingPrice();
26-
$grandTotal = number_format(($grandTotal - $shippingPrice), 2);
27-
}
28-
\PHPUnit_Framework_Assert::assertNotEquals(
29-
$subTotal,
30-
$grandTotal,
31-
'Shopping cart subtotal: \'' . $subTotal . '\' equals with grand total: \'' . $grandTotal . '\''
28+
\PHPUnit_Framework_Assert::assertEquals(
29+
$expectedPrices,
30+
$actualPrices,
31+
'Wrong total cart prices are displayed.'
3232
);
3333
}
3434

dev/tests/functional/tests/app/Magento/SalesRule/Test/Handler/SalesRule/Curl.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,27 @@ class Curl extends Conditions implements SalesRuleInterface
4141
'type' => 'Magento\SalesRule\Model\Rule\Condition\Address',
4242
'attribute' => 'base_subtotal',
4343
],
44+
'Total Items Quantity' => [
45+
'type' => 'Magento\SalesRule\Model\Rule\Condition\Address',
46+
'attribute' => 'total_qty',
47+
],
4448
'Conditions combination' => [
4549
'type' => 'Magento\SalesRule\Model\Rule\Condition\Combine',
4650
'aggregator' => 'all',
4751
'value' => '1',
4852
],
53+
'Products subselection' => [
54+
'type' => 'Magento\SalesRule\Model\Rule\Condition\Product\Subselect',
55+
'attribute' => 'qty',
56+
'operator' => '==',
57+
'value' => '1',
58+
'aggregator' => 'all',
59+
],
60+
'Product attribute combination' => [
61+
'type' => 'Magento\SalesRule\Model\Rule\Condition\Product\Found',
62+
'value' => '1',
63+
'aggregator' => 'all',
64+
],
4965
'Shipping Country' => [
5066
'type' => 'Magento\SalesRule\Model\Rule\Condition\Address',
5167
'attribute' => 'country_id',
@@ -57,6 +73,18 @@ class Curl extends Conditions implements SalesRuleInterface
5773
'Category' => [
5874
'type' => 'Magento\SalesRule\Model\Rule\Condition\Product',
5975
'attribute' => 'category_ids',
76+
],
77+
'Price in cart' => [
78+
'type' => 'Magento\SalesRule\Model\Rule\Condition\Product',
79+
'attribute' => 'quote_item_price',
80+
],
81+
'Quantity in cart' => [
82+
'type' => 'Magento\SalesRule\Model\Rule\Condition\Product',
83+
'attribute' => 'quote_item_qty',
84+
],
85+
'Row total in cart' => [
86+
'type' => 'Magento\SalesRule\Model\Rule\Condition\Product',
87+
'attribute' => 'quote_item_row_total',
6088
]
6189
];
6290

0 commit comments

Comments
 (0)