You are here

public function CommerceOrderUIAdminTest::testCommerceOrderUIUpdateLineItems in Commerce Core 7

Test updating line items within an order.

File

modules/order/tests/commerce_order_ui.test, line 182
Functional tests for the commerce order UI module.

Class

CommerceOrderUIAdminTest
Functional tests for the commerce order UI module.

Code

public function testCommerceOrderUIUpdateLineItems() {

  // Log in as store admin.
  $this
    ->drupalLogin($this->store_admin);

  // Access the edit page of the order.
  $this
    ->drupalGet('admin/commerce/orders/' . $this->order->order_id . '/edit');

  // Add a product line item to the order.
  $this
    ->drupalPostAJAX(NULL, array(
    'commerce_line_items[und][actions][line_item_type]' => 'product',
  ), array(
    'op' => t('Add line item'),
  ));
  $this
    ->drupalPostAJAX(NULL, array(
    'commerce_line_items[und][actions][product_sku]' => $this->product->sku,
  ), array(
    'op' => t('Add product'),
  ));
  $this
    ->drupalPost(NULL, array(), t('Save order', array(), array(
    'context' => 'a drupal commerce order',
  )));

  // Reload the order directly from db and wrap it to get the line item ids.
  $orders = commerce_order_load_multiple(array(
    $this->order->order_id,
  ), array(), TRUE);
  $order = reset($orders);
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);

  // Reset the cache as we don't want to keep the lock.
  entity_get_controller('commerce_order')
    ->resetCache();

  // Also wrap the product to access easier to its price.
  $product_wrapper = entity_metadata_wrapper('commerce_product', $this->product);

  // Get the first line item id.
  $line_item_id = $order_wrapper->commerce_line_items
    ->get(0)
    ->value()->line_item_id;

  // Format the price based in the currency.
  $price = commerce_currency_amount_to_decimal($product_wrapper->commerce_price->amount
    ->value(), $product_wrapper->commerce_price->currency_code
    ->value());

  // Check the existance of the fields for quantity and price of the line
  // item just created.
  $this
    ->pass(t('Check the existance of quantiy, price and currency code fields'));
  $this
    ->assertFieldById('edit-commerce-line-items-und-line-items-' . $line_item_id . '-quantity', 1, t('Quantity field is present and has value 1'));
  $this
    ->assertFieldById('edit-commerce-line-items-und-line-items-' . $line_item_id . '-commerce-unit-price-und-0-amount', $price, t('Price of the product is correct'));
  $this
    ->assertOptionSelected('edit-commerce-line-items-und-line-items-' . $line_item_id . '-commerce-unit-price-und-0-currency-code', $product_wrapper->commerce_price->currency_code
    ->value(), t('Currency code is valid'));

  // Generate new quantity and prices and save them.
  $new_qty = rand(0, 99);
  $new_currency_code = 'EUR';
  $new_price = commerce_currency_amount_to_decimal(rand(2, 500), $new_currency_code);
  $edit = array(
    'commerce_line_items[und][line_items][' . $line_item_id . '][quantity]' => $new_qty,
    'commerce_line_items[und][line_items][' . $line_item_id . '][commerce_unit_price][und][0][amount]' => $new_price,
    'commerce_line_items[und][line_items][' . $line_item_id . '][commerce_unit_price][und][0][currency_code]' => $new_currency_code,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save order', array(), array(
    'context' => 'a drupal commerce order',
  )));

  // Check if the modifications have been correctly done.
  $this
    ->assertFieldById('edit-commerce-line-items-und-line-items-' . $line_item_id . '-quantity', $new_qty, t('Quantity field has been correctly modified'));
  $this
    ->assertFieldById('edit-commerce-line-items-und-line-items-' . $line_item_id . '-commerce-unit-price-und-0-amount', $new_price, t('Price of the product has been correctly modified'));
  $this
    ->assertOptionSelected('edit-commerce-line-items-und-line-items-' . $line_item_id . '-commerce-unit-price-und-0-currency-code', $new_currency_code, t('Currency code has been correctly modified'));
}