You are here

public function CustomerProfileTest::testSingleExisting in Commerce Core 8.2

Tests the address book in "single" mode, on an existing profile entity.

In "single" mode the default address book profile always contains the last entered address, requiring every customer edit to result in a copy, even if the profile was standalone, or referencing a deleted address book profile.

File

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

Class

CustomerProfileTest
Tests the customer_profile inline form.

Namespace

Drupal\Tests\commerce_order\FunctionalJavascript

Code

public function testSingleExisting() {

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

  // Standalone profile.
  $profile = Profile::create([
    'type' => 'customer',
    'uid' => 0,
    'address' => $this->frenchAddress,
  ]);
  $profile
    ->save();
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
    ->id());
  $this
    ->assertSession()
    ->fieldNotExists('select_address');

  // Confirm that the french address is shown rendered.
  $this
    ->assertRenderedAddress($this->frenchAddress);
  $this
    ->submitForm([], 'Submit');
  $this
    ->assertSession()
    ->pageTextContains('The street is "38 Rue du Sentier" and the country code is FR. Address book: No');

  // Confirm that the profile can be edited.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
    ->id());
  $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()
    ->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. Address book: Yes');
  $us_profile = Profile::create([
    'type' => 'customer',
    'uid' => $this->adminUser
      ->id(),
    'address' => $this->usAddress,
  ]);
  $us_profile
    ->save();
  $profile = Profile::create([
    'type' => 'customer',
    'uid' => 0,
  ]);
  $profile
    ->populateFromProfile($us_profile);
  $profile
    ->setData('address_book_profile_id', $us_profile
    ->id());
  $profile
    ->save();
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
    ->id());
  $this
    ->assertSession()
    ->fieldNotExists('select_address');

  // Confirm that the US address is shown rendered.
  $this
    ->assertRenderedAddress($this->usAddress);
  $this
    ->submitForm([], 'Submit');
  $this
    ->assertSession()
    ->pageTextContains('The street is "9 Drupal Ave" and the country code is US. Address book: No');

  // Confirm that the profile can be edited.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
    ->id());
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  foreach ($this->usAddress as $property => $value) {
    $this
      ->assertSession()
      ->fieldValueEquals("profile[address][0][address][{$property}]", $value);
  }
  $this
    ->assertSession()
    ->fieldNotExists('profile[copy_to_address_book]');
  $this
    ->submitForm([
    'profile[address][0][address][address_line1]' => '10 Drupal Ave',
  ], 'Submit');
  $this
    ->assertSession()
    ->pageTextContains('The street is "10 Drupal Ave" and the country code is US. Address book: Yes');

  // Populated from a deleted address book profile.
  $profile = Profile::create([
    'type' => 'customer',
    'uid' => 0,
    'address' => $this->frenchAddress,
  ]);
  $profile
    ->setData('address_book_profile_id', '999');
  $profile
    ->save();
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
    ->id());
  $this
    ->assertSession()
    ->fieldNotExists('select_address');
  $this
    ->assertRenderedAddress($this->frenchAddress);
  $this
    ->submitForm([], 'Submit');
  $this
    ->assertSession()
    ->pageTextContains('The street is "38 Rue du Sentier" and the country code is FR. Address book: No');

  // Confirm that it is possible to edit the profile.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
    ->id());
  $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()
    ->fieldNotExists('select_address');
  $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. Address book: Yes');
}