You are here

public function AddressDefaultWidgetTest::testDefaultValue in Address 8

Tests the default value functionality.

File

tests/src/FunctionalJavascript/AddressDefaultWidgetTest.php, line 275

Class

AddressDefaultWidgetTest
Tests the default address widget.

Namespace

Drupal\Tests\address\FunctionalJavascript

Code

public function testDefaultValue() {
  $this
    ->drupalGet($this->fieldConfigUrl);

  // Confirm that the US is selected by default.
  $this
    ->assertSession()
    ->fieldValueEquals('default_value_input[field_address][0][address][country_code]', 'US');

  // Confirm that it is possible to switch the country to France.
  $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]');

  // Confirm that it is possible to fill-in only certain fields.
  $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]));

  // Confirm that the default value is used on the node form.
  $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]', '');
}