You are here

public function CouponRedemptionElementTest::testMultipleCouponRedemption in Commerce Core 8.2

Tests redeeming coupon on the cart form, with multiple coupons allowed.

See also

commerce_promotion_test_form_views_form_commerce_cart_form_default_alter()

File

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

Class

CouponRedemptionElementTest
Tests the coupon redemption form element.

Namespace

Drupal\Tests\commerce_promotion\FunctionalJavascript

Code

public function testMultipleCouponRedemption() {
  $coupons = $this->promotion
    ->getCoupons();
  $first_coupon = reset($coupons);
  $second_coupon = end($coupons);
  $this
    ->drupalGet(Url::fromRoute('commerce_cart.page', [], [
    'query' => [
      'coupon_cardinality' => 2,
    ],
  ]));

  // First coupon.
  $this
    ->getSession()
    ->getPage()
    ->fillField('Coupon code', $first_coupon
    ->getCode());
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Apply coupon');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains($first_coupon
    ->getCode());
  $this
    ->assertSession()
    ->fieldExists('Coupon code');

  // The coupon code input field needs to be cleared.
  $this
    ->assertSession()
    ->fieldValueNotEquals('Coupon code', $first_coupon
    ->getCode());

  // First coupon, applied for the second time.
  $this
    ->getSession()
    ->getPage()
    ->fillField('Coupon code', $first_coupon
    ->getCode());
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Apply coupon');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextNotContains('The provided coupon code is invalid');
  $this
    ->assertSession()
    ->pageTextContains($first_coupon
    ->getCode());

  // Second coupon.
  $this
    ->getSession()
    ->getPage()
    ->fillField('Coupon code', $second_coupon
    ->getCode());
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Apply coupon');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains($first_coupon
    ->getCode());
  $this
    ->assertSession()
    ->pageTextContains($second_coupon
    ->getCode());

  // Second coupon removal.
  $this
    ->getSession()
    ->getPage()
    ->pressButton('remove_coupon_1');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextNotContains($second_coupon
    ->getCode());
  $this
    ->assertSession()
    ->pageTextContains($first_coupon
    ->getCode());

  // First coupon removal.
  $this
    ->getSession()
    ->getPage()
    ->pressButton('remove_coupon_0');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextNotContains($second_coupon
    ->getCode());
  $this
    ->assertSession()
    ->pageTextNotContains($first_coupon
    ->getCode());
}