You are here

public function GeolocationJavascriptTest::testGoogleMapUsingCurrentLanguage in Geolocation Field 8

Same name and namespace in other branches
  1. 8.3 tests/src/FunctionalJavascript/GeolocationJavascriptTest.php \Drupal\Tests\geolocation\FunctionalJavascript\GeolocationJavascriptTest::testGoogleMapUsingCurrentLanguage()
  2. 8.2 tests/src/FunctionalJavascript/GeolocationJavascriptTest.php \Drupal\Tests\geolocation\FunctionalJavascript\GeolocationJavascriptTest::testGoogleMapUsingCurrentLanguage()

Tests the Use Current Language option from the settings.

Changes the language to French, checking for the French map.

File

tests/src/FunctionalJavascript/GeolocationJavascriptTest.php, line 137

Class

GeolocationJavascriptTest
Tests the JavaScript functionality.

Namespace

Drupal\Tests\geolocation\FunctionalJavascript

Code

public function testGoogleMapUsingCurrentLanguage() {

  // Log in as an administrator and change geolocation and language settings.
  $admin_user = $this
    ->drupalCreateUser([
    'configure geolocation',
    'administer languages',
    'access administration pages',
  ]);
  $this
    ->drupalLogin($admin_user);

  // Get the geolocation configuration settings page.
  $this
    ->drupalGetFilterGoogleKey('admin/config/services/geolocation');

  // Enable the checkbox to use current language.
  $edit = [
    'use_current_language' => 1,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save configuration'));

  // Add and set French as the language. See from LanguageSwitchingTest.
  $edit = [
    'predefined_langcode' => 'fr',
  ];
  $this
    ->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
  \Drupal::service('language.config_factory_override')
    ->getOverride('fr', 'language.entity.fr')
    ->set('label', 'français')
    ->save();

  // Enable URL language detection and selection.
  $edit = [
    'language_interface[enabled][language-url]' => '1',
  ];
  $this
    ->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
  $this
    ->drupalGetFilterGoogleKey('fr/node/4');
  $this
    ->assertSession()
    ->elementExists('css', 'html[lang="fr"]');
  $anchor = $this
    ->assertSession()
    ->waitForElement('css', 'a[href^="https://maps.google.com"][href*="hl="]', 3000);

  // TODO: Google maps loading is not reliable right now.
  if ($anchor) {

    // To control the test messages, search inside the anchor's href.
    // This is achieved by looking for the "hl" parameter in an anchor's href:
    // https://maps.google.com/maps?ll=54,49&z=10&t=m&hl=fr&gl=US&mapclient=apiv3
    $contains_french_link = strpos($anchor
      ->getAttribute('href'), 'hl=fr');
    if ($contains_french_link === FALSE) {
      $this
        ->fail('Did not find expected parameters from Google Maps link for French translation.');
    }
  }
}