You are here

public function WishlistUserTest::testUserForm in Commerce Wishlist 8.3

Tests the user form.

File

tests/src/FunctionalJavascript/WishlistUserTest.php, line 189

Class

WishlistUserTest
Tests the wishlist user pages.

Namespace

Drupal\Tests\commerce_wishlist\FunctionalJavascript

Code

public function testUserForm() {
  $wishlist_item1 = $this
    ->createEntity('commerce_wishlist_item', [
    'type' => 'commerce_product_variation',
    'purchasable_entity' => $this->variation1,
    'quantity' => 1,
  ]);
  $wishlist_item2 = $this
    ->createEntity('commerce_wishlist_item', [
    'type' => 'commerce_product_variation',
    'purchasable_entity' => $this->variation2,
    'quantity' => 2,
  ]);
  $this->wishlist
    ->setItems([
    $wishlist_item1,
    $wishlist_item2,
  ]);
  $this->wishlist
    ->save();
  $this
    ->drupalGet($this->wishlist
    ->toUrl('user-form'));
  $this
    ->assertSession()
    ->elementExists('css', 'input[data-drupal-selector="edit-header-add-all-to-cart"]');
  $this
    ->assertSession()
    ->elementExists('css', 'a[data-drupal-selector="edit-header-share"]');
  $this
    ->assertSession()
    ->pageTextContains('First variation');
  $this
    ->assertSession()
    ->elementExists('css', 'input[name="add-to-cart-1"]');
  $this
    ->assertSession()
    ->elementExists('css', 'input[name="remove-1"]');
  $this
    ->assertSession()
    ->pageTextContains('Second variation');
  $this
    ->assertSession()
    ->elementExists('css', 'input[name="add-to-cart-2"]');
  $this
    ->assertSession()
    ->elementExists('css', 'input[name="remove-2"]');

  // Confirm that the "Add all to cart" button works.
  $cart_provider = $this->container
    ->get('commerce_cart.cart_provider');
  $cart = $cart_provider
    ->getCart('default');
  $this
    ->assertEmpty($cart);
  $this
    ->getSession()
    ->getPage()
    ->findButton('Add the entire list to cart')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $cart_provider
    ->clearCaches();
  $cart = $cart_provider
    ->getCart('default');
  $this
    ->assertCount(2, $cart
    ->getItems());

  // Confirm that the "Add to cart" button works.
  $cart_manager = $this->container
    ->get('commerce_cart.cart_manager');
  $cart_manager
    ->emptyCart($cart);
  $button = $this
    ->getSession()
    ->getPage()
    ->find('css', 'input[name="add-to-cart-2"]');
  $button
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  /** @var \Drupal\commerce_order\Entity\OrderInterface $cart */
  $cart = $this
    ->reloadEntity($cart);
  $this
    ->assertCount(1, $cart
    ->getItems());
  $order_items = $cart
    ->getItems();

  /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
  $order_item = reset($order_items);
  $this
    ->assertEquals($this->variation2
    ->id(), $order_item
    ->getPurchasedEntityId());

  // Confirm that the "Remove" button works.
  $button = $this
    ->getSession()
    ->getPage()
    ->find('css', 'input[name="remove-2"]');
  $button
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('Second variation has been removed from your wishlist');
  $this
    ->assertSession()
    ->pageTextContains('First variation');
  $this
    ->assertSession()
    ->elementExists('css', 'input[name="add-to-cart-1"]');
  $this
    ->assertSession()
    ->elementExists('css', 'input[name="remove-1"]');
  $this
    ->assertSession()
    ->elementNotExists('css', 'input[name="add-to-cart-2"]');
  $this
    ->assertSession()
    ->elementNotExists('css', 'input[name="remove-2"]');
  $this->wishlist = $this
    ->reloadEntity($this->wishlist);
  $this
    ->assertFalse($this->wishlist
    ->hasItem($wishlist_item2));
}