/**
  * Check URL rewriting for the given language.
  *
  * The test is performed with a fixed URL (the default front page) to simply
  * check that language prefixes are not added to it and that the prefixed URL
  * is actually not working.
  *
  * @param \Drupal\Core\Language\LanguageInterface $language
  *   The language object.
  * @param string $message1
  *   Message to display in assertion that language prefixes are not added.
  * @param string $message2
  *   The message to display confirming prefixed URL is not working.
  */
 private function checkUrl(LanguageInterface $language, $message1, $message2)
 {
     $options = array('language' => $language, 'script' => '');
     $base_path = trim(base_path(), '/');
     $rewritten_path = trim(str_replace($base_path, '', \Drupal::url('<front>', array(), $options)), '/');
     $segments = explode('/', $rewritten_path, 2);
     $prefix = $segments[0];
     $path = isset($segments[1]) ? $segments[1] : $prefix;
     // If the rewritten URL has not a language prefix we pick a random prefix so
     // we can always check the prefixed URL.
     $prefixes = language_negotiation_url_prefixes();
     $stored_prefix = isset($prefixes[$language->getId()]) ? $prefixes[$language->getId()] : $this->randomMachineName();
     if ($this->assertNotEqual($stored_prefix, $prefix, $message1)) {
         $prefix = $stored_prefix;
     }
     $this->drupalGet("{$prefix}/{$path}");
     $this->assertResponse(404, $message2);
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     global $base_url;
     $config = $this->config('language.negotiation');
     $form['language_negotiation_url_part'] = array('#title' => $this->t('Part of the URL that determines language'), '#type' => 'radios', '#options' => array(LanguageNegotiationUrl::CONFIG_PATH_PREFIX => $this->t('Path prefix'), LanguageNegotiationUrl::CONFIG_DOMAIN => $this->t('Domain')), '#default_value' => $config->get('url.source'));
     $form['prefix'] = array('#type' => 'details', '#tree' => TRUE, '#title' => $this->t('Path prefix configuration'), '#open' => TRUE, '#description' => $this->t('Language codes or other custom text to use as a path prefix for URL language detection. For the default language, this value may be left blank. <strong>Modifying this value may break existing URLs. Use with caution in a production environment.</strong> Example: Specifying "deutsch" as the path prefix code for German results in URLs like "example.com/deutsch/contact".'), '#states' => array('visible' => array(':input[name="language_negotiation_url_part"]' => array('value' => (string) LanguageNegotiationUrl::CONFIG_PATH_PREFIX))));
     $form['domain'] = array('#type' => 'details', '#tree' => TRUE, '#title' => $this->t('Domain configuration'), '#open' => TRUE, '#description' => $this->t('The domain names to use for these languages. <strong>Modifying this value may break existing URLs. Use with caution in a production environment.</strong> Example: Specifying "de.example.com" as language domain for German will result in an URL like "http://de.example.com/contact".'), '#states' => array('visible' => array(':input[name="language_negotiation_url_part"]' => array('value' => (string) LanguageNegotiationUrl::CONFIG_DOMAIN))));
     $languages = language_list();
     $prefixes = language_negotiation_url_prefixes();
     $domains = language_negotiation_url_domains();
     foreach ($languages as $langcode => $language) {
         $t_args = array('%language' => $language->name, '%langcode' => $language->getId());
         $form['prefix'][$langcode] = array('#type' => 'textfield', '#title' => $language->isDefault() ? $this->t('%language (%langcode) path prefix (Default language)', $t_args) : $this->t('%language (%langcode) path prefix', $t_args), '#maxlength' => 64, '#default_value' => isset($prefixes[$langcode]) ? $prefixes[$langcode] : '', '#field_prefix' => $base_url . '/');
         $form['domain'][$langcode] = array('#type' => 'textfield', '#title' => $this->t('%language (%langcode) domain', array('%language' => $language->name, '%langcode' => $language->getId())), '#maxlength' => 128, '#default_value' => isset($domains[$langcode]) ? $domains[$langcode] : '');
     }
     $form_state->setRedirect('language.negotiation');
     return parent::buildForm($form, $form_state);
 }
 /**
  * Test that comment language is properly set.
  */
 function testCommentLanguage()
 {
     // Create two nodes, one for english and one for french, and comment each
     // node using both english and french as content language by changing URL
     // language prefixes. Meanwhile interface language is always French, which
     // is the user language preference. This way we can ensure that node
     // language and interface language do not influence comment language, as
     // only content language has to.
     foreach ($this->container->get('language_manager')->getLanguages() as $node_langcode => $node_language) {
         // Create "Article" content.
         $title = $this->randomMachineName();
         $edit = array('title[0][value]' => $title, 'body[0][value]' => $this->randomMachineName(), 'langcode' => $node_langcode, 'comment[0][status]' => CommentItemInterface::OPEN);
         $this->drupalPostForm("node/add/article", $edit, t('Save'));
         $node = $this->drupalGetNodeByTitle($title);
         $prefixes = language_negotiation_url_prefixes();
         foreach ($this->container->get('language_manager')->getLanguages() as $langcode => $language) {
             // Post a comment with content language $langcode.
             $prefix = empty($prefixes[$langcode]) ? '' : $prefixes[$langcode] . '/';
             $comment_values[$node_langcode][$langcode] = $this->randomMachineName();
             $edit = array('subject[0][value]' => $this->randomMachineName(), 'comment_body[0][value]' => $comment_values[$node_langcode][$langcode]);
             $this->drupalPostForm($prefix . 'node/' . $node->id(), $edit, t('Preview'));
             $this->drupalPostForm(NULL, $edit, t('Save'));
             // Check that comment language matches the current content language.
             $cid = db_select('comment_field_data', 'c')->fields('c', array('cid'))->condition('entity_id', $node->id())->condition('entity_type', 'node')->condition('field_name', 'comment')->condition('default_langcode', 1)->orderBy('cid', 'DESC')->range(0, 1)->execute()->fetchField();
             $comment = Comment::load($cid);
             $args = array('%node_language' => $node_langcode, '%comment_language' => $comment->langcode->value, '%langcode' => $langcode);
             $this->assertEqual($comment->langcode->value, $langcode, format_string('The comment posted with content language %langcode and belonging to the node with language %node_language has language %comment_language', $args));
             $this->assertEqual($comment->comment_body->value, $comment_values[$node_langcode][$langcode], 'Comment body correctly stored.');
         }
     }
     // Check that comment bodies appear in the administration UI.
     $this->drupalGet('admin/content/comment');
     foreach ($comment_values as $node_values) {
         foreach ($node_values as $value) {
             $this->assertRaw($value);
         }
     }
 }