Commit 552268e2 authored by alexpott's avatar alexpott

Issue #2694243 by marthinal, jhodgdon: node_field_data and search_index tables...

Issue #2694243 by marthinal, jhodgdon: node_field_data and search_index tables should match on langcode
parent bfd7cc23
......@@ -231,7 +231,7 @@ protected function findResults() {
->select('search_index', 'i', array('target' => 'replica'))
->extend('Drupal\search\SearchQuery')
->extend('Drupal\Core\Database\Query\PagerSelectExtender');
$query->join('node_field_data', 'n', 'n.nid = i.sid');
$query->join('node_field_data', 'n', 'n.nid = i.sid AND n.langcode = i.langcode');
$query->condition('n.status', 1)
->addTag('node_access')
->searchExpression($keys, $this->getPluginId());
......
<?php
/**
* @file
* Contains \Drupal\search\Tests\SearchDateIntervalTest.
*/
namespace Drupal\search\Tests;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests searching with date filters that exclude some translations.
*
* @group search
*/
class SearchDateIntervalTest extends SearchTestBase {
/**
* Modules to enable.
*
* @var string[]
*/
public static $modules = ['language', 'search_date_query_alter'];
protected function setUp() {
parent::setUp();
// Create and log in user.
$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);
// Add a new language.
ConfigurableLanguage::createFromLangcode('es')->save();
// Set up times to be applied to the English and Spanish translations of the
// node create time, so that they are filtered in/out in the
// search_date_query_alter test module.
$created_time_en = new \DateTime('February 10 2016 10PM');
$created_time_es = new \DateTime('March 19 2016 10PM');
$default_format = filter_default_format();
$node = $this->drupalCreateNode([
'title' => 'Node EN',
'type' => 'page',
'body' => [
'value' => $this->randomMachineName(32),
'format' => $default_format,
],
'langcode' => 'en',
'created' => $created_time_en->format('U'),
]);
// Add Spanish translation to the node.
$translation = $node->addTranslation('es', ['title' => 'Node ES']);
$translation->body->value = $this->randomMachineName(32);
$translation->created->value = $created_time_es->format('U');
$node->save();
// Update the index.
$plugin = $this->container->get('plugin.manager.search')->createInstance('node_search');
$plugin->updateIndex();
search_update_totals();
}
/**
* Tests searching with date filters that exclude some translations.
*/
public function testDateIntervalQueryAlter() {
// Search for keyword node.
$edit = ['keys' => 'node'];
$this->drupalPostForm('search/node', $edit, t('Search'));
// The nodes must have the same node ID but the created date is different.
// So only the Spanish translation must appear.
$this->assertLink('Node ES', 0, 'Spanish translation found in search results');
$this->assertNoLink('Node EN', 'Search results do not contain English node');
}
}
name: 'Search Date Query Alter'
type: module
description: 'Test module that adds date conditions to node searches.'
package: Testing
version: VERSION
core: 8.x
<?php
/**
* @file
* Adds date conditions to node searches.
*/
use Drupal\Core\Database\Query\AlterableInterface;
/**
* Implements hook_query_TAG_alter(): tag search_$type with $type node_search.
*/
function search_date_query_alter_query_search_node_search_alter(AlterableInterface $query) {
// Start date Sat, 19 Mar 2016 00:00:00 GMT.
$query->condition('n.created', 1458345600, '>=');
// End date Sun, 20 Mar 2016 00:00:00 GMT.
$query->condition('n.created', 1458432000, '<');
}
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