You are here

commerce_discount_shipping.test in Commerce Discount 7

Commerce Discounts shipping tests.

File

tests/commerce_discount_shipping.test
View source
<?php

/**
 * @file
 * Commerce Discounts shipping tests.
 */

/**
 * Testing commerce shipping discounts functionality.
 */
class CommerceDiscountShippingTest extends CommerceDiscountTestBase {

  /**
   * Implementation of getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Shipping discounts',
      'description' => 'Test shipping discounts functionality',
      'group' => 'Commerce Discount',
    );
  }

  /**
   * @inheritdoc
   */
  public function setUp() {
    parent::setUp();
    module_enable(array(
      'commerce_discount_shipping_test',
    ));
    $this
      ->resetAll();
  }

  /**
   * Test shipping discounts.
   */
  public function testCommerceDiscountShippingDiscounts() {
    $shipping_service_name = 'commerce_discount_cheap_shipping';
    $decent_shipping_service_name = 'commerce_discount_decent_shipping';
    $expensive_shipping_service_name = 'commerce_discount_expensive_shipping';

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

    // Check shipping prices shown in checkout.
    $this
      ->drupalLogin($this->store_customer);
    $this
      ->drupalGet('checkout/' . $order->order_id . '/shipping');
    $this
      ->assertText('Cheap shipping: $5.00', t('Cheap shipping price displayed correctly on checkout without a discount.'));
    $this
      ->assertText('Decent shipping: $15.00', t('Decent shipping price displayed correctly on checkout without a discount.'));
    $this
      ->assertText('Expensive shipping: $25.00', t('Expensive shipping price displayed correctly on checkout without a discount.'));
    $this
      ->assertText('Deluxe shipping: $55.00', t('Deluxe shipping price displayed correctly on checkout without a discount.'));

    // Select cheap shipping.
    $edit = array(
      'commerce_shipping[shipping_service]' => $shipping_service_name,
    );
    $this
      ->drupalPostAJAX(NULL, $edit, 'commerce_shipping[shipping_service]');

    // Go to next checkout step.
    $this
      ->drupalPost(NULL, $edit, t('Continue to next step'));
    $this
      ->assertText('Shipping$5.00', t('Cheap shipping price is correctly displayed on checkout review.'));
    $this
      ->assertText('Order total$15.00', t('Undiscounted order total with cheap shipping is correctly displayed on checkout review.'));

    // Create a discount.
    $discount = $this
      ->createDiscount('order_discount', 'free_shipping', $shipping_service_name, 'free_shipping', 'Free cheap shipping');

    // Go back to shipping checkout step.
    $this
      ->drupalPost(NULL, array(), t('Go back'));
    $this
      ->assertText('Cheap shipping: $0.00', t('Cheap shipping price displayed correctly on checkout shipping with a cheap discount.'));
    $this
      ->assertText('Decent shipping: $15.00', t('Decent shipping price displayed correctly on checkout shipping with a cheap discount.'));
    $this
      ->assertText('Expensive shipping: $25.00', t('Expensive shipping price displayed correctly on checkout shipping with a cheap discount.'));
    $this
      ->assertText('Deluxe shipping: $55.00', t('Deluxe shipping price displayed correctly on checkout shipping with a cheap discount.'));

    // Go to next checkout step.
    $this
      ->drupalPost(NULL, $edit, t('Continue to next step'));
    $this
      ->assertText('Shipping$5.00', t('Cheap shipping price is correctly displayed on checkout review.'));
    $this
      ->assertText('Free cheap shipping-$5.00', t('Free cheap shipping discount amount is correctly displayed on checkout review.'));
    $this
      ->assertText('Order total$10.00', t('Order total with free cheap shipping discount is correctly displayed on checkout review.'));
    $this
      ->drupalPost(NULL, array(), t('Go back'));

    // Select decent shipping.
    $edit = array(
      'commerce_shipping[shipping_service]' => $decent_shipping_service_name,
    );
    $this
      ->drupalPostAJAX(NULL, $edit, 'commerce_shipping[shipping_service]');

    // Go to next checkout step.
    $this
      ->drupalPost(NULL, $edit, t('Continue to next step'));
    $this
      ->assertText('Shipping$15.00', t('Decent shipping price is correctly displayed.'));
    $this
      ->assertText('Order total$25.00', t('Undiscounted order total with decent shipping and with unapplied cheap shipping discount is correctly displayed.'));

    // Update discount to have a 'discount_all' strategy.
    $discount_wrapper = entity_metadata_wrapper('commerce_discount', $discount);
    $discount_wrapper->commerce_discount_offer->commerce_free_shipping_strategy = 'discount_all';
    $discount_wrapper->commerce_discount_offer
      ->save();
    $this
      ->drupalPost(NULL, array(), t('Go back'));
    $this
      ->assertText('Cheap shipping: $0.00', t('Cheap shipping price displayed correctly on checkout shipping with a cheap discount with discount_all strategy.'));
    $this
      ->assertText('Decent shipping: $10.00', t('Decent shipping price displayed correctly on checkout shipping with a cheap discount with discount_all strategy.'));
    $this
      ->assertText('Expensive shipping: $20.00', t('Expensive shipping price displayed correctly on checkout shipping with a cheap discount with discount_all strategy.'));
    $this
      ->assertText('Deluxe shipping: $50.00', t('Deluxe shipping price displayed correctly on checkout shipping with a cheap discount with discount_all strategy.'));

    // Select expensive shipping.
    $edit = array(
      'commerce_shipping[shipping_service]' => $expensive_shipping_service_name,
    );
    $this
      ->drupalPostAJAX(NULL, $edit, 'commerce_shipping[shipping_service]');

    // Go to next checkout step.
    $this
      ->drupalPost(NULL, $edit, t('Continue to next step'));
    $this
      ->assertText('Shipping$25.00', t('Expensive shipping price is correctly displayed on checkout review.'));
    $this
      ->assertText('Free cheap shipping-$5.00', t('Discount amount on expensive shipping is correctly displayed in case of a discount_all cheap shipping on checkout review.'));
    $this
      ->assertText('Order total$30.00', t('Undiscounted order total with decent shipping and with unapplied discounted cheap shipping is correctly displayed on checkout review.'));

    // Remove discount.
    entity_delete('commerce_discount', $discount->discount_id);

    // Create discount for the expensive shipping method with 'discount_all' strategy.
    $discount = $this
      ->createDiscount('order_discount', 'free_shipping', $expensive_shipping_service_name, 'free_shipping', 'Free expensive shipping');
    $discount_wrapper = entity_metadata_wrapper('commerce_discount', $discount);
    $discount_wrapper->commerce_discount_offer->commerce_free_shipping_strategy = 'discount_all';
    $discount_wrapper->commerce_discount_offer
      ->save();
    $this
      ->drupalPost(NULL, array(), t('Go back'));
    $this
      ->assertText('Cheap shipping: $0.00', t('Cheap shipping price displayed correctly on checkout shipping with an expensive discount with discount_all strategy.'));
    $this
      ->assertText('Decent shipping: $0.00', t('Decent shipping price displayed correctly on checkout shipping with an expensive discount with discount_all strategy.'));
    $this
      ->assertText('Expensive shipping: $0.00', t('Expensive shipping price displayed correctly on checkout shipping with an expensive discount with discount_all strategy.'));
    $this
      ->assertText('Deluxe shipping: $30.00', t('Deluxe shipping price displayed correctly on checkout shipping with an expensive discount with discount_all strategy.'));

    // Select deluxe shipping.
    $edit = array(
      'commerce_shipping[shipping_service]' => 'commerce_discount_deluxe_shipping',
    );
    $this
      ->drupalPostAJAX(NULL, $edit, 'commerce_shipping[shipping_service]');

    // Go to next checkout step.
    $this
      ->drupalPost(NULL, $edit, t('Continue to next step'));
    $this
      ->assertText('Shipping$55.00', t('Deluxe shipping price is correctly displayed on checkout review.'));
    $this
      ->assertText('Free expensive shipping-$25.00', t('Discount amount on deluxe shipping is correctly displayed in case of a discount_all expensive shipping on checkout review.'));
    $this
      ->assertText('Order total$40.00', t('Order total with deluxe shipping and with discounted expensive shipping discount is correctly displayed on checkout review.'));

    // Remove discount.
    entity_delete('commerce_discount', $discount->discount_id);

    // Create a free cheap shipping discount with discount_all strategy.
    $discount = $this
      ->createDiscount('order_discount', 'free_shipping', $shipping_service_name, 'free_shipping', 'Free cheap shipping');
    $discount_wrapper = entity_metadata_wrapper('commerce_discount', $discount);
    $discount_wrapper->commerce_discount_offer->commerce_free_shipping_strategy = 'discount_all';
    $discount_wrapper->commerce_discount_offer
      ->save();

    // Create a 20% percent off shipping discount.
    $offer = array(
      'percent' => 20,
      'service' => '',
    );
    $percent_discount = $this
      ->createDiscount('order_discount', 'percent_off_shipping', $offer, 'percent_off_shipping', '20% off shipping');
    $percent_discount->sort_order = 9;
    entity_save('commerce_discount', $percent_discount);
    $this
      ->drupalPost(NULL, array(), t('Go back'));
    $this
      ->assertText('Cheap shipping: $0.00', t('Cheap shipping price displayed correctly on checkout shipping with an expensive discount with discount_all strategy.'));
    $this
      ->assertText('Decent shipping: $7.00', t('Decent shipping price displayed correctly on checkout shipping with an expensive discount with discount_all strategy.'));
    $this
      ->assertText('Expensive shipping: $15.00', t('Expensive shipping price displayed correctly on checkout shipping with an expensive discount with discount_all strategy.'));
    $this
      ->assertText('Deluxe shipping: $39.00', t('Deluxe shipping price displayed correctly on checkout shipping with an expensive discount with discount_all strategy.'));

    // Select deluxe shipping.
    $edit = array(
      'commerce_shipping[shipping_service]' => 'commerce_discount_deluxe_shipping',
    );
    $this
      ->drupalPostAJAX(NULL, $edit, 'commerce_shipping[shipping_service]');

    // Go to next checkout step.
    $this
      ->drupalPost(NULL, $edit, t('Continue to next step'));
    $this
      ->assertText('Shipping$55.00', t('Deluxe shipping price is correctly displayed on checkout review.'));
    $this
      ->assertText('Free cheap shipping-$5.00', t('Discount amount on deluxe shipping is correctly displayed in case of a discount_all cheap shipping discount on checkout review.'));
    $this
      ->assertText('20% off shipping-$11.00', t('Discount amount on deluxe shipping is correctly displayed in case of an "all" 20% shipping discount on checkout review.'));
    $this
      ->assertText('Order total$49.00', t('Order total with deluxe shipping and with a discount_all cheap shipping discount and a 20% "all" shipping discount is correctly displayed on checkout review.'));

    // Remove free shipping discount.
    entity_delete('commerce_discount', $discount->discount_id);

    // Create shipping upgrade discount.
    $offer = array(
      'source' => $shipping_service_name,
      'target' => $expensive_shipping_service_name,
    );
    $this
      ->createDiscount('order_discount', 'shipping_upgrade', $offer, 'shipping_upgrade', 'Shipping upgrade', 11);
    $this
      ->drupalPost(NULL, array(), t('Go back'));
    $this
      ->assertText('Cheap shipping: $4.00', t('Cheap shipping price displayed correctly on checkout shipping with a 20% "all" shipping discount and a cheap->expensive upgrade discount.'));
    $this
      ->assertText('Decent shipping: $12.00', t('Decent shipping price displayed correctly on checkout shipping with a 20% "all" shipping discount and a cheap->expensive upgrade discount.'));
    $this
      ->assertText('Expensive shipping: $4.00', t('Expensive shipping price displayed correctly on checkout shipping with a 20% "all" shipping discount and a cheap->expensive upgrade discount.'));
    $this
      ->assertText('Deluxe shipping: $44.00', t('Deluxe shipping price displayed correctly on checkout shipping with a 20% "all" shipping discount and a cheap->expensive upgrade discount.'));

    // Select expensive shipping.
    $edit = array(
      'commerce_shipping[shipping_service]' => $expensive_shipping_service_name,
    );
    $this
      ->drupalPostAJAX(NULL, $edit, 'commerce_shipping[shipping_service]');

    // Go to next checkout step.
    $this
      ->drupalPost(NULL, $edit, t('Continue to next step'));
    $this
      ->assertText('Shipping$25.00', t('Expensive shipping price is correctly displayed on checkout review with a 20% "all" shipping discount and a cheap->expensive upgrade discount.'));
    $this
      ->assertText('Shipping upgrade-$16.00', t('Discount amount on expensive shipping is correctly displayed in case of cheap->expensive upgrade discount on checkout review.'));
    $this
      ->assertText('20% off shipping-$5.00', t('20% off discount amount on expensive shipping is correctly displayed on checkout review.'));
    $this
      ->assertText('Order total$14.00', t('Order total with deluxe shipping and with a 20% "all" shipping discount and a cheap->expensive upgrade discount is correctly displayed on checkout review.'));
  }

  /**
   * Test a free shipping discount combined with a fixed amount discount.
   */
  public function testCommerceDiscountShippingAndFixedAmount() {
    $shipping_service_name = 'commerce_discount_cheap_shipping';
    $order = $this
      ->createDummyOrder($this->store_customer->uid, array(
      $this->product->product_id => 1,
    ), 'completed', NULL);
    module_load_include('inc', 'commerce_shipping', 'commerce_shipping.rules');
    commerce_shipping_rate_apply($order, $shipping_service_name);
    commerce_order_save($order);
    unset($order->shipping_rates);
    $free_shipping_discount = $this
      ->createDiscount('order_discount', 'free_shipping', $shipping_service_name, 'free_shipping', 'Free cheap shipping', 1);
    $this
      ->createDiscount('order_discount', 'fixed_amount', 1500, 'of1');

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

    // Order total before discount is 1000, assert that applying both a shipping
    // discount and a fixed amount discount isn't causing the order total to
    // be negative (make sure order total is properly recalculated after the
    // free shipping discount is applied).
    $this
      ->assertTrue($order_wrapper->commerce_order_total->amount
      ->value() == 0, 'Order total is correct when the free shipping discount has a lower weight than the fixed amount.');

    // Update the free shipping discount weight to make sure the order total
    // is correct when the shipping discount is evaluated after the fixed amount
    // discount.
    $free_shipping_discount->sort_order = 20;
    $free_shipping_discount
      ->save();
    $order_wrapper = commerce_cart_order_refresh($order);
    $this
      ->assertTrue($order_wrapper->commerce_order_total->amount
      ->value() == 0, 'Order total is correct when the free shipping discount has a higher weight than the fixed amount.');
  }

}

Classes

Namesort descending Description
CommerceDiscountShippingTest Testing commerce shipping discounts functionality.