function NodeTranslationUITest::testTranslationRendering

Same name and namespace in other branches
  1. 8.9.x core/modules/node/tests/src/Functional/NodeTranslationUITest.php \Drupal\Tests\node\Functional\NodeTranslationUITest::testTranslationRendering()
  2. 10 core/modules/node/tests/src/Functional/NodeTranslationUITest.php \Drupal\Tests\node\Functional\NodeTranslationUITest::testTranslationRendering()
  3. 11.x core/modules/node/tests/src/Functional/NodeTranslationUITest.php \Drupal\Tests\node\Functional\NodeTranslationUITest::testTranslationRendering()

Tests that translations are rendered properly.

File

core/modules/node/tests/src/Functional/NodeTranslationUITest.php, line 313

Class

NodeTranslationUITest
Tests the Node Translation UI.

Namespace

Drupal\Tests\node\Functional

Code

public function testTranslationRendering() {
    $default_langcode = $this->langcodes[0];
    $values[$default_langcode] = $this->getNewEntityValues($default_langcode);
    $this->entityId = $this->createEntity($values[$default_langcode], $default_langcode);
    $node = \Drupal::entityTypeManager()->getStorage($this->entityTypeId)
        ->load($this->entityId);
    $node->setPromoted(TRUE);
    // Create translations.
    foreach (array_diff($this->langcodes, [
        $default_langcode,
    ]) as $langcode) {
        $values[$langcode] = $this->getNewEntityValues($langcode);
        $translation = $node->addTranslation($langcode, $values[$langcode]);
        // Publish and promote the translation to frontpage.
        $translation->setPromoted(TRUE);
        $translation->setPublished();
    }
    $node->save();
    // Test that the frontpage view displays the correct translations.
    \Drupal::service('module_installer')->install([
        'views',
    ], TRUE);
    $this->rebuildContainer();
    $this->doTestTranslations('node', $values);
    // Enable the translation language renderer.
    $view = \Drupal::entityTypeManager()->getStorage('view')
        ->load('frontpage');
    $display =& $view->getDisplay('default');
    $display['display_options']['rendering_language'] = '***LANGUAGE_entity_translation***';
    $view->save();
    // Need to check from the beginning, including the base_path, in the url
    // since the pattern for the default language might be a substring of
    // the strings for other languages.
    $base_path = base_path();
    // Check the frontpage for 'Read more' links to each translation.
    // See also assertTaxonomyPage() in NodeAccessBaseTableTest.
    $node_href = 'node/' . $node->id();
    foreach ($this->langcodes as $langcode) {
        $this->drupalGet('node', [
            'language' => \Drupal::languageManager()->getLanguage($langcode),
        ]);
        $num_match_found = 0;
        if ($langcode == 'en') {
            // Site default language does not have langcode prefix in the URL.
            $expected_href = $base_path . $node_href;
        }
        else {
            $expected_href = $base_path . $langcode . '/' . $node_href;
        }
        $pattern = '|^' . $expected_href . '$|';
        foreach ($this->xpath("//a[text()='Read more']") as $link) {
            if (preg_match($pattern, $link->getAttribute('href'), $matches) == TRUE) {
                $num_match_found++;
            }
        }
        $this->assertSame(1, $num_match_found, 'There is 1 Read more link, ' . $expected_href . ', for the ' . $langcode . ' translation of a node on the frontpage. (Found ' . $num_match_found . '.)');
    }
    // Check the frontpage for 'Add new comment' links that include the
    // language.
    $comment_form_href = 'node/' . $node->id() . '#comment-form';
    foreach ($this->langcodes as $langcode) {
        $this->drupalGet('node', [
            'language' => \Drupal::languageManager()->getLanguage($langcode),
        ]);
        $num_match_found = 0;
        if ($langcode == 'en') {
            // Site default language does not have langcode prefix in the URL.
            $expected_href = $base_path . $comment_form_href;
        }
        else {
            $expected_href = $base_path . $langcode . '/' . $comment_form_href;
        }
        $pattern = '|^' . $expected_href . '$|';
        foreach ($this->xpath("//a[text()='Add new comment']") as $link) {
            if (preg_match($pattern, $link->getAttribute('href'), $matches) == TRUE) {
                $num_match_found++;
            }
        }
        $this->assertSame(1, $num_match_found, 'There is 1 Add new comment link, ' . $expected_href . ', for the ' . $langcode . ' translation of a node on the frontpage. (Found ' . $num_match_found . '.)');
    }
    // Test that the node page displays the correct translations.
    $this->doTestTranslations('node/' . $node->id(), $values);
    // Test that the node page has the correct alternate hreflang links.
    $this->doTestAlternateHreflangLinks($node);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.