You are here

public function CommerceAjaxAddCartUI::testUpdateableQuantity in Commerce Ajax Add to Cart 7.2

Verify whether item quantity is getting updated.

File

tests/dc_ajax_add_cart_ui.test, line 516
Functional tests for commerce ajax add to cart module.

Class

CommerceAjaxAddCartUI
Functional tests for commerce ajax add to cart module.

Code

public function testUpdateableQuantity() {
  $image_remove_cart_path = drupal_get_path('module', 'dc_ajax_add_cart') . '/images/remove-from-cart.png';
  $this
    ->drupalLogin($this->storeAdmin);

  // First we let display the cart if it is empty.
  $edit = array();
  $edit['dc_ajax_add_cart_update_quantity'] = 1;
  $this
    ->drupalPost('admin/commerce/config/ajax-cart', $edit, t('Save configuration'));
  $this
    ->drupalLogout();

  // Tests for the new configuration.
  $items = array();
  $this
    ->drupalLogin($this->storeCustomer);

  // Randomly add four items to cart.
  for ($i = 0; $i <= 3; $i++) {
    $items[$i] = mt_rand(0, 9);
    $this
      ->drupalPostAJAX('node/' . $this->productNode[$items[$i]]->nid, array(), array(
      'op' => t('Add to cart'),
    ), 'system/ajax', array(), array(), 'commerce-cart-add-to-cart-form-' . $this->productNode[$items[$i]]->nid);
  }

  // @todo Figure out a way to close the popup.
  $this
    ->drupalGet('<front>');

  // Verify whether cart is updateable.
  $this
    ->assertFieldByXPath('//input[@type="text" and contains(@id, "edit-quantity-") and contains(@name, "quantity_")]', NULL, t('Cart is updateable'));

  // Randomly pick an item to update.
  $quantity = mt_rand(1, 4);
  $edit = array();
  $edit['quantity_1'] = $quantity;
  $this
    ->drupalPostAJAX(NULL, $edit, array(
    'op' => t('Update'),
  ), 'system/ajax', array(), array(), 'dc-ajax-add-cart-update-quantity-1');

  // @todo Figure out a way to close the popup.
  $this
    ->drupalGet('<front>');

  // Verify item in cart shows the new quantity.
  $this
    ->assertFieldByXPath($this
    ->constructFieldXpath('id', 'edit-quantity-1'), $quantity, t('Updated item found'));

  // Randomly pick an item to update.
  $edit = array();
  $edit['quantity_1'] = 0;
  $this
    ->drupalPostAJAX(NULL, $edit, array(
    'op' => t('Update'),
  ), 'system/ajax', array(), array(), 'dc-ajax-add-cart-update-quantity-1');

  // @todo Figure out a way to close the popup.
  $this
    ->drupalGet('<front>');

  // Verify item is removed from cart.
  $this
    ->assertNoFieldByXPath($this
    ->constructFieldXpath('id', 'edit-quantity-1'), NULL, t('Item removed from cart'));
  $this
    ->drupalLogout();
}