View source
<?php
namespace Drupal\Tests\search_api_sorts\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
class TranslationTest extends SortsFunctionalBase {
public static $modules = [
'config_translation',
'language',
];
protected $adminUserPermissions = [
'administer search_api',
'access administration pages',
'translate configuration',
];
protected $searchApiSortsFieldStorage;
public function setUp() : void {
parent::setUp();
$entity_type = $this->container
->get('entity_type.manager')
->getDefinition('configurable_language');
$this->container
->get('entity_type.listener')
->onEntityTypeCreate($entity_type);
ConfigurableLanguage::createFromLangcode('fr')
->save();
$this
->config('language.negotiation')
->set('url.source', 'path_prefix')
->set('url.prefixes', [
'en' => 'en',
'fr' => 'fr',
])
->save();
$block_settings = [
'region' => 'footer',
'id' => 'sorts_id',
];
$this
->drupalPlaceBlock('search_api_sorts_block:' . $this->displayId, $block_settings);
$this->searchApiSortsFieldStorage = $this->container
->get('entity_type.manager')
->getStorage('search_api_sorts_field');
}
public function testSingleLanguage() {
$language = ConfigurableLanguage::load('fr');
$language
->delete();
$this
->drupalGet('admin/config/search/search-api/index/' . $this->indexId . '/sorts/' . $this->escapedDisplayId);
$this
->assertSession()
->pageTextNotContains('You are currently editing the English version of the search api sorts fields.');
$this
->assertSession()
->elementNotContains('css', 'table#edit-sorts thead th:last-child', 'Translate');
$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');
$search_api_sorts_field = $this->searchApiSortsFieldStorage
->load($this->escapedDisplayId . '_' . 'id');
$this
->assertEquals('en', $search_api_sorts_field
->language()
->getId());
$this
->assertSession()
->linkByHrefNotExists(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');
}
public function testMultipleLanguages() {
$this
->drupalGet('admin/config/search/search-api/index/' . $this->indexId . '/sorts/' . $this->escapedDisplayId);
$this
->assertSession()
->pageTextNotContains('You are currently editing the English version of the search api sorts fields.');
$this
->assertSession()
->elementContains('css', 'table#edit-sorts thead th:last-child', 'Translate');
$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');
$search_api_sorts_field = $this->searchApiSortsFieldStorage
->load($this->escapedDisplayId . '_' . 'id');
$this
->assertEquals('en', $search_api_sorts_field
->language()
->getId());
$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');
$this
->drupalGet('fr/admin/config/search/search-api/index/' . $this->indexId . '/sorts/' . $this->escapedDisplayId);
$this
->assertSession()
->pageTextContains('You are currently editing the English version of the search api sorts fields.');
$this
->assertSession()
->elementContains('css', 'table#edit-sorts thead th:last-child', 'Translate');
$edit = [
'sorts[created][status]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, 'Save settings');
$search_api_sorts_field = $this->searchApiSortsFieldStorage
->load($this->escapedDisplayId . '_' . 'id');
$this
->assertEquals('en', $search_api_sorts_field
->language()
->getId());
$search_api_sorts_field = $this->searchApiSortsFieldStorage
->load($this->escapedDisplayId . '_' . 'created');
$this
->assertEquals('en', $search_api_sorts_field
->language()
->getId());
$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');
$this
->drupalGet('search-api-sorts-test');
$this
->assertSession()
->linkNotExists('Identifiant');
$this
->assertSession()
->linkExists('ID');
$this
->assertSession()
->linkExists('Authored on');
$this
->drupalGet('fr/search-api-sorts-test');
$this
->assertSession()
->linkExists('Identifiant');
$this
->assertSession()
->linkNotExists('ID');
$this
->assertSession()
->linkExists('Authored on');
}
}