You are here

public function CommerceDiscountTest::testCommerceDiscountFreeProductsOrderDiscount in Commerce Discount 7

Test free bonus products order discounts.

File

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

Class

CommerceDiscountTest
Testing commerce discounts functionality.

Code

public function testCommerceDiscountFreeProductsOrderDiscount() {

  // Create 'free bonus products' product discount.
  $discount = $this
    ->createDiscount('order_discount', 'free_products', array(
    $this->product->product_id,
  ));

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

  // Recalculate discounts.
  $order_wrapper = commerce_cart_order_refresh($order);

  // Check if the discount was applied on the order total price.
  $this
    ->assertEqual($order_wrapper->commerce_order_total->amount
    ->value(), 1000, 'Free Bonus Products order discount has the price of only one product.');
  $this
    ->assertEqual($order_wrapper->commerce_line_items
    ->count(), 2, 'Free Bonus Products order discount is added as a line item.');

  // Recalculate discounts.
  $order_wrapper = commerce_cart_order_refresh($order);

  // Check if the discount was applied on the order total price.
  $this
    ->assertEqual($order_wrapper->commerce_order_total->amount
    ->value(), 1000, 'Free Bonus Products order discount has the price of only one product even after refresh.');
  $this
    ->assertEqual($order_wrapper->commerce_line_items
    ->count(), 2, 'Free Bonus Products order discount is added as a line item even after refresh.');

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

  // Re-save the order.
  // Check if the discount was applied on the order total price.
  $order_wrapper = commerce_cart_order_refresh($order);
  $this
    ->assertEqual($order_wrapper->commerce_order_total->amount
    ->value(), 1000, "Free Bonus Products order discount is removed when it's not applicable and price is the same.");
  $this
    ->assertEqual($order_wrapper->commerce_line_items
    ->count(), 1, "Free Bonus Products order discount is removed when it's not applicable and line item count is only 1");
}