From be529025414722da3bd0c7bd14c152f043184570 Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 30 Oct 2024 21:22:37 +0700 Subject: [PATCH 1/4] [ticket/17422] Fix author_id search results sorting PHPBB-17422 --- phpBB/phpbb/search/base.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/phpBB/phpbb/search/base.php b/phpBB/phpbb/search/base.php index e7d0774b6c..e33dfc8f75 100644 --- a/phpBB/phpbb/search/base.php +++ b/phpBB/phpbb/search/base.php @@ -76,17 +76,10 @@ class base } } - // 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++) @@ -102,11 +95,6 @@ class base } unset($stored_ids); - if ($reverse_ids) - { - $id_ary = array_reverse($id_ary); - } - if (!$complete) { return SEARCH_RESULT_INCOMPLETE; From eeede1ab6c101030e0e2df97eff5fa6f727525ac Mon Sep 17 00:00:00 2001 From: rxu Date: Thu, 31 Oct 2024 00:51:05 +0700 Subject: [PATCH 2/4] [ticket/17422] Add author_id search tests PHPBB-17422 --- tests/functional/search/base.php | 37 ++++++++++++++++++- .../phpbb_functional_test_case.php | 2 +- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/tests/functional/search/base.php b/tests/functional/search/base.php index 0e3aa80fa3..04b69a4934 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_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(); @@ -93,10 +117,16 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case $this->add_lang('common'); // Create a new standard user if needed, topic and post to test searh for author - if (!$this->user_exists('searchforauthoruser')) + $searchforauthoruser_name = 'searchforauthoruser'; + $searchforauthoruser_id = null; // if the user exists, array with user_id will be returned + if (!$this->user_exists($searchforauthoruser_name, $searchforauthoruser_id)) { $searchforauthoruser_id = $this->create_user('searchforauthoruser'); } + else + { + $searchforauthoruser_id = (int) $searchforauthoruser_id[0]; + } $this->remove_user_group('NEWLY_REGISTERED', ['searchforauthoruser']); $this->set_flood_interval(0); $this->login('searchforauthoruser'); @@ -161,6 +191,11 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case $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'); diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index 9e4cb364d0..1652be8e44 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -1525,7 +1525,7 @@ class phpbb_functional_test_case extends phpbb_test_case * * @return bool Returns true if a user exists, false otherwise */ - protected function user_exists($username, $user_id = null) + protected function user_exists(&$username, &$user_id = null) { global $db; From 7086fa746fcba97ffe03df55f49a11b24b35a55a Mon Sep 17 00:00:00 2001 From: rxu Date: Thu, 31 Oct 2024 09:29:25 +0700 Subject: [PATCH 3/4] [ticket/17422] Fix tests PHPBB-17422 --- tests/functional/ucp_attachments_test.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/functional/ucp_attachments_test.php b/tests/functional/ucp_attachments_test.php index d8aac88b49..9614911786 100644 --- a/tests/functional/ucp_attachments_test.php +++ b/tests/functional/ucp_attachments_test.php @@ -24,7 +24,8 @@ class phpbb_functional_ucp_attachments_test extends phpbb_functional_test_case $this->path = __DIR__ . '/fixtures/files/'; $this->add_lang('posting'); - if (!$this->user_exists('ucp-file-test')) + $username = 'ucp-file-test'; + if (!$this->user_exists($username)) { $this->create_user('ucp-file-test'); } From 4194cb2228ad257cda5db8963990d95f30a362c6 Mon Sep 17 00:00:00 2001 From: rxu Date: Thu, 31 Oct 2024 14:55:35 +0700 Subject: [PATCH 4/4] [ticket/17422] Adjust tests code PHPBB-17422 --- tests/functional/search/base.php | 6 ++---- tests/functional/ucp_attachments_test.php | 3 +-- tests/test_framework/phpbb_functional_test_case.php | 8 +++++--- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/tests/functional/search/base.php b/tests/functional/search/base.php index 04b69a4934..e5ee1feee3 100644 --- a/tests/functional/search/base.php +++ b/tests/functional/search/base.php @@ -117,15 +117,13 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case $this->add_lang('common'); // Create a new standard user if needed, topic and post to test searh for author - $searchforauthoruser_name = 'searchforauthoruser'; - $searchforauthoruser_id = null; // if the user exists, array with user_id will be returned - if (!$this->user_exists($searchforauthoruser_name, $searchforauthoruser_id)) + if (!$searchforauthoruser_id = $this->user_exists('searchforauthoruser')) { $searchforauthoruser_id = $this->create_user('searchforauthoruser'); } else { - $searchforauthoruser_id = (int) $searchforauthoruser_id[0]; + $searchforauthoruser_id = key($searchforauthoruser_id); } $this->remove_user_group('NEWLY_REGISTERED', ['searchforauthoruser']); $this->set_flood_interval(0); diff --git a/tests/functional/ucp_attachments_test.php b/tests/functional/ucp_attachments_test.php index 9614911786..d8aac88b49 100644 --- a/tests/functional/ucp_attachments_test.php +++ b/tests/functional/ucp_attachments_test.php @@ -24,8 +24,7 @@ class phpbb_functional_ucp_attachments_test extends phpbb_functional_test_case $this->path = __DIR__ . '/fixtures/files/'; $this->add_lang('posting'); - $username = 'ucp-file-test'; - if (!$this->user_exists($username)) + if (!$this->user_exists('ucp-file-test')) { $this->create_user('ucp-file-test'); } diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index 1652be8e44..6f8bc2d013 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -1523,9 +1523,9 @@ class phpbb_functional_test_case extends phpbb_test_case * @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; @@ -1540,6 +1540,8 @@ class phpbb_functional_test_case extends phpbb_test_case 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; } }