diff --git a/phpBB/phpbb/search/fulltext_mysql.php b/phpBB/phpbb/search/fulltext_mysql.php index c280f095ef..a9ea893682 100644 --- a/phpBB/phpbb/search/fulltext_mysql.php +++ b/phpBB/phpbb/search/fulltext_mysql.php @@ -568,7 +568,7 @@ class fulltext_mysql extends \phpbb\search\base ); extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_keywords_main_query_before', compact($vars))); - $sql_select = ($type == 'posts') ? 'p.post_id' : 'DISTINCT t.topic_id'; + $sql_select = ($type == 'posts') ? 'DISTINCT p.post_id' : 'DISTINCT t.topic_id'; $sql_select .= $sort_by_sql[$sort_key] ? ", {$sort_by_sql[$sort_key]}" : ''; $sql_from = ($join_topic) ? TOPICS_TABLE . ' t, ' : ''; $field = ($type == 'posts') ? 'post_id' : 'topic_id'; @@ -613,7 +613,7 @@ class fulltext_mysql extends \phpbb\search\base // if the total result count is not cached yet, retrieve it from the db if (!$result_count && count($id_ary)) { - $sql_found_rows = str_replace("SELECT $sql_select", "SELECT COUNT(*) as result_count", $sql); + $sql_found_rows = str_replace("SELECT $sql_select", "SELECT COUNT($sql_select) as result_count", $sql); $result = $this->db->sql_query($sql_found_rows); $result_count = (int) $this->db->sql_fetchfield('result_count'); $this->db->sql_freeresult($result); diff --git a/phpBB/phpbb/search/fulltext_native.php b/phpBB/phpbb/search/fulltext_native.php index 689d2a8f40..51d4514866 100644 --- a/phpBB/phpbb/search/fulltext_native.php +++ b/phpBB/phpbb/search/fulltext_native.php @@ -634,7 +634,7 @@ class fulltext_native extends \phpbb\search\base $w_num = 0; $sql_array = array( - 'SELECT' => ($type == 'posts') ? 'p.post_id' : 'p.topic_id', + 'SELECT' => ($type == 'posts') ? 'DISTINCT p.post_id' : 'DISTINCT p.topic_id', 'FROM' => array( SEARCH_WORDMATCH_TABLE => array(), SEARCH_WORDLIST_TABLE => array(), @@ -984,9 +984,9 @@ class fulltext_native extends \phpbb\search\base // If using mysql and the total result count is not calculated yet, get it from the db if (!$total_results && $is_mysql) { - $sql_count = str_replace("SELECT {$sql_array['SELECT']}", "SELECT COUNT(DISTINCT {$sql_array['SELECT']}) as total_results", $sql); + $sql_count = str_replace("SELECT {$sql_array['SELECT']}", "SELECT COUNT({$sql_array['SELECT']}) as total_results", $sql); $result = $this->db->sql_query($sql_count); - $total_results = (int) $this->db->sql_fetchfield('total_results'); + $total_results = $sql_array['GROUP_BY'] ? count($this->db->sql_fetchrowset($result)) : $this->db->sql_fetchfield('total_results'); $this->db->sql_freeresult($result); if (!$total_results) diff --git a/phpBB/phpbb/search/fulltext_postgres.php b/phpBB/phpbb/search/fulltext_postgres.php index a0bb6242ec..7bbdda3bbc 100644 --- a/phpBB/phpbb/search/fulltext_postgres.php +++ b/phpBB/phpbb/search/fulltext_postgres.php @@ -550,7 +550,7 @@ class fulltext_postgres extends \phpbb\search\base // if the total result count is not cached yet, retrieve it from the db if (!$result_count) { - $sql_count = "SELECT COUNT(*) as result_count + $sql_count = "SELECT COUNT(DISTINCT " . (($type == 'posts') ? 'p.post_id' : 't.topic_id') . ") as result_count $sql_from $sql_where"; $result = $this->db->sql_query($sql_count); diff --git a/tests/functional/search/base.php b/tests/functional/search/base.php index b414e42f10..582ff917f5 100644 --- a/tests/functional/search/base.php +++ b/tests/functional/search/base.php @@ -49,6 +49,30 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case $this->assertStringContainsString("Search found $topics_found match", $crawler->filter('.searchresults-title')->text(), $this->search_backend); } + protected function assert_search_in_topic($topic_id, $keywords, $posts_found, $sort_key = '') + { + $this->purge_cache(); + $crawler = self::request('GET', "search.php?t=$topic_id&sf=msgonly&keywords=$keywords" . ($sort_key ? "&sk=$sort_key" : '')); + $this->assertEquals($posts_found, $crawler->filter('.postbody')->count(), $this->search_backend); + $this->assertStringContainsString("Search found $posts_found match", $crawler->filter('.searchresults-title')->text(), $this->search_backend); + } + + protected function assert_search_in_forum($forum_id, $keywords, $posts_found, $sort_key = '') + { + $this->purge_cache(); + $crawler = self::request('GET', "search.php?fid[]=$forum_id&keywords=$keywords" . ($sort_key ? "&sk=$sort_key" : '')); + $this->assertEquals($posts_found, $crawler->filter('.postbody')->count(), $this->search_backend); + $this->assertStringContainsString("Search found $posts_found match", $crawler->filter('.searchresults-title')->text(), $this->search_backend); + } + + protected function assert_search_topics_in_forum($forum_id, $keywords, $topics_found, $sort_key = '') + { + $this->purge_cache(); + $crawler = self::request('GET', "search.php?fid[]=$forum_id&sr=topics&keywords=$keywords" . ($sort_key ? "&sk=$sort_key" : '')); + $this->assertEquals($topics_found, $crawler->filter('.row')->count(), $this->search_backend); + $this->assertStringContainsString("Search found $topics_found match", $crawler->filter('.searchresults-title')->text(), $this->search_backend); + } + protected function assert_search_not_found($keywords) { $crawler = self::request('GET', 'search.php?keywords=' . $keywords); @@ -86,6 +110,9 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case $this->create_search_index('\phpbb\search\fulltext_native'); $post = $this->create_topic(2, 'Test Topic 1 foosubject', 'This is a test topic posted by the barsearch testing framework.'); + $topic_multiple_results_count1 = $this->create_topic(2, 'Test Topic for multiple search results', 'This is a test topic posted to test multiple results count.'); + $this->create_post(2, $topic_multiple_results_count1['topic_id'], 'Re: Test Topic for multiple search results', 'This is a test post 2 posted to test multiple results count.'); + $topic_multiple_results_count2 = $this->create_topic(2, 'Test Topic 2 for multiple search results', 'This is a test topic 2 posted to test multiple results count.'); $this->set_flood_interval(15); $crawler = self::request('GET', 'adm/index.php?i=acp_search&mode=settings&sid=' . $this->sid); @@ -107,6 +134,8 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case { $this->delete_topic($post['topic_id']); $this->delete_topic($topic_by_author['topic_id']); + $this->delete_topic($topic_multiple_results_count1['topic_id']); + $this->delete_topic($topic_multiple_results_count2['topic_id']); $this->markTestSkipped("Search backend is not supported/running"); } @@ -121,9 +150,15 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case $this->assert_search_found('foosubject+barsearch', 1, 2, $sort_key); $this->assert_search_found('barsearch-testing', 1, 2, $sort_key); // test hyphen ignored $this->assert_search_found('barsearch+-+testing', 1, 2, $sort_key); // test hyphen wrapped with space ignored + $this->assert_search_found('multiple+results+count', 3, 15, $sort_key); // test multiple results count - posts + $this->assert_search_found_topics('multiple+results+count', 2, $sort_key); // test multiple results count - topics $this->assert_search_found_topics('phpbb3+installation', 1, $sort_key); $this->assert_search_found_topics('foosubject+barsearch', 1, $sort_key); + $this->assert_search_in_forum(2, 'multiple+search+results', 3, $sort_key); // test multiple results count - forum search - posts + $this->assert_search_topics_in_forum(2, 'multiple+search+results', 2, $sort_key); // test multiple results count - forum search - topics + $this->assert_search_in_topic((int) $topic_multiple_results_count1['topic_id'], 'multiple+results', 2, $sort_key); // test multiple results count - topic search + $this->assert_search_posts_by_author('searchforauthoruser', 2, $sort_key); $this->assert_search_topics_by_author('searchforauthoruser', 1, $sort_key); } @@ -137,6 +172,8 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case $this->delete_search_index(); $this->delete_topic($post['topic_id']); $this->delete_topic($topic_by_author['topic_id']); + $this->delete_topic($topic_multiple_results_count1['topic_id']); + $this->delete_topic($topic_multiple_results_count2['topic_id']); } protected function create_search_index($backend = null)