From 552268e2e1261eb8171de91ea0a63e583f59ac8d Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Tue, 5 Apr 2016 11:47:37 +0100 Subject: [PATCH] Issue #2694243 by marthinal, jhodgdon: node_field_data and search_index tables should match on langcode --- .../node/src/Plugin/Search/NodeSearch.php | 2 +- .../src/Tests/SearchDateIntervalTest.php | 79 +++++++++++++++++++ .../search_date_query_alter.info.yml | 6 ++ .../search_date_query_alter.module | 18 +++++ 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 core/modules/search/src/Tests/SearchDateIntervalTest.php create mode 100644 core/modules/search/tests/modules/search_date_query_alter/search_date_query_alter.info.yml create mode 100644 core/modules/search/tests/modules/search_date_query_alter/search_date_query_alter.module diff --git a/core/modules/node/src/Plugin/Search/NodeSearch.php b/core/modules/node/src/Plugin/Search/NodeSearch.php index 85515fd2d876..d00cd6a48e4a 100644 --- a/core/modules/node/src/Plugin/Search/NodeSearch.php +++ b/core/modules/node/src/Plugin/Search/NodeSearch.php @@ -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()); diff --git a/core/modules/search/src/Tests/SearchDateIntervalTest.php b/core/modules/search/src/Tests/SearchDateIntervalTest.php new file mode 100644 index 000000000000..ec053942d6a7 --- /dev/null +++ b/core/modules/search/src/Tests/SearchDateIntervalTest.php @@ -0,0 +1,79 @@ +<?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'); + } +} diff --git a/core/modules/search/tests/modules/search_date_query_alter/search_date_query_alter.info.yml b/core/modules/search/tests/modules/search_date_query_alter/search_date_query_alter.info.yml new file mode 100644 index 000000000000..5b573478b4f5 --- /dev/null +++ b/core/modules/search/tests/modules/search_date_query_alter/search_date_query_alter.info.yml @@ -0,0 +1,6 @@ +name: 'Search Date Query Alter' +type: module +description: 'Test module that adds date conditions to node searches.' +package: Testing +version: VERSION +core: 8.x diff --git a/core/modules/search/tests/modules/search_date_query_alter/search_date_query_alter.module b/core/modules/search/tests/modules/search_date_query_alter/search_date_query_alter.module new file mode 100644 index 000000000000..e595796e1f8d --- /dev/null +++ b/core/modules/search/tests/modules/search_date_query_alter/search_date_query_alter.module @@ -0,0 +1,18 @@ +<?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, '<'); +} -- GitLab