You are here

public function CustomerProfileTest::testSingleAdministrator in Commerce Core 8.2

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

File

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

Class

CustomerProfileTest
Tests the customer_profile inline form.

Namespace

Drupal\Tests\commerce_order\FunctionalJavascript

Code

public function testSingleAdministrator() {

  /** @var \Drupal\profile\Entity\ProfileTypeInterface $profile_type */
  $profile_type = ProfileType::load('customer');
  $profile_type
    ->setMultiple(FALSE);
  $profile_type
    ->save();

  // 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
    ->assertSession()
    ->fieldNotExists('select_address');
  $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]' => '1',
  ], '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
    ->assertSession()
    ->fieldNotExists('select_address');
  $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("Also update the address in the customer's address book");
  $this
    ->submitForm([
    'profile[address][0][address][address_line1]' => '37 Rue du Sentier',
    'profile[copy_to_address_book]' => TRUE,
  ], '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());

  // Confirm that a deleted address book profile is detected.
  $address_book_profile
    ->delete();
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
    ->id() . '/TRUE');
  $rendered_address = [
    'address_line1' => '37 Rue du Sentier',
  ] + $this->frenchAddress;
  $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");
}