You are here

public function AdministrativeAreaFilterTest::testExposedCountryFilterAdministrativeAreaOptions in Address 8

Test options for administrative area using an exposed country filter.

File

tests/src/Functional/Views/AdministrativeAreaFilterTest.php, line 98

Class

AdministrativeAreaFilterTest
Tests the administrative area Views filter for Address fields.

Namespace

Drupal\Tests\address\Functional\Views

Code

public function testExposedCountryFilterAdministrativeAreaOptions() {
  $view = Views::getView('address_test_filter_administrative_area');
  $filters = $view
    ->getDisplay()
    ->getOption('filters');
  $filters['field_address_test_administrative_area']['country']['country_source'] = 'filter';
  $filters['field_address_test_administrative_area']['country']['country_filter_id'] = 'field_address_test_country_code';
  $view
    ->getDisplay()
    ->overrideOption('filters', $filters);
  $view
    ->save();

  // With no country selected, the administrative area shouldn't exist.
  $this
    ->drupalGet('address-test/views/filter-administrative-area');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->fieldNotExists('field_address_test_administrative_area');

  // For a country without admin areas, the filter still shouldn't exist.
  $options = [
    'query' => [
      'field_address_test_country_code' => 'CR',
    ],
  ];
  $this
    ->drupalGet('address-test/views/filter-administrative-area', $options);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->fieldNotExists('field_address_test_administrative_area');

  // For countries with admin areas, validate the options.
  foreach ([
    'BR',
    'EG',
    'MX',
    'US',
  ] as $country) {
    $options = [
      'query' => [
        'field_address_test_country_code' => $country,
      ],
    ];
    $this
      ->drupalGet('address-test/views/filter-administrative-area', $options);
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->assertSession()
      ->fieldExists('field_address_test_administrative_area');
    $this
      ->assertAdministrativeAreaOptions($country);
  }
}