You are here

commerce_price_table.test in Commerce Price Table 7

Functional tests for the commerce price table module.

File

tests/commerce_price_table.test
View source
<?php

/**
 * @file
 * Functional tests for the commerce price table module.
 */

/**
 * Test price table features.
 */
class CommercePriceTableTest extends CommerceBaseTestCase {

  /**
   * Product generated.
   */
  protected $product;

  /**
   * Product node.
   */
  protected $product_node;

  /**
   * Display node type.
   */
  protected $display_type;

  /**
   * Price table field name
   */
  protected $field_name;

  /**
   * Price table field info.
   */
  protected $field;

  /**
   * Price table field instance info.
   */
  protected $field_instance;

  /**
   * Implementation of getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Price table',
      'description' => 'Test the price table field',
      'group' => 'Drupal Commerce',
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    $modules = parent::setUpHelper('all', array(
      'commerce_price_table',
    ));
    parent::setUp($modules);

    // Create a site admin + store admin user and login.
    $this->site_admin = $this
      ->createUserWithPermissionHelper(array(
      'site admin',
      'store admin',
    ));
    $this
      ->drupalLogin($this->site_admin);

    // Also create a store customer.
    $this->store_customer = $this
      ->createStoreCustomer();

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

    // Create dummy product display nodes (and their corresponding product
    //  entities).
    $sku = 'PROD-01';
    $product_name = 'Product One';
    $this->product = $this
      ->createDummyProduct($sku, $product_name);
    $this->product_node = $this
      ->createDummyProductNode(array(
      $this->product->product_id,
    ), $product_name);

    // Access to the manage fields screen.
    $this
      ->drupalGet('admin/commerce/products/types/product/fields');

    // Add a new price table field
    $edit = array(
      'fields[_add_new_field][label]' => 'Price table',
      'fields[_add_new_field][field_name]' => 'field_price_table',
      'fields[_add_new_field][type]' => 'commerce_price_table',
      'fields[_add_new_field][widget_type]' => 'commerce_price_table_multiple',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));

    // Save the field settings, which are empty.
    $this
      ->drupalPost(NULL, array(), t('Save field settings'));

    // Set unlimited values.
    $this
      ->drupalPost(NULL, array(
      'field[cardinality]' => '-1',
    ), t('Save settings'));

    // Clear field's cache.
    field_cache_clear();

    // Get the field information just saved.
    $this->field_name = 'field_price_table';
    $this->field = field_info_field($this->field_name);
    $this->field_instance = field_info_instance('commerce_product', $this->field_name, 'product');
  }

  /**
   * Test if the field is correctly created and attached to the product entity.
   */
  public function testCommercePriceTableCreateField() {

    // Check at database level.
    $this
      ->assertTrue(in_array('product', $this->field['bundles']['commerce_product']), t('Field is present in the product bundle'));
    $this
      ->assertTrue($this->field_instance['field_name'] == $this->field_name, t('Field instance is present in the product bundle'));

    // Access product fields admin and check if the price table field is there.
    $this
      ->drupalGet('admin/commerce/products/types/product/fields');
    $this
      ->assertText('Price table', t('Price table field label found in the product fields admin screen'));
    $this
      ->assertText($this->field_name, t('Price table field name found in the product fields admin screen'));

    // Check if it's configured to unlimited.
    $this
      ->drupalGet('admin/commerce/products/types/product/fields/' . $this->field_name);
    $this
      ->assertOptionSelected('edit-field-cardinality', -1, t('Price table field is configured to have unlimited values'));

    // When creating a product, verify that the field is there.
    $this
      ->drupalGet('admin/commerce/products/add/product');
    $this
      ->assertText('Price table', t('Price table field label found in the add product form.'));
    $this
      ->assertFieldByName($this->field_name . '[und][0][amount]', NULL, t('Price table amount is present'));
    $this
      ->assertFieldByName($this->field_name . '[und][0][min_qty]', NULL, t('Price table min quantity is present'));
    $this
      ->assertFieldByName($this->field_name . '[und][0][max_qty]', NULL, t('Price table max quantity is present'));
    $this
      ->assertFieldById('edit-field-price-table-und-add-more', t('Add another item'), t('\'Add another item\' button is present'));
  }

  /**
   * Save a product with price table information.
   */
  public function testCommercePriceTableSaveProduct() {

    // Access to a product edit.
    $this
      ->drupalGet('admin/commerce/products/' . $this->product->product_id . '/edit');

    // Add price table information and save.
    $edit = array(
      'field_price_table[und][0][amount]' => 100,
      'field_price_table[und][0][min_qty]' => 1,
      'field_price_table[und][0][max_qty]' => 10,
    );
    $this
      ->drupalPostAJAX(NULL, $edit, 'field_price_table_add_more');
    $edit = array(
      'field_price_table[und][1][amount]' => 50,
      'field_price_table[und][1][min_qty]' => 11,
      'field_price_table[und][1][max_qty]' => 20,
    );
    $this
      ->drupalPostAJAX(NULL, $edit, 'field_price_table_add_more');
    $edit = array(
      'field_price_table[und][2][amount]' => 10,
      'field_price_table[und][2][min_qty]' => 21,
      'field_price_table[und][2][max_qty]' => -1,
    );
    $this
      ->drupalPostAJAX(NULL, $edit, 'field_price_table_add_more');
    $this
      ->drupalPost(NULL, array(), t('Save product'));

    // Access the edit page again to see if the values have been saved.
    $this
      ->drupalGet('admin/commerce/products/1/edit');
    $this
      ->assertFieldByName($this->field_name . '[und][0][amount]', 100, t('First amount for price table is correct.'));
    $this
      ->assertFieldByName($this->field_name . '[und][0][min_qty]', 1, t('First min quantity for price table is correct.'));
    $this
      ->assertFieldByName($this->field_name . '[und][0][max_qty]', 10, t('First max quantity for price table is correct.'));
    $this
      ->assertFieldByName($this->field_name . '[und][1][amount]', 50, t('Second amount for price table is correct.'));
    $this
      ->assertFieldByName($this->field_name . '[und][1][min_qty]', 11, t('Second min quantity for price table is correct.'));
    $this
      ->assertFieldByName($this->field_name . '[und][1][max_qty]', 20, t('Second max quantity for price table is correct.'));
    $this
      ->assertFieldByName($this->field_name . '[und][2][amount]', 10, t('Third amount for price table is correct.'));
    $this
      ->assertFieldByName($this->field_name . '[und][2][min_qty]', 21, t('Third min quantity for price table is correct.'));
    $this
      ->assertFieldByName($this->field_name . '[und][2][max_qty]', -1, t('Third max quantity for price table is correct.'));

    // Load the product with id 1 and check the field values.
    $product = commerce_product_load(1);
    $field_items = field_get_items('commerce_product', $product, $this->field_name);
    $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
    $this
      ->assertTrue($product_wrapper->{$this->field_name}
      ->get(0)->amount
      ->value() == 10000, t('First amount for price table is stored correctly.'));
    $this
      ->assertTrue($product_wrapper->{$this->field_name}
      ->get(0)->min_qty
      ->value() == 1, t('First min quantity for price table is stored correctly.'));
    $this
      ->assertTrue($product_wrapper->{$this->field_name}
      ->get(0)->max_qty
      ->value() == 10, t('First max quantity for price table is stored correctly.'));
    $this
      ->assertTrue($product_wrapper->{$this->field_name}
      ->get(1)->amount
      ->value() == 5000, t('Second amount for price table is stored correctly.'));
    $this
      ->assertTrue($product_wrapper->{$this->field_name}
      ->get(1)->min_qty
      ->value() == 11, t('Second min quantity for price table is stored correctly.'));
    $this
      ->assertTrue($product_wrapper->{$this->field_name}
      ->get(1)->max_qty
      ->value() == 20, t('Second max quantity for price table is stored correctly.'));
    $this
      ->assertTrue($product_wrapper->{$this->field_name}
      ->get(2)->amount
      ->value() == 1000, t('Third amount for price table is stored correctly.'));
    $this
      ->assertTrue($product_wrapper->{$this->field_name}
      ->get(2)->min_qty
      ->value() == 21, t('Third min quantity for price table is stored correctly.'));
    $this
      ->assertTrue($product_wrapper->{$this->field_name}
      ->get(2)->max_qty
      ->value() == -1, t('Third max quantity for price table is stored correctly.'));

    // Login with customer
    $this
      ->drupalLogin($this->store_customer);

    // Access to the product display page.
    $this
      ->drupalGet('node/' . $this->product_node->nid);
    $this
      ->assertResponse(200, t('Product node is accessible by store customer.'));

    // Check if the price table is there and it's right.
    $this
      ->assertRaw(drupal_render(field_view_field('commerce_product', $product, $this->field_name)), t('Price table is displayed correctly in the product display'));
  }

  /**
   * Test cart behaviors such as adding quantities.
   */
  public function testCommercePriceTableCart() {

    // First add price table info to a product.
    // Access to a product edit.
    $this
      ->drupalGet('admin/commerce/products/' . $this->product->product_id . '/edit');

    // Add price table information and save.
    $edit = array(
      'field_price_table[und][0][amount]' => 100,
      'field_price_table[und][0][min_qty]' => 1,
      'field_price_table[und][0][max_qty]' => 10,
    );
    $this
      ->drupalPostAJAX(NULL, $edit, 'field_price_table_add_more');
    $edit = array(
      'field_price_table[und][1][amount]' => 50,
      'field_price_table[und][1][min_qty]' => 11,
      'field_price_table[und][1][max_qty]' => 20,
    );
    $this
      ->drupalPostAJAX(NULL, $edit, 'field_price_table_add_more');
    $edit = array(
      'field_price_table[und][2][amount]' => 10,
      'field_price_table[und][2][min_qty]' => 21,
      'field_price_table[und][2][max_qty]' => -1,
    );
    $this
      ->drupalPostAJAX(NULL, $edit, 'field_price_table_add_more');
    $this
      ->drupalPost(NULL, array(), t('Save product'));

    // Login with customer
    $this
      ->drupalLogin($this->store_customer);

    // Access the product display page and add it to the cart.
    $this
      ->drupalPost('node/' . $this->product_node->nid, array(), t('Add to cart'));

    // Access the cart and check if the price is right.
    $this
      ->drupalGet($this
      ->getCommerceUrl('cart'));

    // Modify the quantity several times and check the price.
    $qty = $this
      ->xpath("//input[starts-with(@name, 'edit_quantity')]");
    $this
      ->assertRaw(commerce_currency_format(10000, 'USD'), t('Price amount for 1 item is correct.'));
    $this
      ->drupalPost($this
      ->getCommerceUrl('cart'), array(
      (string) $qty[0]['name'] => 11,
    ), t('Update cart'));
    $this
      ->assertFieldByXPath("//input[starts-with(@id, 'edit-edit-quantity')]", 11, t('Cart updated with new quantity: 11'));
    $this
      ->assertRaw(commerce_currency_format(5000 * 11, 'USD'), t('Price amount for 11 items is correct.'));
    $this
      ->drupalPost($this
      ->getCommerceUrl('cart'), array(
      (string) $qty[0]['name'] => 21,
    ), t('Update cart'));
    $this
      ->assertFieldByXPath("//input[starts-with(@id, 'edit-edit-quantity')]", 21, t('Cart updated with new quantity: 21'));
    $this
      ->assertRaw(commerce_currency_format(1000 * 21, 'USD'), t('Price amount for 21 items is correct.'));
  }

  /**
   * Hide the original price.
   */
  public function testCommercePriceTableHideOriginalPrice() {
    $this
      ->drupalGet('admin/commerce/products/' . $this->product->product_id . '/edit');
    $this
      ->drupalGet('node/' . $this->product_node->nid);

    // Access the field admin page to check if the hide option is there.
    $this
      ->drupalGet('admin/commerce/products/types/product/fields/' . $this->field_name);
    $this
      ->assertFieldByName('instance[settings][commerce_price_table][hide_default_price]', NULL, t('Hide option for original price is present in the field admin page.'));

    // Enable hide original price.
    $edit = array(
      'instance[settings][commerce_price_table][hide_default_price]' => 1,
    );
    $this
      ->drupalPost(NULL, $edit, t('Save settings'));

    // Check if the checkbox is selected.
    $this
      ->drupalGet('admin/commerce/products/types/product/fields/' . $this->field_name);
    $this
      ->assertFieldByName('instance[settings][commerce_price_table][hide_default_price]', 1, t('Hide option for original price is selected.'));

    // Access product edit, price textfield should be hidden.
    $this
      ->drupalGet('admin/commerce/products/' . $this->product->product_id . '/edit');
    $price_field = $this
      ->xpath("//input[@id='edit-commerce-price-und-0-amount']");
    $this
      ->assertTrue(empty($price_field), t('Original price field is hidden in the product edit page.'));

    // Access product display, original price shouldn't be there.
    $this
      ->drupalGet('node/' . $this->product_node->nid);
    $this
      ->assertNoRaw(drupal_render(field_view_field('commerce_product', $this->product, 'commerce_price')), t('Original price is not displayed in the product display'));
  }

}

Classes

Namesort descending Description
CommercePriceTableTest Test price table features.