You are here

public function ProfileFieldCopyTest::testCheckoutWithFields in Commerce Shipping 8.2

Tests checkout with additional billing fields.

File

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

Class

ProfileFieldCopyTest
Tests the "Shipping information" checkout pane.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

public function testCheckoutWithFields() {
  $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]';

  // Expose the tax_number field on the default form mode.
  // commerce_shipping_entity_form_display_alter() will hide it for shipping.
  $form_display = commerce_get_entity_display('profile', 'customer', 'form');
  $form_display
    ->setComponent('tax_number', [
    'type' => 'commerce_tax_number_default',
  ]);
  $form_display
    ->save();
  $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()
    ->fieldExists($billing_prefix . '[copy_fields][tax_number][0][value]');
  $this
    ->assertSession()
    ->fieldNotExists($billing_prefix . '[address][0][address][address_line1]');
  $billing_profile = $this->order
    ->getBillingProfile();
  $this
    ->assertEmpty($billing_profile);

  // Confirm that validation is performed, based on the
  // shipping profile's country code (FR).
  $this
    ->submitForm([
    $billing_prefix . '[copy_fields][tax_number][0][value]' => 'ABC123456',
  ], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextContains('Tax number is not in the right format. Examples: DE123456789, HU12345678.');

  // Confirm that the shipping fields and the tax_number value
  // were copied on page submit.
  $this
    ->submitForm([
    $billing_prefix . '[copy_fields][tax_number][0][value]' => 'FR40303265045',
  ], '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
    ->assertEquals('FR40303265045', $billing_profile
    ->get('tax_number')->value);
  $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 select the US profile for shipping.
  $this
    ->clickLink('Go back');
  $this
    ->assertSession()
    ->checkboxChecked($billing_prefix . '[copy_fields][enable]');
  $this
    ->getSession()
    ->getPage()
    ->fillField('shipping_information[shipping_profile][select_address]', $second_address_book_profile
    ->id());
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->saveHtmlOutput();

  // Try to re-enter the dummy tax number. This should work because a
  // French address is no longer selected, turning off EU validation.
  $this
    ->assertSession()
    ->fieldValueEquals($billing_prefix . '[copy_fields][tax_number][0][value]', 'FR40303265045');
  $this
    ->submitForm([
    $billing_prefix . '[copy_fields][tax_number][0][value]' => 'ABC123456',
  ], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextNotContains('Tax number is not in the right format. Examples: DE123456789, HU12345678.');
  $this
    ->assertRenderedAddress($this->usAddress, $billing_profile
    ->id());

  /** @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
    ->assertEquals('ABC123456', $billing_profile
    ->get('tax_number')->value);
  $this
    ->assertNotEmpty($billing_profile
    ->getData('copy_fields'));
  $this
    ->assertEmpty($billing_profile
    ->getData('copy_to_address_book'));
  $this
    ->assertEquals($second_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();
  $this
    ->assertRenderedAddress($this->usAddress, $billing_profile
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('ABC123456');

  // Confirm that the profile can be edited.
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->fieldValueEquals($billing_prefix . '[tax_number][0][value]', 'ABC123456');
  $this
    ->submitForm([
    $billing_prefix . '[tax_number][0][value]' => 'ABC987',
  ], '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
    ->assertEquals('ABC987', $billing_profile
    ->get('tax_number')->value);
  $this
    ->assertEmpty($billing_profile
    ->getData('copy_fields'));
  $this
    ->assertEmpty($billing_profile
    ->getData('copy_to_address_book'));
  $this
    ->assertEquals($second_address_book_profile
    ->id(), $billing_profile
    ->getData('address_book_profile_id'));
}