View source
<?php
namespace Drupal\Tests\commerce_promotion\FunctionalJavascript;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_order\Entity\OrderItemType;
use Drupal\commerce_price\Price;
use Drupal\Core\Url;
use Drupal\Tests\commerce\FunctionalJavascript\CommerceWebDriverTestBase;
class CouponRedemptionElementTest extends CommerceWebDriverTestBase {
protected $cart;
protected $cartManager;
protected $promotion;
public static $modules = [
'block',
'commerce_cart',
'commerce_promotion',
'commerce_promotion_test',
];
protected function setUp() : void {
parent::setUp();
$this->cart = $this->container
->get('commerce_cart.cart_provider')
->createCart('default', $this->store, $this->adminUser);
$this->cartManager = $this->container
->get('commerce_cart.cart_manager');
OrderItemType::create([
'id' => 'test',
'label' => 'Test',
'orderType' => 'default',
])
->save();
$order_item = OrderItem::create([
'type' => 'test',
'quantity' => 1,
'unit_price' => new Price('12.00', 'USD'),
]);
$order_item
->save();
$this->cartManager
->addOrderItem($this->cart, $order_item);
$this->promotion = $this
->createEntity('commerce_promotion', [
'name' => 'Promotion (with coupon)',
'order_types' => [
'default',
],
'stores' => [
$this->store
->id(),
],
'status' => TRUE,
'offer' => [
'target_plugin_id' => 'order_percentage_off',
'target_plugin_configuration' => [
'percentage' => '0.10',
],
],
'start_date' => '2017-01-01',
'conditions' => [],
]);
$first_coupon = $this
->createEntity('commerce_promotion_coupon', [
'code' => $this
->getRandomGenerator()
->word(8),
'status' => TRUE,
]);
$first_coupon
->save();
$second_coupon = $this
->createEntity('commerce_promotion_coupon', [
'code' => $this
->getRandomGenerator()
->word(8),
'status' => TRUE,
]);
$second_coupon
->save();
$this->promotion
->setCoupons([
$first_coupon,
$second_coupon,
]);
$this->promotion
->save();
}
public function testSingleCouponRedemption() {
$coupons = $this->promotion
->getCoupons();
$coupon = reset($coupons);
$this
->drupalGet(Url::fromRoute('commerce_cart.page', [], [
'query' => [
'coupon_cardinality' => 1,
],
]));
$this
->getSession()
->getPage()
->pressButton('Apply coupon');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('Please provide a coupon code');
$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');
$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
->getSession()
->getPage()
->pressButton('Remove coupon');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextNotContains($coupon
->getCode());
$this
->assertSession()
->fieldExists('Coupon code');
$this
->assertSession()
->buttonExists('Apply coupon');
}
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,
],
]));
$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');
$this
->assertSession()
->fieldValueNotEquals('Coupon code', $first_coupon
->getCode());
$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());
$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());
$this
->getSession()
->getPage()
->pressButton('remove_coupon_1');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextNotContains($second_coupon
->getCode());
$this
->assertSession()
->pageTextContains($first_coupon
->getCode());
$this
->getSession()
->getPage()
->pressButton('remove_coupon_0');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextNotContains($second_coupon
->getCode());
$this
->assertSession()
->pageTextNotContains($first_coupon
->getCode());
}
}