You are here

public function ProfileFieldCopyTest::testCheckout in Commerce Shipping 8.2

Tests checkout.

File

tests/src/FunctionalJavascript/ProfileFieldCopyTest.php, line 437

Class

ProfileFieldCopyTest
Tests the "Shipping information" checkout pane.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

public function testCheckout() {
  $first_address_book_profile = $this
    ->createEntity('profile', [
    'type' => 'customer',
    'uid' => $this->adminUser
      ->id(),
    'address' => $this->frenchAddress,
    'is_default' => TRUE,
  ]);
  $second_address_book_profile = $this
    ->createEntity('profile', [
    'type' => 'customer',
    'uid' => $this->adminUser
      ->id(),
    'address' => $this->usAddress,
  ]);
  $billing_prefix = 'payment_information[billing_information]';
  $this
    ->drupalGet(Url::fromRoute('commerce_checkout.form', [
    'commerce_order' => $this->order
      ->id(),
  ]));
  $this
    ->assertSession()
    ->pageTextContains('Shipping information');
  $this
    ->assertRenderedAddress($this->frenchAddress);
  $this
    ->assertSession()
    ->pageTextContains('Payment information');
  $this
    ->assertSession()
    ->checkboxChecked($billing_prefix . '[copy_fields][enable]');
  $this
    ->assertSession()
    ->fieldNotExists($billing_prefix . '[address][0][address][address_line1]');
  $billing_profile = $this->order
    ->getBillingProfile();
  $this
    ->assertEmpty($billing_profile);
  $this
    ->getSession()
    ->getPage()
    ->uncheckField($billing_prefix . '[copy_fields][enable]');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->fillField('payment_information[billing_information][select_address]', '_new');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->checkboxNotChecked($billing_prefix . '[copy_fields][enable]');
  $this
    ->getSession()
    ->getPage()
    ->checkField($billing_prefix . '[copy_fields][enable]');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Confirm that the shipping fields were copied on page submit.
  $this
    ->submitForm([], 'Continue to review');
  $this->order = $this
    ->reloadEntity($this->order);
  $billing_profile = $this->order
    ->getBillingProfile();
  $this
    ->assertNotEmpty($billing_profile);

  /** @var \Drupal\address\AddressInterface $address */
  $address = $billing_profile
    ->get('address')
    ->first();
  $this
    ->assertEquals($this->frenchAddress, array_filter($address
    ->toArray()));
  $this
    ->assertNotEmpty($billing_profile
    ->getData('copy_fields'));
  $this
    ->assertEmpty($billing_profile
    ->getData('copy_to_address_book'));
  $this
    ->assertEquals($first_address_book_profile
    ->id(), $billing_profile
    ->getData('address_book_profile_id'));

  // Go back, and edit the shipping profile. Confirm changes are carried over.
  $this
    ->clickLink('Go back');
  $this
    ->assertSession()
    ->checkboxChecked($billing_prefix . '[copy_fields][enable]');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('shipping_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([
    'shipping_information[shipping_profile][address][0][address][postal_code]' => '75003',
  ], 'Continue to review');
  $expected_address = [
    'postal_code' => '75003',
  ] + $this->frenchAddress;
  $this
    ->assertRenderedAddress($expected_address, $billing_profile
    ->id());

  /** @var \Drupal\profile\Entity\ProfileInterface $billing_profile */
  $billing_profile = $this
    ->reloadEntity($billing_profile);
  $address = $billing_profile
    ->get('address')
    ->first();
  $this
    ->assertEquals($expected_address, array_filter($address
    ->toArray()));
  $this
    ->assertNotEmpty($billing_profile
    ->getData('copy_fields'));
  $this
    ->assertEmpty($billing_profile
    ->getData('copy_to_address_book'));
  $this
    ->assertEquals($first_address_book_profile
    ->id(), $billing_profile
    ->getData('address_book_profile_id'));

  // Confirm that copy_fields can be unchecked, showing the address book.
  $this
    ->clickLink('Go back');
  $this
    ->assertSession()
    ->checkboxChecked($billing_prefix . '[copy_fields][enable]');
  $this
    ->getSession()
    ->getPage()
    ->uncheckField($billing_prefix . '[copy_fields][enable]');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $options = $this
    ->xpath('//select[@name="payment_information[billing_information][select_address]"]/option');
  $this
    ->assertCount(4, $options);
  $this
    ->assertEquals($first_address_book_profile
    ->id(), $options[0]
    ->getValue());
  $this
    ->assertEquals($second_address_book_profile
    ->id(), $options[1]
    ->getValue());
  $this
    ->assertEquals('_original', $options[2]
    ->getValue());
  $this
    ->assertEquals('_new', $options[3]
    ->getValue());

  // Confirm that a different profile can be selected.
  $this
    ->getSession()
    ->getPage()
    ->fillField('payment_information[billing_information][select_address]', $second_address_book_profile
    ->id());
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([], 'Continue to review');

  /** @var \Drupal\profile\Entity\ProfileInterface $billing_profile */
  $billing_profile = $this
    ->reloadEntity($billing_profile);
  $address = $billing_profile
    ->get('address')
    ->first();
  $this
    ->assertEquals($this->usAddress, array_filter($address
    ->toArray()));
  $this
    ->assertEmpty($billing_profile
    ->getData('copy_fields'));
  $this
    ->assertEquals($second_address_book_profile
    ->id(), $billing_profile
    ->getData('address_book_profile_id'));

  // Confirm that the copy_fields checkbox is still unchecked.
  $this
    ->clickLink('Go back');
  $this
    ->saveHtmlOutput();
  $this
    ->assertSession()
    ->checkboxNotChecked($billing_prefix . '[copy_fields][enable]');
  $this
    ->assertRenderedAddress($this->usAddress, $billing_profile
    ->id());

  // Confirm that the _original option is no longer present.
  $options = $this
    ->xpath('//select[@name="payment_information[billing_information][select_address]"]/option');
  $this
    ->assertCount(3, $options);
  $this
    ->assertEquals($first_address_book_profile
    ->id(), $options[0]
    ->getValue());
  $this
    ->assertEquals($second_address_book_profile
    ->id(), $options[1]
    ->getValue());
  $this
    ->assertEquals('_new', $options[2]
    ->getValue());
}