function TranslationTest::testLanguageSwitcherBlockIntegration

Tests that the language switcher block alterations work as intended.

File

drupal/core/modules/translation/lib/Drupal/translation/Tests/TranslationTest.php, line 191
Definition of Drupal\translation\Tests\TranslationTest.

Class

TranslationTest
Functional tests for the Translation module.

Namespace

Drupal\translation\Tests

Code

function testLanguageSwitcherBlockIntegration() {

  // Add Italian to have three items in the language switcher block.
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->addLanguage('it');
  $this
    ->resetCaches();
  $this
    ->drupalLogin($this->translator);

  // Create a Basic page in English.
  $type = 'block-language';
  $node = $this
    ->createPage($this
    ->randomName(), $this
    ->randomName(), 'en');
  $this
    ->assertLanguageSwitchLinks($node, $node, TRUE, $type);
  $this
    ->assertLanguageSwitchLinks($node, $this
    ->emptyNode('es'), TRUE, $type);
  $this
    ->assertLanguageSwitchLinks($node, $this
    ->emptyNode('it'), TRUE, $type);

  // Create the Spanish translation.
  $translation_es = $this
    ->createTranslation($node, $this
    ->randomName(), $this
    ->randomName(), 'es');
  $this
    ->assertLanguageSwitchLinks($node, $node, TRUE, $type);
  $this
    ->assertLanguageSwitchLinks($node, $translation_es, TRUE, $type);
  $this
    ->assertLanguageSwitchLinks($node, $this
    ->emptyNode('it'), TRUE, $type);

  // Create the Italian translation.
  $translation_it = $this
    ->createTranslation($node, $this
    ->randomName(), $this
    ->randomName(), 'it');
  $this
    ->assertLanguageSwitchLinks($node, $node, TRUE, $type);
  $this
    ->assertLanguageSwitchLinks($node, $translation_es, TRUE, $type);
  $this
    ->assertLanguageSwitchLinks($node, $translation_it, TRUE, $type);

  // Create a language neutral node and check that the language switcher is
  // left untouched.
  $node2 = $this
    ->createPage($this
    ->randomName(), $this
    ->randomName(), Language::LANGCODE_NOT_SPECIFIED);
  $node2_en = (object) array(
    'nid' => $node2->nid,
    'langcode' => 'en',
  );
  $node2_es = (object) array(
    'nid' => $node2->nid,
    'langcode' => 'es',
  );
  $node2_it = (object) array(
    'nid' => $node2->nid,
    'langcode' => 'it',
  );
  $this
    ->assertLanguageSwitchLinks($node2_en, $node2_en, TRUE, $type);
  $this
    ->assertLanguageSwitchLinks($node2_en, $node2_es, TRUE, $type);
  $this
    ->assertLanguageSwitchLinks($node2_en, $node2_it, TRUE, $type);

  // Disable translation support to check that the language switcher is left
  // untouched only for new nodes.
  $this
    ->drupalLogin($this->admin_user);
  $edit = array(
    'language_configuration[language_show]' => FALSE,
    'node_type_language_translation_enabled' => FALSE,
  );
  $this
    ->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  $this
    ->drupalLogin($this->translator);

  // Existing translations trigger alterations even if translation support is
  // disabled.
  $this
    ->assertLanguageSwitchLinks($node, $node, TRUE, $type);
  $this
    ->assertLanguageSwitchLinks($node, $translation_es, TRUE, $type);
  $this
    ->assertLanguageSwitchLinks($node, $translation_it, TRUE, $type);

  // Check that new nodes with a language assigned do not trigger language
  // switcher alterations when translation support is disabled.
  $node = $this
    ->createPage($this
    ->randomName(), $this
    ->randomName());
  $node_es = (object) array(
    'nid' => $node->nid,
    'langcode' => 'es',
  );
  $node_it = (object) array(
    'nid' => $node->nid,
    'langcode' => 'it',
  );
  $this
    ->assertLanguageSwitchLinks($node, $node, TRUE, $type);
  $this
    ->assertLanguageSwitchLinks($node, $node_es, TRUE, $type);
  $this
    ->assertLanguageSwitchLinks($node, $node_it, TRUE, $type);
}