You are here

public function TranslationTest::testMultipleLanguages in Search API sorts 8

Test search_api_sorts with multiple languagesenabled.

File

tests/src/Functional/TranslationTest.php, line 103

Class

TranslationTest
Tests Search API sorts translation.

Namespace

Drupal\Tests\search_api_sorts\Functional

Code

public function testMultipleLanguages() {
  $this
    ->drupalGet('admin/config/search/search-api/index/' . $this->indexId . '/sorts/' . $this->escapedDisplayId);

  // Check if the translate warning is not shown.
  $this
    ->assertSession()
    ->pageTextNotContains('You are currently editing the English version of the search api sorts fields.');

  // Check if the translate column is present.
  $this
    ->assertSession()
    ->elementContains('css', 'table#edit-sorts thead th:last-child', 'Translate');

  // Check if translate link is not present.
  $this
    ->assertSession()
    ->linkByHrefNotExists(sprintf('admin/config/search/search-api/sorts/%s/translate', $this->escapedDisplayId . '_' . 'id'));
  $edit = [
    'sorts[id][status]' => TRUE,
    'default_sort' => 'id',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save settings');

  // Check if config is saved in the default language (EN).
  $search_api_sorts_field = $this->searchApiSortsFieldStorage
    ->load($this->escapedDisplayId . '_' . 'id');
  $this
    ->assertEquals('en', $search_api_sorts_field
    ->language()
    ->getId());

  // Check if translate link is present.
  $this
    ->assertSession()
    ->linkByHrefExists(sprintf('admin/config/search/search-api/sorts/%s/translate', $this->escapedDisplayId . '_' . 'id'));
  $this
    ->drupalGet('search-api-sorts-test');
  $this
    ->assertSession()
    ->linkNotExists('Identifiant');
  $this
    ->assertSession()
    ->linkExists('ID');

  // Switch to FR version.
  $this
    ->drupalGet('fr/admin/config/search/search-api/index/' . $this->indexId . '/sorts/' . $this->escapedDisplayId);

  // Check if the translate warning is shown.
  $this
    ->assertSession()
    ->pageTextContains('You are currently editing the English version of the search api sorts fields.');

  // Check if the translate column is present.
  $this
    ->assertSession()
    ->elementContains('css', 'table#edit-sorts thead th:last-child', 'Translate');
  $edit = [
    'sorts[created][status]' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save settings');

  // Check if ID field config is still saved in the default language.
  $search_api_sorts_field = $this->searchApiSortsFieldStorage
    ->load($this->escapedDisplayId . '_' . 'id');
  $this
    ->assertEquals('en', $search_api_sorts_field
    ->language()
    ->getId());

  // Check if created config is also saved in the default language.
  $search_api_sorts_field = $this->searchApiSortsFieldStorage
    ->load($this->escapedDisplayId . '_' . 'created');
  $this
    ->assertEquals('en', $search_api_sorts_field
    ->language()
    ->getId());

  // Translate the ID field.
  $this
    ->drupalPostForm('admin/config/search/search-api/sorts/' . $this->escapedDisplayId . '_' . 'id' . '/translate/fr/add', [
    'translation[config_names][search_api_sorts.search_api_sorts_field.' . $this->escapedDisplayId . '_' . 'id' . '][label]' => 'Identifiant',
  ], 'Save translation');

  // Visit the EN version of the search_api overview and check if the labels
  // are shown in the default language.
  $this
    ->drupalGet('search-api-sorts-test');
  $this
    ->assertSession()
    ->linkNotExists('Identifiant');
  $this
    ->assertSession()
    ->linkExists('ID');
  $this
    ->assertSession()
    ->linkExists('Authored on');

  // Visit the EN version of the search_api overview and check if the labels
  // are shown in the translated language.
  $this
    ->drupalGet('fr/search-api-sorts-test');
  $this
    ->assertSession()
    ->linkExists('Identifiant');
  $this
    ->assertSession()
    ->linkNotExists('ID');
  $this
    ->assertSession()
    ->linkExists('Authored on');
}