You are here

public function CartCheckoutTest::testCartPage in Ubercart 8.4

Tests basic cart page functionality.

File

uc_cart/tests/src/Functional/CartCheckoutTest.php, line 128

Class

CartCheckoutTest
Tests the cart and checkout functionality.

Namespace

Drupal\Tests\uc_cart\Functional

Code

public function testCartPage() {

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();
  \Drupal::service('module_installer')
    ->install([
    'uc_cart_entity_test',
  ], FALSE);

  // Test the empty cart.
  $this
    ->drupalGet('cart');
  $assert
    ->pageTextContains('There are no products in your shopping cart.');

  // Add an item to the cart.
  $this
    ->addToCart($this->product);
  $assert
    ->pageTextContains($this->product
    ->label() . ' added to your shopping cart.');
  $assert
    ->pageTextContains('hook_uc_cart_item_insert fired');

  // Test that the item shows up in the cart.
  $this
    ->drupalGet('cart');
  $assert
    ->pageTextContains($this->product
    ->label(), 'The product is in the cart.');

  // Verify the product quantity is 1.
  $assert
    ->fieldValueEquals('items[0][qty]', 1);

  // Add the item again.
  $this
    ->addToCart($this->product);
  $assert
    ->pageTextContains('Your item(s) have been updated.');
  $assert
    ->pageTextContains('hook_uc_cart_item_update fired');

  // Test that there are now two of the item in the cart.
  $this
    ->drupalGet('cart');

  // Verify the product quantity is 2.
  $assert
    ->fieldValueEquals('items[0][qty]', 2);

  // Update the quantity.
  $qty = mt_rand(3, 100);
  $this
    ->drupalGet('cart');
  $this
    ->submitForm([
    'items[0][qty]' => $qty,
  ], 'Update cart');
  $assert
    ->pageTextContains('Your cart has been updated.');

  // Verify the product quantity was updated.
  $assert
    ->fieldValueEquals('items[0][qty]', $qty);
  $assert
    ->pageTextContains('hook_uc_cart_item_update fired');

  // Update the quantity to zero.
  $this
    ->drupalGet('cart');
  $this
    ->submitForm([
    'items[0][qty]' => 0,
  ], 'Update cart');
  $assert
    ->pageTextContains('Your cart has been updated.');
  $assert
    ->pageTextContains('There are no products in your shopping cart.');
  $assert
    ->pageTextContains('hook_uc_cart_item_delete fired');

  // Test the remove item button.
  $this
    ->addToCart($this->product);
  $this
    ->drupalGet('cart');
  $this
    ->submitForm([], 'Remove');
  $assert
    ->pageTextContains($this->product
    ->label() . ' removed from your shopping cart.');
  $assert
    ->pageTextContains('There are no products in your shopping cart.');
  $assert
    ->pageTextContains('hook_uc_cart_item_delete fired');

  // Test the empty cart button.
  $this
    ->addToCart($this->product);
  $this
    ->drupalGet('cart');

  // Test that the empty cart button is not shown by default.
  $assert
    ->pageTextNotContains('Empty cart');

  // Enable the empty cart button.
  \Drupal::configFactory()
    ->getEditable('uc_cart.settings')
    ->set('empty_button', TRUE)
    ->save();
  $this
    ->drupalGet('cart');
  $this
    ->submitForm([], 'Empty cart');

  // Verify that we get a confirmation page.
  $assert
    ->pageTextContains('Are you sure you want to empty your shopping cart?');
  $this
    ->submitForm([], 'Confirm');

  // Verify that the cart is now empty.
  $assert
    ->pageTextContains('There are no products in your shopping cart.');
  $assert
    ->pageTextContains('hook_uc_cart_item_delete fired');
}