You are here

public function CommerceProductUIAdminTest::testCommerceProductUIAddProduct in Commerce Core 7

Test the add product process.

File

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

Class

CommerceProductUIAdminTest
Test the product and product type CRUD.

Code

public function testCommerceProductUIAddProduct() {

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

  // Access to the admin product creation page.
  $this
    ->drupalGet('admin/commerce/products/add/product');
  $this
    ->assertResponse(403, t('Normal user is not able to add a product using the admin interface'));

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

  // Access to the admin product creation page.
  $this
    ->drupalGet('admin/commerce/products/add/product');
  $this
    ->assertResponse(200, t('Store admin user is allowed to add a product using the admin interface'));

  // Check the integrity of the add form.
  $this
    ->pass(t('Test the integrity of the product add form:'));
  $this
    ->assertFieldByName('sku', NULL, t('SKU field is present'));
  $this
    ->assertFieldByXPath('//input[@name="sku" and contains(@class, "required")]', NULL, t('SKU field is required'));
  $this
    ->assertFieldByName('title', NULL, t('Title field is present'));
  $this
    ->assertFieldByXPath('//input[@name="title" and contains(@class, "required")]', NULL, t('Title field is required'));
  $this
    ->assertFieldByName('commerce_price[und][0][amount]', NULL, t('Price field is present'));
  $this
    ->assertFieldByName('status', NULL, t('Status field is present'));
  $status_required = $this
    ->xpath('//div[contains(@class, "form-item-status")]/label/span[contains(@class, "form-required")]');
  $this
    ->assertFalse(empty($status_required), t('Status field is required'));
  $this
    ->assertFieldById('edit-submit', t('Save product'), t('Save product button is present'));
  $this
    ->assertFieldById('edit-save-continue', t('Save and add another'), t('Save an add another button is present'));
  $this
    ->assertRaw(l(t('Cancel'), 'admin/commerce/products'), t('Cancel link is present'));

  // Try to save the product and check validation messages.
  $this
    ->drupalPost(NULL, array(), t('Save product'));
  $this
    ->assertText(t('Product SKU field is required'), t('Validation message for SKU is displayed when tryin to submit the form with the field empty.'));
  $this
    ->assertText(t('Title field is required'), t('Validation message for title is displayed when tryin to submit the form with the field empty.'));

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

  // Load the product and wrap it.
  $product = commerce_product_load_by_sku($edit['sku']);
  $product_wrapper = entity_metadata_wrapper('commerce_product', $product);

  // Check the product in database
  $this
    ->pass(t('Test the product creation 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 listing after saving a product'));
  $this
    ->assertTrue($this->url == url('admin/commerce/products', 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
    ->assertText($edit['sku'], t('SKU of the product is present'));
  $this
    ->assertText($edit['title'], t('Title of the product is present'));

  // Assert the product creation.
  $this
    ->assertProductCreated($product, $this->store_admin);
}