diff --git a/phpBB/phpbb/search/backend/fulltext_native.php b/phpBB/phpbb/search/backend/fulltext_native.php index ca71ae8bae..ffdcbff3db 100644 --- a/phpBB/phpbb/search/backend/fulltext_native.php +++ b/phpBB/phpbb/search/backend/fulltext_native.php @@ -308,7 +308,11 @@ class fulltext_native extends base implements search_backend_interface ); $keywords = preg_replace($match, $replace, $keywords); - $num_keywords = count(explode(' ', $keywords)); + + // Ensure a space exists before +, - and | to make the split and count work correctly + $countable_keywords = preg_replace('/(?config['max_num_search_keywords'] && $num_keywords > $this->config['max_num_search_keywords']) diff --git a/tests/search/native_test.php b/tests/search/native_test.php index 234bfff47d..50f2119e7f 100644 --- a/tests/search/native_test.php +++ b/tests/search/native_test.php @@ -34,6 +34,10 @@ class phpbb_search_native_test extends phpbb_search_test_case $language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); $user = $this->createMock('\phpbb\user'); + $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx); + $lang = new \phpbb\language\language($lang_loader); + $user = new \phpbb\user($lang, '\phpbb\datetime');; + $this->db = $this->new_dbal(); $tools_factory = new \phpbb\db\tools\factory(); $this->db_tools = $tools_factory->get($this->new_doctrine_dbal()); @@ -41,6 +45,7 @@ class phpbb_search_native_test extends phpbb_search_test_case $class = self::get_search_wrapper('\phpbb\search\backend\fulltext_native'); $config['fulltext_native_min_chars'] = 2; $config['fulltext_native_max_chars'] = 14; + $config['max_num_search_keywords'] = 10; $this->search = new $class($config, $this->db, $this->db_tools, $phpbb_dispatcher, $language, $user, SEARCH_RESULTS_TABLE, SEARCH_WORDLIST_TABLE, SEARCH_WORDMATCH_TABLE, $phpbb_root_path, $phpEx); } @@ -262,4 +267,78 @@ class phpbb_search_native_test extends phpbb_search_test_case } $this->assert_array_content_equals($common, $this->search->get_common_words()); } + + public function data_split_keywords_max(): array + { + return [ + 'character count within limits separated by more spaces' => [ + 'foo bar baz boo far faz roo rar raz zoo', + 'all', + false, + ], + 'character count within limits separated by spaces' => [ + 'foo bar baz boo far faz roo rar raz zoo', + 'all', + false, + ], + 'character count within limits separated by +, spaces after +' => [ + 'foo+ bar+ baz+ boo+ far+ faz+ roo+ rar+ raz+ zoo', + 'all', + false, + ], + 'character count within limits separated by +, no spaces' => [ + 'foo+bar+baz+boo+far+faz+roo+rar+raz+zoo', + 'all', + false, + ], + 'character count outside limits separated by +, no spaces' => [ + 'foo+bar+baz+boo+far+faz+roo+rar+raz+zoo+zar', + 'all', + true, + ], + 'character count outside limits separated by + and spaces' => [ + 'foo +bar +baz +boo +far +faz +roo +rar +raz +zoo +zar', + 'all', + true, + ], + 'character count outside limits separated by spaces' => [ + 'foo bar baz boo far faz roo rar raz zoo zar', + 'all', + true, + ], + 'character count outside limits separated by -, no spaces' => [ + 'foo-bar-baz-boo-far-faz-roo-rar-raz-zoo-zar', + 'all', + true, + ], + 'character count outside limits separated by - and spaces' => [ + 'foo -bar -baz -boo -far -faz -roo -rar -raz -zoo -zar', + 'all', + true, + ], + 'character count outside limits separated by |, no spaces' => [ + 'foo|bar|baz|boo|far|faz|roo|rar|raz|zoo|zar', + 'all', + true, + ], + 'character count outside limits separated by | and spaces' => [ + 'foo |bar |baz |boo |far |faz |roo |rar |raz |zoo |zar', + 'all', + true, + ], + ]; + } + + /** + * @dataProvider data_split_keywords_max + */ + public function test_split_max_keywords($keywords, $terms, $expect_error) + { + if ($expect_error) + { + $this->setExpectedTriggerError(E_USER_NOTICE, 'MAX_NUM_SEARCH_KEYWORDS_REFINE'); + } + + $this->assertTrue($this->search->split_keywords($keywords, $terms)); + } }