/** * Tests language fallback candidates. */ public function testCandidates() { $language_list = $this->languageManager->getLanguages(); $expected = array_keys($language_list + array(LanguageInterface::LANGCODE_NOT_SPECIFIED => NULL)); // Check that language fallback candidates by default are all the available // languages sorted by weight. $candidates = $this->languageManager->getFallbackCandidates(); $this->assertEqual(array_values($candidates), $expected, 'Language fallback candidates are properly returned.'); // Check that candidates are alterable. $this->state->set('language_test.fallback_alter.candidates', TRUE); $expected = array_slice($expected, 0, count($expected) - 1); $candidates = $this->languageManager->getFallbackCandidates(); $this->assertEqual(array_values($candidates), $expected, 'Language fallback candidates are alterable.'); // Check that candidates are alterable for specific operations. $this->state->set('language_test.fallback_alter.candidates', FALSE); $this->state->set('language_test.fallback_operation_alter.candidates', TRUE); $expected[] = LanguageInterface::LANGCODE_NOT_SPECIFIED; $expected[] = LanguageInterface::LANGCODE_NOT_APPLICABLE; $candidates = $this->languageManager->getFallbackCandidates(NULL, array('operation' => 'test')); $this->assertEqual(array_values($candidates), $expected, 'Language fallback candidates are alterable for specific operations.'); // Check that when the site is monolingual no language fallback is applied. $default_langcode = $this->languageManager->getDefaultLanguage()->id; foreach ($language_list as $langcode => $language) { if ($langcode != $default_langcode) { language_delete($langcode); } } $candidates = $this->languageManager->getFallbackCandidates(); $this->assertEqual(array_values($candidates), array(LanguageInterface::LANGCODE_DEFAULT), 'Language fallback is not applied when the Language module is not enabled.'); }
/** * Test dependency injected Language object against a new default language * object. * * @see \Drupal\Core\Language\Language */ function testDependencyInjectedNewDefaultLanguage() { $default_language = \Drupal::languageManager()->getDefaultLanguage(); // Change the language default object to different values. $new_language_default = new Language(array('id' => 'fr', 'name' => 'French', 'direction' => Language::DIRECTION_LTR, 'weight' => 0, 'method_id' => 'language-default', 'default' => TRUE)); language_save($new_language_default); // The language system creates a Language object which contains the // same properties as the new default language object. $result = \Drupal::languageManager()->getCurrentLanguage(); $this->assertIdentical($result->id, 'fr'); // Delete the language to check that we fallback to the default. try { language_delete('fr'); $this->fail('Expected DeleteDefaultLanguageException thrown.'); } catch (DeleteDefaultLanguageException $e) { $this->pass('Expected DeleteDefaultLanguageException thrown.'); } // Re-save the previous default language and the delete should work. language_save($default_language); language_delete('fr'); $result = \Drupal::languageManager()->getCurrentLanguage(); $this->assertIdentical($result->id, $default_language->id); }
/** * Tests translating the site name. */ function testSiteNameTranslation() { $adminUser = $this->drupalCreateUser(array('administer site configuration', 'administer languages')); $this->drupalLogin($adminUser); // Add a custom lanugage. $langcode = 'xx'; $name = $this->randomName(16); $edit = array('predefined_langcode' => 'custom', 'langcode' => $langcode, 'name' => $name, 'direction' => LanguageInterface::DIRECTION_LTR); $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language')); \Drupal::languageManager()->getLanguageConfigOverride($langcode, 'system.site')->set('name', 'XX site name')->save(); $this->drupalLogout(); // The home page in English should not have the override. $this->drupalGet(''); $this->assertNoText('XX site name'); // During path resolution the system.site configuration object is used to // determine the front page. This occurs before language negotiation causing // the configuration factory to cache an object without the correct // overrides. We are testing that the configuration factory is // re-initialised after language negotiation. Ensure that it applies when // we access the XX front page. // @see \Drupal\Core\PathProcessor::processInbound() $this->drupalGet('xx'); $this->assertText('XX site name'); // Set the xx language to be the default language and delete the English // language so the site is no longer multilingual and confirm configuration // overrides still work. $language_manager = \Drupal::languageManager()->reset(); $this->assertTrue($language_manager->isMultilingual(), 'The test site is multilingual.'); $language = \Drupal::languageManager()->getLanguage('xx'); $language->default = TRUE; language_save($language); language_delete('en'); $this->assertFalse($language_manager->isMultilingual(), 'The test site is monolingual.'); $this->drupalGet('xx'); $this->assertText('XX site name'); }
/** * Tests menu configuration is still English after English has been deleted. */ function testMenuLanguageRemovedEnglish() { // Create a test menu to test language settings. // Machine name has to be lowercase. $menu_name = Unicode::strtolower($this->randomMachineName(16)); $edit = array('id' => $menu_name, 'description' => '', 'label' => $this->randomString(), 'langcode' => 'en'); $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save')); // Check that the language settings were saved. $menu = Menu::load($menu_name); $this->assertEqual($menu->language()->getId(), 'en'); // Remove English language. To do that another language has to be set as // default. $language = language_load('cs'); $language->default = TRUE; language_save($language); language_delete('en'); // Save the menu again and check if the language is still the same. $this->drupalPostForm("admin/structure/menu/manage/{$menu_name}", array(), t('Save')); $menu = Menu::load($menu_name); $this->assertEqual($menu->language()->getId(), 'en'); }
/** * {@inheritdoc} */ public function submit(array $form, FormStateInterface $form_state) { // @todo This should be replaced with $this->entity->delete() when the // additional logic in language_delete() is ported. $success = language_delete($this->entity->id()); if ($success) { drupal_set_message($this->t('The %language (%langcode) language has been removed.', array('%language' => $this->entity->label(), '%langcode' => $this->entity->id()))); } $form_state->setRedirectUrl($this->getCancelUrl()); }