You are here

public function CommerceDiscountUITest::testCommerceDiscountUIEditDiscount in Commerce Discount 7

Test the Edit discount UI.

File

tests/commerce_discount_ui.test, line 127
Commerce Discounts UI tests.

Class

CommerceDiscountUITest
Testing commerce discounts UI.

Code

public function testCommerceDiscountUIEditDiscount() {

  // Create a discount.
  $discount = $this
    ->createDiscount('order_discount', 'fixed_amount', 300);

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

  // Access to the admin discount edit page.
  $this
    ->drupalGet('admin/commerce/discounts/manage/' . $discount->name);
  $this
    ->assertResponse(403, 'Normal user is not able to edit a discount using the admin interface');

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

  // Access to the admin discount edit page.
  $this
    ->drupalGet('admin/commerce/discounts/manage/' . $discount->name);
  $this
    ->assertResponse(200, 'Store admin user is allowed to edit a discount using the admin interface');

  // Check the integrity of the add form.
  $this
    ->assertFieldByName('commerce_discount_type', NULL, 'Discount type field is present');
  $this
    ->assertFieldByName('label', NULL, 'Label field is present');
  $this
    ->assertFieldByName('component_title', NULL, 'Name field is present');
  $this
    ->assertFieldByName('commerce_discount_fields[commerce_discount_offer][und][form][type]', NULL, 'Offer type field is present');
  $this
    ->assertFieldByName('commerce_discount_fields[commerce_discount_offer][und][form][commerce_fixed_amount][und][0][amount]', NULL, 'Amount field is present');
  $this
    ->assertFieldByName('status', NULL, 'Status field is present');
  $this
    ->assertFieldById('edit-submit', t('Save discount'), 'Save discount button is present');
  $this
    ->assertFieldById('edit-delete', t('Delete discount'), 'Delete discount button is present');

  // Empty values for validation assertions.
  $values = array(
    'label' => '',
    'name' => '',
    'component_title' => '',
    'commerce_discount_fields[commerce_discount_offer][und][form][commerce_fixed_amount][und][0][amount]' => '',
  );

  // Try to save the product and check validation messages.
  $this
    ->drupalPost(NULL, $values, t('Save discount'));
  $this
    ->assertText(t('Admin title field is required.'), 'Validation message for missing label.');
  $this
    ->assertText(t('Machine-readable name field is required.'), 'Validation message for missing machine-name.');
  $this
    ->assertText(t('Fixed amount field is required.'), 'Validation message for missing amount.');

  // Discount new values.
  $values = array(
    'label' => 'Order discount - fixed',
    'name' => 'order_discount_fixed',
    'component_title' => 'Order discount',
    'commerce_discount_fields[commerce_discount_offer][und][form][commerce_fixed_amount][und][0][amount]' => 12.77,
  );
  $this
    ->drupalPost(NULL, $values, t('Save discount'));

  // Load the discount and wrap it.
  $discounts = entity_load('commerce_discount', array(
    $discount->discount_id,
  ), $conditions = array(), $reset = TRUE);
  $discount = reset($discounts);
  $discount_wrapper = entity_metadata_wrapper('commerce_discount', $discount);

  // Check the stored discount.
  $this
    ->assertEqual($discount->label, $values['label'], 'Label stored correctly.');
  $this
    ->assertEqual($discount->name, 'discount_' . $values['name'], 'Name stored correctly.');
  $this
    ->assertEqual($discount->component_title, $values['component_title'], 'Name for customer stored correctly.');
  $this
    ->assertEqual($discount->status, 1, 'Enabled stored correctly.');
  $this
    ->assertEqual($discount_wrapper->commerce_discount_offer
    ->getBundle(), 'fixed_amount', 'Offer type stored correctly.');
  $this
    ->assertEqual($discount_wrapper->commerce_discount_offer->commerce_fixed_amount->amount
    ->value(), 1277, 'Amount stored correctly.');

  // Check the discounts listing.
  $this
    ->assertUrl('admin/commerce/discounts', array(
    'query' => array(
      'type' => 'order_discount',
    ),
  ), 'Landing page after save is the order discounts list.');
  $this
    ->assertText($values['label'], 'Label of the discount is present.');
}