You are here

public function CouponTest::testGenerateCoupons in Commerce Core 8.2

Tests bulk generation of coupons.

File

modules/promotion/tests/src/Functional/CouponTest.php, line 137

Class

CouponTest
Tests the admin UI for coupons.

Namespace

Drupal\Tests\commerce_promotion\Functional

Code

public function testGenerateCoupons() {

  // Generate 50 single-use coupons (The view shows 50 coupons at once).
  $coupon_quantity = 50;
  $this
    ->drupalGet('/promotion/' . $this->promotion
    ->id() . '/coupons');
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Generate coupons');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('format', 'numeric');
  $this
    ->getSession()
    ->getPage()
    ->fillField('quantity', (string) $coupon_quantity);
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Generate');
  $this
    ->checkForMetaRefresh();
  $this
    ->assertSession()
    ->pageTextContains("Generated {$coupon_quantity} coupons.");
  $coupon_count = $this
    ->getSession()
    ->getPage()
    ->findAll('xpath', '//table/tbody/tr/td[text()[contains(., "0 / 1")]]');
  $this
    ->assertEquals(count($coupon_count), $coupon_quantity);
  $coupons = Coupon::loadMultiple();
  $this
    ->assertEquals(count($coupons), $coupon_quantity);
  foreach ($coupons as $id => $coupon) {
    $this
      ->assertEquals($this->promotion
      ->id(), $coupon
      ->getPromotionId());
    $this
      ->assertTrue(ctype_digit($coupon
      ->getCode()));
    $this
      ->assertEquals(strlen($coupon
      ->getCode()), 8);
    $this
      ->assertEquals(1, $coupon
      ->getUsageLimit());
  }
  $this->container
    ->get('entity_type.manager')
    ->getStorage('commerce_promotion')
    ->resetCache([
    $this->promotion
      ->id(),
  ]);
  $this->promotion = Promotion::load($this->promotion
    ->id());
  foreach ($coupons as $id => $coupon) {
    $this
      ->assertTrue($this->promotion
      ->hasCoupon($coupon));
  }

  // Generate 6 unlimited-use coupons.
  $coupon_quantity = 6;
  $this
    ->drupalGet('/promotion/' . $this->promotion
    ->id() . '/coupons');
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Generate coupons');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('format', 'numeric');
  $this
    ->getSession()
    ->getPage()
    ->fillField('quantity', (string) $coupon_quantity);
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption('limit', 0);
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Generate');
  $this
    ->checkForMetaRefresh();
  $this
    ->assertSession()
    ->pageTextContains("Generated {$coupon_quantity} coupons.");
  $coupon_count = $this
    ->getSession()
    ->getPage()
    ->findAll('xpath', '//table/tbody/tr/td[text()[contains(., "0 / Unlimited")]]');
  $this
    ->assertEquals(count($coupon_count), $coupon_quantity);
}