You are here

public function ProfileFieldCopyTest::testAdmin in Commerce Shipping 8.2

Tests the admin UI.

File

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

Class

ProfileFieldCopyTest
Tests the "Shipping information" checkout pane.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

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

  // Confirm that the checkbox is not shown until a shipping profile is added.
  $this
    ->drupalGet($this->order
    ->toUrl('edit-form'));
  $this
    ->getSession()
    ->getPage()
    ->findButton('add_billing_information')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->fieldNotExists($billing_prefix . '[copy_fields][enable]');
  $this
    ->assertSession()
    ->fieldExists($billing_prefix . '[address][0][address][address_line1]');
  $this
    ->assertSession()
    ->fieldExists($billing_prefix . '[copy_to_address_book]');
  $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 checkbox is now shown and checked.
  $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()
    ->fieldNotExists($billing_prefix . '[address][0][address][address_line1]');
  $this
    ->assertSession()
    ->fieldNotExists($billing_prefix . '[copy_to_address_book]');

  // Confirm that submitting the form populates the billing profile.
  $this
    ->submitForm([], '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
    ->assertNotEmpty($billing_profile
    ->getData('copy_fields'));
  $this
    ->assertEmpty($billing_profile
    ->getData('address_book_profile_id'));

  // Confirm that the checkbox can be unchecked.
  $this
    ->drupalGet($this->order
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->checkboxChecked($billing_prefix . '[copy_fields][enable]');
  $this
    ->getSession()
    ->getPage()
    ->uncheckField($billing_prefix . '[copy_fields][enable]');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRenderedAddress($this->frenchAddress);

  // Confirm that the address book form still works.
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->fieldValueEquals($billing_prefix . '[address][0][address][address_line1]', $this->frenchAddress['address_line1']);
  $this
    ->submitForm([
    $billing_prefix . '[address][0][address][address_line1]' => '37 Rue du Sentier',
    $billing_prefix . '[copy_to_address_book]' => TRUE,
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The order 2020/01 has been successfully saved.');
  $expected_address = [
    'address_line1' => '37 Rue du Sentier',
  ] + $this->frenchAddress;

  /** @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($expected_address, array_filter($address
    ->toArray()));
  $this
    ->assertEmpty($billing_profile
    ->getData('copy_fields'));
  $this
    ->assertNotEmpty($billing_profile
    ->getData('address_book_profile_id'));

  // Confirm that the checkbox is still unchecked after the page is reloaded.
  $this
    ->drupalGet($this->order
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->checkboxNotChecked($billing_prefix . '[copy_fields][enable]');
  $this
    ->assertRenderedAddress($expected_address);
}