You are here

public function CouponRedemptionElementTest::testSingleCouponRedemption in Commerce Core 8.2

Tests redeeming a single coupon.

See also

commerce_promotion_test_form_views_form_commerce_cart_form_default_alter()

File

modules/promotion/tests/src/FunctionalJavascript/CouponRedemptionElementTest.php, line 108

Class

CouponRedemptionElementTest
Tests the coupon redemption form element.

Namespace

Drupal\Tests\commerce_promotion\FunctionalJavascript

Code

public function testSingleCouponRedemption() {
  $coupons = $this->promotion
    ->getCoupons();
  $coupon = reset($coupons);
  $this
    ->drupalGet(Url::fromRoute('commerce_cart.page', [], [
    'query' => [
      'coupon_cardinality' => 1,
    ],
  ]));

  // Empty coupon.
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Apply coupon');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('Please provide a coupon code');

  // Non-existent coupon.
  $this
    ->getSession()
    ->getPage()
    ->fillField('Coupon code', $this
    ->randomString());
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Apply coupon');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('The provided coupon code is invalid');

  // Valid coupon.
  $this
    ->getSession()
    ->getPage()
    ->fillField('Coupon code', $coupon
    ->getCode());
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Apply coupon');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains($coupon
    ->getCode());
  $this
    ->assertSession()
    ->fieldNotExists('Coupon code');
  $this
    ->assertSession()
    ->buttonNotExists('Apply coupon');

  // Coupon removal.
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Remove coupon');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextNotContains($coupon
    ->getCode());
  $this
    ->assertSession()
    ->fieldExists('Coupon code');
  $this
    ->assertSession()
    ->buttonExists('Apply coupon');
}