View source
<?php
namespace Drupal\Tests\search\Functional;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
class SearchLanguageTest extends BrowserTestBase {
protected static $modules = [
'language',
'node',
'search',
];
protected $defaultTheme = 'stark';
protected $searchableNodes;
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$test_user = $this
->drupalCreateUser([
'access content',
'search content',
'use advanced search',
'administer nodes',
'administer languages',
'access administration pages',
'administer site configuration',
]);
$this
->drupalLogin($test_user);
ConfigurableLanguage::createFromLangcode('es')
->save();
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$field_storage
->setTranslatable(TRUE);
$field_storage
->save();
$default_format = filter_default_format();
$nodes = [
[
'title' => 'First node en',
'type' => 'page',
'body' => [
[
'value' => $this
->randomMachineName(32),
'format' => $default_format,
],
],
'langcode' => 'en',
],
[
'title' => 'Second node this is the Spanish title',
'type' => 'page',
'body' => [
[
'value' => $this
->randomMachineName(32),
'format' => $default_format,
],
],
'langcode' => 'es',
],
[
'title' => 'Third node en',
'type' => 'page',
'body' => [
[
'value' => $this
->randomMachineName(32),
'format' => $default_format,
],
],
'langcode' => 'en',
],
];
$this->searchableNodes = [];
foreach ($nodes as $setting) {
$this->searchableNodes[] = $this
->drupalCreateNode($setting);
}
$translation = $this->searchableNodes[1]
->addTranslation('en', [
'title' => 'Second node en',
]);
$translation->body->value = $this
->randomMachineName(32);
$this->searchableNodes[1]
->save();
$translation = $this->searchableNodes[2]
->addTranslation('es', [
'title' => 'Third node es',
]);
$translation->body->value = $this
->randomMachineName(32);
$this->searchableNodes[2]
->save();
$plugin = $this->container
->get('plugin.manager.search')
->createInstance('node_search');
$plugin
->updateIndex();
}
public function testLanguages() {
$edit = [
'predefined_langcode' => 'fr',
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$this
->assertSession()
->pageTextContains('French');
$this
->drupalGet('search/node');
$this
->assertSession()
->pageTextContains('Languages');
$this
->assertSession()
->pageTextContains('English');
$this
->assertSession()
->pageTextContains('French');
$this
->drupalGet('search/node');
$this
->submitForm([], 'edit-submit--2');
$this
->assertSession()
->addressEquals(Url::fromRoute('search.view_node_search', [], [
'query' => [
'keys' => '',
],
]));
$edit = [
'language[fr]' => TRUE,
];
$this
->drupalGet('search/node');
$this
->submitForm($edit, 'edit-submit--2');
$url = $this
->getUrl();
$parts = parse_url($url);
$query_string = isset($parts['query']) ? rawurldecode($parts['query']) : '';
$this
->assertStringContainsString('=language:fr', $query_string, 'Language filter language:fr add to the query string.');
$edit = [
'keys' => 'node',
'language[es]' => TRUE,
];
$this
->drupalGet('search/node');
$this
->submitForm($edit, 'edit-submit--2');
$this
->assertSession()
->linkExists('Second node this is the Spanish title', 0, 'Second node Spanish title found in search results');
$this
->assertSession()
->linkExists('Third node es', 0, 'Third node Spanish found in search results');
$this
->assertSession()
->linkNotExists('First node en', 'Search results do not contain first English node');
$this
->assertSession()
->linkNotExists('Second node en', 'Search results do not contain second English node');
$this
->assertSession()
->linkNotExists('Third node en', 'Search results do not contain third English node');
$path = 'admin/config/regional/language';
$this
->drupalGet($path);
$this
->assertSession()
->checkboxChecked('edit-site-default-language-en');
$edit = [
'site_default_language' => 'fr',
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save configuration');
$this
->assertSession()
->checkboxNotChecked('edit-site-default-language-en');
$this
->drupalGet('admin/config/regional/language/delete/en');
$this
->submitForm([], 'Delete');
}
public function testLanguageAttributes() {
$this
->drupalGet('search/node');
$this
->submitForm([
'keys' => 'the Spanish title',
], 'Search');
$node = $this->searchableNodes[1]
->getTranslation('es');
$this
->assertSession()
->elementExists('xpath', '//div[@class="layout-content"]//ol/li/h3[contains(@lang, "es")]');
$this
->assertSession()
->elementTextEquals('xpath', '//div[@class="layout-content"]//ol/li/h3[contains(@lang, "es")]/a', $node
->getTitle());
$this
->assertSession()
->elementExists('xpath', '//div[@class="layout-content"]//ol/li/p[contains(@lang, "es")]');
$this
->drupalGet('es/search/node');
$this
->submitForm([
'keys' => 'First node',
], 'Search');
$this
->assertSession()
->elementExists('xpath', '//div[@class="layout-content"]//ol/li/h3[contains(@lang, "en")]');
$node = $this->searchableNodes[0]
->getTranslation('en');
$this
->assertSession()
->elementTextEquals('xpath', '//div[@class="layout-content"]//ol/li/h3[contains(@lang, "en")]/a', $node
->getTitle());
$this
->assertSession()
->elementExists('xpath', '//div[@class="layout-content"]//ol/li/p[contains(@lang, "en")]');
}
}