You are here

public function CartCouponRedemptionElementTest::testSingleCouponRedemption in Commerce Core 8.2

Tests redeeming a single coupon.

File

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

Class

CartCouponRedemptionElementTest
Tests the coupon redemption form element.

Namespace

Drupal\Tests\commerce_promotion\FunctionalJavascript

Code

public function testSingleCouponRedemption() {

  // 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' => FALSE,
  ];
  $view
    ->save();
  $coupons = $this->promotion
    ->getCoupons();
  $coupon = reset($coupons);
  $this
    ->drupalGet(Url::fromRoute('commerce_cart.page'));

  // 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()
    ->pageTextNotContains('The provided coupon code is invalid');
  $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');
}