You are here

public function CouponRedemptionPaneTest::testCouponRedemption in Commerce Core 8.2

Tests redeeming a coupon using the coupon redemption pane.

File

modules/promotion/tests/src/FunctionalJavascript/CouponRedemptionPaneTest.php, line 167

Class

CouponRedemptionPaneTest
Tests the coupon redemption checkout pane.

Namespace

Drupal\Tests\commerce_promotion\FunctionalJavascript

Code

public function testCouponRedemption() {
  $coupons = $this->promotion
    ->getCoupons();
  $coupon = reset($coupons);
  $checkout_url = Url::fromRoute('commerce_checkout.form', [
    'commerce_order' => $this->cart
      ->id(),
  ]);
  $this
    ->drupalGet($checkout_url);

  // Confirm that validation errors set by the form element are visible.
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Apply coupon');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('Please provide a coupon code');

  // 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');
  $this
    ->assertSession()
    ->pageTextContains('-$99.90');
  $this
    ->assertSession()
    ->pageTextContains('$899.10');

  // 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');
  $this
    ->assertSession()
    ->pageTextNotContains('-$99.90');
  $this
    ->assertSession()
    ->pageTextContains('$999');

  // 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');
  $this
    ->assertSession()
    ->pageTextContains('-$99.90');
  $this
    ->assertSession()
    ->pageTextContains('$899.10');

  // 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');
  $this
    ->assertSession()
    ->pageTextNotContains('-$99.90');
  $this
    ->assertSession()
    ->pageTextContains('$999');

  // Confirm that the order summary is refreshed when outside of the sidebar.
  $checkout_flow = CheckoutFlow::load('default');
  $configuration = $checkout_flow
    ->get('configuration');
  $configuration['panes']['order_summary']['step'] = 'order_information';
  $checkout_flow
    ->set('configuration', $configuration);
  $checkout_flow
    ->save();
  $this
    ->drupalGet($checkout_url);
  $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');
  $this
    ->assertSession()
    ->pageTextContains('-$99.90');
  $this
    ->assertSession()
    ->pageTextContains('$899.10');
}