You are here

public function ProductKitTest::testProductKitMutability in Ubercart 8.4

Tests product kit mutability.

File

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

Class

ProductKitTest
Tests product kit functionality.

Namespace

Drupal\Tests\uc_product_kit\Functional

Code

public function testProductKitMutability() {

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

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

  // Test kits with no listing.
  $kit_data['mutable'] = UC_PRODUCT_KIT_UNMUTABLE_NO_LIST;
  $kit = $this
    ->drupalCreateNode($kit_data);
  $this
    ->drupalGet('node/' . $kit
    ->id());
  $assert
    ->pageTextContains($kit
    ->label(), 'Product kit title found.');
  $assert
    ->pageTextNotContains($products[0]
    ->label(), 'Product 1 title not shown.');
  $assert
    ->pageTextNotContains($products[1]
    ->label(), 'Product 2 title not shown.');
  $assert
    ->pageTextNotContains($products[2]
    ->label(), 'Product 3 title not shown.');
  $this
    ->addToCart($kit);
  $this
    ->drupalGet('cart');
  $assert
    ->pageTextContains($kit
    ->label(), 'Product kit title found.');
  $assert
    ->pageTextNotContains($products[0]
    ->label(), 'Product 1 title not shown.');
  $assert
    ->pageTextNotContains($products[1]
    ->label(), 'Product 2 title not shown.');
  $assert
    ->pageTextNotContains($products[2]
    ->label(), 'Product 3 title not shown.');
  $total = $products[0]->price->value + $products[1]->price->value + $products[2]->price->value;
  $assert
    ->pageTextMatches('/Subtotal:\\s*' . preg_quote(uc_currency_format($total)) . '/', 'Product kit total found.');
  $qty = mt_rand(2, 10);
  $this
    ->submitForm([
    'items[2][qty]' => $qty,
  ], 'Update cart');
  $assert
    ->pageTextMatches('/Subtotal:\\s*' . preg_quote(uc_currency_format($total * $qty)) . '/', 'Updated product kit total found.');
  $this
    ->submitForm([], 'Remove');
  $assert
    ->pageTextContains('There are no products in your shopping cart.');

  // Test kits with listing.
  $kit_data['mutable'] = UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST;
  $kit = $this
    ->drupalCreateNode($kit_data);
  $this
    ->drupalGet('node/' . $kit
    ->id());
  $assert
    ->pageTextContains($kit
    ->label(), 'Product kit title found.');
  $assert
    ->pageTextContains($products[0]
    ->label(), 'Product 1 title shown.');
  $assert
    ->pageTextContains($products[1]
    ->label(), 'Product 2 title shown.');
  $assert
    ->pageTextContains($products[2]
    ->label(), 'Product 3 title shown.');
  $this
    ->addToCart($kit);
  $this
    ->drupalGet('cart');
  $assert
    ->pageTextContains($kit
    ->label(), 'Product kit title found.');
  $assert
    ->pageTextContains($products[0]
    ->label(), 'Product 1 title shown.');
  $assert
    ->pageTextContains($products[1]
    ->label(), 'Product 2 title shown.');
  $assert
    ->pageTextContains($products[2]
    ->label(), 'Product 3 title shown.');
  $total = $products[0]->price->value + $products[1]->price->value + $products[2]->price->value;
  $assert
    ->pageTextMatches('/Subtotal:\\s*' . preg_quote(uc_currency_format($total)) . '/', 'Product kit total found.');
  $qty = mt_rand(2, 10);
  $this
    ->submitForm([
    'items[2][qty]' => $qty,
  ], 'Update cart');
  $assert
    ->pageTextMatches('/Subtotal:\\s*' . preg_quote(uc_currency_format($total * $qty)) . '/', 'Updated product kit total found.');
  $this
    ->submitForm([], 'Remove');
  $assert
    ->pageTextContains('There are no products in your shopping cart.');

  // Test mutable kits.
  $kit_data['mutable'] = UC_PRODUCT_KIT_MUTABLE;
  $kit = $this
    ->drupalCreateNode($kit_data);
  $this
    ->drupalGet('node/' . $kit
    ->id());
  $assert
    ->pageTextContains($kit
    ->label(), 'Product kit title found.');
  $assert
    ->pageTextContains($products[0]
    ->label(), 'Product 1 title shown.');
  $assert
    ->pageTextContains($products[1]
    ->label(), 'Product 2 title shown.');
  $assert
    ->pageTextContains($products[2]
    ->label(), 'Product 3 title shown.');
  $this
    ->addToCart($kit);
  $this
    ->drupalGet('cart');
  $assert
    ->pageTextNotContains($kit
    ->label(), 'Product kit title not shown.');
  $assert
    ->pageTextContains($products[0]
    ->label(), 'Product 1 title shown.');
  $assert
    ->pageTextContains($products[1]
    ->label(), 'Product 2 title shown.');
  $assert
    ->pageTextContains($products[2]
    ->label(), 'Product 3 title shown.');
  $total = $products[0]->price->value + $products[1]->price->value + $products[2]->price->value;
  $assert
    ->pageTextMatches('/Subtotal:\\s*' . preg_quote(uc_currency_format($total)) . '/', 'Product kit total found.');
  $qty = [
    mt_rand(2, 10),
    mt_rand(2, 10),
    mt_rand(2, 10),
  ];
  $edit = [
    'items[0][qty]' => $qty[0],
    'items[1][qty]' => $qty[1],
    'items[2][qty]' => $qty[2],
  ];
  $this
    ->submitForm($edit, 'Update cart');
  $total = $products[0]->price->value * $qty[0];
  $total += $products[1]->price->value * $qty[1];
  $total += $products[2]->price->value * $qty[2];
  $assert
    ->pageTextMatches('/Subtotal:\\s*' . preg_quote(uc_currency_format($total)) . '/', 'Updated product kit total found.');
  $this
    ->submitForm([], 'Remove');
  $this
    ->submitForm([], 'Remove');
  $this
    ->submitForm([], 'Remove');
  $assert
    ->pageTextContains('There are no products in your shopping cart.');
}