You are here

public function AddressDefaultWidgetTest::testFields in Address 8

Tests expected and overridden fields.

File

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

Class

AddressDefaultWidgetTest
Tests the default address widget.

Namespace

Drupal\Tests\address\FunctionalJavascript

Code

public function testFields() {
  $field_name = $this->field
    ->getName();

  // Keys are field names from the field instance.
  // Values are corresponding field names from add article form.
  $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]',
  ];

  // US has all fields except sorting code and dependent locality.
  // France has sorting code, and China has dependent locality, so these
  // countries cover all fields.
  $this
    ->drupalGet($this->nodeAddUrl);
  foreach ([
    'US',
    'FR',
    'CN',
  ] as $country) {

    /** @var \CommerceGuys\Addressing\AddressFormat\AddressFormat $address_format */
    $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();

    // Compare the found fields to the address format.
    // Make one assert instead of many asserts for each field's existence.
    $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));
  }

  // Test field overrides.
  $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]"]'));

  // Test creating an address without the optional and hidden fields.
  $edit = [];
  $edit['title[0][value]'] = $this
    ->randomMachineName(8);

  // Use javascript to fill country_code so other fields can be loaded.
  $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]']);
}