You are here

function LanguageNegotiationInfoTest::testInfoAlterations in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/language/src/Tests/LanguageNegotiationInfoTest.php \Drupal\language\Tests\LanguageNegotiationInfoTest::testInfoAlterations()

Tests alterations to language types/negotiation info.

File

core/modules/language/src/Tests/LanguageNegotiationInfoTest.php, line 69
Contains \Drupal\language\Tests\LanguageNegotiationInfoTest.

Class

LanguageNegotiationInfoTest
Tests alterations to language types/negotiation info.

Namespace

Drupal\language\Tests

Code

function testInfoAlterations() {
  $this
    ->stateSet(array(
    // Enable language_test type info.
    'language_test.language_types' => TRUE,
    // Enable language_test negotiation info (not altered yet).
    'language_test.language_negotiation_info' => TRUE,
    // Alter LanguageInterface::TYPE_CONTENT to be configurable.
    'language_test.content_language_type' => TRUE,
  ));
  $this->container
    ->get('module_installer')
    ->install(array(
    'language_test',
  ));
  $this
    ->resetAll();

  // Check that fixed language types are properly configured without the need
  // of saving the language negotiation settings.
  $this
    ->checkFixedLanguageTypes();
  $type = LanguageInterface::TYPE_CONTENT;
  $language_types = $this
    ->languageManager()
    ->getLanguageTypes();
  $this
    ->assertTrue(in_array($type, $language_types), 'Content language type is configurable.');

  // Enable some core and custom language negotiation methods. The test
  // language type is supposed to be configurable.
  $test_type = 'test_language_type';
  $interface_method_id = LanguageNegotiationUI::METHOD_ID;
  $test_method_id = 'test_language_negotiation_method';
  $form_field = $type . '[enabled][' . $interface_method_id . ']';
  $edit = array(
    $form_field => TRUE,
    $type . '[enabled][' . $test_method_id . ']' => TRUE,
    $test_type . '[enabled][' . $test_method_id . ']' => TRUE,
    $test_type . '[configurable]' => TRUE,
  );
  $this
    ->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));

  // Alter language negotiation info to remove interface language negotiation
  // method.
  $this
    ->stateSet(array(
    'language_test.language_negotiation_info_alter' => TRUE,
  ));
  $negotiation = $this
    ->config('language.types')
    ->get('negotiation.' . $type . '.enabled');
  $this
    ->assertFalse(isset($negotiation[$interface_method_id]), 'Interface language negotiation method removed from the stored settings.');
  $this
    ->drupalGet('admin/config/regional/language/detection');
  $this
    ->assertNoFieldByName($form_field, NULL, 'Interface language negotiation method unavailable.');

  // Check that type-specific language negotiation methods can be assigned
  // only to the corresponding language types.
  foreach ($this
    ->languageManager()
    ->getLanguageTypes() as $type) {
    $form_field = $type . '[enabled][test_language_negotiation_method_ts]';
    if ($type == $test_type) {
      $this
        ->assertFieldByName($form_field, NULL, format_string('Type-specific test language negotiation method available for %type.', array(
        '%type' => $type,
      )));
    }
    else {
      $this
        ->assertNoFieldByName($form_field, NULL, format_string('Type-specific test language negotiation method unavailable for %type.', array(
        '%type' => $type,
      )));
    }
  }

  // Check language negotiation results.
  $this
    ->drupalGet('');
  $last = $this->container
    ->get('state')
    ->get('language_test.language_negotiation_last');
  foreach ($this
    ->languageManager()
    ->getDefinedLanguageTypes() as $type) {
    $langcode = $last[$type];
    $value = $type == LanguageInterface::TYPE_CONTENT || strpos($type, 'test') !== FALSE ? 'it' : 'en';
    $this
      ->assertEqual($langcode, $value, format_string('The negotiated language for %type is %language', array(
      '%type' => $type,
      '%language' => $value,
    )));
  }

  // Uninstall language_test and check that everything is set back to the
  // original status.
  $this->container
    ->get('module_installer')
    ->uninstall(array(
    'language_test',
  ));
  $this
    ->rebuildContainer();

  // Check that only the core language types are available.
  foreach ($this
    ->languageManager()
    ->getDefinedLanguageTypes() as $type) {
    $this
      ->assertTrue(strpos($type, 'test') === FALSE, format_string('The %type language is still available', array(
      '%type' => $type,
    )));
  }

  // Check that fixed language types are properly configured, even those
  // previously set to configurable.
  $this
    ->checkFixedLanguageTypes();

  // Check that unavailable language negotiation methods are not present in
  // the negotiation settings.
  $negotiation = $this
    ->config('language.types')
    ->get('negotiation.' . $type . '.enabled');
  $this
    ->assertFalse(isset($negotiation[$test_method_id]), 'The disabled test language negotiation method is not part of the content language negotiation settings.');

  // Check that configuration page presents the correct options and settings.
  $this
    ->assertNoRaw(t('Test language detection'), 'No test language type configuration available.');
  $this
    ->assertNoRaw(t('This is a test language negotiation method'), 'No test language negotiation method available.');
}