View source
<?php
namespace Drupal\Tests\address\FunctionalJavascript;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\node\Entity\NodeType;
class AddressDefaultWidgetTest extends WebDriverTestBase {
protected static $modules = [
'system',
'language',
'user',
'field',
'field_ui',
'node',
'address',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $field;
protected $formDisplay;
protected $nodeAddUrl;
protected $fieldConfigUrl;
protected $countryRepository;
protected $subdivisionRepository;
protected $addressFormatRepository;
protected function setUp() : void {
parent::setUp();
$type = NodeType::create([
'name' => 'Article',
'type' => 'article',
]);
$type
->save();
$this->adminUser = $this
->createUser([
'create article content',
'edit own article content',
'administer content types',
'administer node fields',
]);
$this
->drupalLogin($this->adminUser);
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_address',
'entity_type' => 'node',
'type' => 'address',
]);
$field_storage
->save();
$this->field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
'label' => 'Address',
'default_value' => [
[
'country_code' => 'US',
],
],
]);
$this->field
->save();
$this->formDisplay = EntityFormDisplay::load('node.article.default');
if (!$this->formDisplay) {
EntityFormDisplay::create([
'targetEntityType' => 'node',
'bundle' => 'article',
'mode' => 'default',
'status' => TRUE,
])
->save();
$this->formDisplay = EntityFormDisplay::load('node.article.default');
}
$this->formDisplay
->setComponent($this->field
->getName(), [
'type' => 'address_default',
])
->save();
$this->nodeAddUrl = 'node/add/article';
$this->fieldConfigUrl = 'admin/structure/types/manage/article/fields/node.article.' . $this->field
->getName();
$this->countryRepository = \Drupal::service('address.country_repository');
$this->subdivisionRepository = \Drupal::service('address.subdivision_repository');
$this->addressFormatRepository = \Drupal::service('address.address_format_repository');
}
public function testCountries() {
$field_name = $this->field
->getName();
$this
->drupalGet($this->nodeAddUrl);
$this
->assertEmpty((bool) $this
->xpath('//select[@name="' . $field_name . '[0][address][country_code]" and boolean(@required)]'), 'Country is shown as optional.');
$this
->assertOptionSelected($field_name . '[0][address][country_code]', 'US', 'The configured default_country is selected.');
$this->field
->setRequired(TRUE);
$this->field
->save();
$this
->drupalGet($this->nodeAddUrl);
$this
->assertNotEmpty((bool) $this
->xpath('//select[@name="' . $field_name . '[0][address][country_code]" and boolean(@required)]'), 'Country is shown as required.');
$this
->assertOptionSelected($field_name . '[0][address][country_code]', 'US', 'The configured default_country is selected.');
$countries = array_keys($this->countryRepository
->getList());
$this
->assertOptions($field_name . '[0][address][country_code]', $countries, 'All countries are present.');
$countries = [
'US',
'FR',
'BR',
'JP',
];
$edit = [];
$edit['settings[available_countries][]'] = array_map(function ($country) {
return $country;
}, $countries);
$this
->drupalGet($this->fieldConfigUrl);
$this
->submitForm($edit, t('Save settings'));
$this
->drupalGet($this->nodeAddUrl);
$this
->assertOptions($field_name . '[0][address][country_code]', $countries, 'The restricted list of available countries is present.');
$country_code = 'US';
$this
->getSession()
->getPage()
->fillField($field_name . '[0][address][country_code]', 'US');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$address = [
'given_name' => 'John',
'family_name' => 'Smith',
'organization' => 'Some Organization',
'address_line1' => '1098 Alta Ave',
'locality' => 'Mountain View',
'administrative_area' => 'CA',
'postal_code' => '94043',
];
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName(8);
foreach ($address as $property => $value) {
$path = $field_name . '[0][address][' . $property . ']';
$edit[$path] = $value;
}
$this
->submitForm($edit, t('Save'));
$node = $this
->getNodeByTitle($edit['title[0][value]']);
$this
->assertNotEmpty($node, 'Created article ' . $edit['title[0][value]']);
$countries = [
'FR',
];
$edit = [];
$edit['settings[available_countries][]'] = array_map(function ($country) {
return $country;
}, $countries);
$this
->drupalGet($this->fieldConfigUrl);
$this
->submitForm($edit, 'Save settings');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][given_name]', $address['given_name']);
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][family_name]', $address['family_name']);
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][organization]', $address['organization']);
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][address_line1]', $address['address_line1']);
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][locality]', $address['locality']);
$this
->assertOptionSelected($field_name . '[0][address][administrative_area]', $address['administrative_area']);
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][postal_code]', $address['postal_code']);
$this
->assertOptionSelected($field_name . '[0][address][country_code]', $country_code);
$this
->getSession()
->getPage()
->fillField($field_name . '[0][address][country_code]', 'FR');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldNotExists($field_name . '[0][address][administrative_area]');
$this
->getSession()
->getPage()
->fillField($field_name . '[0][address][country_code]', 'US');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldExists($field_name . '[0][address][administrative_area]');
$countries = [
'US',
];
$edit = [];
$edit['settings[available_countries][]'] = array_map(function ($country) {
return $country;
}, $countries);
$this
->drupalGet($this->fieldConfigUrl);
$this
->submitForm($edit, 'Save settings');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->fieldNotExists($field_name . '[0][address][country_code]');
$this
->submitForm([], t('Save'));
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][given_name]', $address['given_name']);
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][family_name]', $address['family_name']);
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][organization]', $address['organization']);
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][address_line1]', $address['address_line1']);
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][locality]', $address['locality']);
$this
->assertOptionSelected($field_name . '[0][address][administrative_area]', $address['administrative_area']);
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][postal_code]', $address['postal_code']);
}
public function testDefaultValue() {
$this
->drupalGet($this->fieldConfigUrl);
$this
->assertSession()
->fieldValueEquals('default_value_input[field_address][0][address][country_code]', 'US');
$this
->getSession()
->getPage()
->fillField('default_value_input[field_address][0][address][country_code]', 'FR');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldNotExists('default_value_input[field_address][0][address][administrative_area]');
$edit = [
'default_value_input[field_address][0][address][given_name]' => 'John',
'default_value_input[field_address][0][address][family_name]' => 'Smith',
];
$this
->submitForm($edit, t('Save settings'));
$this
->assertSession()
->pageTextContains('Saved Address configuration.');
$this->container
->get('entity_type.manager')
->getStorage('field_config')
->resetCache();
$this->field = FieldConfig::load($this->field
->id());
$default_value = $this->field
->getDefaultValueLiteral();
$expected_default_value = [
'country_code' => 'FR',
'given_name' => 'John',
'family_name' => 'Smith',
];
$this
->assertCount(1, $default_value);
$this
->assertEquals($expected_default_value, array_filter($default_value[0]));
$this
->drupalGet($this->nodeAddUrl);
$this
->assertSession()
->fieldValueEquals('field_address[0][address][country_code]', 'FR');
$this
->assertSession()
->fieldValueEquals('field_address[0][address][given_name]', 'John');
$this
->assertSession()
->fieldValueEquals('field_address[0][address][family_name]', 'Smith');
$this
->assertSession()
->fieldValueEquals('field_address[0][address][postal_code]', '');
}
public function testEvents() {
$field_name = $this->field
->getName();
self::$modules[] = 'address_test';
$this->container
->get('module_installer')
->install(self::$modules);
$this->container = $this->kernel
->rebuildContainer();
$subscriber = \Drupal::service('address_test.event_subscriber');
$available_countries = array_keys($subscriber
->getAvailableCountries());
$this
->drupalGet($this->nodeAddUrl);
$this
->assertOptions($field_name . '[0][address][country_code]', $available_countries);
$expected_counties = [
'Anglesey',
'Blaenau Gwent',
'Bridgend',
'Caerphilly',
'Cardiff',
'Carmarthenshire',
'Ceredigion',
'Conwy',
'Denbighshire',
'Flintshire',
'Gwynedd',
'Merthyr Tydfil',
'Monmouthshire',
'Neath Port Talbot',
'Newport',
'Pembrokeshire',
'Powys',
'Rhondda Cynon Taf',
'Swansea',
'Tarfaen',
'Vale of Glamorgan',
'Wrexham',
];
$this
->drupalGet($this->nodeAddUrl);
$this
->getSession()
->getPage()
->fillField($field_name . '[0][address][country_code]', 'GB');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains(t('County'));
$this
->assertSession()
->fieldExists($field_name . '[0][address][administrative_area]');
$this
->assertOptions($field_name . '[0][address][administrative_area]', $expected_counties);
$this->container
->get('module_installer')
->uninstall([
'address_test',
]);
$this->container = $this->kernel
->rebuildContainer();
array_pop(self::$modules);
}
public function testFields() {
$field_name = $this->field
->getName();
$all_fields = [
'administrativeArea' => $field_name . '[0][address][administrative_area]',
'locality' => $field_name . '[0][address][locality]',
'dependentLocality' => $field_name . '[0][address][dependent_locality]',
'postalCode' => $field_name . '[0][address][postal_code]',
'sortingCode' => $field_name . '[0][address][sorting_code]',
'addressLine1' => $field_name . '[0][address][address_line1]',
'addressLine2' => $field_name . '[0][address][address_line2]',
'organization' => $field_name . '[0][address][organization]',
'givenName' => $field_name . '[0][address][given_name]',
'familyName' => $field_name . '[0][address][family_name]',
];
$this
->drupalGet($this->nodeAddUrl);
foreach ([
'US',
'FR',
'CN',
] as $country) {
$address_format = $this->addressFormatRepository
->get($country);
$used_fields = $address_format
->getUsedFields();
$this
->getSession()
->getPage()
->fillField($field_name . '[0][address][country_code]', $country);
$this
->assertSession()
->assertWaitOnAjaxRequest();
$elements = $this
->xpath('//input[starts-with(@name,"' . $field_name . '")] | //select[starts-with(@name,"' . $field_name . '")]');
$form_fields = [];
foreach ($elements as $element) {
if ($field = array_search($element
->getAttribute('name'), $all_fields)) {
$form_fields[] = $field;
}
}
$this
->assertFieldValues($used_fields, $form_fields, 'Expected fields ' . implode(', ', $used_fields) . ' exists for country ' . $country . ", only found " . implode(', ', $form_fields));
}
$edit = [
'settings[field_overrides][givenName][override]' => 'optional',
'settings[field_overrides][familyName][override]' => 'optional',
'settings[field_overrides][organization][override]' => 'required',
'settings[field_overrides][postalCode][override]' => 'hidden',
];
$this
->drupalGet($this->fieldConfigUrl);
$this
->submitForm($edit, t('Save settings'));
$this
->drupalGet($this->nodeAddUrl);
$this
->assertEmpty((bool) $this
->xpath('//input[@name="field_address[0][address][given_name]" and contains(@required, "required")]'));
$this
->assertEmpty((bool) $this
->xpath('//input[@name="field_address[0][address][family_name]" and contains(@required, "required")]'));
$this
->assertNotEmpty((bool) $this
->xpath('//input[@name="field_address[0][address][organization]" and contains(@required, "required")]'));
$this
->assertEmpty((bool) $this
->xpath('//input[@name="field_address[0][address][postal_code]"]'));
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName(8);
$this
->getSession()
->getPage()
->fillField($field_name . '[0][address][country_code]', 'US');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$edit[$field_name . '[0][address][organization]'] = 'Some Organization';
$edit[$field_name . '[0][address][address_line1]'] = '1098 Alta Ave';
$edit[$field_name . '[0][address][address_line2]'] = 'Street 2';
$edit[$field_name . '[0][address][locality]'] = 'Mountain View';
$edit[$field_name . '[0][address][administrative_area]'] = 'CA';
$this
->submitForm($edit, t('Save'));
$node = $this
->getNodeByTitle($edit['title[0][value]']);
$this
->assertNotEmpty($node, 'Created article ' . $edit['title[0][value]']);
}
public function testSubdivisions() {
$field_name = $this->field
->getName();
$country = 'CN';
$administrative_area = 'Hebei Sheng';
$locality = 'Chengde Shi';
$administrative_areas = $this->subdivisionRepository
->getList([
$country,
]);
$localities = $this->subdivisionRepository
->getList([
$country,
$administrative_area,
]);
$dependent_localities = $this->subdivisionRepository
->getList([
$country,
$administrative_area,
$locality,
]);
$this
->drupalGet($this->nodeAddUrl);
$this
->getSession()
->getPage()
->fillField($field_name . '[0][address][country_code]', $country);
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertOptions($field_name . '[0][address][administrative_area]', array_keys($administrative_areas), 'All administrative areas for country ' . $country . ' are present.');
$this
->getSession()
->getPage()
->fillField($field_name . '[0][address][administrative_area]', $administrative_area);
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertOptionSelected($field_name . '[0][address][administrative_area]', $administrative_area, 'Selected administrative area ' . $administrative_areas[$administrative_area]);
$this
->assertOptions($field_name . '[0][address][locality]', array_keys($localities), 'All localities for administrative area ' . $administrative_areas[$administrative_area] . ' are present.');
$this
->getSession()
->getPage()
->fillField($field_name . '[0][address][locality]', $locality);
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertOptionSelected($field_name . '[0][address][locality]', $locality, 'Selected locality ' . $localities[$locality]);
$this
->assertOptions($field_name . '[0][address][dependent_locality]', array_keys($dependent_localities), 'All dependent localities for locality ' . $localities[$locality] . ' are present.');
}
public function testClearValues() {
$field_name = $this->field
->getName();
$this
->drupalGet($this->nodeAddUrl);
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName(8);
$this
->getSession()
->getPage()
->fillField($field_name . '[0][address][country_code]', 'US');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$edit[$field_name . '[0][address][given_name]'] = 'John';
$edit[$field_name . '[0][address][family_name]'] = 'Smith';
$edit[$field_name . '[0][address][organization]'] = 'Some Organization';
$edit[$field_name . '[0][address][address_line1]'] = '1098 Alta Ave';
$edit[$field_name . '[0][address][address_line2]'] = 'Street 2';
$edit[$field_name . '[0][address][locality]'] = 'Mountain View';
$edit[$field_name . '[0][address][administrative_area]'] = 'CA';
$edit[$field_name . '[0][address][postal_code]'] = '94043';
$this
->submitForm($edit, t('Save'));
$node = $this
->getNodeByTitle($edit['title[0][value]']);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][country_code]', 'US');
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][administrative_area]', 'CA');
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][locality]', 'Mountain View');
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][postal_code]', '94043');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->getSession()
->getPage()
->fillField($field_name . '[0][address][country_code]', 'CN');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->submitForm([], t('Save'));
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][country_code]', 'CN');
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][administrative_area]', '');
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][locality]', '');
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][dependent_locality]', '');
$this
->assertSession()
->fieldValueEquals($field_name . '[0][address][postal_code]', '');
}
protected function assertOptions($id, array $options, $message = '') {
$elements = $this
->xpath('//select[@name="' . $id . '"]/option');
$found_options = [];
foreach ($elements as $element) {
if ($option = $element
->getValue()) {
$found_options[] = $option;
}
}
$this
->assertFieldValues($found_options, $options, $message);
}
protected function assertOptionSelected($id, $option, $message = '') {
$elements = $this
->xpath('//select[@name=:id]//option[@value=:option]', [
':id' => $id,
':option' => $option,
]);
foreach ($elements as $element) {
$this
->assertNotEmpty($element
->isSelected(), $message ? $message : new FormattableMarkup('Option @option for field @id is selected.', [
'@option' => $option,
'@id' => $id,
]));
}
}
protected function assertFieldValues(array $field_values, array $expected_values, $message = '') {
$valid = TRUE;
if (count($field_values) == count($expected_values)) {
foreach ($expected_values as $value) {
if (!in_array($value, $field_values)) {
$valid = FALSE;
break;
}
}
}
else {
$valid = FALSE;
}
$this
->assertTrue($valid, $message);
}
}