commerce_discount_base.test 7.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
<?php

/**
 * @file
 * Commerce Discounts test base.
 */

/**
 * Base class for commerce discount tests.
 */
class CommerceDiscountTestBase extends CommerceBaseTestCase {

  /**
   * Don't need most of default core modules.
   */
  protected $profile = 'minimal';

  /**
   * Dummy commerce_product and related product node.
   */
  protected $product;
  protected $product_node;

  /**
   * User accounts for testing.
   */
  protected $store_admin;
  protected $store_customer;

  /**
   * Allows submodules to define themselves for setup.
   *
   * @var string
   */
  protected $sub_module;

  /**
   * Overrides CommerceBaseTestCase::permissionBuilder().
   */
  protected function permissionBuilder($set) {
    $permissions = parent::permissionBuilder($set);

    switch ($set) {
      case 'store admin':
      case 'site admin':
        $permissions[] = 'administer commerce discounts';
        break;
    }

    return $permissions;
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    // Enable all commerce modules + commerce_discount.
    $modules = parent::setUpHelper('all');

    $modules[] = 'commerce_discount';
    if ($this->sub_module) {
      $modules[] = $this->sub_module;
    }
    parent::setUp($modules);

    // User creation for different operations.
    $this->store_admin = $this->createStoreAdmin();
    $this->store_customer = $this->createStoreCustomer();

    // Create a dummy product.
    $this->product = $this->createDummyProduct('PROD-01', 'Product One', 1000);

    // Create a dummy product display content type.
    $this->createDummyProductDisplayContentType();

    // Create a product display node.
    $this->product_node = $this->createDummyProductNode(array($this->product->product_id), 'Product One node');

    // Set the default country to US.
    variable_set('site_default_country', 'US');
  }

  /**
   * Create a discount.
   *
   * @param string $discount_type
   *   The discount type; Either 'order_discount' or 'product_discount'.
   * @param string $offer_type
89
   *   The discount offer type; One of 'fixed_amount', 'percentage',
90 91
   *   'free_products', 'free_shipping', 'percent_off_shipping' or
   *   'shipping_upgrade'.
92
   * @param int|array $amount
93 94 95
   *   In case of 'fixed_amount' a number: the discount amount.
   *   In case of 'percentage' a number: the percentage (a number less than 1).
   *   In case of 'free_products' an array of the product ids.
96 97 98 99 100 101
   *   In case of 'free_shipping' a string of the free shipping service name.
   *   In case of 'percent_off_shipping' an array with the percentage amount
   *     (key: 'percent', should be < 100) and the shipping service name string
   *     (key: 'service').
   *   In case of 'shipping_upgrade' an array with the source service (key: '
   *     source') and the target service (key: 'target').
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
   * @param string $name
   *   Discount name - Optional. If given, CANNOT start with a number.
   * @param string $component_title
   *   Component title - Optional.
   *
   * @return object
   *   The newly created commerce_discount entity.
   */
  protected function createDiscount($discount_type, $offer_type, $amount, $name = '', $component_title = '', $sort_order = 10) {
    // Create the discount offer.
    $commerce_discount_offer = entity_create('commerce_discount_offer', array('type' => $offer_type));
    $offer_wrapper = entity_metadata_wrapper('commerce_discount_offer', $commerce_discount_offer);
    switch ($offer_type) {
      case 'fixed_amount':
        $offer_wrapper->commerce_fixed_amount->amount = $amount;
        $offer_wrapper->commerce_fixed_amount->currency_code = 'USD';
        break;

      case 'percentage':
        $offer_wrapper->commerce_percentage = $amount;
        break;

      case 'free_products':
        // Product ids array should be provided for $amount.
        $offer_wrapper->commerce_free_products = $amount;
        break;
128 129

      case 'free_shipping':
130 131 132 133 134 135 136 137
        $offer_wrapper->commerce_free_shipping = $amount;
        $offer_wrapper->commerce_free_shipping_strategy = 'only_selected';
        break;

      case 'percent_off_shipping':
        $offer_wrapper->commerce_percent_off_shipping = $amount['percent'];
        if (!empty($amount[1])) {
          $offer_wrapper->commerce_percent_off_ship_serv = $amount['service'];
138 139
        }
        break;
140 141 142 143 144

      case 'shipping_upgrade':
        $offer_wrapper->commerce_shipping_upgrade_target = $amount['target'];
        $offer_wrapper->commerce_shipping_upgrade_source = $amount['source'];
        break;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    }

    $offer_wrapper->save();

    // Provide default name.
    $name = $name ? $name : $discount_type . '_' . $offer_type;
    $component_title = $component_title ? $component_title : $name;

    // Create the discount.
    $values = array(
      'name' => $name,
      'label' => $name,
      'type' => $discount_type,
      'sort_order' => $sort_order,
      'component_title' => $component_title,
      'status' => TRUE,
      'export_status' => TRUE,
    );
    $commerce_discount = entity_create('commerce_discount', $values);
    $discount_wrapper = entity_metadata_wrapper('commerce_discount', $commerce_discount);
    $discount_wrapper->commerce_discount_offer = $commerce_discount_offer;
    $discount_wrapper->save();

    return $discount_wrapper->value();
  }

  /**
   * Determines whether or not a discount has been applied to an order.
   *
174
   * @param string $discount_name
175
   *   The machine-name of the discount to look for.
176
   * @param object $order
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
   *   The order object to inspect for the discount.
   *
   * @return bool
   *   Boolean indicating whether or not the discount is applied to the order.
   */
  public function discountAppliedToOrder($discount_name, $order) {
    // Fetch the list of discounts applied to the order based on the price
    // components in its order total array.
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $order_total = $order_wrapper->commerce_order_total->value();
    $applied_discounts = commerce_discount_get_discounts_applied_to_price($order_total);

    // Look for the given discount in the list of applied discounts.
    return in_array($discount_name, $applied_discounts);
  }

  /**
   * Create a discount.
   *
   * @param string $discount_type
   *   The discount type; Either 'order_discount' or 'product_discount'.
   * @param string $offer_type
   *   The discount offer type; Either 'fixed_amount' or 'percentage'.
   * @param int $amount
   *   The discount offer amount.
   * @param int $max_usage
   *   Maximal uses for the discount.
   *
   * @return object
   *   The newly created commerce_discount entity.
   */
  protected function createUsageDiscount($discount_type, $offer_type, $amount, $max_usage) {
    // Use the base class to create a discount.
    $discount = $this->createDiscount($discount_type, $offer_type, $amount);

    // Populate the max usage field.
    $wrapper = entity_metadata_wrapper('commerce_discount', $discount);
    $wrapper->discount_usage_limit = $max_usage;
    $wrapper->save();

    return $wrapper->value();
  }

  /**
   * Create a date discount.
   *
   * @param string $discount_type
   *   The discount type; Either 'order_discount' or 'product_discount'.
   * @param string $offer_type
   *   The discount offer type; Either 'fixed_amount' or 'percentage'.
   * @param int $amount
   *   The discount offer amount.
   * @param int $start_time
   *   Discount valid from.
   * @param int $end_time
   *   Discount valid until.
   *
   * @return object
   *   The newly created commerce_discount entity.
   */
  protected function createDateDiscount($discount_type, $offer_type, $amount, $start_time, $end_time) {
    // Use the base class to create a discount.
    $discount = $this->createDiscount($discount_type, $offer_type, $amount);

    // Populate the date fields.
    $discount_wrapper = entity_metadata_wrapper('commerce_discount', $discount);
    $discount_wrapper->commerce_discount_date = array(
      'value' => $start_time,
      'value2' => $end_time,
      'date_type' => 'datestamp',
    );
    $discount_wrapper->save();

    return $discount_wrapper->value();
  }

}