You are here

public function CustomerProfileTest::testMultipleNew in Commerce Core 8.2

Tests the address book in "multiple" mode, on a new profile entity.

File

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

Class

CustomerProfileTest
Tests the customer_profile inline form.

Namespace

Drupal\Tests\commerce_order\FunctionalJavascript

Code

public function testMultipleNew() {

  // Test the initial state, with no address book profiles available.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form');
  $this
    ->assertSession()
    ->fieldNotExists('select_address');
  foreach ($this->emptyAddress as $property => $value) {
    $this
      ->assertSession()
      ->fieldValueEquals("profile[address][0][address][{$property}]", $value);
  }
  $this
    ->assertSession()
    ->checkboxChecked('profile[copy_to_address_book]');
  $this
    ->assertSession()
    ->pageTextContains('Save to my 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',
  ], 'Submit');
  $this
    ->assertSession()
    ->pageTextContains('The street is "Cetinjska 13" and the country code is RS. Address book: Yes');
  $us_profile = Profile::create([
    'type' => 'customer',
    'uid' => $this->adminUser
      ->id(),
    'address' => $this->usAddress,
  ]);
  $us_profile
    ->save();
  sleep(1);
  $french_profile = Profile::create([
    'type' => 'customer',
    'uid' => $this->adminUser
      ->id(),
    'address' => $this->frenchAddress,
  ]);
  $french_profile
    ->save();

  // Confirm that the US profile is first and selected, since it is
  // the default.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form');
  $options = $this
    ->xpath('//select[@name="profile[select_address]"]/option');
  $this
    ->assertCount(3, $options);
  $this
    ->assertEquals($this->usAddress['address_line1'], $options[0]
    ->getText());
  $this
    ->assertEquals($this->frenchAddress['address_line1'], $options[1]
    ->getText());
  $this
    ->assertEquals('+ Enter a new address', $options[2]
    ->getText());
  $this
    ->assertNotEmpty($options[0]
    ->getAttribute('selected'));

  // Confirm that the US profile 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 it is possible to edit and submit the US profile.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  foreach ($this->usAddress as $property => $value) {
    $this
      ->assertSession()
      ->fieldValueEquals("profile[address][0][address][{$property}]", $value);
  }

  // The checkbox should be hidden and TRUE for every edit operation.
  $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');

  // Confirm that selecting "Enter a new address" clears the form.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form');
  $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
    ->getSession()
    ->getPage()
    ->fillField('profile[select_address]', '_new');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->saveHtmlOutput();
  foreach ($this->emptyAddress as $property => $value) {
    $this
      ->assertSession()
      ->fieldValueEquals("profile[address][0][address][{$property}]", $value);
  }
  $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',
  ], 'Submit');
  $this
    ->assertSession()
    ->pageTextContains('The street is "Cetinjska 13" and the country code is RS. Address book: Yes');

  // Confirm that it is possible to select the French profile.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form');
  $this
    ->getSession()
    ->getPage()
    ->fillField('profile[select_address]', $french_profile
    ->id());
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $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 select and edit the French profile.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form');
  $this
    ->getSession()
    ->getPage()
    ->fillField('profile[select_address]', $french_profile
    ->id());
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $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');

  // Confirm that selecting a different address reverts to render mode.
  $this
    ->drupalGet('/commerce_order_test/customer_profile_test_form');
  $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
    ->getSession()
    ->getPage()
    ->fillField('profile[select_address]', $french_profile
    ->id());
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRenderedAddress($this->frenchAddress);

  // Confirm that it is possible to add a new profile.
  $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()
    ->checkboxChecked('profile[copy_to_address_book]');
  $this
    ->assertSession()
    ->pageTextContains('Save to my 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',
  ], 'Submit');
  $this
    ->assertSession()
    ->pageTextContains('The street is "Cetinjska 13" and the country code is RS. Address book: Yes');
}