You are here

public function PaymentOrderUpdaterTest::testUpdate in Commerce Core 8.2

@covers ::requestUpdate @covers ::needsUpdate @covers ::updateOrders @covers ::updateOrder

File

modules/payment/tests/src/Kernel/PaymentOrderUpdaterTest.php, line 159

Class

PaymentOrderUpdaterTest
Tests the payment order updater.

Namespace

Drupal\Tests\commerce_payment\Kernel

Code

public function testUpdate() {
  $this
    ->assertTrue($this->firstOrder
    ->getTotalPaid()
    ->isZero());
  $this
    ->assertTrue($this->secondOrder
    ->getTotalPaid()
    ->isZero());
  $this
    ->assertFalse($this->paymentOrderUpdater
    ->needsUpdate($this->firstOrder));
  $this
    ->assertFalse($this->paymentOrderUpdater
    ->needsUpdate($this->secondOrder));
  $this->paymentOrderUpdater
    ->requestUpdate($this->firstOrder);
  $this
    ->assertTrue($this->paymentOrderUpdater
    ->needsUpdate($this->firstOrder));
  $this
    ->assertFalse($this->paymentOrderUpdater
    ->needsUpdate($this->secondOrder));

  /** @var \Drupal\commerce_payment\Entity\PaymentInterface $first_payment */
  $first_payment = Payment::create([
    'type' => 'payment_default',
    'payment_gateway' => 'onsite',
    'payment_method' => $this->paymentMethod
      ->id(),
    'order_id' => $this->firstOrder
      ->id(),
    'amount' => new Price('10', 'USD'),
    'state' => 'completed',
  ]);
  $first_payment
    ->save();

  /** @var \Drupal\commerce_payment\Entity\PaymentInterface $second_payment */
  $second_payment = Payment::create([
    'type' => 'payment_default',
    'payment_gateway' => 'onsite',
    'order_id' => $this->firstOrder
      ->id(),
    'amount' => new Price('10', 'USD'),
    'state' => 'authorization',
  ]);
  $second_payment
    ->save();
  $this->paymentOrderUpdater
    ->updateOrders();
  $this->firstOrder = $this
    ->reloadEntity($this->firstOrder);
  $this->secondOrder = $this
    ->reloadEntity($this->secondOrder);

  // Confirm that only the first payment was counted, since the second one
  // hasn't been completed yet.
  $this
    ->assertEquals($first_payment
    ->getAmount(), $this->firstOrder
    ->getTotalPaid());
  $this
    ->assertTrue($this->secondOrder
    ->getTotalPaid()
    ->isZero());

  // Confirm that order payment_gateway and payment_method fields received
  // values.
  $this
    ->assertFalse($this->firstOrder
    ->get('payment_gateway')
    ->isEmpty());
  $this
    ->assertFalse($this->firstOrder
    ->get('payment_method')
    ->isEmpty());

  // Confirm that the order is not resaved if total_paid hasn't changed.
  $changed = $this->firstOrder
    ->getChangedTime();
  sleep(1);
  $this->paymentOrderUpdater
    ->requestUpdate($this->firstOrder);
  $this->paymentOrderUpdater
    ->updateOrders();
  $this->firstOrder = $this
    ->reloadEntity($this->firstOrder);
  $this
    ->assertEquals($changed, $this->firstOrder
    ->getChangedTime());
}