You are here

public function UbercartProductKitTestCase::testProductKitDiscounts in Ubercart 6.2

Same name and namespace in other branches
  1. 7.3 uc_product_kit/tests/uc_product_kit.test \UbercartProductKitTestCase::testProductKitDiscounts()

File

uc_product_kit/uc_product_kit.test, line 69
Ubercart product kit tests

Class

UbercartProductKitTestCase

Code

public function testProductKitDiscounts() {
  $this
    ->drupalLogin($this->adminUser);

  // Create some test products and a kit.
  $products = array();
  for ($i = 0; $i < 3; $i++) {
    $products[$i] = $this
      ->createProduct();
  }
  $kit = $this
    ->drupalCreateNode(array(
    'type' => 'product_kit',
    'title' => $this
      ->randomName(32),
    'products' => array(
      $products[0]->nid,
      $products[1]->nid,
      $products[2]->nid,
    ),
    'mutable' => UC_PRODUCT_KIT_UNMUTABLE_NO_LIST,
    'default_qty' => 1,
    'ordering' => 0,
  ));

  // Test the product kit extra fields available to configure discounts.
  $this
    ->drupalGet('node/' . $kit->nid . '/edit');
  $this
    ->assertFieldByName('kit_total', NULL);
  foreach ($products as $product) {
    $this
      ->assertFieldByName('items[' . $product->nid . '][qty]', NULL);
    $this
      ->assertFieldByName('items[' . $product->nid . '][ordering]', NULL);
    $this
      ->assertFieldByName('items[' . $product->nid . '][discount]', NULL);
  }

  // Set some discounts.
  $discounts = array(
    mt_rand(-100, 100),
    mt_rand(-100, 100),
    mt_rand(-100, 100),
  );
  $edit = array(
    'items[' . $products[0]->nid . '][discount]' => $discounts[0],
    'items[' . $products[1]->nid . '][discount]' => $discounts[1],
    'items[' . $products[2]->nid . '][discount]' => $discounts[2],
  );
  $this
    ->drupalPost('node/' . $kit->nid . '/edit', $edit, 'Save');

  // Check the discounted total.
  $total = $products[0]->sell_price + $products[1]->sell_price + $products[2]->sell_price;
  $total += array_sum($discounts);
  $this
    ->assertText(uc_currency_format($total), 'Discounted product kit total found.');

  // Check the discounts on the edit page.
  $this
    ->drupalGet('node/' . $kit->nid . '/edit');
  $this
    ->assertText('Currently, the total sell price is ' . uc_currency_format($total), 'Discounted product kit total found.');
  $this
    ->assertFieldByName('items[' . $products[0]->nid . '][discount]', $discounts[0]);
  $this
    ->assertFieldByName('items[' . $products[1]->nid . '][discount]', $discounts[1]);
  $this
    ->assertFieldByName('items[' . $products[2]->nid . '][discount]', $discounts[2]);

  // Set the kit total.
  $total = 2 * ($products[0]->sell_price + $products[1]->sell_price + $products[2]->sell_price);
  $this
    ->drupalPost('node/' . $kit->nid . '/edit', array(
    'kit_total' => $total,
  ), 'Save');

  // Check the fixed total.
  $this
    ->assertText(uc_currency_format($total), 'Fixed product kit total found.');

  // Check the discounts on the edit page.
  $this
    ->drupalGet('node/' . $kit->nid . '/edit');
  $this
    ->assertFieldByName('kit_total', $total);
  $this
    ->assertFieldByName('items[' . $products[0]->nid . '][discount]', $products[0]->sell_price);
  $this
    ->assertFieldByName('items[' . $products[1]->nid . '][discount]', $products[1]->sell_price);
  $this
    ->assertFieldByName('items[' . $products[2]->nid . '][discount]', $products[2]->sell_price);
}