You are here

public function AdministrativeAreaFilterTest::testContextualCountryFilterAdministrativeAreaOptions in Address 8

Test options for administrative area using a contextual country filter.

File

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

Class

AdministrativeAreaFilterTest
Tests the administrative area Views filter for Address fields.

Namespace

Drupal\Tests\address\Functional\Views

Code

public function testContextualCountryFilterAdministrativeAreaOptions() {
  $view = Views::getView('address_test_filter_administrative_area');
  $filters = $view
    ->getDisplay()
    ->getOption('filters');
  $filters['field_address_test_administrative_area']['country']['country_source'] = 'argument';
  $filters['field_address_test_administrative_area']['country']['country_argument_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.
  $this
    ->drupalGet('address-test/views/filter-administrative-area/CR');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->fieldNotExists('field_address_test_administrative_area');

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