You are here

public function CustomerProfileTest::testMultipleAdministrator in Commerce Core 8.2

Tests the address book in "multiple" mode, for administrators.

File

modules/order/tests/src/FunctionalJavascript/CustomerProfileTest.php, line 515

Class

CustomerProfileTest
Tests the customer_profile inline form.

Namespace

Drupal\Tests\commerce_order\FunctionalJavascript

Code

public function testMultipleAdministrator() {

  // Start from a one-off profile, then add it to the address book.
  $profile = Profile::create([
    'type' => 'customer',
    'uid' => 0,
    'address' => $this->frenchAddress,
  ]);
  $profile
    ->save();
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
    ->id() . '/TRUE');
  $this
    ->assertRenderedAddress($this->frenchAddress);
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  foreach ($this->frenchAddress as $property => $value) {
    $this
      ->assertSession()
      ->fieldValueEquals("profile[address][0][address][{$property}]", $value);
  }
  $this
    ->assertSession()
    ->checkboxNotChecked('profile[copy_to_address_book]');
  $this
    ->assertSession()
    ->pageTextContains("Save to the customer's address book");
  $this
    ->submitForm([
    'profile[copy_to_address_book]' => TRUE,
  ], 'Submit');
  $this
    ->assertSession()
    ->pageTextContains('The street is "38 Rue du Sentier" and the country code is FR.');

  /** @var \Drupal\profile\Entity\ProfileInterface $profile */
  $profile = $this
    ->reloadEntity($profile);
  $this
    ->assertNotEmpty($profile
    ->getData('address_book_profile_id'));
  $address_book_profile_id = $profile
    ->getData('address_book_profile_id');
  $address_book_profile = Profile::load($address_book_profile_id);
  $this
    ->assertTrue($address_book_profile
    ->isDefault());
  $this
    ->assertEquals($this->frenchAddress, array_filter($address_book_profile
    ->get('address')
    ->first()
    ->toArray()));

  // Update the profile.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
    ->id() . '/TRUE');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  foreach ($this->frenchAddress as $property => $value) {
    $this
      ->assertSession()
      ->fieldValueEquals("profile[address][0][address][{$property}]", $value);
  }

  // The copy checkbox should be hidden and checked.
  $this
    ->assertSession()
    ->fieldNotExists('profile[copy_to_address_book]');
  $this
    ->submitForm([
    'profile[address][0][address][address_line1]' => '37 Rue du Sentier',
  ], 'Submit');
  $this
    ->assertSession()
    ->pageTextContains('The street is "37 Rue du Sentier" and the country code is FR.');

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

  /** @var \Drupal\profile\Entity\ProfileInterface $address_book_profile */
  $address_book_profile = $this
    ->reloadEntity($address_book_profile);
  $this
    ->assertEquals('37 Rue du Sentier', $profile
    ->get('address')
    ->first()
    ->getAddressLine1());
  $this
    ->assertEquals('37 Rue du Sentier', $address_book_profile
    ->get('address')
    ->first()
    ->getAddressLine1());

  // Replace the profile with a new one.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
    ->id() . '/TRUE');
  $this
    ->getSession()
    ->getPage()
    ->fillField('profile[select_address]', '_new');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  foreach ($this->emptyAddress as $property => $value) {
    $this
      ->assertSession()
      ->fieldValueEquals("profile[address][0][address][{$property}]", $value);
  }
  $this
    ->assertSession()
    ->checkboxNotChecked('profile[copy_to_address_book]');
  $this
    ->assertSession()
    ->pageTextContains("Save to the customer's address book");
  $this
    ->submitForm([
    'profile[address][0][address][given_name]' => 'John',
    'profile[address][0][address][family_name]' => 'Smith',
    'profile[address][0][address][address_line1]' => 'Cetinjska 13',
    'profile[address][0][address][locality]' => 'Belgrade',
    'profile[copy_to_address_book]' => TRUE,
  ], 'Submit');

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

  /** @var \Drupal\profile\Entity\ProfileInterface $address_book_profile */
  $address_book_profile = $this
    ->reloadEntity($address_book_profile);
  $new_address_book_profile_id = $profile
    ->getData('address_book_profile_id');
  $new_address_book_profile = Profile::load($new_address_book_profile_id);

  // Confirm that the previous address book profile is unchanged.
  $this
    ->assertEquals('37 Rue du Sentier', $address_book_profile
    ->get('address')
    ->first()
    ->getAddressLine1());
  $this
    ->assertNotEquals($new_address_book_profile
    ->id(), $address_book_profile
    ->id());

  // Confirm that the profile and the new address book profile have the
  // expected values.
  $this
    ->assertEquals('Cetinjska 13', $profile
    ->get('address')
    ->first()
    ->getAddressLine1());
  $this
    ->assertEquals('Cetinjska 13', $new_address_book_profile
    ->get('address')
    ->first()
    ->getAddressLine1());

  // Confirm that both address book profiles are now present in the dropdown.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
    ->id() . '/TRUE');
  $options = $this
    ->xpath('//select[@name="profile[select_address]"]/option');
  $this
    ->assertCount(3, $options);
  $this
    ->assertEquals('37 Rue du Sentier', $options[0]
    ->getText());
  $this
    ->assertEquals('Cetinjska 13', $options[1]
    ->getText());
  $this
    ->assertEquals('+ Enter a new address', $options[2]
    ->getText());
  $this
    ->assertNotEmpty($options[1]
    ->getAttribute('selected'));

  // Confirm that a deleted address book profile is detected.
  $new_address_book_profile
    ->delete();
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
    ->id() . '/TRUE');
  $options = $this
    ->xpath('//select[@name="profile[select_address]"]/option');
  $this
    ->assertCount(3, $options);
  $this
    ->assertEquals('37 Rue du Sentier', $options[0]
    ->getText());
  $this
    ->assertEquals('Cetinjska 13', $options[1]
    ->getText());
  $this
    ->assertEquals('+ Enter a new address', $options[2]
    ->getText());
  $this
    ->assertNotEmpty($options[1]
    ->getAttribute('selected'));
  $this
    ->assertEquals('_original', $options[1]
    ->getValue());
  $rendered_address = [
    'country_code' => 'RS',
    'locality' => 'Belgrade',
    'address_line1' => 'Cetinjska 13',
    'given_name' => 'John',
    'family_name' => 'Smith',
  ];
  $this
    ->assertRenderedAddress($rendered_address);
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->checkboxNotChecked('profile[copy_to_address_book]');
  $this
    ->assertSession()
    ->pageTextContains("Save to the customer's address book");
}