You are here

protected function PaymentOptionsBuilderTest::setUp in Commerce Core 8.2

Overrides OrderKernelTestBase::setUp

File

modules/payment/tests/src/Kernel/PaymentOptionsBuilderTest.php, line 50

Class

PaymentOptionsBuilderTest
Tests the payment options builder.

Namespace

Drupal\Tests\commerce_payment\Kernel

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->installEntitySchema('commerce_payment');
  $this
    ->installEntitySchema('commerce_payment_method');
  $this
    ->installConfig('commerce_payment');
  $this->paymentOptionsBuilder = $this->container
    ->get('commerce_payment.options_builder');
  $user = $this
    ->createUser();
  $another_user = $this
    ->createUser();
  $payment_gateway = PaymentGateway::create([
    'id' => 'onsite',
    'label' => 'On-site',
    'plugin' => 'example_onsite',
  ]);
  $payment_gateway
    ->save();
  $payment_gateway = PaymentGateway::create([
    'id' => 'offsite',
    'label' => 'Off-site',
    'plugin' => 'example_offsite_redirect',
    'configuration' => [
      'redirect_method' => 'post',
      'payment_method_types' => [
        'credit_card',
      ],
    ],
  ]);
  $payment_gateway
    ->save();
  $payment_gateway = PaymentGateway::create([
    'id' => 'cash_on_delivery',
    'label' => 'Manual',
    'plugin' => 'manual',
    'configuration' => [
      'display_label' => 'Cash on delivery',
      'instructions' => [
        'value' => 'Sample payment instructions.',
        'format' => 'plain_text',
      ],
    ],
    'weight' => 10,
  ]);
  $payment_gateway
    ->save();

  // A manual gateway with a condition that won't be satisfied, to ensure
  // that it's not offered to the user.
  $payment_gateway = PaymentGateway::create([
    'id' => 'card_on_delivery',
    'label' => 'Manual',
    'plugin' => 'manual',
    'configuration' => [
      'display_label' => 'Card on delivery',
      'instructions' => [
        'value' => 'Sample payment instructions.',
        'format' => 'plain_text',
      ],
    ],
    'conditions' => [
      [
        'plugin' => 'order_total_price',
        'configuration' => [
          'operator' => '>',
          'amount' => [
            'number' => '99.00',
            'currency_code' => 'USD',
          ],
        ],
      ],
    ],
    'weight' => 10,
  ]);
  $payment_gateway
    ->save();
  $profile = Profile::create([
    'type' => 'customer',
    'address' => [
      'country_code' => 'US',
      'postal_code' => '53177',
      'locality' => 'Milwaukee',
      'address_line1' => 'Pabst Blue Ribbon Dr',
      'administrative_area' => 'WI',
      'given_name' => 'Frederick',
      'family_name' => 'Pabst',
    ],
    'uid' => $user
      ->id(),
  ]);
  $profile
    ->save();
  $payment_method = PaymentMethod::create([
    'uid' => $user
      ->id(),
    'type' => 'credit_card',
    'payment_gateway' => 'onsite',
    'card_type' => 'visa',
    'card_number' => '1111',
    'billing_profile' => $profile,
    'reusable' => TRUE,
    'expires' => strtotime('2028/03/24'),
  ]);
  $payment_method
    ->setBillingProfile($profile);
  $payment_method
    ->save();

  // Create a payment method for $another_user as well, to confirm that it's
  // not offered to the first user.
  $payment_method = PaymentMethod::create([
    'uid' => $another_user
      ->id(),
    'type' => 'credit_card',
    'payment_gateway' => 'onsite',
    'card_type' => 'visa',
    'card_number' => '1112',
    'billing_profile' => $profile,
    'reusable' => TRUE,
    'expires' => strtotime('2028/03/24'),
  ]);
  $payment_method
    ->setBillingProfile($profile);
  $payment_method
    ->save();
  $order_payment_method = PaymentMethod::create([
    'type' => 'credit_card',
    'payment_gateway' => 'onsite',
    'card_type' => 'visa',
    'card_number' => '9999',
    'reusable' => FALSE,
  ]);
  $order_payment_method
    ->save();
  $order_item = OrderItem::create([
    'type' => 'test',
    'quantity' => 1,
    'unit_price' => new Price('10', 'USD'),
  ]);
  $order_item
    ->save();
  $this->order = Order::create([
    'uid' => $user
      ->id(),
    'type' => 'default',
    'state' => 'draft',
    'order_items' => [
      $order_item,
    ],
    'payment_gateway' => 'onsite',
    'payment_method' => $order_payment_method,
    'store_id' => $this->store,
  ]);
  $this->order
    ->save();
}