diff --git a/core/modules/search/src/Plugin/views/argument/Search.php b/core/modules/search/src/Plugin/views/argument/Search.php index cbba272..2cafe48 100644 --- a/core/modules/search/src/Plugin/views/argument/Search.php +++ b/core/modules/search/src/Plugin/views/argument/Search.php @@ -93,33 +93,22 @@ public function query($group_by = FALSE) { $join = Views::pluginManager('join')->createInstance('standard', $definition); $search_total = $this->query->addRelationship('search_total', $join, $search_index); - $this->search_score = $this->query->addField('', "SUM($search_index.score * $search_total.count)", 'score', array('aggregate' => TRUE)); + $this->search_score = $this->query->addField('', "$search_index.score * $search_total.count", 'score', array('function' => 'sum')); $search_condition->condition("$search_index.type", $this->searchType); - if (!$this->searchQuery->simple()) { - $search_dataset = $this->query->addTable('node_search_dataset'); - $conditions = $this->searchQuery->conditions(); - $condition_conditions =& $conditions->conditions(); - foreach ($condition_conditions as $key => &$condition) { - // Make sure we just look at real conditions. - if (is_numeric($key)) { - // Replace the conditions with the table alias of views. - $this->searchQuery->conditionReplaceString('d.', "$search_dataset.", $condition); - } + $search_dataset = $this->query->addTable('node_search_dataset'); + $conditions = $this->searchQuery->conditions(); + $condition_conditions =& $conditions->conditions(); + foreach ($condition_conditions as $key => &$condition) { + // Make sure we just look at real conditions. + if (is_numeric($key)) { + // Replace the conditions with the table alias of views. + $this->searchQuery->conditionReplaceString('d.', "$search_dataset.", $condition); } - $search_conditions =& $search_condition->conditions(); - $search_conditions = array_merge($search_conditions, $condition_conditions); - } - else { - // Stores each condition, so and/or on the filter level will still work. - $or = db_or(); - foreach ($words as $word) { - $or->condition("$search_index.word", $word); - } - - $search_condition->condition($or); } + $search_conditions =& $search_condition->conditions(); + $search_conditions = array_merge($search_conditions, $condition_conditions); $this->query->addWhere(0, $search_condition); $this->query->addGroupBy("$search_index.sid"); diff --git a/core/modules/search/src/Plugin/views/filter/Search.php b/core/modules/search/src/Plugin/views/filter/Search.php index 078d894..69f53b7 100644 --- a/core/modules/search/src/Plugin/views/filter/Search.php +++ b/core/modules/search/src/Plugin/views/filter/Search.php @@ -169,32 +169,21 @@ public function query() { $search_total = $this->query->addRelationship('search_total', $join, $search_index); - $this->search_score = $this->query->addField('', "SUM($search_index.score * $search_total.count)", 'score', array('aggregate' => TRUE)); + $this->search_score = $this->query->addField('', "$search_index.score * $search_total.count", 'score', array('function' => 'sum')); $search_condition->condition("$search_index.type", $this->searchType); - if (!$this->searchQuery->simple()) { - $search_dataset = $this->query->addTable('node_search_dataset'); - $conditions = $this->searchQuery->conditions(); - $condition_conditions =& $conditions->conditions(); - foreach ($condition_conditions as $key => &$condition) { - // Make sure we just look at real conditions. - if (is_numeric($key)) { - // Replace the conditions with the table alias of views. - $this->searchQuery->conditionReplaceString('d.', "$search_dataset.", $condition); - } + $search_dataset = $this->query->addTable('node_search_dataset'); + $conditions = $this->searchQuery->conditions(); + $condition_conditions =& $conditions->conditions(); + foreach ($condition_conditions as $key => &$condition) { + // Make sure we just look at real conditions. + if (is_numeric($key)) { + // Replace the conditions with the table alias of views. + $this->searchQuery->conditionReplaceString('d.', "$search_dataset.", $condition); } - $search_conditions =& $search_condition->conditions(); - $search_conditions = array_merge($search_conditions, $condition_conditions); - } - else { - // Stores each condition, so and/or on the filter level will still work. - $or = db_or(); - foreach ($words as $word) { - $or->condition("$search_index.word", $word); - } - - $search_condition->condition($or); } + $search_conditions =& $search_condition->conditions(); + $search_conditions = array_merge($search_conditions, $condition_conditions); $this->query->addWhere($this->options['group'], $search_condition); $this->query->addGroupBy("$search_index.sid"); diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php index 72cc5f9..a110af0 100644 --- a/core/modules/views/src/Plugin/views/query/Sql.php +++ b/core/modules/views/src/Plugin/views/query/Sql.php @@ -722,7 +722,11 @@ public function getTableInfo($table) { * alias be used. * @param $params * An array of parameters additional to the field that will control items - * such as aggregation functions and DISTINCT. + * such as aggregation functions and DISTINCT. Some values that are + * recognized: + * - function: An aggregation function to apply, such as SUM. + * - aggregate: Set to TRUE to indicate that this value should be + * aggregated in a GROUP BY. * * @return $name * The name that this field can be referred to as. Usually this is the alias. diff --git a/core/modules/views/src/Tests/SearchIntegrationTest.php b/core/modules/views/src/Tests/SearchIntegrationTest.php index c36eca2..0223054 100644 --- a/core/modules/views/src/Tests/SearchIntegrationTest.php +++ b/core/modules/views/src/Tests/SearchIntegrationTest.php @@ -7,6 +7,8 @@ namespace Drupal\views\Tests; +use Drupal\Component\Utility\String; + /** * Tests search integration filters. * @@ -68,36 +70,52 @@ public function testSearchIntegration() { $this->assertNoLink('sandwich'); $this->assertLink('cola'); - // Page with a keyword argument. + // Page with a keyword argument, various argument values. + // Verify that the correct nodes are shown, and only once. $this->drupalGet('test-arg/pizza'); - $this->assertLink('pizza'); + $this->assertOneLink('pizza'); $this->assertNoLink('sandwich'); - $this->assertLink('cola'); + $this->assertOneLink('cola'); $this->drupalGet('test-arg/sandwich'); $this->assertNoLink('pizza'); - $this->assertLink('sandwich'); + $this->assertOneLink('sandwich'); $this->assertNoLink('cola'); - $this->drupalGet('test-arg/' . urlencode('pizza OR sandwich')); - $this->assertLink('pizza'); - $this->assertLink('sandwich'); - $this->assertLink('cola'); + $this->drupalGet('test-arg/pizza OR sandwich'); + $this->assertOneLink('pizza'); + $this->assertOneLink('sandwich'); + $this->assertOneLink('cola'); - $this->drupalGet('test-arg/' . urlencode('pizza sandwich OR cola')); + $this->drupalGet('test-arg/pizza sandwich OR cola'); $this->assertNoLink('pizza'); $this->assertNoLink('sandwich'); - $this->assertLink('cola'); + $this->assertOneLink('cola'); - $this->drupalGet('test-arg/' . urlencode('cola pizza')); + $this->drupalGet('test-arg/cola pizza'); $this->assertNoLink('pizza'); $this->assertNoLink('sandwich'); - $this->assertLink('cola'); + $this->assertOneLink('cola'); - $this->drupalGet('test-arg/' . urlencode('"cola is good"')); + $this->drupalGet('test-arg/"cola is good"'); $this->assertNoLink('pizza'); $this->assertNoLink('sandwich'); - $this->assertLink('cola'); + $this->assertOneLink('cola'); + } + + /** + * Asserts that exactly one link exists with the given text. + * + * @param string $label + * Link label to assert. + * + * @return bool + * TRUE if the assertion succeeded, FALSE otherwise. + */ + protected function assertOneLink($label) { + $links = $this->xpath('//a[normalize-space(text())=:label]', array(':label' => $label)); + $message = String::format('Link with label %label found once.', array('%label' => $label)); + return $this->assert(isset($links[0]) && !isset($links[1]), $message); } }