You are here

public function AddressBookTest::testCopyMultiple in Commerce Core 8.2

Test copying when multiple profiles are allowed per customer.

@covers ::copy @covers ::allowsMultiple

File

modules/order/tests/src/Kernel/AddressBookTest.php, line 209

Class

AddressBookTest
Tests the address book.

Namespace

Drupal\Tests\commerce_order\Kernel

Code

public function testCopyMultiple() {
  $order_address = array_filter($this->orderProfile
    ->get('address')
    ->first()
    ->getValue());

  // Confirm that trying to copy to an anonymous user doesn't explode, or
  // create ghost profiles.
  $this->addressBook
    ->copy($this->orderProfile, User::getAnonymousUser());
  $new_profile = Profile::load(3);
  $this
    ->assertEmpty($new_profile);
  $this->addressBook
    ->copy($this->orderProfile, $this->user);

  // Confirm that a new profile was created with the original field data.
  $new_profile = Profile::load(3);
  $this
    ->assertNotEmpty($new_profile);
  $this
    ->assertFalse($new_profile
    ->isDefault());
  $this
    ->assertEquals($this->user
    ->id(), $new_profile
    ->getOwnerId());
  $this
    ->assertEquals($order_address, array_filter($new_profile
    ->get('address')
    ->first()
    ->getValue()));
  $this
    ->assertNull($new_profile
    ->getData('copy_to_address_book'));

  // Confirm that the order profile was updated to point to the new profile.
  $this->orderProfile = $this
    ->reloadEntity($this->orderProfile);
  $this
    ->assertNull($this->orderProfile
    ->getData('copy_to_address_book'));
  $this
    ->assertEquals($new_profile
    ->id(), $this->orderProfile
    ->getData('address_book_profile_id'));

  // Confirm that copying the profile again updates the address book profile.
  $order_address = [
    'country_code' => 'US',
    'postal_code' => '53177',
    'locality' => 'Milwaukee',
    'address_line1' => 'Pabst Blue Ribbon Dr',
    'administrative_area' => 'WI',
    'given_name' => 'Frederick',
    'family_name' => 'Pabst Jr.',
  ];
  $this->orderProfile
    ->set('address', $order_address);
  $this->orderProfile
    ->save();
  $this->addressBook
    ->copy($this->orderProfile, $this->user);
  $new_profile = $this
    ->reloadEntity($new_profile);
  $this
    ->assertEquals($order_address, array_filter($new_profile
    ->get('address')
    ->first()
    ->getValue()));
  $non_expected_profile = Profile::load(4);
  $this
    ->assertEmpty($non_expected_profile);
}