public function CommerceCouponTest::testCommerceCouponUIAddCoupon in Commerce Coupon 7.2
File
- ./
commerce_coupon.test, line 189 - Commerce Coupon tests.
Class
- CommerceCouponTest
- Testing commerce coupon UI and functionality.
Code
public function testCommerceCouponUIAddCoupon() {
// Login with normal user.
$this
->drupalLogin($this->store_customer);
// Access to the admin discount creation page.
$this
->drupalGet('admin/commerce/coupons/add');
$this
->assertResponse(403, 'Normal user is not able to add a coupon using the admin interface');
// Login with store admin.
$this
->drupalLogin($this->store_admin);
// Access to the admin discount creation page.
$this
->drupalGet('admin/commerce/coupons/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('code', NULL, 'Coupon code field is present');
$this
->assertFieldByName('generate', NULL, 'Coupon code generate field is present');
$this
->assertFieldByName('commerce_discount_reference[und][0][target_id]', NULL, 'Discount reference field is present');
$this
->assertFieldByName('status', 1, 'Coupon status field is present and enabled');
$this
->assertFieldById('edit-submit', t('Save coupon'), 'Save coupon button is present');
// Try to save the product and check validation messages.
$this
->drupalPost(NULL, array(), t('Save coupon'));
$this
->assertText(t('You must enter a code.'), 'Validation message for missing code.');
$this
->assertText(t('field is required.'), 'Validation message for discount reference.');
// Create a discount and make a legit coupon.
$discount = $this
->createDiscount('order_discount', 'fixed_amount', 300);
// Load a fresh form.
$this
->drupalGet('admin/commerce/coupons/add');
$values = array(
// Add a trailing space to test trimming.
'code' => 'TEST_COUPON ',
'commerce_discount_reference[und][0][target_id]' => $discount->label . ' (' . $discount
->internalIdentifier() . ')',
);
$this
->drupalPost(NULL, $values, t('Save coupon'));
// Try to verify coupon.
$coupon = entity_load_single('commerce_coupon', 1);
$this
->assertEqual($coupon->code, 'TEST_COUPON', 'Coupon code is correct');
// Check the coupons listing.
$this
->assertUrl('admin/commerce/coupons', array(), 'Landing page after save is the coupons list.');
$this
->assertText($values['code'], 'Label of the coupon is present.');
}