View source
<?php
namespace Drupal\Tests\commerce_payment\Kernel;
use Drupal\commerce_payment\Entity\PaymentGateway;
use Drupal\commerce_payment\Entity\PaymentMethod;
use Drupal\profile\Entity\Profile;
use Drupal\Tests\commerce_order\Kernel\OrderKernelTestBase;
use Drupal\user\Entity\User;
class PaymentMethodStorageTest extends OrderKernelTestBase {
protected $user;
protected $paymentGateway;
protected $storage;
protected $futureExpire;
public static $modules = [
'commerce_payment',
'commerce_payment_example',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('commerce_payment');
$this
->installEntitySchema('commerce_payment_method');
$this
->installConfig('commerce_payment');
$payment_gateway = PaymentGateway::create([
'id' => 'example',
'label' => 'Example',
'plugin' => 'example_onsite',
]);
$payment_gateway
->setPluginConfiguration([
'api_key' => '2342fewfsfs',
'mode' => 'test',
'payment_method_types' => [
'credit_card',
],
]);
$payment_gateway
->save();
$this->paymentGateway = $this
->reloadEntity($payment_gateway);
$user = $this
->createUser();
$this->user = $this
->reloadEntity($user);
$this->storage = $this->container
->get('entity_type.manager')
->getStorage('commerce_payment_method');
$this->futureExpire = time() + 2 * 365 * 24 * 60 * 60;
}
public function testLoadReusable() {
$payment_method_expired = PaymentMethod::create([
'type' => 'credit_card',
'payment_gateway' => 'example',
'payment_gateway_mode' => 'test',
'expires' => '1452902400',
'uid' => $this->user
->id(),
]);
$payment_method_expired
->save();
$payment_method_active = PaymentMethod::create([
'type' => 'credit_card',
'payment_gateway' => 'example',
'payment_gateway_mode' => 'test',
'expires' => $this->futureExpire,
'uid' => $this->user
->id(),
]);
$payment_method_active
->save();
$payment_method_unlimited = PaymentMethod::create([
'type' => 'credit_card',
'payment_gateway' => 'example',
'payment_gateway_mode' => 'test',
'expires' => 0,
'uid' => $this->user
->id(),
]);
$payment_method_unlimited
->save();
$reusable_payment_methods = $this->storage
->loadReusable($this->user, $this->paymentGateway);
$this
->assertEquals([
$payment_method_unlimited
->id(),
$payment_method_active
->id(),
], array_keys($reusable_payment_methods));
$payment_method_active
->setOwnerId(0);
$payment_method_active
->save();
$payment_method_unlimited
->setOwnerId(0);
$payment_method_unlimited
->save();
$this
->assertEmpty($this->storage
->loadReusable(User::getAnonymousUser(), $this->paymentGateway));
$this
->assertEmpty($this->storage
->loadReusable($this->user, $this->paymentGateway));
$payment_gateway_configuration = $this->paymentGateway
->getPluginConfiguration();
$payment_gateway_configuration['mode'] = 'live';
$this->paymentGateway
->setPluginConfiguration($payment_gateway_configuration);
$this->paymentGateway
->save();
$reusable_payment_methods = $this->storage
->loadReusable($this->user, $this->paymentGateway);
$this
->assertEmpty($reusable_payment_methods);
}
public function testBillingCountryFiltering() {
$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();
$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();
$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));
$this->paymentGateway
->setPluginConfiguration([
'collect_billing_information' => FALSE,
'api_key' => '2342fewfsfs',
'mode' => 'test',
'payment_method_types' => [
'credit_card',
],
]);
$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));
}
}