You are here

public function CommercePriceTableTest::testCommercePriceTableSaveProduct in Commerce Price Table 7

Save a product with price table information.

File

tests/commerce_price_table.test, line 133
Functional tests for the commerce price table module.

Class

CommercePriceTableTest
Test price table features.

Code

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'));
}