You are here

public function OrderAssignmentTest::testAssign in Commerce Core 8.2

Tests assigning an order to a new customer.

@covers ::assignMultiple @covers ::assign

File

modules/order/tests/src/Kernel/OrderAssignmentTest.php, line 156

Class

OrderAssignmentTest
Tests the order assignment service.

Namespace

Drupal\Tests\commerce_order\Kernel

Code

public function testAssign() {
  $second_user = $this
    ->createUser([
    'mail' => $this
      ->randomString() . '@example.com',
  ]);
  $this->orderAssignment
    ->assignMultiple([
    $this->order,
  ], $second_user);
  $this->order = $this
    ->reloadEntity($this->order);
  $this
    ->assertEquals($second_user
    ->id(), $this->order
    ->getCustomerId());
  $this
    ->assertEquals($second_user
    ->getEmail(), $this->order
    ->getEmail());
  $billing_profile = $this->order
    ->getBillingProfile();

  /** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
  $payment_method = $this->order
    ->get('payment_method')->entity;
  $payment_method_profile = $payment_method
    ->getBillingProfile();

  // Confirm that the billing profile was not reassigned.
  $this
    ->assertEquals(0, $billing_profile
    ->getOwnerId());

  // Confirm that the payment method was reassigned.
  $this
    ->assertEquals($second_user
    ->id(), $payment_method
    ->getOwnerId());

  // Confirm that the payment method profile was copied to the address book.
  $this
    ->assertEmpty($payment_method_profile
    ->getData('copy_to_address_book'));
  $this
    ->assertNotEmpty($payment_method_profile
    ->getData('address_book_profile_id'));

  // Confirm that the new ID was also copied to the billing profile.
  $this
    ->assertNotEmpty($billing_profile
    ->getData('address_book_profile_id'));
  $third_user = $this
    ->createUser([
    'mail' => $this
      ->randomString() . '@example.com',
  ]);
  $this->orderAssignment
    ->assignMultiple([
    $this->order,
  ], $third_user);
  $this->order = $this
    ->reloadEntity($this->order);
  $this
    ->assertEquals($third_user
    ->id(), $this->order
    ->getCustomerId());
  $this
    ->assertEquals($third_user
    ->getEmail(), $this->order
    ->getEmail());
  $billing_profile = $this
    ->reloadEntity($billing_profile);
  $payment_method = $this
    ->reloadEntity($payment_method);

  // Confirm that the billing profile and payment method were not reassigned.
  $this
    ->assertEquals(0, $billing_profile
    ->getOwnerId());
  $this
    ->assertEquals($second_user
    ->id(), $payment_method
    ->getOwnerId());
}