|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\commerce_payment\Kernel; |
| 4 | + |
| 5 | +use Drupal\commerce_order\Entity\OrderItemType; |
| 6 | +use Drupal\commerce_payment\Entity\PaymentGateway; |
| 7 | +use Drupal\commerce_payment\Entity\PaymentMethod; |
| 8 | +use Drupal\profile\Entity\Profile; |
| 9 | +use Drupal\user\Entity\User; |
| 10 | +use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests the payment method storage. |
| 14 | + * |
| 15 | + * @group commerce |
| 16 | + */ |
| 17 | +class PaymentMethodStorageTest extends CommerceKernelTestBase { |
| 18 | + |
| 19 | + /** |
| 20 | + * A sample user. |
| 21 | + * |
| 22 | + * @var \Drupal\user\UserInterface |
| 23 | + */ |
| 24 | + protected $user; |
| 25 | + |
| 26 | + /** |
| 27 | + * A payment gateway. |
| 28 | + * |
| 29 | + * @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface |
| 30 | + */ |
| 31 | + protected $paymentGateway; |
| 32 | + |
| 33 | + /** |
| 34 | + * The payment method storage. |
| 35 | + * |
| 36 | + * @var \Drupal\commerce_payment\PaymentMethodStorageInterface |
| 37 | + */ |
| 38 | + protected $storage; |
| 39 | + |
| 40 | + /** |
| 41 | + * Modules to enable. |
| 42 | + * |
| 43 | + * @var array |
| 44 | + */ |
| 45 | + public static $modules = [ |
| 46 | + 'address', |
| 47 | + 'entity_reference_revisions', |
| 48 | + 'profile', |
| 49 | + 'state_machine', |
| 50 | + 'commerce_product', |
| 51 | + 'commerce_order', |
| 52 | + 'commerce_payment', |
| 53 | + 'commerce_payment_example', |
| 54 | + ]; |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritdoc} |
| 58 | + */ |
| 59 | + protected function setUp() { |
| 60 | + parent::setUp(); |
| 61 | + |
| 62 | + $this->installEntitySchema('profile'); |
| 63 | + $this->installEntitySchema('commerce_order'); |
| 64 | + $this->installEntitySchema('commerce_order_item'); |
| 65 | + $this->installEntitySchema('commerce_payment'); |
| 66 | + $this->installEntitySchema('commerce_payment_method'); |
| 67 | + $this->installConfig('commerce_order'); |
| 68 | + $this->installConfig('commerce_payment'); |
| 69 | + |
| 70 | + // An order item type that doesn't need a purchasable entity, for simplicity. |
| 71 | + OrderItemType::create([ |
| 72 | + 'id' => 'test', |
| 73 | + 'label' => 'Test', |
| 74 | + 'orderType' => 'default', |
| 75 | + ])->save(); |
| 76 | + |
| 77 | + $payment_gateway = PaymentGateway::create([ |
| 78 | + 'id' => 'example', |
| 79 | + 'label' => 'Example', |
| 80 | + 'plugin' => 'example_onsite', |
| 81 | + ]); |
| 82 | + $payment_gateway->save(); |
| 83 | + $this->paymentGateway = $this->reloadEntity($payment_gateway); |
| 84 | + |
| 85 | + $user = $this->createUser(); |
| 86 | + $this->user = $this->reloadEntity($user); |
| 87 | + |
| 88 | + $this->storage = $this->container->get('entity_type.manager')->getStorage('commerce_payment_method'); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Tests loading reusable payment methods. |
| 93 | + */ |
| 94 | + public function testLoadReusable() { |
| 95 | + /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method_expired */ |
| 96 | + $payment_method_expired = PaymentMethod::create([ |
| 97 | + 'type' => 'credit_card', |
| 98 | + 'payment_gateway' => 'example', |
| 99 | + // Sat, 16 Jan 2016. |
| 100 | + 'expires' => '1452902400', |
| 101 | + 'uid' => $this->user->id(), |
| 102 | + ]); |
| 103 | + $payment_method_expired->save(); |
| 104 | + /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */ |
| 105 | + $payment_method_active = PaymentMethod::create([ |
| 106 | + 'type' => 'credit_card', |
| 107 | + 'payment_gateway' => 'example', |
| 108 | + // Thu, 16 Jan 2020. |
| 109 | + 'expires' => '1579132800', |
| 110 | + 'uid' => $this->user->id(), |
| 111 | + ]); |
| 112 | + $payment_method_active->save(); |
| 113 | + // Confirm that only the active payment method was loaded. |
| 114 | + $reusable_payment_methods = $this->storage->loadReusable($this->user, $this->paymentGateway); |
| 115 | + $this->assertEquals([$payment_method_active->id()], array_keys($reusable_payment_methods)); |
| 116 | + |
| 117 | + // Confirm that anonymous users cannot have reusable payment methods. |
| 118 | + $payment_method_active->setOwnerId(0); |
| 119 | + $payment_method_active->save(); |
| 120 | + $this->assertEmpty($this->storage->loadReusable(User::getAnonymousUser(), $this->paymentGateway)); |
| 121 | + $this->assertEmpty($this->storage->loadReusable($this->user, $this->paymentGateway)); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Tests filtering reusable payment methods by billing country. |
| 126 | + */ |
| 127 | + public function testBillingCountryFiltering() { |
| 128 | + /** @var \Drupal\profile\Entity\Profile $profile_fr */ |
| 129 | + $profile_fr = Profile::create([ |
| 130 | + 'type' => 'customer', |
| 131 | + 'address' => [ |
| 132 | + 'organization' => '', |
| 133 | + 'country_code' => 'FR', |
| 134 | + 'postal_code' => '75002', |
| 135 | + 'locality' => 'Paris', |
| 136 | + 'address_line1' => 'A french street', |
| 137 | + 'given_name' => 'John', |
| 138 | + 'family_name' => 'LeSmith', |
| 139 | + ], |
| 140 | + 'uid' => $this->user->id(), |
| 141 | + ]); |
| 142 | + $profile_fr->save(); |
| 143 | + /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method_fr */ |
| 144 | + $payment_method_fr = PaymentMethod::create([ |
| 145 | + 'type' => 'credit_card', |
| 146 | + 'payment_gateway' => 'example', |
| 147 | + 'expires' => '1579132800', |
| 148 | + 'uid' => $this->user->id(), |
| 149 | + 'billing_profile' => $profile_fr, |
| 150 | + ]); |
| 151 | + $payment_method_fr->save(); |
| 152 | + |
| 153 | + $this->assertEmpty($this->storage->loadReusable($this->user, $this->paymentGateway, ['US'])); |
| 154 | + |
| 155 | + $profile_us = Profile::create([ |
| 156 | + 'type' => 'customer', |
| 157 | + 'address' => [ |
| 158 | + 'country_code' => 'US', |
| 159 | + 'postal_code' => '53177', |
| 160 | + 'locality' => 'Milwaukee', |
| 161 | + 'address_line1' => 'Pabst Blue Ribbon Dr', |
| 162 | + 'administrative_area' => 'WI', |
| 163 | + 'given_name' => 'Frederick', |
| 164 | + 'family_name' => 'Pabst', |
| 165 | + ], |
| 166 | + 'uid' => $this->user->id(), |
| 167 | + ]); |
| 168 | + $profile_us->save(); |
| 169 | + /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method_fr */ |
| 170 | + $payment_method_us = PaymentMethod::create([ |
| 171 | + 'type' => 'credit_card', |
| 172 | + 'payment_gateway' => 'example', |
| 173 | + 'expires' => '1579132800', |
| 174 | + 'uid' => $this->user->id(), |
| 175 | + 'billing_profile' => $profile_us, |
| 176 | + ]); |
| 177 | + $payment_method_us->save(); |
| 178 | + |
| 179 | + $this->assertTrue($this->storage->loadReusable($this->user, $this->paymentGateway, ['US'])); |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments