You are here

public function FilterPaymentGatewaysEventTest::testEvent in Commerce Core 8.2

Tests that the proper gateway is filtered out.

File

modules/payment/tests/src/Kernel/FilterPaymentGatewaysEventTest.php, line 49

Class

FilterPaymentGatewaysEventTest
Tests the FilterPaymentGatewaysEvent.

Namespace

Drupal\Tests\commerce_payment\Kernel

Code

public function testEvent() {
  $payment_gateway_example = PaymentGateway::create([
    'id' => 'example',
    'label' => 'Example',
    'plugin' => 'example_onsite',
    'weight' => 1,
  ]);
  $payment_gateway_example
    ->save();
  $payment_gateway_filtered = PaymentGateway::create([
    'id' => 'example_filtered',
    'label' => 'Example (Filtered)',
    'plugin' => 'example_onsite',
    'weight' => 2,
  ]);
  $payment_gateway_filtered
    ->save();
  $profile = Profile::create([
    'type' => 'customer',
  ]);
  $profile
    ->save();
  $profile = $this
    ->reloadEntity($profile);
  $user = $this
    ->createUser();
  $order = Order::create([
    'type' => 'default',
    'state' => 'draft',
    'mail' => $user
      ->getEmail(),
    'uid' => $user
      ->id(),
    'ip_address' => '127.0.0.1',
    'order_number' => '6',
    'billing_profile' => $profile,
    'store_id' => $this->store
      ->id(),
  ]);
  $order
    ->save();
  $available_payment_gateways = $this->storage
    ->loadMultipleForOrder($order);
  $this
    ->assertEquals(2, count($available_payment_gateways));
  $payment_gateway = array_shift($available_payment_gateways);
  $this
    ->assertEquals($payment_gateway_example
    ->label(), $payment_gateway
    ->label());
  $payment_gateway = array_shift($available_payment_gateways);
  $this
    ->assertEquals($payment_gateway_filtered
    ->label(), $payment_gateway
    ->label());
  $order
    ->setData('excluded_gateways', [
    $payment_gateway_filtered
      ->id(),
  ]);
  $available_payment_gateways = $this->storage
    ->loadMultipleForOrder($order);
  $this
    ->assertEquals(1, count($available_payment_gateways));
  $payment_gateway = array_shift($available_payment_gateways);
  $this
    ->assertEquals($payment_gateway_example
    ->label(), $payment_gateway
    ->label());
}