You are here

public function CommerceDiscountTest::testCommerceDiscountCompatibilityStrategies in Commerce Discount 7

Test discount compatibility strategies.

Currently implemented strategies include:

  • any: discount is compatible with any other discount.
  • except: discount is compatible with any discount except selected ones.
  • only: discount is only compatible with selected ones.
  • none: discount is not compatible with any other discount.

Compatibility is checked first to ensure that discounts already on an order are not incompatible with the discount being added. It is then checked to ensure the discount being added is not incompatible with any discount that has already been added to the order.

File

tests/commerce_discount.test, line 398
Commerce Discounts tests.

Class

CommerceDiscountTest
Testing commerce discounts functionality.

Code

public function testCommerceDiscountCompatibilityStrategies() {

  // Create two discounts set to execute one after the other.
  $discount_one = $this
    ->createDiscount('order_discount', 'fixed_amount', 100, 'of1', 1);
  $discount_one_wrapper = entity_metadata_wrapper('commerce_discount', $discount_one);
  $discount_two = $this
    ->createDiscount('order_discount', 'fixed_amount', 200, 'of2', 2);
  $discount_two_wrapper = entity_metadata_wrapper('commerce_discount', $discount_two);

  // Create an order and recalculate discounts.
  $order = $this
    ->createDummyOrder($this->store_customer->uid, array(
    $this->product->product_id => 1,
  ), 'completed');

  // Test compatibility with both discounts using the "any" strategy. Both
  // discounts should be applied.
  commerce_cart_order_refresh($order);
  $properly_applied = $this
    ->discountAppliedToOrder('of1', $order) && $this
    ->discountAppliedToOrder('of2', $order);
  $this
    ->assertTrue($properly_applied, t('Discount one and two applied when both are compatible with any discount.'));

  // Test compatibility with only discount one using the "none" strategy. Only
  // discount one should be applied.
  $discount_one_wrapper->commerce_compatibility_strategy = 'none';
  $discount_one_wrapper
    ->save();
  commerce_cart_order_refresh($order);
  $properly_applied = $this
    ->discountAppliedToOrder('of1', $order) && !$this
    ->discountAppliedToOrder('of2', $order);
  $this
    ->assertTrue($properly_applied, t('Only discount one applied when it is not compatible with any other discount.'));

  // Test compatibility with only discount two using the "none" strategy. Only
  // discount one should be applied.
  $discount_one_wrapper->commerce_compatibility_strategy = 'any';
  $discount_one_wrapper
    ->save();
  $discount_two_wrapper->commerce_compatibility_strategy = 'none';
  $discount_two_wrapper
    ->save();
  commerce_cart_order_refresh($order);
  $properly_applied = $this
    ->discountAppliedToOrder('of1', $order) && !$this
    ->discountAppliedToOrder('of2', $order);
  $this
    ->assertTrue($properly_applied, t('Only discount one applied when discount two is not compatible with any other discount.'));

  // Test compatibility with discount one compatible with any discount
  // except discount two. Only discount one should be applied.
  $discount_one_wrapper->commerce_compatibility_strategy = 'except';
  $discount_one_wrapper->commerce_compatibility_selection = array(
    $discount_two->discount_id,
  );
  $discount_one_wrapper
    ->save();
  $discount_two_wrapper->commerce_compatibility_strategy = 'any';
  $discount_two_wrapper
    ->save();
  commerce_cart_order_refresh($order);
  $properly_applied = $this
    ->discountAppliedToOrder('of1', $order) && !$this
    ->discountAppliedToOrder('of2', $order);
  $this
    ->assertTrue($properly_applied, t('Only discount one applied when it is compatible with any discount except discount two.'));

  // Test compatibility with discount two compatible with any discount
  // except discount one. Only discount one should be applied.
  $discount_one_wrapper->commerce_compatibility_strategy = 'any';
  $discount_one_wrapper
    ->save();
  $discount_two_wrapper->commerce_compatibility_strategy = 'except';
  $discount_two_wrapper->commerce_compatibility_selection = array(
    $discount_one->discount_id,
  );
  $discount_two_wrapper
    ->save();
  commerce_cart_order_refresh($order);
  $properly_applied = $this
    ->discountAppliedToOrder('of1', $order) && !$this
    ->discountAppliedToOrder('of2', $order);
  $this
    ->assertTrue($properly_applied, t('Only discount one applied when it is compatible with any discount and discount two is compatible with any discount except discount one.'));

  // Test compatibility with discount two compatible with only discount
  // one. Both discounts should be applied.
  $discount_two_wrapper->commerce_compatibility_strategy = 'only';
  $discount_two_wrapper
    ->save();
  commerce_cart_order_refresh($order);
  $properly_applied = $this
    ->discountAppliedToOrder('of1', $order) && $this
    ->discountAppliedToOrder('of2', $order);
  $this
    ->assertTrue($properly_applied, t('Both discounts applied when discount one is compatible with any discount and discount two is compatible only with discount one.'));

  // Test compatibility with discount two compatible with only discount
  // one. Both discounts should be applied.
  $discount_one_wrapper->commerce_compatibility_strategy = 'only';
  $discount_one_wrapper
    ->save();
  $discount_two_wrapper->commerce_compatibility_strategy = 'any';
  $discount_two_wrapper
    ->save();
  commerce_cart_order_refresh($order);
  $properly_applied = $this
    ->discountAppliedToOrder('of1', $order) && $this
    ->discountAppliedToOrder('of2', $order);
  $this
    ->assertTrue($properly_applied, t('Both discounts applied when discount one is only compatible with discount two and discount two is compatible with any discount.'));
}