public function CommerceDiscountUITest::testCommerceDiscountUIAddDiscount in Commerce Discount 7
Test the add discount UI.
File
- tests/
commerce_discount_ui.test, line 61 - Commerce Discounts UI tests.
Class
- CommerceDiscountUITest
- Testing commerce discounts UI.
Code
public function testCommerceDiscountUIAddDiscount() {
// Login with normal user.
$this
->drupalLogin($this->store_customer);
// Access to the admin discount creation page.
$this
->drupalGet('admin/commerce/discounts/add');
$this
->assertResponse(403, 'Normal user is not able to add a discount using the admin interface');
// Login with store admin.
$this
->drupalLogin($this->store_admin);
// Access to the admin discount creation page.
$this
->drupalGet('admin/commerce/discounts/add');
$this
->assertResponse(200, 'Store admin user is allowed to add 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');
// Try to save the product and check validation messages.
$this
->drupalPost(NULL, array(), 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.');
// Load a clean discount add form.
$this
->drupalGet('admin/commerce/discounts/add');
// Create a discount.
$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.
$discount = entity_load_single('commerce_discount', 1);
$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->export_status, 1, 'Active 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.');
}