mirror of
https://github.com/phpbb/phpbb.git
synced 2025-04-21 16:22:22 +02:00
[ticket/16902] Improve test, use DISTINCT for precise results count
PHPBB3-16902
This commit is contained in:
parent
bf7936380a
commit
e7c81cd1a6
@ -569,6 +569,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 .= $sort_by_sql[$sort_key] ? ", {$sort_by_sql[$sort_key]}" : '';
|
||||
$sql_from = ($join_topic) ? TOPICS_TABLE . ' t, ' : '';
|
||||
$field = ($type == 'posts') ? 'post_id' : 'topic_id';
|
||||
if (count($author_ary) && $author_name)
|
||||
@ -609,11 +610,10 @@ class fulltext_mysql extends \phpbb\search\base
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
$id_ary = array_unique($id_ary);
|
||||
|
||||
// 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($sql_select) as result_count", $sql);
|
||||
$sql_found_rows = str_replace("SELECT $sql_select", "SELECT COUNT(*) 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);
|
||||
@ -828,6 +828,7 @@ class fulltext_mysql extends \phpbb\search\base
|
||||
|
||||
// If the cache was completely empty count the results
|
||||
$sql_select = ($type == 'posts') ? 'p.post_id' : 't.topic_id';
|
||||
$sql_select .= $sort_by_sql[$sort_key] ? ", {$sort_by_sql[$sort_key]}" : '';
|
||||
|
||||
// Build the query for really selecting the post_ids
|
||||
if ($type == 'posts')
|
||||
@ -856,7 +857,7 @@ class fulltext_mysql extends \phpbb\search\base
|
||||
AND t.topic_id = p.topic_id
|
||||
$sql_sort_join
|
||||
$sql_time
|
||||
GROUP BY t.topic_id
|
||||
GROUP BY $sql_select
|
||||
ORDER BY $sql_sort";
|
||||
$field = 'topic_id';
|
||||
}
|
||||
@ -873,9 +874,10 @@ class fulltext_mysql extends \phpbb\search\base
|
||||
// retrieve the total result count if needed
|
||||
if (!$result_count)
|
||||
{
|
||||
$sql_found_rows = str_replace("SELECT $sql_select", "SELECT COUNT($sql_select) as result_count", $sql);
|
||||
$sql_found_rows = str_replace("SELECT $sql_select", "SELECT COUNT(*) as result_count", $sql);
|
||||
$result = $this->db->sql_query($sql_found_rows);
|
||||
$result_count = (int) $this->db->sql_fetchfield('result_count');
|
||||
$result_count = ($type == 'posts') ? (int) $this->db->sql_fetchfield('result_count') : count($this->db->sql_fetchrowset($result));
|
||||
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
if (!$result_count)
|
||||
|
@ -968,6 +968,7 @@ class fulltext_native extends \phpbb\search\base
|
||||
$sql_array['WHERE'] = implode(' AND ', $sql_where);
|
||||
$sql_array['GROUP_BY'] = ($group_by) ? (($type == 'posts') ? 'p.post_id' : 'p.topic_id') . ', ' . $sort_by_sql[$sort_key] : '';
|
||||
$sql_array['ORDER_BY'] = $sql_sort;
|
||||
$sql_array['SELECT'] .= $sort_by_sql[$sort_key] ? ", {$sort_by_sql[$sort_key]}" : '';
|
||||
|
||||
unset($sql_where, $sql_sort, $group_by);
|
||||
|
||||
@ -983,7 +984,7 @@ 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({$sql_array['SELECT']}) as total_results", $sql);
|
||||
$sql_count = str_replace("SELECT {$sql_array['SELECT']}", "SELECT COUNT(DISTINCT {$sql_array['SELECT']}) as total_results", $sql);
|
||||
$result = $this->db->sql_query($sql_count);
|
||||
$total_results = (int) $this->db->sql_fetchfield('total_results');
|
||||
$this->db->sql_freeresult($result);
|
||||
@ -1005,7 +1006,6 @@ class fulltext_native extends \phpbb\search\base
|
||||
$id_ary[] = (int) $row[(($type == 'posts') ? 'post_id' : 'topic_id')];
|
||||
}
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
}
|
||||
|
||||
// store the ids, from start on then delete anything that isn't on the current page because we only need ids for one page
|
||||
@ -1137,6 +1137,7 @@ class fulltext_native extends \phpbb\search\base
|
||||
}
|
||||
|
||||
$select = ($type == 'posts') ? 'p.post_id' : 't.topic_id';
|
||||
$select .= $sort_by_sql[$sort_key] ? ", {$sort_by_sql[$sort_key]}" : '';
|
||||
$is_mysql = false;
|
||||
|
||||
/**
|
||||
@ -1284,9 +1285,9 @@ class fulltext_native extends \phpbb\search\base
|
||||
|
||||
if (!$total_results && $is_mysql)
|
||||
{
|
||||
$sql_count = str_replace("SELECT $select", "SELECT COUNT($select) as total_results", $sql);
|
||||
$sql_count = str_replace("SELECT $select", "SELECT COUNT(*) as total_results", $sql);
|
||||
$result = $this->db->sql_query($sql_count);
|
||||
$total_results = (int) $this->db->sql_fetchfield('total_results');
|
||||
$total_results = ($type == 'posts') ? (int) $this->db->sql_fetchfield('total_results') : count($this->db->sql_fetchrowset($result));
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
if (!$total_results)
|
||||
|
@ -18,17 +18,31 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case
|
||||
{
|
||||
protected function assert_search_found($keywords, $posts_found, $words_highlighted)
|
||||
{
|
||||
$this->purge_cache();
|
||||
$crawler = self::request('GET', 'search.php?keywords=' . $keywords);
|
||||
$this->assertEquals($posts_found, $crawler->filter('.postbody')->count());
|
||||
$this->assertEquals($words_highlighted, $crawler->filter('.posthilit')->count());
|
||||
$this->assertEquals($posts_found, $crawler->filter('.postbody')->count(), $this->search_backend);
|
||||
$this->assertEquals($words_highlighted, $crawler->filter('.posthilit')->count(), $this->search_backend);
|
||||
$this->assertStringContainsString("Search found $posts_found match", $crawler->filter('.searchresults-title')->text(), $this->search_backend);
|
||||
}
|
||||
|
||||
protected function assert_search_found_topics($keywords, $topics_found)
|
||||
{
|
||||
$this->purge_cache();
|
||||
$crawler = self::request('GET', 'search.php?sr=topics&keywords=' . $keywords);
|
||||
$html = '';
|
||||
foreach ($crawler as $domElement) {
|
||||
$html .= $domElement->ownerDocument->saveHTML($domElement);
|
||||
}
|
||||
$this->assertEquals($topics_found, $crawler->filter('.row')->count(), $html);
|
||||
$this->assertStringContainsString("Search found $topics_found match", $crawler->filter('.searchresults-title')->text(), $html);
|
||||
}
|
||||
|
||||
protected function assert_search_not_found($keywords)
|
||||
{
|
||||
$crawler = self::request('GET', 'search.php?keywords=' . $keywords);
|
||||
$this->assertEquals(0, $crawler->filter('.postbody')->count());
|
||||
$this->assertEquals(0, $crawler->filter('.postbody')->count(),$this->search_backend);
|
||||
$split_keywords_string = str_replace('+', ' ', $keywords);
|
||||
$this->assertEquals($split_keywords_string, $crawler->filter('#keywords')->attr('value'));
|
||||
$this->assertEquals($split_keywords_string, $crawler->filter('#keywords')->attr('value'), $this->search_backend);
|
||||
}
|
||||
|
||||
public function test_search_backend()
|
||||
@ -67,6 +81,9 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case
|
||||
$this->logout();
|
||||
$this->assert_search_found('phpbb3+installation', 1, 3);
|
||||
$this->assert_search_found('foosubject+barsearch', 1, 2);
|
||||
$this->assert_search_found_topics('phpbb3+installation', 1);
|
||||
$this->assert_search_found_topics('foosubject+barsearch', 1);
|
||||
|
||||
$this->assert_search_not_found('loremipsumdedo');
|
||||
$this->assert_search_found('barsearch-testing', 1, 2); // test hyphen ignored
|
||||
$this->assert_search_found('barsearch+-+testing', 1, 2); // test hyphen wrapped with space ignored
|
||||
|
Loading…
x
Reference in New Issue
Block a user