View source
<?php
namespace Drupal\Tests\commerce_order\FunctionalJavascript;
use Drupal\profile\Entity\Profile;
use Drupal\profile\Entity\ProfileType;
class CustomerProfileTest extends OrderWebDriverTestBase {
protected $emptyAddress = [
'country_code' => 'RS',
'locality' => '',
'postal_code' => '',
'address_line1' => '',
'given_name' => '',
'family_name' => '',
];
protected $frenchAddress = [
'country_code' => 'FR',
'locality' => 'Paris',
'postal_code' => '75002',
'address_line1' => '38 Rue du Sentier',
'given_name' => 'Leon',
'family_name' => 'Blum',
];
protected $usAddress = [
'country_code' => 'US',
'administrative_area' => 'SC',
'locality' => 'Greenville',
'postal_code' => '29616',
'address_line1' => '9 Drupal Ave',
'given_name' => 'Bryan',
'family_name' => 'Centarro',
];
protected $huAddress = [
'country_code' => 'HU',
'locality' => 'Vecsés',
'postal_code' => '',
'address_line1' => 'Árpád u. 6.',
'given_name' => 'Péter',
'family_name' => 'Tordai',
];
protected function setUp() : void {
parent::setUp();
$this->store
->set('address', [
'country_code' => 'RS',
'postal_code' => '11000',
'locality' => 'Belgrade',
'address_line1' => 'Cetinjska 15',
]);
$this->store
->save();
}
public function testCountries() {
$this
->drupalGet('/commerce_order_test/customer_profile_test_form');
$options = $this
->xpath('//select[@name="profile[address][0][address][country_code]"]/option');
$this
->assertCount(4, $options);
$this
->assertEquals('FR', $options[0]
->getValue());
$this
->assertEquals('HU', $options[1]
->getValue());
$this
->assertEquals('RS', $options[2]
->getValue());
$this
->assertEquals('US', $options[3]
->getValue());
$this
->assertNotEmpty($options[2]
->getAttribute('selected'));
$this
->getSession()
->getPage()
->fillField('profile[address][0][address][country_code]', 'FR');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->submitForm([
'profile[address][0][address][given_name]' => 'Leon',
'profile[address][0][address][family_name]' => 'Blum',
'profile[address][0][address][address_line1]' => '38 Rue du Sentier',
'profile[address][0][address][postal_code]' => '75002',
'profile[address][0][address][locality]' => 'Paris',
], 'Submit');
$this
->assertSession()
->pageTextContains('The street is "38 Rue du Sentier" and the country code is FR.');
}
public function testMultipleNew() {
$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();
$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'));
$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');
$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
->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');
$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');
$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');
$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');
$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);
$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');
}
public function testMultipleExisting() {
$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');
$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');
$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()
->checkboxNotChecked('profile[copy_to_address_book]');
$this
->assertSession()
->pageTextContains('Save to my 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: No');
$profile
->setData('copy_to_address_book', TRUE);
$profile
->save();
$this
->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
->id());
$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: Yes');
$this
->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
->id());
$this
->getSession()
->getPage()
->pressButton('billing_edit');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->checkboxChecked('profile[copy_to_address_book]');
$this
->assertSession()
->pageTextContains('Save to my address book');
$this
->submitForm([
'profile[copy_to_address_book]' => FALSE,
], 'Submit');
$this
->assertSession()
->pageTextContains('The street is "38 Rue du Sentier" and the country code is FR. Address book: No');
$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();
$profile = Profile::create([
'type' => 'customer',
'uid' => 0,
'address' => $this->frenchAddress,
]);
$profile
->populateFromProfile($french_profile);
$profile
->setData('address_book_profile_id', $french_profile
->id());
$profile
->save();
$this
->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
->id());
$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[1]
->getAttribute('selected'));
$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');
$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');
$profile = Profile::create([
'type' => 'customer',
'uid' => 0,
'address' => $this->frenchAddress,
]);
$profile
->setData('address_book_profile_id', $french_profile
->id());
$profile
->save();
$new_french_address = [
'country_code' => 'FR',
'locality' => 'Paris',
'postal_code' => '75002',
'address_line1' => '39 Rue du Sentier',
'given_name' => 'Leon',
'family_name' => 'Blum',
];
$french_profile
->set('address', $new_french_address);
$french_profile
->save();
$this
->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
->id());
$options = $this
->xpath('//select[@name="profile[select_address]"]/option');
$this
->assertCount(4, $options);
$this
->assertEquals($this->usAddress['address_line1'], $options[0]
->getText());
$this
->assertEquals($new_french_address['address_line1'], $options[1]
->getText());
$this
->assertEquals($this->frenchAddress['address_line1'], $options[2]
->getText());
$this
->assertEquals('+ Enter a new address', $options[3]
->getText());
$this
->assertNotEmpty($options[2]
->getAttribute('selected'));
$this
->assertEquals('_original', $options[2]
->getValue());
$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');
$this
->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
->id());
$this
->getSession()
->getPage()
->fillField('profile[select_address]', $french_profile
->id());
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertRenderedAddress($new_french_address);
$this
->submitForm([], 'Submit');
$this
->assertSession()
->pageTextContains('The street is "39 Rue du Sentier" and the country code is FR. Address book: No');
$this
->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
->id());
$this
->getSession()
->getPage()
->fillField('profile[select_address]', $french_profile
->id());
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertRenderedAddress($new_french_address);
$this
->getSession()
->getPage()
->fillField('profile[select_address]', '_original');
$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');
$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: No');
$profile = Profile::create([
'type' => 'customer',
'uid' => 0,
'address' => $this->frenchAddress,
]);
$profile
->setData('address_book_profile_id', $french_profile
->id());
$profile
->save();
$new_french_address = [
'country_code' => 'FR',
'locality' => 'Paris',
'postal_code' => '75002',
'address_line1' => '38 Rue du Sentier',
'given_name' => 'Leon',
'family_name' => 'Leon',
];
$french_profile
->set('address', $new_french_address);
$french_profile
->save();
$this
->drupalGet('/commerce_order_test/customer_profile_test_form/' . $profile
->id());
$options = $this
->xpath('//select[@name="profile[select_address]"]/option');
$this
->assertCount(4, $options);
$this
->assertEquals($this->usAddress['address_line1'], $options[0]
->getText());
$this
->assertEquals($this->frenchAddress['address_line1'], $options[1]
->getText());
$this
->assertEquals($this->frenchAddress['address_line1'] . ' (current version)', $options[2]
->getText());
$this
->assertEquals('+ Enter a new address', $options[3]
->getText());
$this
->assertNotEmpty($options[2]
->getAttribute('selected'));
$this
->assertEquals('_original', $options[2]
->getValue());
$this
->assertRenderedAddress($new_french_address);
$this
->submitForm([], 'Submit');
$this
->assertSession()
->pageTextContains('The street is "38 Rue du Sentier" and the country code is FR. Address book: No');
$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: No');
$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()
->fieldValueEquals('profile[select_address]', '_original');
$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');
$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()
->checkboxNotChecked('profile[copy_to_address_book]');
$this
->assertSession()
->pageTextContains('Save to my 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: No');
}
public function testMultipleAdministrator() {
$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.');
$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()));
$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);
}
$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.');
$profile = $this
->reloadEntity($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());
$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');
$profile = $this
->reloadEntity($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);
$this
->assertEquals('37 Rue du Sentier', $address_book_profile
->get('address')
->first()
->getAddressLine1());
$this
->assertNotEquals($new_address_book_profile
->id(), $address_book_profile
->id());
$this
->assertEquals('Cetinjska 13', $profile
->get('address')
->first()
->getAddressLine1());
$this
->assertEquals('Cetinjska 13', $new_address_book_profile
->get('address')
->first()
->getAddressLine1());
$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'));
$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");
}
public function testSingleNew() {
$profile_type = ProfileType::load('customer');
$profile_type
->setMultiple(FALSE);
$profile_type
->save();
$this
->drupalGet('/commerce_order_test/customer_profile_test_form');
foreach ($this->emptyAddress 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][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');
$default_profile = Profile::create([
'type' => 'customer',
'uid' => $this->adminUser
->id(),
'address' => $this->frenchAddress,
]);
$default_profile
->save();
$this
->drupalGet('/commerce_order_test/customer_profile_test_form');
$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');
$this
->drupalGet('/commerce_order_test/customer_profile_test_form');
$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');
}
public function testSingleExisting() {
$profile_type = ProfileType::load('customer');
$profile_type
->setMultiple(FALSE);
$profile_type
->save();
$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');
$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');
$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');
$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');
$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');
$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');
$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');
}
public function testSingleAdministrator() {
$profile_type = ProfileType::load('customer');
$profile_type
->setMultiple(FALSE);
$profile_type
->save();
$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.');
$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()));
$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.');
$profile = $this
->reloadEntity($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());
$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");
}
public function testAnonymous() {
$this
->drupalLogout();
$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()
->fieldNotExists('profile[copy_to_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');
$profile_type = ProfileType::load('customer');
$profile_type
->setMultiple(FALSE);
$profile_type
->save();
$this
->drupalGet('/commerce_order_test/customer_profile_test_form');
foreach ($this->emptyAddress 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][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');
}
public function testIncompleteProfiles() {
$us_profile = Profile::create([
'type' => 'customer',
'uid' => $this->adminUser
->id(),
'address' => $this->usAddress,
]);
$us_profile
->save();
$empty_profile = Profile::create([
'type' => 'customer',
'uid' => $this->adminUser
->id(),
'address' => $this->emptyAddress,
]);
$empty_profile
->save();
$hu_profile = Profile::create([
'type' => 'customer',
'uid' => $this->adminUser
->id(),
'address' => $this->huAddress,
]);
$hu_profile
->save();
$this
->drupalGet('/commerce_order_test/customer_profile_test_form/' . $us_profile
->id() . '/TRUE');
$this
->assertRenderedAddress($this->usAddress);
$this
->getSession()
->getPage()
->fillField('profile[select_address]', $empty_profile
->id());
$this
->assertSession()
->assertWaitOnAjaxRequest();
foreach ($this->emptyAddress as $property => $value) {
$this
->assertSession()
->fieldValueEquals("profile[address][0][address][{$property}]", $value);
}
$this
->getSession()
->getPage()
->fillField('profile[select_address]', $hu_profile
->id());
$this
->assertSession()
->assertWaitOnAjaxRequest();
foreach ($this->huAddress as $property => $value) {
$this
->assertSession()
->fieldValueEquals("profile[address][0][address][{$property}]", $value);
}
$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
->getSession()
->getPage()
->fillField('profile[select_address]', $us_profile
->id());
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertRenderedAddress($this->usAddress);
}
}