Skip to content

Commit

Permalink
Merge pull request phpbb#6744 from rxu/ticket/17422
Browse files Browse the repository at this point in the history
[ticket/17422] Fix search results sorting - 3.3.x
  • Loading branch information
marc1706 committed Nov 24, 2024
2 parents 5c7cbbc + 4194cb2 commit c7e68fb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
16 changes: 2 additions & 14 deletions phpBB/phpbb/search/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,10 @@ function obtain_ids($search_key, &$result_count, &$id_ary, &$start, $per_page, $
}
}

// change the start to the actual end of the current request if the sort direction differs
// from the dirction in the cache and reverse the ids later
// If the sort direction differs from the direction in the cache, then reverse the ids array
if ($reverse_ids)
{
$start = $result_count - $start - $per_page;

// the user requested a page past the last index
if ($start < 0)
{
return SEARCH_RESULT_NOT_IN_CACHE;
}
$stored_ids = array_reverse($stored_ids);
}

for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
Expand All @@ -102,11 +95,6 @@ function obtain_ids($search_key, &$result_count, &$id_ary, &$start, $per_page, $
}
unset($stored_ids);

if ($reverse_ids)
{
$id_ary = array_reverse($id_ary);
}

if (!$complete)
{
return SEARCH_RESULT_INCOMPLETE;
Expand Down
35 changes: 34 additions & 1 deletion tests/functional/search/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ protected function assert_search_topics_by_author($author, $topics_found, $sort_
$this->assertStringContainsString("Search found $topics_found match", $crawler->filter('.searchresults-title')->text(), $this->search_backend);
}

protected function assert_search_posts_by_author_id($author_id, $posts_found, $sort_key = '', $sort_dir = '')
{
// Test obtaining data from cache if sorting direction is set
if (!$sort_dir)
{
$this->purge_cache();
}
$crawler = self::request('GET', 'search.php?author_id=' . $author_id . ($sort_key ? "&sk=$sort_key" : '') . ($sort_dir ? "&sk=$sort_dir" : ''));
$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_by_author_id($author_id, $topics_found, $sort_key = '', $sort_dir = '')
{
// Test obtaining data from cache if sorting direction is set
if (!$sort_dir)
{
$this->purge_cache();
}
$crawler = self::request('GET', 'search.php?sr=topics&author_id=' . $author_id . ($sort_key ? "&sk=$sort_key" : '') . ($sort_dir ? "&sk=$sort_dir" : ''));
$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_in_topic($topic_id, $keywords, $posts_found, $sort_key = '')
{
$this->purge_cache();
Expand Down Expand Up @@ -93,10 +117,14 @@ public function test_search_backend()
$this->add_lang('common');

// Create a new standard user if needed, topic and post to test searh for author
if (!$this->user_exists('searchforauthoruser'))
if (!$searchforauthoruser_id = $this->user_exists('searchforauthoruser'))
{
$searchforauthoruser_id = $this->create_user('searchforauthoruser');
}
else
{
$searchforauthoruser_id = key($searchforauthoruser_id);
}
$this->remove_user_group('NEWLY_REGISTERED', ['searchforauthoruser']);
$this->set_flood_interval(0);
$this->login('searchforauthoruser');
Expand Down Expand Up @@ -161,6 +189,11 @@ public function test_search_backend()

$this->assert_search_posts_by_author('searchforauthoruser', 2, $sort_key);
$this->assert_search_topics_by_author('searchforauthoruser', 1, $sort_key);

$this->assert_search_posts_by_author_id($searchforauthoruser_id, 2, $sort_key);
$this->assert_search_topics_by_author_id($searchforauthoruser_id, 1, $sort_key);
$this->assert_search_posts_by_author_id($searchforauthoruser_id, 2, $sort_key, 'a'); //search asc order
$this->assert_search_topics_by_author_id($searchforauthoruser_id, 1, $sort_key, 'a'); // search asc order
}

$this->assert_search_not_found('loremipsumdedo');
Expand Down
8 changes: 5 additions & 3 deletions tests/test_framework/phpbb_functional_test_case.php
Original file line number Diff line number Diff line change
Expand Up @@ -1523,9 +1523,9 @@ protected function set_flood_interval($flood_interval)
* @param string $username The username to check or empty if user_id is used
* @param int $user_id The user id to check or empty if username is used
*
* @return bool Returns true if a user exists, false otherwise
* @return array Returns user_id => username array or empty array if user does not exist
*/
protected function user_exists($username, $user_id = null)
protected function user_exists($username = '', $user_id = '')
{
global $db;

Expand All @@ -1540,6 +1540,8 @@ protected function user_exists($username, $user_id = null)
require_once(__DIR__ . '/../../phpBB/includes/functions_user.php');
}

return user_get_id_name($user_id, $username) ? false : true;
user_get_id_name($user_id, $username, false, true);

return $username;
}
}

0 comments on commit c7e68fb

Please sign in to comment.