Skip to content

Commit

Permalink
Merge pull request #262 from magento-fearless-kiwis/develop
Browse files Browse the repository at this point in the history
[FearlessKiwis] FAT coverage for SalesRule and bug fix
  • Loading branch information
Tang, Yu(ytang1) committed Dec 24, 2015
2 parents e2e4d7e + fb45f19 commit b37999c
Show file tree
Hide file tree
Showing 14 changed files with 587 additions and 60 deletions.
4 changes: 2 additions & 2 deletions app/code/Magento/Rule/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ public function beforeSave()
// Serialize conditions
if ($this->getConditions()) {
$this->setConditionsSerialized(serialize($this->getConditions()->asArray()));
$this->unsConditions();
$this->_conditions = null;
}

// Serialize actions
if ($this->getActions()) {
$this->setActionsSerialized(serialize($this->getActions()->asArray()));
$this->unsActions();
$this->_actions = null;
}

/**
Expand Down
82 changes: 81 additions & 1 deletion app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class RuleTest extends \PHPUnit_Framework_TestCase
*/
protected $coupon;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\SalesRule\Model\Rule\Condition\CombineFactory
*/
protected $conditionCombineFactoryMock;

/**
* @var \Magento\SalesRule\Model\Rule\Condition\Product\CombineFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $condProdCombineFactoryMock;

public function setUp()
{
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand All @@ -35,10 +45,22 @@ public function setUp()
->method('create')
->willReturn($this->coupon);

$this->conditionCombineFactoryMock = $this->getMockBuilder(
'\Magento\SalesRule\Model\Rule\Condition\CombineFactory'
)->disableOriginalConstructor()
->getMock();

$this->condProdCombineFactoryMock = $this->getMockBuilder(
'\Magento\SalesRule\Model\Rule\Condition\Product\CombineFactory'
)->disableOriginalConstructor()
->getMock();

$this->model = $objectManager->getObject(
'Magento\SalesRule\Model\Rule',
[
'couponFactory' => $couponFactory
'couponFactory' => $couponFactory,
'condCombineFactory' => $this->conditionCombineFactoryMock,
'condProdCombineF' => $this->condProdCombineFactoryMock,
]
);
}
Expand Down Expand Up @@ -70,4 +92,62 @@ public function testLoadCouponCode()
$this->model->loadCouponCode();
$this->assertEquals(1, $this->model->getUsesPerCoupon());
}

public function testBeforeSaveResetConditionToNull()
{
$conditionMock = $this->setupConditionMock();

//Make sure that we reset _condition in beforeSave method
$this->conditionCombineFactoryMock->expects($this->exactly(2))
->method('create')
->willReturn($conditionMock);

$prodConditionMock = $this->setupProdConditionMock();
$this->condProdCombineFactoryMock->expects($this->exactly(2))
->method('create')
->willReturn($prodConditionMock);

$this->model->beforeSave();
$this->model->getConditions();
$this->model->getActions();
}

protected function setupProdConditionMock()
{
$prodConditionMock = $this->getMockBuilder('\Magento\SalesRule\Model\Rule\Condition\Product\Combine')
->disableOriginalConstructor()
->setMethods(['setRule', 'setId', 'loadArray', 'getConditions'])
->getMock();

$prodConditionMock->expects($this->any())
->method('setRule')
->willReturnSelf();
$prodConditionMock->expects($this->any())
->method('setId')
->willReturnSelf();
$prodConditionMock->expects($this->any())
->method('getConditions')
->willReturn([]);

return $prodConditionMock;
}

protected function setupConditionMock()
{
$conditionMock = $this->getMockBuilder('\Magento\SalesRule\Model\Rule\Condition\Combine')
->disableOriginalConstructor()
->setMethods(['setRule', 'setId', 'loadArray', 'getConditions'])
->getMock();
$conditionMock->expects($this->any())
->method('setRule')
->willReturnSelf();
$conditionMock->expects($this->any())
->method('setId')
->willReturnSelf();
$conditionMock->expects($this->any())
->method('getConditions')
->willReturn([]);

return $conditionMock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ abstract class Conditions extends Curl
'is not' => '!=',
'equal to' => '==',
'matches' => '==',
'greater than' => '>',
'equals or greater than' => '>=',
],
'value_type' => [
'same_as' => 'the Same as Matched Product Categories',
Expand All @@ -60,9 +62,15 @@ abstract class Conditions extends Curl
'California' => '12',
'United States' => 'US',
'[flatrate] Fixed' => 'flatrate_flatrate',
'FOUND' => '1',
'TRUE' => '1',
],
'aggregator' => [
'ALL' => 'all',
'ANY' => 'any',
],
'attribute'=> [
'total quantity' => 'qty',
],
];

Expand Down Expand Up @@ -181,7 +189,7 @@ private function convertSingleCondition($condition)
);
}

return $typeParam + $ruleParam;
return $ruleParam + $typeParam;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@

<dataset name="simple_for_salesrule_2">
<field name="attribute_set_id" xsi:type="array">
<item name="dataset" xsi:type="string">default</item>
<item name="dataset" xsi:type="string">custom_attribute_set_with_colors</item>
</field>
<field name="name" xsi:type="string">Simple Product %isolation%</field>
<field name="sku" xsi:type="string">sku_simple_product_%isolation%</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Magento\SalesRule\Test\Block\Adminhtml\Promo\Quote\Edit;

use Magento\Backend\Test\Block\Widget\FormTabs;
use Magento\Mtf\Client\Element\SimpleElement;
use Magento\Mtf\Fixture\FixtureInterface;

/**
* Sales rule edit form.
Expand All @@ -25,4 +27,45 @@ class PromoQuoteForm extends FormTabs
* @var boolean
*/
protected $waitForSelectorVisible = false;

/**
* Fill form with tabs.
*
* @param FixtureInterface $fixture
* @param SimpleElement $element
* @param array $replace
* @return $this|FormTabs
*/
public function fill(FixtureInterface $fixture, SimpleElement $element = null, array $replace = null)
{
$tabs = $this->getFieldsByTabs($fixture);
if ($replace) {
$tabs = $this->prepareData($tabs, $replace);
}
$this->fillTabs($tabs, $element);
}

/**
* Replace placeholders in each values of data.
*
* @param array $tabs
* @param array $replace
* @return array
*/
protected function prepareData(array $tabs, array $replace)
{
foreach ($replace as $tabName => $fields) {
foreach ($fields as $key => $pairs) {
if (isset($tabs[$tabName][$key])) {
$tabs[$tabName][$key]['value'] = str_replace(
array_keys($pairs),
array_values($pairs),
$tabs[$tabName][$key]['value']
);
}
}
}

return $tabs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ abstract class AssertCartPriceRuleApplying extends AbstractConstraint
*/
protected $productForSalesRule2;

/**
* Cart prices to compare.
*
* @array cartPrice
*/
protected $cartPrice;

/**
* Implementation assert.
*
Expand Down Expand Up @@ -120,15 +127,16 @@ abstract protected function assert();
* @param CustomerAccountLogout $customerAccountLogout
* @param CatalogCategoryView $catalogCategoryView
* @param CatalogProductView $catalogProductView
* @param Customer $customer
* @param SalesRule $salesRule
* @param SalesRule $salesRuleOrigin
* @param array $productQuantity
* @param CatalogProductSimple $productForSalesRule1
* @param CatalogProductSimple $productForSalesRule2
* @param Customer $customer
* @param Address $address
* @param int|null $isLoggedIn
* @param array $shipping
* @param array $cartPrice
* @return void
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand All @@ -140,25 +148,29 @@ public function processAssert(
CustomerAccountLogout $customerAccountLogout,
CatalogCategoryView $catalogCategoryView,
CatalogProductView $catalogProductView,
Customer $customer,
SalesRule $salesRule,
SalesRule $salesRuleOrigin,
array $productQuantity,
CatalogProductSimple $productForSalesRule1,
CatalogProductSimple $productForSalesRule2 = null,
Customer $customer = null,
Address $address = null,
$isLoggedIn = null,
array $shipping = []
array $shipping = [],
array $cartPrice = []
) {
$this->checkoutCart = $checkoutCart;
$this->cmsIndex = $cmsIndex;
$this->customerAccountLogin = $customerAccountLogin;
$this->customerAccountLogout = $customerAccountLogout;
$this->catalogCategoryView = $catalogCategoryView;
$this->catalogProductView = $catalogProductView;
$this->customer = $customer;
$this->productForSalesRule1 = $productForSalesRule1;
$this->productForSalesRule2 = $productForSalesRule2;
$this->cartPrice = $cartPrice;
if ($customer !== null) {
$this->customer = $customer;
}
$isLoggedIn ? $this->login() : $this->customerAccountLogout->open();
$this->checkoutCart->open()->getCartBlock()->clearShoppingCart();
$this->addProductsToCart($productQuantity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@
namespace Magento\SalesRule\Test\Constraint;

/**
* Check that shopping cart subtotal not equals with grand total(excluding shipping price if exist).
* Assert that Catalog Price Rule is applied in Shopping Cart.
*/
class AssertCartPriceRuleConditionIsApplied extends AssertCartPriceRuleApplying
{
/**
* Assert that shopping cart subtotal not equals with grand total.
* Assert that Catalog Price Rule is applied in Shopping Cart.
*
* @return void
*/
protected function assert()
{
$subTotal = $this->checkoutCart->getTotalsBlock()->getSubtotal();
$grandTotal = $this->checkoutCart->getTotalsBlock()->getGrandTotal();
$this->checkoutCart->getTotalsBlock()->waitForShippingPriceBlock();
$this->checkoutCart->getTotalsBlock()->waitForUpdatedTotals();
$actualPrices['sub_total'] = $this->checkoutCart->getTotalsBlock()->getSubtotal();
$actualPrices['grand_total'] = $this->checkoutCart->getTotalsBlock()->getGrandTotal();
$actualPrices['discount'] = $this->checkoutCart->getTotalsBlock()->getDiscount();
$expectedPrices = $this->cartPrice;

if ($this->checkoutCart->getTotalsBlock()->isVisibleShippingPriceBlock()) {
$shippingPrice = $this->checkoutCart->getTotalsBlock()->getShippingPrice();
$grandTotal = number_format(($grandTotal - $shippingPrice), 2);
}
\PHPUnit_Framework_Assert::assertNotEquals(
$subTotal,
$grandTotal,
'Shopping cart subtotal: \'' . $subTotal . '\' equals with grand total: \'' . $grandTotal . '\''
\PHPUnit_Framework_Assert::assertEquals(
$expectedPrices,
$actualPrices,
'Wrong total cart prices are displayed.'
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,27 @@ class Curl extends Conditions implements SalesRuleInterface
'type' => 'Magento\SalesRule\Model\Rule\Condition\Address',
'attribute' => 'base_subtotal',
],
'Total Items Quantity' => [
'type' => 'Magento\SalesRule\Model\Rule\Condition\Address',
'attribute' => 'total_qty',
],
'Conditions combination' => [
'type' => 'Magento\SalesRule\Model\Rule\Condition\Combine',
'aggregator' => 'all',
'value' => '1',
],
'Products subselection' => [
'type' => 'Magento\SalesRule\Model\Rule\Condition\Product\Subselect',
'attribute' => 'qty',
'operator' => '==',
'value' => '1',
'aggregator' => 'all',
],
'Product attribute combination' => [
'type' => 'Magento\SalesRule\Model\Rule\Condition\Product\Found',
'value' => '1',
'aggregator' => 'all',
],
'Shipping Country' => [
'type' => 'Magento\SalesRule\Model\Rule\Condition\Address',
'attribute' => 'country_id',
Expand All @@ -57,6 +73,18 @@ class Curl extends Conditions implements SalesRuleInterface
'Category' => [
'type' => 'Magento\SalesRule\Model\Rule\Condition\Product',
'attribute' => 'category_ids',
],
'Price in cart' => [
'type' => 'Magento\SalesRule\Model\Rule\Condition\Product',
'attribute' => 'quote_item_price',
],
'Quantity in cart' => [
'type' => 'Magento\SalesRule\Model\Rule\Condition\Product',
'attribute' => 'quote_item_qty',
],
'Row total in cart' => [
'type' => 'Magento\SalesRule\Model\Rule\Condition\Product',
'attribute' => 'quote_item_row_total',
]
];

Expand Down
Loading

0 comments on commit b37999c

Please sign in to comment.