You are here

public function ProfileFieldCopyTest::testAdminWithFields in Commerce Shipping 8.2

Tests the admin UI with additional billing fields.

File

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

Class

ProfileFieldCopyTest
Tests the "Shipping information" checkout pane.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

public function testAdminWithFields() {
  $billing_prefix = 'billing_profile[0][profile]';

  // 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();
  $shipping_profile = $this->shippingOrderManager
    ->createProfile($this->order, [
    'address' => $this->frenchAddress,
  ]);
  $shipping_profile
    ->save();
  $shipments = $this->shippingOrderManager
    ->pack($this->order, $shipping_profile);
  $this->order
    ->set('shipments', $shipments);
  $this->order
    ->save();

  // Confirm that the tax_number field is shown when copying is enabled.
  $this
    ->drupalGet($this->order
    ->toUrl('edit-form'));
  $this
    ->getSession()
    ->getPage()
    ->findButton('add_billing_information')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $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]');
  $this
    ->assertSession()
    ->fieldNotExists($billing_prefix . '[copy_to_address_book]');

  // 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',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Tax number is not in the right format. Examples: DE123456789, HU12345678.');

  // Confirm that the tax_number value is saved on the billing profile.
  $this
    ->submitForm([
    $billing_prefix . '[copy_fields][tax_number][0][value]' => 'FR40303265045',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The order 2020/01 has been successfully saved.');
  $this->order = $this
    ->reloadEntity($this->order);
  $billing_profile = $this->order
    ->getBillingProfile();

  /** @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('address_book_profile_id'));

  // Confirm that the tax_number value is available on the edit form.
  $this
    ->drupalGet($this->order
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->fieldValueEquals($billing_prefix . '[copy_fields][tax_number][0][value]', 'FR40303265045');
  $this
    ->assertSession()
    ->checkboxChecked($billing_prefix . '[copy_fields][enable]');
  $this
    ->getSession()
    ->getPage()
    ->uncheckField($billing_prefix . '[copy_fields][enable]');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRenderedAddress($this->frenchAddress);
  $this
    ->assertSession()
    ->pageTextContains('FR40303265045');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Confirm that the tax_number can be edited via the address book form.
  $this
    ->assertSession()
    ->fieldValueEquals($billing_prefix . '[tax_number][0][value]', 'FR40303265045');
  $this
    ->submitForm([
    $billing_prefix . '[tax_number][0][value]' => 'FRK7399859412',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The order 2020/01 has been successfully saved.');

  /** @var \Drupal\profile\Entity\ProfileInterface $billing_profile */
  $billing_profile = $this
    ->reloadEntity($billing_profile);

  /** @var \Drupal\address\AddressInterface $address */
  $address = $billing_profile
    ->get('address')
    ->first();
  $this
    ->assertEquals($this->frenchAddress, array_filter($address
    ->toArray()));
  $this
    ->assertEquals('FRK7399859412', $billing_profile
    ->get('tax_number')->value);
  $this
    ->assertEmpty($billing_profile
    ->getData('copy_fields'));
}