You are here

public function CommerceDiscountTest::testCommerceDiscountOneHundredPercentOff in Commerce Discount 7

Test a 100% off product percentage discount.

A 100% off product-level discount differs from the "Free product" discount offer type since those are normally used as an "add-on" that is added without the user's interaction (maybe as part of a package, or bonus); whereas a 100% off discount should be used for any product purposely added to a cart order.

File

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

Class

CommerceDiscountTest
Testing commerce discounts functionality.

Code

public function testCommerceDiscountOneHundredPercentOff() {

  // Login as the store admin.
  $this
    ->drupalLogin($this->store_admin);

  // Create a 100% off discount and create a test product.
  $discount = $this
    ->createDiscount('product_discount', 'percentage', 100, 'freebie');
  $product = $this
    ->createDummyProduct('TEST-PRODUCT', 'Test Product', 999);

  // Create the order and apply the freebie discount.
  $order = $this
    ->createDummyOrder($this->store_admin->uid, array(
    $product->product_id => 1,
  ));
  $order_wrapper = commerce_cart_order_refresh($order);
  $properly_applied = $this
    ->discountAppliedToOrder('freebie', $order);
  $this
    ->assertTrue($properly_applied, t('100% off discount applied to a product.'));

  // Verify that the product is now free.
  $unit_price = $order_wrapper->commerce_line_items
    ->get(0)->commerce_unit_price
    ->value();
  $this
    ->assertEqual($unit_price['amount'], 0, 'Product line item unit price amount is properly set to 0.');
  $this
    ->assertEqual($unit_price['data']['components'][1]['price']['amount'], -999, 'Product line item unit price discount component properly set to 100% of the product price.');
  $order_wrapper = commerce_cart_order_refresh($order);
  $properly_applied = $this
    ->discountAppliedToOrder('freebie', $order);
  $this
    ->assertTrue($properly_applied, t('100% off discount applied to a product even after refresh.'));

  // Verify that the product is now free.
  $unit_price = $order_wrapper->commerce_line_items
    ->get(0)->commerce_unit_price
    ->value();
  $this
    ->assertEqual($unit_price['amount'], 0, 'Product line item unit price amount is properly set to 0 even after refresh.');
  $this
    ->assertEqual($unit_price['data']['components'][1]['price']['amount'], -999, 'Product line item unit price discount component properly set to 100% of the product price even after refresh.');

  // Disable the discount.
  $discount->status = FALSE;
  entity_save('commerce_discount', $discount);

  // Recalculate discounts.
  $order_wrapper = commerce_cart_order_refresh($order);
  $unit_price = $order_wrapper->commerce_line_items
    ->get(0)->commerce_unit_price
    ->value();
  $properly_applied = $this
    ->discountAppliedToOrder('freebie', $order);
  $this
    ->assertFalse($properly_applied, t('100% off discount not applied to a product.'));
  $this
    ->assertEqual($unit_price['amount'], 999, 'Product line item unit price amount is 999.');
}