protected function setUp()
 {
     parent::setUp();
     // Set up an additional language.
     $this->langcodes = array(language_default()->getId(), 'es');
     ConfigurableLanguage::createFromLangcode('es')->save();
     // Create a content type.
     $this->bundle = $this->randomMachineName();
     $this->contentType = $this->drupalCreateContentType(array('type' => $this->bundle));
     // Enable translation for the current entity type and ensure the change is
     // picked up.
     content_translation_set_config('node', $this->bundle, 'enabled', TRUE);
     drupal_static_reset();
     \Drupal::entityManager()->clearCachedBundles();
     \Drupal::service('router.builder')->rebuild();
     // Add a translatable field to the content type.
     entity_create('field_storage_config', array('field_name' => 'field_test_text', 'entity_type' => 'node', 'type' => 'text', 'cardinality' => 1, 'translatable' => TRUE))->save();
     entity_create('field_config', array('entity_type' => 'node', 'field_name' => 'field_test_text', 'bundle' => $this->bundle, 'label' => 'Test text-field'))->save();
     entity_get_form_display('node', $this->bundle, 'default')->setComponent('field_test_text', array('type' => 'text_textfield', 'weight' => 0))->save();
     // Enable content translation.
     $configuration = array('langcode' => language_default()->getId(), 'language_show' => TRUE);
     language_save_default_configuration('node', $this->bundle, $configuration);
     // Create a translator user.
     $permissions = array('access contextual links', 'administer nodes', "edit any {$this->bundle} content", 'translate any entity');
     $this->translator = $this->drupalCreateUser($permissions);
 }
Exemplo n.º 2
0
 /**
  * Tests menu language settings and the defaults for menu link items.
  */
 function testMenuLanguage()
 {
     // Create a test menu to test the various language-related settings.
     // Machine name has to be lowercase.
     $menu_name = Unicode::strtolower($this->randomMachineName(16));
     $label = $this->randomString();
     $edit = array('id' => $menu_name, 'description' => '', 'label' => $label, 'langcode' => 'aa');
     $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
     language_save_default_configuration('menu_link_content', 'menu_link_content', array('langcode' => 'bb', 'language_show' => TRUE));
     // Check menu language.
     $this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The menu language was correctly selected.');
     // Test menu link language.
     $link_path = '<front>';
     // Add a menu link.
     $link_title = $this->randomString();
     $edit = array('title[0][value]' => $link_title, 'url' => $link_path);
     $this->drupalPostForm("admin/structure/menu/manage/{$menu_name}/add", $edit, t('Save'));
     // Check the link was added with the correct menu link default language.
     $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $link_title));
     $menu_link = reset($menu_links);
     $this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'route_name' => '<front>', 'langcode' => 'bb'));
     // Edit menu link default, changing it to cc.
     language_save_default_configuration('menu_link_content', 'menu_link_content', array('langcode' => 'cc', 'language_show' => TRUE));
     // Add a menu link.
     $link_title = $this->randomString();
     $edit = array('title[0][value]' => $link_title, 'url' => $link_path);
     $this->drupalPostForm("admin/structure/menu/manage/{$menu_name}/add", $edit, t('Save'));
     // Check the link was added with the correct new menu link default language.
     $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $link_title));
     $menu_link = reset($menu_links);
     $this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'route_name' => '<front>', 'langcode' => 'cc'));
     // Now change the language of the new link to 'bb'.
     $edit = array('langcode' => 'bb');
     $this->drupalPostForm('admin/structure/menu/item/' . $menu_link->id() . '/edit', $edit, t('Save'));
     $this->assertMenuLink($menu_link->getPluginId(), array('menu_name' => $menu_name, 'route_name' => '<front>', 'langcode' => 'bb'));
     // Saving menu link items ends up on the edit menu page. To check the menu
     // link has the correct language default on edit, go to the menu link edit
     // page first.
     $this->drupalGet('admin/structure/menu/item/' . $menu_link->id() . '/edit');
     // Check that the language selector has the correct default value.
     $this->assertOptionSelected('edit-langcode', 'bb', 'The menu link language was correctly selected.');
     // Edit menu to hide the language select on menu link item add.
     language_save_default_configuration('menu_link_content', 'menu_link_content', array('langcode' => 'cc', 'language_show' => FALSE));
     // Check that the language selector is not available on menu link add page.
     $this->drupalGet("admin/structure/menu/manage/{$menu_name}/add");
     $this->assertNoField('edit-langcode', 'The language selector field was hidden the page');
 }
 /**
  * Creates a new node content type.
  *
  * @param name
  *   The content type name.
  * @param $langcode
  *   Default language code of the nodes of this type.
  */
 protected function createContentType($name, $langcode)
 {
     $content_type = $this->container->get('entity.manager')->getStorage('node_type')->create(array('name' => 'Test ' . $name, 'title_label' => 'Title', 'type' => $name, 'create_body' => FALSE));
     $content_type->save();
     language_save_default_configuration('node', $name, array('langcode' => $langcode, 'language_show' => FALSE));
 }
 /**
  * Tests that the language_get_default_langcode() returns the correct values.
  */
 public function testDefaultLangcode()
 {
     // Add some custom languages.
     foreach (array('aa', 'bb', 'cc') as $language_code) {
         $language = new Language(array('id' => $language_code, 'name' => $this->randomName()));
         language_save($language);
     }
     // Fixed language.
     language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => 'bb', 'language_show' => TRUE));
     $langcode = language_get_default_langcode('custom_type', 'custom_bundle');
     $this->assertEqual($langcode, 'bb');
     // Current interface.
     language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => 'current_interface', 'language_show' => TRUE));
     $langcode = language_get_default_langcode('custom_type', 'custom_bundle');
     $language_interface = \Drupal::languageManager()->getCurrentLanguage();
     $this->assertEqual($langcode, $language_interface->id);
     // Site's default.
     $old_default = \Drupal::languageManager()->getDefaultLanguage();
     // Ensure the language entity default value is correct.
     $language_entity = entity_load('language_entity', $old_default->getId());
     $this->assertTrue($language_entity->get('default'), 'The en language entity is flagged as the default language.');
     $old_default->default = FALSE;
     language_save($old_default);
     $new_default = \Drupal::languageManager()->getLanguage('cc');
     $new_default->default = TRUE;
     language_save($new_default);
     language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => 'site_default', 'language_show' => TRUE));
     $langcode = language_get_default_langcode('custom_type', 'custom_bundle');
     $this->assertEqual($langcode, 'cc');
     // Ensure the language entity default value is correct.
     $language_entity = entity_load('language_entity', $old_default->getId());
     $this->assertFalse($language_entity->get('default'), 'The en language entity is not flagged as the default language.');
     $language_entity = entity_load('language_entity', 'cc');
     // Check calling the Drupal\language\Entity\Language::isDefault() method
     // directly.
     $this->assertTrue($language_entity->isDefault(), 'The cc language entity is flagged as the default language.');
     // Check the default value of a language field when authors preferred option
     // is selected.
     // Create first an user and assign a preferred langcode to him.
     $some_user = $this->drupalCreateUser();
     $some_user->preferred_langcode = 'bb';
     $some_user->save();
     $this->drupalLogin($some_user);
     language_save_default_configuration('custom_type', 'some_bundle', array('langcode' => 'authors_default', 'language_show' => TRUE));
     $this->drupalGet('language-tests/language_configuration_element_test');
     $this->assertOptionSelected('edit-langcode', 'bb');
 }