You are here

public function PaymentMethodStorageTest::testBillingCountryFiltering in Commerce Core 8.2

Tests filtering reusable payment methods by billing country.

File

modules/payment/tests/src/Kernel/PaymentMethodStorageTest.php, line 145

Class

PaymentMethodStorageTest
Tests the payment method storage.

Namespace

Drupal\Tests\commerce_payment\Kernel

Code

public function testBillingCountryFiltering() {

  /** @var \Drupal\profile\Entity\Profile $profile_fr */
  $profile_fr = Profile::create([
    'type' => 'customer',
    'uid' => 0,
    'address' => [
      'organization' => '',
      'country_code' => 'FR',
      'postal_code' => '75002',
      'locality' => 'Paris',
      'address_line1' => 'A french street',
      'given_name' => 'John',
      'family_name' => 'LeSmith',
    ],
  ]);
  $profile_fr
    ->save();

  /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method_fr */
  $payment_method_fr = PaymentMethod::create([
    'type' => 'credit_card',
    'payment_gateway' => 'example',
    'expires' => $this->futureExpire,
    'uid' => $this->user
      ->id(),
    'billing_profile' => $profile_fr,
  ]);
  $payment_method_fr
    ->save();
  $payment_method_fr = $this
    ->reloadEntity($payment_method_fr);
  $profile_us = Profile::create([
    'type' => 'customer',
    'uid' => 0,
    'address' => [
      'country_code' => 'US',
      'postal_code' => '53177',
      'locality' => 'Milwaukee',
      'address_line1' => 'Pabst Blue Ribbon Dr',
      'administrative_area' => 'WI',
      'given_name' => 'Frederick',
      'family_name' => 'Pabst',
    ],
  ]);
  $profile_us
    ->save();

  /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method_fr */
  $payment_method_us = PaymentMethod::create([
    'type' => 'credit_card',
    'payment_gateway' => 'example',
    'expires' => $this->futureExpire,
    'uid' => $this->user
      ->id(),
    'billing_profile' => $profile_us,
  ]);
  $payment_method_us
    ->save();
  $payment_method_us = $this
    ->reloadEntity($payment_method_us);
  $payment_methods = $this->storage
    ->loadReusable($this->user, $this->paymentGateway, [
    'US',
  ]);
  $this
    ->assertCount(1, $payment_methods);
  $this
    ->assertEquals([
    $payment_method_us,
  ], array_values($payment_methods));
  $payment_methods = $this->storage
    ->loadReusable($this->user, $this->paymentGateway, [
    'FR',
  ]);
  $this
    ->assertCount(1, $payment_methods);
  $this
    ->assertEquals([
    $payment_method_fr,
  ], array_values($payment_methods));

  // Disable the collection of billing information.
  $this->paymentGateway
    ->setPluginConfiguration([
    'collect_billing_information' => FALSE,
    'api_key' => '2342fewfsfs',
    'mode' => 'test',
    'payment_method_types' => [
      'credit_card',
    ],
  ]);

  // Confirm that no filtering is done.
  $payment_methods = $this->storage
    ->loadReusable($this->user, $this->paymentGateway, [
    'FR',
  ]);
  $this
    ->assertCount(2, $payment_methods);
  $this
    ->assertEquals([
    $payment_method_us,
    $payment_method_fr,
  ], array_values($payment_methods));
}