You are here

public function AddressDefaultWidgetTest::testEvents in Address 8

Tests the alter events.

File

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

Class

AddressDefaultWidgetTest
Tests the default address widget.

Namespace

Drupal\Tests\address\FunctionalJavascript

Code

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

  // The address_test module is installed here, not in setUp().
  // This way the module's events will not affect other tests.
  self::$modules[] = 'address_test';
  $this->container
    ->get('module_installer')
    ->install(self::$modules);
  $this->container = $this->kernel
    ->rebuildContainer();

  // Confirm that the list of available countries was altered.
  $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);

  // Confirm that counties for Great Britain were added.
  $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);

  // Uninstall and remove the address_test module.
  $this->container
    ->get('module_installer')
    ->uninstall([
    'address_test',
  ]);
  $this->container = $this->kernel
    ->rebuildContainer();
  array_pop(self::$modules);
}