You are here

commerce_pricelist.test in Commerce Pricelist 7

Functional tests for the commerce price list module.

File

tests/commerce_pricelist.test
View source
<?php

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

/**
 * Abstract class for Commerce price list testing. All Commerce price list tests should
 * extend this class.
 */
abstract class CommercePricelistTestCase extends CommerceBaseTestCase {

  /**
   * Product that is being added to the cart.
   */
  protected $product;

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

  /**
   * Pricelist with higher priority.
   */
  protected $pricelist_a;

  /**
   * Pricelist with lower priority.
   */
  protected $pricelist_b;

  /**
   * User with admin role and all pricelist permissions.
   */
  protected $site_admin;

  /**
   * User with customer permissions.
   */
  protected $store_customer;

  /**
   * Helper function to  perform the common test tasks for price list testing.
   * @see CommerceBaseTestCase::setUpHelper()
   */
  protected function setUp($other_modules = array()) {
    $other_modules[] = 'commerce_pricelist';
    $modules = parent::setUpHelper('ui', $other_modules);
    parent::setUp($modules);

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

    // Create dummy product display nodes (and their corresponding product
    //  entities).
    $this->pricelist_a = $this
      ->createDummyPricelist('A', 0);
    $this->pricelist_b = $this
      ->createDummyPricelist('B', 1);
    $this->product = $this
      ->createDummyProduct('', '', 0);
    $this->product_node = $this
      ->createDummyProductNode(array(
      $this->product->product_id,
    ));
    $this->store_admin = $this
      ->createStoreAdmin();
    $this->store_customer = $this
      ->createStoreCustomer();
    $this->site_admin = $this
      ->createAdminUser();
  }

  /**
   * Create a pricelist for testing.
   *
   * @param string $title
   * @param int $weight
   * @return mixed
   */
  public function createDummyPricelist($title = '', $weight = 0) {
    $new_pricelist = commerce_pricelist_list_new();
    $new_pricelist->title = empty($title) ? $this
      ->randomName(10) : $title;
    $new_pricelist->weight = $weight;
    commerce_pricelist_list_save($new_pricelist);
    return $new_pricelist;
  }

  /**
   * Create a pricelist item for testing.
   *
   * @param array $vars
   * @return mixed
   */
  public function createDummyPricelistItem($vars = array()) {
    $defaults = array(
      'pricelist_id' => $this->pricelist_a->list_id,
      'sku' => $this->product->sku,
      'price_amount' => 100,
      'currency_code' => 'USD',
    );
    $vars += $defaults;
    $new_pricelist_item = entity_get_controller('commerce_pricelist_item')
      ->create();
    foreach ($vars as $key => $value) {
      $new_pricelist_item->{$key} = $value;
    }
    commerce_pricelist_item_save($new_pricelist_item);
    return $new_pricelist_item;
  }

  /**
   * Creates an admin user.
   * @param array $permissions
   * @return user object
   */
  public function createAdminUser(array $permissions = array()) {
    $permissions += array(
      'administer commerce_pricelist_list entities',
      'view any commerce_pricelist_list entity',
      'edit any commerce_pricelist_list entity',
      'create commerce_pricelist_list entities',
    );
    $permissions += $this
      ->permissionBuilder(array(
      'store admin',
      'site admin',
    ));
    return $this
      ->drupalCreateUser($permissions);
  }

  /**
   * Go through the checkout process.
   */
  protected function commercePricelistHelperCompleteCheckout() {

    // Get the checkout url and navigate to it.
    $links = commerce_line_item_summary_links();
    $this->checkout_url = $links['checkout']['href'];
    $this
      ->drupalGet($this->checkout_url);

    // The rule that sends a mail after checkout completion should be disabled
    //  as it returns an error caused by how mail messages are stored.
    $rules_config = rules_config_load('commerce_checkout_order_email');
    $rules_config->active = FALSE;
    $rules_config
      ->save();

    // Complete the order process.
    $billing_pane = $this
      ->xpath("//select[starts-with(@name, 'customer_profile_billing[commerce_customer_address]')]");
    $this
      ->drupalPostAJAX(NULL, array(
      (string) $billing_pane[0]['name'] => 'US',
    ), (string) $billing_pane[0]['name']);
    $info = array(
      'customer_profile_billing[commerce_customer_address][und][0][name_line]' => $this
        ->randomName(),
      'customer_profile_billing[commerce_customer_address][und][0][thoroughfare]' => $this
        ->randomName(),
      'customer_profile_billing[commerce_customer_address][und][0][locality]' => $this
        ->randomName(),
      'customer_profile_billing[commerce_customer_address][und][0][administrative_area]' => 'KY',
      'customer_profile_billing[commerce_customer_address][und][0][postal_code]' => rand(00, 99999),
    );
    $this
      ->drupalPost(NULL, $info, t('Continue to next step'));
    $this
      ->drupalPost(NULL, array(), t('Continue to next step'));
  }

}

/**
 * Test price list features for a product display that only has one product attached.
 */
class CommercePricelistTestCaseSimpleProduct extends CommercePricelistTestCase {

  /**
   * Implementation of getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Commerce Pricelist',
      'description' => 'Test price list features like setting the price from a price list',
      'group' => 'Commerce Pricelist',
    );
  }

  /**
   * Test that product with price zero are deactivated.
   */
  public function testCommercePricelistDeactivateZeroPriceProduct() {

    // Log in as a normal user to test price list process.
    $this
      ->drupalLogin($this->store_customer);
    $this
      ->drupalGet('node/' . $this->product_node->nid);
    $this
      ->assertRaw(t('Product not available'), t('Product was correctly disabled'));
  }

  /**
   * Test if the price of a product changes when a price list item is added.
   */
  public function testCommercePricelistItemAdd() {

    // Log in as a normal user to test price list process.
    $this
      ->drupalLogin($this->store_customer);

    // Add pricelist item.
    $item_a = $this
      ->createDummyPricelistItem();
    $item_b = $this
      ->createDummyPricelistItem(array(
      'price_amount' => 75,
      'quantity' => 2,
    ));

    // Go to product display page
    $this
      ->drupalGet('node/' . $this->product_node->nid);
    $this
      ->assertRaw(t('Add to cart'), t('Product was correctly enabled.'));
    $this
      ->assertText('$1.00', t('Product price was correctly set from price list.'));

    // Add to product to cart.
    $this
      ->drupalPost('node/' . $this->product_node->nid, array(), t('Add to cart'));

    // Go to cart.
    $this
      ->drupalGet($this
      ->getCommerceUrl('cart'));

    // Change quantity in the cart view form.
    // We search for the first quantity field in the html and change the
    //   amount there.
    $qty = $this
      ->xpath("//input[starts-with(@name, 'edit_quantity')]");
    $this
      ->drupalPost($this
      ->getCommerceUrl('cart'), array(
      (string) $qty[0]['name'] => 2,
    ), t('Update cart'));

    // Check if the amount has been really changed.
    $this
      ->assertText('$0.75', t('Product price was correctly set from price list for different quantities.'));

    // Clean up.
    commerce_pricelist_item_delete($item_a->item_id);
    commerce_pricelist_item_delete($item_b->item_id);
  }

}

Classes

Namesort descending Description
CommercePricelistTestCase Abstract class for Commerce price list testing. All Commerce price list tests should extend this class.
CommercePricelistTestCaseSimpleProduct Test price list features for a product display that only has one product attached.