You are here

public function ProductKitTest::testProductKitDiscounts in Ubercart 8.4

Tests product kit discounting.

File

uc_product_kit/tests/src/Functional/ProductKitTest.php, line 94

Class

ProductKitTest
Tests product kit functionality.

Namespace

Drupal\Tests\uc_product_kit\Functional

Code

public function testProductKitDiscounts() {

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();
  $this
    ->drupalLogin($this->adminUser);

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

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

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

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

  // Check the discounts on the edit page.
  $this
    ->drupalGet('node/' . $kit
    ->id() . '/edit');
  $assert
    ->pageTextContains('Currently, the total price is ' . uc_currency_format($total), 'Discounted product kit total found.');

  // Discounts were set as integers above, but are stored in the database as
  // floats. Compare field value to actual discount by appending a decimal
  // point and three zeros.
  $assert
    ->fieldValueEquals('items[' . $products[0]
    ->id() . '][discount]', $discounts[0] . ".000");
  $assert
    ->fieldValueEquals('items[' . $products[1]
    ->id() . '][discount]', $discounts[1] . ".000");
  $assert
    ->fieldValueEquals('items[' . $products[2]
    ->id() . '][discount]', $discounts[2] . ".000");

  // Set the kit total.
  $total = 2 * ($products[0]->price->value + $products[1]->price->value + $products[2]->price->value);
  $this
    ->drupalGet('node/' . $kit
    ->id() . '/edit');
  $this
    ->submitForm([
    'kit_total' => $total,
  ], 'Save');

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

  // Check the discounts on the edit page.
  $this
    ->drupalGet('node/' . $kit
    ->id() . '/edit');
  $assert
    ->fieldValueEquals('kit_total', uc_currency_format($total, FALSE, FALSE));
  $assert
    ->fieldValueEquals('items[' . $products[0]
    ->id() . '][discount]', $products[0]->price->value . ".000");
  $assert
    ->fieldValueEquals('items[' . $products[1]
    ->id() . '][discount]', $products[1]->price->value . ".000");
  $assert
    ->fieldValueEquals('items[' . $products[2]
    ->id() . '][discount]', $products[2]->price->value . ".000");

  // Reset the kit prices so the discounts should equal zero.
  $edit = [
    'price[0][value]' => $total - ($products[1]->price->value + $products[2]->price->value),
  ];
  $this
    ->drupalGet('node/' . $products[0]
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'Save');

  // Check the kit total is still the same.
  $this
    ->drupalGet('node/' . $kit
    ->id());
  $assert
    ->pageTextContains(uc_currency_format($total), 'Fixed product kit total found.');

  // Check the discounts are zeroed on the edit page.
  $this
    ->drupalGet('node/' . $kit
    ->id() . '/edit');
  $assert
    ->fieldValueEquals('kit_total', uc_currency_format($total, FALSE, FALSE));
  $assert
    ->fieldValueEquals('items[' . $products[0]
    ->id() . '][discount]', '0.000');
  $assert
    ->fieldValueEquals('items[' . $products[1]
    ->id() . '][discount]', '0.000');
  $assert
    ->fieldValueEquals('items[' . $products[2]
    ->id() . '][discount]', '0.000');
}