View source
<?php
namespace Drupal\Tests\facets\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
class LanguageIntegrationTest extends FacetsTestBase {
public static $modules = [
'views',
'search_api',
'facets',
'block',
'facets_search_api_dependency',
'language',
'config_translation',
];
public function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer search_api',
'administer facets',
'access administration pages',
'administer nodes',
'access content overview',
'administer content types',
'administer blocks',
'translate configuration',
]);
$this
->drupalLogin($this->adminUser);
ConfigurableLanguage::create([
'id' => 'xx-lolspeak',
'label' => 'Lolspeak',
])
->save();
ConfigurableLanguage::create([
'id' => 'nl',
'label' => 'Dutch',
])
->save();
ConfigurableLanguage::create([
'id' => 'es',
'label' => 'Spanish',
])
->save();
$this
->setUpExampleStructure();
$this
->insertExampleContent();
$this
->assertEquals($this
->indexItems($this->indexId), 5, '5 items were indexed.');
$this->blocks = NULL;
}
public function testLanguageIntegration() {
$facet_id = 'owl';
$facet_name = 'Owl';
$this
->createFacet($facet_name, $facet_id);
$this
->drupalGet('xx-lolspeak/search-api-test-fulltext');
$this
->assertSession()
->pageTextContains('item');
$this
->assertSession()
->pageTextContains('article');
$this
->clickLink('item');
$this
->assertNotFalse(strpos($this
->getUrl(), 'xx-lolspeak/'), 'Found the language code in the url');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('item');
$this
->assertSession()
->pageTextContains('article');
}
public function testSpecialCharacters() {
$id = 'water_bear';
$name = 'Water bear';
$this
->createFacet($name, $id, 'keywords');
$this
->drupalGet('search-api-test-fulltext');
$this
->assertFacetBlocksAppear();
$this
->assertFacetLabel('orange');
$entity_test_storage = \Drupal::entityTypeManager()
->getStorage('entity_test_mulrev_changed');
$entity_test_storage
->create([
'name' => 'special-chars 1',
'body' => 'test test test',
'type' => 'article',
'keywords' => [
'ƒäüö',
'test_key-word',
'special^%s',
'Key Word',
],
'category' => 'article_category',
])
->save();
$entity_test_storage
->create([
'name' => 'special-chars 2',
'body' => 'test test test',
'type' => 'article',
'keywords' => [
'ƒäüö',
'special^%s',
'aáå',
],
'category' => 'article_category',
])
->save();
$this
->assertEquals(2, $this
->indexItems($this->indexId), '2 items were indexed.');
$this
->drupalGet('search-api-test-fulltext');
$this
->assertFacetBlocksAppear();
$this
->assertFacetLabel('orange');
$this
->assertFacetLabel('ƒäüö');
$this
->assertFacetLabel('aáå');
$this
->assertFacetLabel('special^%s');
$this
->assertFacetLabel('test_key-word');
$this
->assertFacetLabel('Key Word');
}
public function testUrlAliasTranslation() {
$facet_id = 'barn_owl';
$facet_name = 'Barn owl';
$this
->createFacet($facet_name, $facet_id);
$this
->drupalGet('xx-lolspeak/search-api-test-fulltext');
$this
->assertFacetBlocksAppear();
$this
->clickLink('item');
$this
->assertTrue((bool) strpos($this
->getUrl(), 'xx-lolspeak/'), 'Found the language code in the url');
$this
->assertTrue((bool) strpos($this
->getUrl(), 'barn_owl'), 'Found the facet in the url');
$this
->drupalGet('admin/config/search/facets/' . $facet_id . '/edit/translate/xx-lolspeak/add');
$this
->drupalPostForm(NULL, [
'translation[config_names][facets.facet.barn_owl][url_alias]' => 'tyto_alba',
], 'Save translation');
$this
->drupalGet('admin/config/search/facets/' . $facet_id . '/edit/translate/nl/add');
$this
->drupalPostForm(NULL, [
'translation[config_names][facets.facet.barn_owl][url_alias]' => 'uil',
], 'Save translation');
$this
->drupalGet('admin/config/search/facets/' . $facet_id . '/edit/translate/es/add');
$this
->drupalPostForm(NULL, [
'translation[config_names][facets.facet.barn_owl][url_alias]' => 'buho',
], 'Save translation');
$this
->drupalGet('xx-lolspeak/search-api-test-fulltext');
$this
->assertFacetBlocksAppear();
$this
->clickLink('item');
$this
->assertTrue((bool) strpos($this
->getUrl(), 'xx-lolspeak/'), 'Found the language code in the url');
$this
->assertTrue((bool) strpos($this
->getUrl(), 'tyto_alba'), 'Found the facet in the url');
\Drupal::service('module_installer')
->install([
'locale',
]);
$block = $this
->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, [
'id' => 'test_language_block',
]);
$this
->drupalGet('xx-lolspeak/search-api-test-fulltext');
$this
->assertSession()
->pageTextContains($block
->label());
$this
->clickLink('item');
$links = $this
->findFacetLink('item');
$this
->assertEquals('is-active', $links[0]
->getParent()
->getAttribute('class'));
$this
->clickLink('English');
$links = $this
->findFacetLink('item');
$this
->assertEquals('is-active', $links[0]
->getParent()
->getAttribute('class'));
$this
->assertFalse((bool) strpos($this
->getUrl(), 'xx-lolspeak/'), 'Found the language code in the url');
$this
->assertFalse((bool) strpos($this
->getUrl(), 'tyto_alba'), 'Found the facet in the url');
$this
->assertTrue((bool) strpos($this
->getUrl(), 'barn_owl'), 'Found the facet in the url');
$this
->clickLink('Lolspeak');
$links = $this
->findFacetLink('item');
$this
->assertEquals('is-active', $links[0]
->getParent()
->getAttribute('class'));
$this
->assertTrue((bool) strpos($this
->getUrl(), 'xx-lolspeak/'), 'Found the language code in the url');
$this
->assertTrue((bool) strpos($this
->getUrl(), 'tyto_alba'), 'Found the facet in the url');
$this
->assertFalse((bool) strpos($this
->getUrl(), 'barn_owl'), 'Found the facet in the url');
$this
->clickLink('Dutch');
$links = $this
->findFacetLink('item');
$this
->assertEquals('is-active', $links[0]
->getParent()
->getAttribute('class'));
$this
->assertTrue((bool) strpos($this
->getUrl(), 'nl/'), 'Found the language code in the url');
$this
->assertTrue((bool) strpos($this
->getUrl(), 'uil'), 'Found the facet in the url');
$this
->clickLink('Spanish');
$links = $this
->findFacetLink('item');
$this
->assertEquals('is-active', $links[0]
->getParent()
->getAttribute('class'));
$this
->assertTrue((bool) strpos($this
->getUrl(), 'es/'), 'Found the language code in the url');
$this
->assertTrue((bool) strpos($this
->getUrl(), 'buho'), 'Found the facet in the url');
$this
->clickLink('English');
$links = $this
->findFacetLink('item');
$this
->assertEquals('is-active', $links[0]
->getParent()
->getAttribute('class'));
$this
->assertTrue((bool) strpos($this
->getUrl(), 'barn_owl'), 'Found the facet in the url');
}
public function testLanguageDifferences() {
$entity_test_storage = \Drupal::entityTypeManager()
->getStorage('entity_test_mulrev_changed');
$entity_test_storage
->create([
'name' => 'foo bar baz',
'body' => 'test test',
'type' => 'item',
'keywords' => [
'orange',
'lol',
],
'category' => 'item_category',
'langcode' => 'xx-lolspeak',
])
->save();
$entity_test_storage
->create([
'name' => 'foo bar baz',
'body' => 'test test',
'type' => 'item',
'keywords' => [
'orange',
'rofl',
],
'category' => 'item_category',
'langcode' => 'xx-lolspeak',
])
->save();
$id = 'water_bear';
$this
->createFacet('Water bear', $id, 'keywords');
$this
->drupalGet('admin/config/search/search-api/index/' . $this->indexId . '/edit');
$this
->assertEquals(2, $this
->indexItems($this->indexId), '2 items were indexed.');
$this
->drupalGet('search-api-test-fulltext');
$this
->assertFacetBlocksAppear();
$this
->assertSession()
->pageTextContains('orange');
$this
->assertSession()
->pageTextContains('grape');
$this
->assertSession()
->pageTextContains('rofl');
$this
->drupalPostForm(NULL, [
'language' => 'xx-lolspeak',
], 'Search');
$this
->assertFacetBlocksAppear();
$this
->assertSession()
->pageTextContains('orange');
$this
->assertSession()
->pageTextContains('rofl');
$this
->assertSession()
->pageTextNotContains('grape');
$this
->drupalPostForm(NULL, [
'language' => 'en',
], 'Search');
$this
->assertFacetBlocksAppear();
$this
->assertSession()
->pageTextContains('orange');
$this
->assertSession()
->pageTextContains('grape');
$this
->assertSession()
->pageTextNotContains('rofl');
}
public function testAdminTranslation() {
$id = 'water_bear';
$this
->createFacet('Water bear', $id);
$this
->drupalGet('admin/config/search/facets/' . $id . '/edit/translate/xx-lolspeak/add');
$this
->drupalPostForm(NULL, [
'translation[config_names][facets.facet.water_bear][name]' => 'Tardigrade',
], 'Save translation');
$this
->drupalGet('admin/config/search/facets');
$this
->assertSession()
->pageTextContains('Water bear');
$this
->drupalGet('xx-lolspeak/admin/config/search/facets');
$this
->assertSession()
->pageTextContains('Tardigrade');
}
}