You are here

public function CartCouponRedemptionElementTest::testMultipleCouponRedemption in Commerce Core 8.2

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

File

modules/promotion/tests/src/FunctionalJavascript/CartCouponRedemptionElementTest.php, line 160

Class

CartCouponRedemptionElementTest
Tests the coupon redemption form element.

Namespace

Drupal\Tests\commerce_promotion\FunctionalJavascript

Code

public function testMultipleCouponRedemption() {

  // Update the default cart form view to use the commerce_coupon_redemption
  // area plugin for coupon redemption.

  /** @var \Drupal\views\Entity\View $view */
  $view = View::load('commerce_cart_form');
  $display =& $view
    ->getDisplay('default');
  $display['display_options']['footer']['commerce_coupon_redemption'] = [
    'id' => 'commerce_coupon_redemption',
    'table' => 'views',
    'field' => 'commerce_coupon_redemption',
    'relationship' => 'none',
    'group_type' => 'group',
    'admin_label' => '',
    'empty' => FALSE,
    'plugin_id' => 'commerce_coupon_redemption',
    'allow_multiple' => TRUE,
  ];
  $view
    ->save();
  $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());
}