You are here

dc_ajax_add_cart_ui.test in Commerce Ajax Add to Cart 7.2

Functional tests for commerce ajax add to cart module.

File

tests/dc_ajax_add_cart_ui.test
View source
<?php

/**
 * @file
 * Functional tests for commerce ajax add to cart module.
 */

/**
 * Abstract class for commerce ajax add to cart testing.
 *
 * All other tests should extend this class.
 */
abstract class CommerceAjaxAddCartBase extends CommerceBaseTestCase {

  /**
   * Overrides CommerceBaseTestCase::permissionBuilder().
   */
  protected function permissionBuilder($set) {
    $permissions = parent::permissionBuilder($set);
    switch ($set) {
      case 'store admin':
        $permissions[] = 'administer commerce ajax add to cart';
        $permissions[] = 'administer blocks';
        break;
    }
    return $permissions;
  }

  /**
   * Helper function to  perform the common test tasks for cart testing.
   *
   * @see CommerceBaseTestCase::setUpHelper()
   */
  protected function setUpHelper($set = 'all', array $other_modules = array()) {

    // Enable all commerce modules + dc_ajax_add_cart.
    $modules = parent::setUpHelper($set, $other_modules);
    $modules[] = 'dc_ajax_add_cart';
    parent::setUp($modules);
    $this->storeAdmin = $this
      ->createStoreAdmin();
    $this->storeCustomer = $this
      ->createStoreCustomer();
  }

  /**
   * Ajax follows a link by name.
   *
   * Test API is similar to clickLink().
   *
   * @todo This does not perfectly checks that ajax link is working properly.
   * Find a solution to check whether ajax links are working.
   *
   * If the link is discovered and clicked, the test passes. Fail otherwise.
   *
   * @param string $label
   *   Text between the anchor tags.
   * @param int $index
   *   Link position counting from zero.
   *
   * @return array/boolean
   *   Json decode array on success, or FALSE on failure.
   */
  protected function clickLinkAjax($label, $index = 0) {
    $url_before = $this
      ->getUrl();
    $urls = $this
      ->xpath('//a[normalize-space(text())=:label]', array(
      ':label' => $label,
    ));
    if (isset($urls[$index])) {
      $url_target = $this
        ->getAbsoluteUrl($urls[$index]['href']);
      $this
        ->pass(t('Clicked link %label (@url_target) from @url_before', array(
        '%label' => $label,
        '@url_target' => $url_target,
        '@url_before' => $url_before,
      )), 'Browser');
      return $this
        ->drupalGetAJAX($url_target);
    }
    $this
      ->fail(t('Link %label does not exist on @url_before', array(
      '%label' => $label,
      '@url_before' => $url_before,
    )), 'Browser');
    return FALSE;
  }

}

/**
 * Functional tests for commerce ajax add to cart module.
 */
class CommerceAjaxAddCartUI extends CommerceAjaxAddCartBase {

  /**
   * Product that is being added to the cart.
   */
  protected $product = array();

  /**
   * Product display.
   */
  protected $productNode = array();

  /**
   * Implementation of getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Commerce ajax add to cart UI test',
      'description' => 'Functional tests for commerce ajax add to cart module.',
      'group' => 'Commerce ajax add to cart',
    );
  }

  /**
   * Implementation of setUp().
   */
  public function setUp() {
    $modules = parent::setUpHelper('all');
    $this->product = array();
    $this->productNode = array();

    // Create a dummy product display content type.
    $this
      ->createDummyProductDisplayContentType();

    // Create dummy product display nodes (and their corresponding product
    // entities).
    for ($i = 0; $i <= 9; $i++) {
      $sku = "PROD-{$i}";
      $product_name = "Product {$i}";
      $this->product[$i] = $this
        ->createDummyProduct($sku, $product_name);
      $this->productNode[$i] = $this
        ->createDummyProductNode(array(
        $this->product[$i]->product_id,
      ), $product_name);
    }

    // Place the blocks.
    $this
      ->drupalLogin($this->storeAdmin);
    $this
      ->drupalGet('admin/structure/block');
    $edit = array();
    $edit['blocks[dc_ajax_add_cart_ajax_shopping_cart][region]'] = 'featured';
    $edit['blocks[dc_ajax_add_cart_ajax_shopping_cart_teaser][region]'] = 'sidebar_first';
    $this
      ->drupalPost('admin/structure/block', $edit, t('Save blocks'));
    $this
      ->drupalLogout();
  }

  /**
   * Common tests carried out with default configuations of the module.
   */
  public function defaultConfigurationTests($cart_page = FALSE) {
    $items = array();
    $this
      ->drupalLogin($this->storeCustomer);

    // Randomly add two items to cart.
    for ($i = 0; $i <= 1; $i++) {
      $items[$i] = mt_rand(0, 9);
      $this
        ->drupalPostAJAX('node/' . $this->productNode[$items[$i]]->nid, array(), array(
        'op' => t('Add to cart'),
      ));

      // Verify popup message is displayed by default.
      $this
        ->assertRaw('<div class="add-cart-message-wrapper">', t('Popup message is displayed by default'));

      // Verify default popup success message.
      $this
        ->assertRaw('<div class="added-product-message">' . t('Item successfully added to cart') . '</div>', t('Default popup message is found'));

      // Verify product name label is not shown by default.
      $this
        ->assertNoRaw('<p class="name-label">' . t('Name') . ':</p>', t('Product name is not shown by default'));

      // Verify product quantity is shown and quantity label is shown by
      // default.
      $this
        ->assertRaw('<p class="quantity-label">' . t('Quantity') . ':</p>', t('Quantity is shown by default'));
      $this
        ->assertRaw('<p class="quantity">1</p>');

      // Verify product price is shown and price label is shown by default.
      $this
        ->assertRaw('<p class="cost-incl-tax-label">' . t('Price') . ':</p>', t('Price label is shown by default'));
      $this
        ->assertRaw('<p class="cost-incl-tax">' . commerce_currency_format($this->product[$items[$i]]->commerce_price[LANGUAGE_NONE][0]['amount'], $this->product[$items[$i]]->commerce_price[LANGUAGE_NONE][0]['currency_code']) . '</p>', t('Price is shown by default'));

      // Verify total price and total price label is shown by default.
      $this
        ->assertRaw('<p class="total-label">' . t('Total') . ':</p>', t('Total label is shown by default'));
      $this
        ->assertRaw('<p class="total-incl-tax">' . commerce_currency_format($this->product[$items[$i]]->commerce_price[LANGUAGE_NONE][0]['amount'], $this->product[$items[$i]]->commerce_price[LANGUAGE_NONE][0]['currency_code']) . '</p>', t('Total is shown by default'));

      // Verify default checkout text in popup.
      $this
        ->assertRaw('<a href="' . url('cart') . '">' . t('Go to checkout') . '</a>', t('Default checkout text in popup is found'));

      // Verify default continue shopping text in popup.
      $this
        ->assertRaw('<div class="option-button continue" data-dismiss="add-cart-message">' . t('Continue shopping') . '</div>', t('Default continue shopping text in popup is found'));
    }

    // @todo Figure out a way to close the popup.
    if (!$cart_page) {
      $this
        ->drupalGet('<front>');
    }
    else {
      $this
        ->drupalGet('cart');
    }

    // Verify default checkout link path.
    $this
      ->assertLinkByHref(url('cart'), 0, t('Checkout link path point to cart page by default'));

    // Verify labels are displayed by default.
    $this
      ->assertTrue($this
      ->xpath('//th[@class=:class and text()=:label]', array(
      ':class' => 'quantity-label',
      ':label' => t('Quantity'),
    )), t('Default quantity label found'));
    $this
      ->assertTrue($this
      ->xpath('//th[@class=:class and text()=:label]', array(
      ':class' => 'item-label',
      ':label' => t('Items'),
    )), t('Default item label found'));
    $this
      ->assertTrue($this
      ->xpath('//th[@class=:class and text()=:label]', array(
      ':class' => 'price-label',
      ':label' => t('Price'),
    )), t('Default price label found'));

    // Verify default suffix text in teaser cart.
    $this
      ->assertTrue($this
      ->xpath('//a[contains(@class, "quantity") and @href=:href]', array(
      ':href' => url('cart'),
    )), t('Default plural suffix text found'));

    // Verify whether remove cart is shown as link by default.
    $this
      ->assertTrue($this
      ->xpath('//a[contains(@href, "remove-product/nojs") and text()=:label]', array(
      ':label' => t('Remove from cart'),
    )), t('Remove cart is shown as link by default'));

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

    // Verify default empty cart message.
    // Verify cart is not hidden when empty by default.
    // Verify default empty cart teaser message.
    $remove_cart_link = $this
      ->xpath('//a[contains(@href, "remove-product/nojs")]');
    for ($i = 0; $i < count($remove_cart_link); $i++) {
      $this
        ->clickLinkAjax(t('Remove from cart'));
    }
    $this
      ->assertText(t('Shopping cart is empty'), t('Default empty cart message found'));
    $empty_cart_teaser_message = $this
      ->xpath('//p[@class=:class and text()=:message]', array(
      ':class' => 'empty-cart',
      ':message' => t('Empty'),
    ));
    $this
      ->assertTrue($empty_cart_teaser_message, t('Default empty teaser cart message found'));
    $this
      ->drupalLogout();
  }

  /**
   * Verify whether default module configuations are working.
   */
  public function testDefaultConfiguration() {
    $this
      ->defaultConfigurationTests();
  }

  /**
   * Verify whether default module configuations are working in cart page.
   */
  public function testDefaultConfigurationCartPage() {
    $this
      ->defaultConfigurationTests(TRUE);
  }

  /**
   * Verify whether changed module configuations are working.
   *
   * Tests carried out with popup confirmation popup message enabled.
   */
  public function testPopupEnabledCustomConfiguration() {
    $image_remove_cart_path = drupal_get_path('module', 'dc_ajax_add_cart') . '/images/remove-from-cart.png';

    // Alter default configurations.
    $this
      ->drupalLogin($this->storeAdmin);

    // First we let display the cart if it is empty.
    $edit = array();
    $edit['dc_ajax_add_cart_checkout_redirect'] = 'checkout_page';
    $edit['dc_ajax_add_cart_show_labels'] = 'no_label';
    $edit['dc_ajax_add_cart_empty_cart_message'] = 'Khali gadi';
    $edit['dc_ajax_add_cart_remove_cart'] = 'image';
    $edit['dc_ajax_add_cart_update_quantity'] = 1;
    $this
      ->drupalPost('admin/commerce/config/ajax-cart', $edit, t('Save configuration'));
    $edit = array();
    $edit['dc_ajax_add_cart_empty_cart_teaser_message'] = 'Khali hai';
    $edit['dc_ajax_add_cart_item_suffix_text_singular'] = 'saman';
    $edit['dc_ajax_add_cart_item_suffix_text_plural'] = 'samans';
    $this
      ->drupalPost('admin/commerce/config/ajax-cart/cart-teaser', $edit, t('Save configuration'));

    // First we check with visibility of quantity and price turned on.
    $edit = array();
    $edit['dc_ajax_add_cart_success_message'] = 'Saman gadi me aa gya hai';
    $edit['dc_ajax_add_cart_popup_product_name_label'] = 'display_label';
    $edit['dc_ajax_add_cart_popup_product_quantity_label'] = 'no_display_label';
    $edit['dc_ajax_add_cart_popup_product_price_label'] = 'no_display_label';
    $edit['dc_ajax_add_cart_popup_product_total_label'] = 'no_display_label';
    $edit['dc_ajax_add_cart_popup_checkout'] = 'Paise do';
    $edit['dc_ajax_add_cart_popup_continue_shopping'] = 'Aur kharido';
    $this
      ->drupalPost('admin/commerce/config/ajax-cart/pop-up-message', $edit, t('Save configuration'));
    $this
      ->drupalLogout();

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

    // Randomly add two items to cart.
    for ($i = 0; $i <= 1; $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);

      // Verify popup message is displayed.
      $this
        ->assertRaw('<div class="add-cart-message-wrapper">', t('Popup message is displayed'));

      // Verify popup success message.
      $this
        ->assertRaw('<div class="added-product-message">' . variable_get('dc_ajax_add_cart_success_message') . '</div>', t('Popup message is found'));

      // Verify product name label is shown.
      $this
        ->assertRaw('<p class="name-label">' . t('Name') . ':</p>', t('Product name is shown'));

      // Verify product quantity is shown and quantity label is not shown.
      $this
        ->assertNoRaw('<p class="quantity-label">' . t('Quantity') . ':</p>', t('Quantity label is not shown'));
      $this
        ->assertRaw('<p class="quantity">1</p>');

      // Verify product price is shown and price label is not shown.
      $this
        ->assertNoRaw('<p class="cost-incl-tax-label">' . t('Price') . ':</p>', t('Price label is not shown'));
      $this
        ->assertRaw('<p class="cost-incl-tax">' . commerce_currency_format($this->product[$items[$i]]->commerce_price[LANGUAGE_NONE][0]['amount'], $this->product[$items[$i]]->commerce_price[LANGUAGE_NONE][0]['currency_code']) . '</p>', t('Price is shown'));

      // Verify total price and total price label is not shown.
      $this
        ->assertNoRaw('<p class="total-label">' . t('Total') . ':</p>', t('Total label is not shown'));
      $this
        ->assertRaw('<p class="total-incl-tax">' . commerce_currency_format($this->product[$items[$i]]->commerce_price[LANGUAGE_NONE][0]['amount'], $this->product[$items[$i]]->commerce_price[LANGUAGE_NONE][0]['currency_code']) . '</p>', t('Total is shown'));

      // Verify checkout text in popup.
      $this
        ->assertRaw('<a href="' . url('cart') . '">' . variable_get('dc_ajax_add_cart_popup_checkout') . '</a>', t('Checkout text in popup is found'));

      // Verify continue shopping text in popup.
      $this
        ->assertRaw('<div class="option-button continue" data-dismiss="add-cart-message">' . variable_get('dc_ajax_add_cart_popup_continue_shopping') . '</div>', t('Continue shopping text in popup is found'));
    }

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

    // Verify checkout link path.
    $this
      ->assertLinkByHref(url('checkout'), 0, t('Checkout link path point to checkout page'));

    // Verify labels are not displayed.
    $this
      ->assertFalse($this
      ->xpath('//th[@class=:class and text()=:label]', array(
      ':class' => 'quantity-label',
      ':label' => t('Quantity'),
    )), t('Quantity label not found'));
    $this
      ->assertFalse($this
      ->xpath('//th[@class=:class and text()=:label]', array(
      ':class' => 'item-label',
      ':label' => t('Items'),
    )), t('Item label not found'));
    $this
      ->assertFalse($this
      ->xpath('//th[@class=:class and text()=:label]', array(
      ':class' => 'price-label',
      ':label' => t('Price'),
    )), t('Price label not found'));

    // Verify suffix text in teaser cart.
    $this
      ->assertRaw('<a href="' . url('cart') . '" class="quantity">' . count($items) . ' samans</a>', t('Plural suffix text found'));

    // Verify whether remove cart is shown as image.
    $this
      ->assertTrue($this
      ->xpath('//a[contains(@href, "remove-product/nojs")]/img[contains(@src, :src) and @title=:title]', array(
      ':title' => t('Remove from cart'),
      ':src' => $image_remove_cart_path,
    )), t('Remove cart is shown as image'));

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

    // Verify empty cart message.
    // Verify empty cart teaser message.
    // @todo Use API to click image and remove item from cart.
    $order = commerce_cart_order_load($this->storeCustomer->uid);
    commerce_cart_order_empty($order);
    $this
      ->drupalGet('<front>');
    $this
      ->assertText(variable_get('dc_ajax_add_cart_empty_cart_message'), t('Empty cart message found'));
    $empty_cart_teaser_message = $this
      ->xpath('//p[@class=:class and text()=:message]', array(
      ':class' => 'empty-cart',
      ':message' => variable_get('dc_ajax_add_cart_empty_cart_teaser_message'),
    ));
    $this
      ->assertTrue($empty_cart_teaser_message, t('Empty teaser cart message found'));
    $this
      ->drupalLogout();

    // Now we check for remaining settings.
    $this
      ->drupalLogin($this->storeAdmin);

    // Hide cart setting turned on.
    $edit = array();
    $edit['dc_ajax_add_cart_hide_empty_cart'] = 1;
    $this
      ->drupalPost('admin/commerce/config/ajax-cart', $edit, t('Save configuration'));

    // Hide quantity and price display setting turned on.
    // We are not POSTing the admin form because drupalPost() does not works
    // well with ajax checkboxes.
    variable_set('dc_ajax_add_cart_popup_product_quantity_display', 0);
    variable_set('dc_ajax_add_cart_popup_product_price_display', 0);
    $this
      ->drupalLogout();

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

    // Randomly add two items to cart.
    for ($i = 0; $i <= 1; $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);

      // Verify popup message is displayed.
      $this
        ->assertRaw('<div class="add-cart-message-wrapper">', t('Popup message is displayed'));

      // Verify popup success message.
      $this
        ->assertRaw('<div class="added-product-message">' . variable_get('dc_ajax_add_cart_success_message') . '</div>', t('Popup message is found'));

      // Verify product name label is shown.
      $this
        ->assertRaw('<p class="name-label">' . t('Name') . ':</p>', t('Product name is shown'));

      // Verify product quantity is not shown.
      $this
        ->assertNoRaw('<div class="product-quantity">', t('Quantity is not shown'));

      // Verify product price is not shown.
      $this
        ->assertNoRaw('<div class="product-cost-incl-tax">', t('Price is not shown'));

      // Verify total price and total price label is not shown.
      $this
        ->assertNoRaw('<p class="total-label">' . t('Total') . ':</p>', t('Total label is not shown'));
      $this
        ->assertRaw('<p class="total-incl-tax">' . commerce_currency_format($this->product[$items[$i]]->commerce_price[LANGUAGE_NONE][0]['amount'], $this->product[$items[$i]]->commerce_price[LANGUAGE_NONE][0]['currency_code']) . '</p>', t('Total is shown'));

      // Verify checkout text in popup.
      $this
        ->assertRaw('<a href="' . url('cart') . '">' . variable_get('dc_ajax_add_cart_popup_checkout') . '</a>', t('Checkout text in popup is found'));

      // Verify continue shopping text in popup.
      $this
        ->assertRaw('<div class="option-button continue" data-dismiss="add-cart-message">' . variable_get('dc_ajax_add_cart_popup_continue_shopping') . '</div>', t('Continue shopping text in popup is found'));
    }

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

    // Verify checkout link path.
    $this
      ->assertLinkByHref(url('checkout'), 0, t('Checkout link path point to checkout page'));

    // Verify labels are not displayed.
    $this
      ->assertFalse($this
      ->xpath('//th[@class=:class and text()=:label]', array(
      ':class' => 'quantity-label',
      ':label' => t('Quantity'),
    )), t('Quantity label not found'));
    $this
      ->assertFalse($this
      ->xpath('//th[@class=:class and text()=:label]', array(
      ':class' => 'item-label',
      ':label' => t('Items'),
    )), t('Item label not found'));
    $this
      ->assertFalse($this
      ->xpath('//th[@class=:class and text()=:label]', array(
      ':class' => 'price-label',
      ':label' => t('Price'),
    )), t('Price label not found'));

    // Verify suffix text in teaser cart.
    $this
      ->assertRaw('<a href="' . url('cart') . '" class="quantity">' . count($items) . ' samans</a>', t('Plural suffix text found'));

    // Verify whether remove cart is shown as image.
    $this
      ->assertTrue($this
      ->xpath('//a[contains(@href, "remove-product/nojs")]/img[contains(@src, :src) and @title=:title]', array(
      ':title' => t('Remove from cart'),
      ':src' => $image_remove_cart_path,
    )), t('Remove cart is shown as image'));

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

    // Verify empty cart message.
    // Verify empty cart teaser message.
    // @todo Use API to click image and remove item from cart.
    $order = commerce_cart_order_load($this->storeCustomer->uid);
    commerce_cart_order_empty($order);
    $this
      ->drupalGet('<front>');
    $this
      ->assertNoText(variable_get('dc_ajax_add_cart_empty_cart_message'), t('Empty cart message not found'));
    $empty_cart_teaser_message = $this
      ->xpath('//p[@class=:class and text()=:message]', array(
      ':class' => 'empty-cart',
      ':message' => variable_get('dc_ajax_add_cart_empty_cart_teaser_message'),
    ));
    $this
      ->assertTrue($empty_cart_teaser_message, t('Empty teaser cart message found'));
    $this
      ->drupalLogout();
  }

  /**
   * Verify whether changed module configuations are working.
   *
   * Tests carried out with popup confirmation popup message disabled.
   */
  public function testPopupDisabledCustomConfiguration() {
    $image_remove_cart_path = drupal_get_path('module', 'dc_ajax_add_cart') . '/images/remove-from-cart.png';

    // Alter default configurations.
    $this
      ->drupalLogin($this->storeAdmin);

    // First we let display the cart if it is empty.
    $edit = array();
    $edit['dc_ajax_add_cart_checkout_redirect'] = 'checkout_page';
    $edit['dc_ajax_add_cart_show_labels'] = 'no_label';
    $edit['dc_ajax_add_cart_empty_cart_message'] = 'Khali gadi';
    $edit['dc_ajax_add_cart_remove_cart'] = 'image';
    $edit['dc_ajax_add_cart_update_quantity'] = 1;
    $this
      ->drupalPost('admin/commerce/config/ajax-cart', $edit, t('Save configuration'));
    $edit = array();
    $edit['dc_ajax_add_cart_empty_cart_teaser_message'] = 'Khali hai';
    $edit['dc_ajax_add_cart_item_suffix_text_singular'] = 'saman';
    $edit['dc_ajax_add_cart_item_suffix_text_plural'] = 'samans';
    $this
      ->drupalPost('admin/commerce/config/ajax-cart/cart-teaser', $edit, t('Save configuration'));

    // First we check with visibility of quantity and price turned on.
    $edit = array();
    $edit['dc_ajax_add_cart_display_popup'] = 'no_display_popup_message';
    $this
      ->drupalPost('admin/commerce/config/ajax-cart/pop-up-message', $edit, t('Save configuration'));
    $this
      ->drupalLogout();

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

    // Randomly add two items to cart.
    for ($i = 0; $i <= 1; $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);

      // Verify popup message is not displayed.
      $this
        ->assertNoRaw('<div class="add-cart-message-wrapper">', t('Popup message is displayed'));
    }

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

    // Verify checkout link path.
    $this
      ->assertLinkByHref(url('checkout'), 0, t('Checkout link path point to checkout page'));

    // Verify labels are not displayed.
    $this
      ->assertFalse($this
      ->xpath('//th[@class=:class and text()=:label]', array(
      ':class' => 'quantity-label',
      ':label' => t('Quantity'),
    )), t('Quantity label not found'));
    $this
      ->assertFalse($this
      ->xpath('//th[@class=:class and text()=:label]', array(
      ':class' => 'item-label',
      ':label' => t('Items'),
    )), t('Item label not found'));
    $this
      ->assertFalse($this
      ->xpath('//th[@class=:class and text()=:label]', array(
      ':class' => 'price-label',
      ':label' => t('Price'),
    )), t('Price label not found'));

    // Verify suffix text in teaser cart.
    $this
      ->assertRaw('<a href="' . url('cart') . '" class="quantity">' . count($items) . ' samans</a>', t('Plural suffix text found'));

    // Verify whether remove cart is shown as image.
    $this
      ->assertTrue($this
      ->xpath('//a[contains(@href, "remove-product/nojs")]/img[contains(@src, :src) and @title=:title]', array(
      ':title' => t('Remove from cart'),
      ':src' => $image_remove_cart_path,
    )), t('Remove cart is shown as image'));

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

    // Verify empty cart message.
    // Verify empty cart teaser message.
    // @todo Use API to click image and remove item from cart.
    $order = commerce_cart_order_load($this->storeCustomer->uid);
    commerce_cart_order_empty($order);
    $this
      ->drupalGet('<front>');
    $this
      ->assertText(variable_get('dc_ajax_add_cart_empty_cart_message'), t('Empty cart message found'));
    $empty_cart_teaser_message = $this
      ->xpath('//p[@class=:class and text()=:message]', array(
      ':class' => 'empty-cart',
      ':message' => variable_get('dc_ajax_add_cart_empty_cart_teaser_message'),
    ));
    $this
      ->assertTrue($empty_cart_teaser_message, t('Empty teaser cart message found'));
    $this
      ->drupalLogout();
  }

  /**
   * Verify whether item quantity is getting updated.
   */
  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();
  }

}

Classes

Namesort descending Description
CommerceAjaxAddCartBase Abstract class for commerce ajax add to cart testing.
CommerceAjaxAddCartUI Functional tests for commerce ajax add to cart module.