You are here

public function CommerceProductUIAdminTest::testCommerceProductUIEditProduct in Commerce Core 7

Test the edit process for a product.

File

modules/product/tests/commerce_product_ui.test, line 186
Functional tests for the commerce product ui module.

Class

CommerceProductUIAdminTest
Test the product and product type CRUD.

Code

public function testCommerceProductUIEditProduct() {

  // Create dummy product.
  $product = $this
    ->createDummyProduct('PROD-01', 'Product One');
  $product_wrapper = entity_metadata_wrapper('commerce_product', $product);

  // Login with normal user.
  $this
    ->drupalLogin($this->store_customer);

  // Access to the edit product page.
  $this
    ->drupalGet('admin/commerce/products/' . $product->product_id . '/edit');
  $this
    ->assertResponse(403, t('Normal user is not able to access the product edit page'));

  // Login with store admin.
  $this
    ->drupalLogin($this->store_admin);

  // Access to the edit product page.
  $this
    ->drupalGet('admin/commerce/products/' . $product->product_id . '/edit');
  $this
    ->assertResponse(200, t('Store admin user can access the product edit page'));

  // Check the fields and buttons for the edit product form.
  $this
    ->pass(t('Test the integrity of the edit product form:'));
  $this
    ->assertFieldByName('sku', $product_wrapper->sku
    ->value(), t('SKU field is present'));
  $this
    ->assertFieldByName('title', $product_wrapper->title
    ->value(), t('Title field is present'));
  $this
    ->assertFieldByName('commerce_price[und][0][amount]', commerce_currency_amount_to_decimal($product_wrapper->commerce_price->amount
    ->value(), $product_wrapper->commerce_price->currency_code
    ->value()), t('Price field is present'));
  $this
    ->assertFieldByName('status', $product->status, t('Status field is present'));
  $this
    ->assertFieldById('edit-submit', t('Save product'), t('Save product button is present'));
  $this
    ->assertRaw(l(t('Cancel'), 'admin/commerce/products'), t('Cancel link is present'));

  // Change the product values.
  $price = rand(2, 500);
  $edit = array(
    'sku' => 'PROD-02',
    'title' => $this
      ->randomName(10),
    'commerce_price[und][0][amount]' => commerce_currency_amount_to_decimal($price, 'USD'),
    'status' => 0,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save product'));

  // Check the product in database
  $product = commerce_product_load_by_sku($edit['sku']);
  $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
  $this
    ->pass(t('Test the product edit in database:'));
  $this
    ->assertTrue($product_wrapper->sku
    ->value() == $edit['sku'], t('SKU stored in database correctly set'));
  $this
    ->assertTrue($product_wrapper->title
    ->value() == $edit['title'], t('Title stored in database correctly set'));
  $this
    ->assertTrue($product_wrapper->commerce_price->amount
    ->value() == $price, t('Amount stored in database correctly set'));
  $this
    ->assertTrue($product->status == $edit['status'], t('Status stored in database correctly set'));

  // Check the product listing
  $this
    ->pass(t('Test the product form after editing a product'));
  $this
    ->assertTrue($this->url == url('admin/commerce/products/' . $product->product_id . '/edit', array(
    'absolute' => TRUE,
  )), t('Landing page after save is the list of products'));
  $this
    ->assertText(t('Product saved.'), t('%message message is present', array(
    '%message' => t('Product saved'),
  )));
  $this
    ->assertFieldByName('sku', $edit['sku'], t('SKU field has the correct value'));
  $this
    ->assertFieldByName('title', $edit['title'], t('Title field has the correct value'));
  $this
    ->assertFieldByName('commerce_price[und][0][amount]', $edit['commerce_price[und][0][amount]'], t('Price field has the correct value'));
  $this
    ->assertFieldByName('status', $edit['status'], t('Status field is present'));
  $this
    ->assertFieldById('edit-submit', t('Save product'), t('Save product button is present'));
  $this
    ->assertRaw(l(t('Cancel'), 'admin/commerce/products'), t('Cancel link is present'));
}