Commit 3f9cd457 authored by catch's avatar catch

Issue #2161067 by chakrapani, Nick_vh: Advanced search with Language filter does not work.

parent 33ade8c2
......@@ -96,7 +96,7 @@ class NodeSearch extends ConfigurableSearchPluginBase implements AccessibleInter
*/
protected $advanced = array(
'type' => array('column' => 'n.type'),
'langcode' => array('column' => 'i.langcode'),
'language' => array('column' => 'i.langcode'),
'author' => array('column' => 'n.uid'),
'term' => array('column' => 'ti.tid', 'join' => array('table' => 'taxonomy_index', 'alias' => 'ti', 'condition' => 'n.nid = ti.nid')),
);
......@@ -432,7 +432,8 @@ public function searchFormAlter(array &$form, array &$form_state) {
// Add languages.
$language_options = array();
foreach (language_list(Language::STATE_ALL) as $langcode => $language) {
$language_list = \Drupal::languageManager()->getLanguages(Language::STATE_ALL);
foreach ($language_list as $langcode => $language) {
// Make locked languages appear special in the list.
$language_options[$langcode] = $language->locked ? t('- @name -', array('@name' => $language->name)) : $language->name;
}
......
......@@ -7,6 +7,9 @@
namespace Drupal\search\Tests;
use Drupal\Core\Language\Language;
use Drupal\field\Field;
/**
* Test node search with multiple languages.
*/
......@@ -33,6 +36,62 @@ function setUp() {
// Create and login user.
$test_user = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search', 'administer nodes', 'administer languages', 'access administration pages', 'administer site configuration'));
$this->drupalLogin($test_user);
// Add a new language.
$language = new Language(array(
'id' => 'es',
'name' => 'Spanish',
));
language_save($language);
// Make the body field translatable. The title is already translatable by
// definition. The parent class has already created the article and page
// content types.
$field = Field::fieldInfo()->getField('node', 'body');
$field->translatable = TRUE;
$field->save();
// Create a few page nodes with multilingual body values.
$default_format = filter_default_format();
$nodes = array(
array(
'title' => 'First node en',
'type' => 'page',
'body' => array(array('value' => $this->randomName(32), 'format' => $default_format)),
'langcode' => 'en',
),
array(
'title' => 'Second node this is the Spanish title',
'type' => 'page',
'body' => array(array('value' => $this->randomName(32), 'format' => $default_format)),
'langcode' => 'es',
),
array(
'title' => 'Third node en',
'type' => 'page',
'body' => array(array('value' => $this->randomName(32), 'format' => $default_format)),
'langcode' => 'en',
),
);
$this->searchable_nodes = array();
foreach ($nodes as $setting) {
$this->searchable_nodes[] = $this->drupalCreateNode($setting);
}
// Add English translation to the second node.
$translation = $this->searchable_nodes[1]->addTranslation('en', array('title' => 'Second node en'));
$translation->body->value = $this->randomName(32);
$this->searchable_nodes[1]->save();
// Add Spanish translation to the third node.
$translation = $this->searchable_nodes[2]->addTranslation('es', array('title' => 'Third node es'));
$translation->body->value = $this->randomName(32);
$this->searchable_nodes[2]->save();
// Update the index and then run the shutdown method.
$plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
$plugin->updateIndex();
search_update_totals();
}
function testLanguages() {
......@@ -60,6 +119,17 @@ function testLanguages() {
$query_string = isset($parts['query']) ? rawurldecode($parts['query']) : '';
$this->assertTrue(strpos($query_string, '=language:fr') !== FALSE, 'Language filter language:fr add to the query string.');
// Search for keyword node and language filter as Spanish.
$edit = array('keys' => 'node', 'language[es]' => TRUE);
$this->drupalPostForm('search/node', $edit, t('Advanced search'));
// Check for Spanish results.
$this->assertLink('Second node this is the Spanish title', 0, 'Second node Spanish title found in search results');
$this->assertLink('Third node es', 0, 'Third node Spanish found in search results');
// Ensure that results doesn't contain other language nodes.
$this->assertNoLink('First node en', 'Search results does not contain first English node');
$this->assertNoLink('Second node en', 'Search results does not contain second English node');
$this->assertNoLink('Third node en', 'Search results does not contain third English node');
// Change the default language and delete English.
$path = 'admin/config/regional/settings';
$this->drupalGet($path);
......
......@@ -8,6 +8,7 @@
namespace Drupal\search\Tests;
use Drupal\Core\Language\Language;
use Drupal\field\Field;
/**
* Tests entities with multilingual fields.
......@@ -50,7 +51,7 @@ function setUp() {
// Make the body field translatable. The title is already translatable by
// definition. The parent class has already created the article and page
// content types.
$field = field_info_field('node', 'body');
$field = Field::fieldInfo()->getField('node', 'body');
$field->translatable = TRUE;
$field->save();
......@@ -133,10 +134,27 @@ function testSearchingMultilingualFieldValues() {
$this->assertEqual($search_result[1]['title'], 'Second node this is the English title', 'The search finds the correct English title.');
// Now filter for Hungarian results only.
$plugin->setSearch('English OR Hungarian', array('f' => array('langcode:hu')), array());
$plugin->setSearch('English OR Hungarian', array('f' => array('language:hu')), array());
$search_result = $plugin->execute();
$this->assertEqual(count($search_result), 1, 'The search found only one result');
$this->assertEqual($search_result[0]['title'], 'Third node this is the Hungarian title', 'The search finds the correct Hungarian title.');
// Test for search with common key word across multiple languages.
$plugin->setSearch('node', array(), array());
$search_result = $plugin->execute();
$this->assertEqual(count($search_result), 6, 'The search found total six results');
// Test with language filters and common key word.
$plugin->setSearch('node', array('f' => array('language:hu')), array());
$search_result = $plugin->execute();
$this->assertEqual(count($search_result), 2, 'The search found 2 results');
// Test to check for the language of result items.
foreach($search_result as $result) {
$this->assertEqual($result['langcode'], 'hu', 'The search found the correct Hungarian result');
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment