diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-05-12 15:53:43 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-05-12 15:53:43 +0000 |
commit | 6987dd4cab489d47a42f1331e882d3dbf7635913 (patch) | |
tree | d6f5852c9a63cfb3f4f5b099a4bf3f9b15f94f82 /modules | |
parent | 6cb598ec3c76cb579d20e970c80c5db46261f0b3 (diff) | |
download | brdo-6987dd4cab489d47a42f1331e882d3dbf7635913.tar.gz brdo-6987dd4cab489d47a42f1331e882d3dbf7635913.tar.bz2 |
- Patch #791270 by duellj, lotyrin, Berdir: SearchQuery exact search conditions not added to CountQuery.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/node/node.module | 8 | ||||
-rw-r--r-- | modules/search/search.api.php | 8 | ||||
-rw-r--r-- | modules/search/search.extender.inc | 32 | ||||
-rw-r--r-- | modules/search/search.test | 60 |
4 files changed, 94 insertions, 14 deletions
diff --git a/modules/node/node.module b/modules/node/node.module index dc6532483..8ea70f98b 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -1542,16 +1542,10 @@ function node_search_execute($keys = NULL) { // Add the ranking expressions. _node_rankings($query); - // Add a count query. - $inner_query = clone $query; - $count_query = db_select($inner_query->fields('i', array('sid')), NULL, array('target' => 'slave')); - $count_query->addExpression('COUNT(*)'); - $query->setCountQuery($count_query); + // Load results. $find = $query ->limit(10) ->execute(); - - // Load results. $results = array(); foreach ($find as $item) { // Render the node. diff --git a/modules/search/search.api.php b/modules/search/search.api.php index 408cff34f..21f4174b9 100644 --- a/modules/search/search.api.php +++ b/modules/search/search.api.php @@ -177,16 +177,10 @@ function hook_search_execute($keys = NULL) { // Add the ranking expressions. _node_rankings($query); - // Add a count query. - $inner_query = clone $query; - $count_query = db_select($inner_query->fields('i', array('sid'))); - $count_query->addExpression('COUNT(*)'); - $query->setCountQuery($count_query); + // Load results. $find = $query ->limit(10) ->execute(); - - // Load results. $results = array(); foreach ($find as $item) { // Build the node body. diff --git a/modules/search/search.extender.inc b/modules/search/search.extender.inc index 047853a9c..1eae2caa6 100644 --- a/modules/search/search.extender.inc +++ b/modules/search/search.extender.inc @@ -407,6 +407,7 @@ class SearchQuery extends SelectQueryExtender { return new DatabaseStatementEmpty(); } + // Add conditions to query. $this->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type'); $this->condition($this->conditions); @@ -443,4 +444,35 @@ class SearchQuery extends SelectQueryExtender { return $this->query->execute(); } + + /** + * Build the default count query for SearchQuery. + * + * Since SearchQuery always uses GROUP BY, we can default to a subquery. Also + * adding the same conditions as execute() because countQuery() is called + * first. + */ + public function countQuery() { + // Clone the inner query. + $inner = clone $this->query; + + // Add conditions to query. + $inner->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type'); + $inner->condition($this->conditions); + + // Remove existing fields and expressions, they are not needed for a count + // query. + $fields =& $inner->getFields(); + $fields = array(); + $expressions =& $inner->getExpressions(); + $expressions = array(); + + // Add the sid as the only field and count them as a subquery. + $count = db_select($inner->fields('i', array('sid')), NULL, array('target' => 'slave')); + + // Add the COUNT() expression. + $count->addExpression('COUNT(*)'); + + return $count; + } } diff --git a/modules/search/search.test b/modules/search/search.test index 1a78c9eb4..0a5e069e3 100644 --- a/modules/search/search.test +++ b/modules/search/search.test @@ -593,6 +593,66 @@ class SearchBlockTestCase extends DrupalWebTestCase { } } +/** + * Tests that searching for a phrase gets the correct page count. + */ +class SearchExactTestCase extends DrupalWebTestCase { + public static function getInfo() { + return array( + 'name' => 'Search engine phrase queries', + 'description' => 'Tests that searching for a phrase gets the correct page count.', + 'group' => 'Search', + ); + } + + function setUp() { + parent::setUp('search'); + } + + /** + * Tests that the correct number of pager links are found for both keywords and phrases. + */ + function testExactQuery() { + // Login with sufficient privileges. + $this->drupalLogin($this->drupalCreateUser(array('create page content', 'search content'))); + + $settings = array( + 'type' => 'page', + 'title' => 'Simple Node', + ); + // Create nodes with exact phrase. + for ($i = 0; $i <= 17; $i++) { + $settings['body'] = array(LANGUAGE_NONE => array(array('value' => 'love pizza'))); + $this->drupalCreateNode($settings); + } + // Create nodes containing keywords. + for ($i = 0; $i <= 17; $i++) { + $settings['body'] = array(LANGUAGE_NONE => array(array('value' => 'love cheesy pizza'))); + $this->drupalCreateNode($settings); + } + + // Update the search index. + module_invoke_all('update_index'); + search_update_totals(); + + // Refresh variables after the treatment. + $this->refreshVariables(); + + // Test that the correct number of pager links are found for keyword search. + $edit = array('keys' => 'love pizza'); + $this->drupalPost('search/node', $edit, t('Search')); + $this->assertLinkByHref('page=1', 0, '2nd page link is found for keyword search.'); + $this->assertLinkByHref('page=2', 0, '3rd page link is found for keyword search.'); + $this->assertLinkByHref('page=3', 0, '4th page link is found for keyword search.'); + $this->assertNoLinkByHref('page=4', '5th page link is not found for keyword search.'); + + // Test that the correct number of pager links are found for exact phrase search. + $edit = array('keys' => '"love pizza"'); + $this->drupalPost('search/node', $edit, t('Search')); + $this->assertLinkByHref('page=1', 0, '2nd page link is found for exact phrase search.'); + $this->assertNoLinkByHref('page=2', '3rd page link is not found for exact phrase search.'); + } +} /** * Test integration searching comments. |