diff --git a/tests/functional/search/base.php b/tests/functional/search/base.php index 004ee13c25..ed0acc12bd 100644 --- a/tests/functional/search/base.php +++ b/tests/functional/search/base.php @@ -16,25 +16,37 @@ */ abstract class phpbb_functional_search_base extends phpbb_functional_test_case { - protected function assert_search_found($keywords, $posts_found, $words_highlighted) + protected function assert_search_found($keywords, $posts_found, $words_highlighted, $sort_key = '') { $this->purge_cache(); - $crawler = self::request('GET', 'search.php?keywords=' . $keywords); + $crawler = self::request('GET', 'search.php?keywords=' . $keywords . ($sort_key ? "&sk=$sort_key" : '')); $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) + protected function assert_search_found_topics($keywords, $topics_found, $sort_key = '') { $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); + $crawler = self::request('GET', 'search.php?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_posts_by_author($author, $posts_found, $sort_key = '') + { + $this->purge_cache(); + $crawler = self::request('GET', 'search.php?author=' . $author . ($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_by_author($author, $topics_found, $sort_key = '') + { + $this->purge_cache(); + $crawler = self::request('GET', 'search.php?sr=topics&author=' . $author . ($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) @@ -45,8 +57,27 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case $this->assertEquals($split_keywords_string, $crawler->filter('#keywords')->attr('value'), $this->search_backend); } + protected function assert_search_for_author_not_found($author) + { + $this->add_lang('search'); + $crawler = self::request('GET', 'search.php?author=' . $author); + $this->assertContainsLang('NO_SEARCH_RESULTS', $crawler->text(), $this->search_backend); + } + public function test_search_backend() { + // Create a new standard user if needed, topic and post to test searh for author + if (!$this->user_exists('searchforauthoruser')) + { + $searchforauthoruser_id = $this->create_user('searchforauthoruser'); + } + $this->remove_user_group('NEWLY_REGISTERED', ['searchforauthoruser']); + $this->disable_flood_interval(); + $this->login('searchforauthoruser'); + $topic_by_author = $this->create_topic(2, 'Test Topic from searchforauthoruser', 'This is a test topic posted by searchforauthoruser to test searching by author.'); + $this->create_post(2, $topic_by_author['topic_id'], 'Re: Test Topic from searchforauthoruser', 'This is a test post posted by searchforauthoruser'); + $this->logout(); + $this->login(); $this->admin_login(); @@ -54,6 +85,7 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case $post = $this->create_topic(2, 'Test Topic 1 foosubject', 'This is a test topic posted by the barsearch testing framework.'); + $crawler = self::request('GET', 'adm/index.php?i=acp_search&mode=settings&sid=' . $this->sid); $form = $crawler->selectButton('Submit')->form(); $values = $form->getValues(); @@ -72,6 +104,7 @@ abstract class phpbb_functional_search_base extends phpbb_functional_test_case if ($crawler->filter('.errorbox')->count() > 0) { $this->delete_topic($post['topic_id']); + $this->delete_topic($topic_by_author['topic_id']); $this->markTestSkipped("Search backend is not supported/running"); } @@ -79,20 +112,29 @@ 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); + + foreach (['', 'a', 't', 'f', 'i', 's'] as $sort_key) + { + $this->assert_search_found('phpbb3+installation', 1, 3, $sort_key); + $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_topics('phpbb3+installation', 1, $sort_key); + $this->assert_search_found_topics('foosubject+barsearch', 1, $sort_key); + + $this->assert_search_posts_by_author('searchforauthoruser', 2, $sort_key); + $this->assert_search_topics_by_author('searchforauthoruser', 1, $sort_key); + } $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 $this->assert_search_not_found('barsearch+-testing'); // test excluding keyword + $this->assert_search_for_author_not_found('authornotexists'); $this->login(); $this->admin_login(); $this->delete_search_index(); $this->delete_topic($post['topic_id']); + $this->delete_topic($topic_by_author['topic_id']); } protected function create_search_index($backend = null) diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index 824dc2811a..35a1338850 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -1437,4 +1437,112 @@ class phpbb_functional_test_case extends phpbb_test_case return $file_form_data; } + + /** + * Get HTML of the crawler + * See https://symfony.com/doc/current/components/dom_crawler.html#component-dom-crawler-dumping + * + * @param Symfony\Component\DomCrawler\Crawler $crawler Crawler instance + * @param string $url Request URL + * + * @return array Hidden form fields array + */ + protected function dump_crawler($crawler) + { + if (!$crawler) + { + return; + } + + $html = ''; + foreach ($crawler as $domElement) + { + $html .= $domElement->ownerDocument->saveHTML($domElement); + } + + return $html; + } + + /** + * Get username of currently logged in user + * + * @return string|bool username if logged in, false otherwise + */ + protected function get_logged_in_user() + { + $username_logged_in = false; + $crawler = self::request('GET', 'index.php'); + $is_logged_in = strpos($crawler->filter('div[class="navbar"]')->text(), 'Login') === false; + if ($is_logged_in) + { + $username_logged_in = $crawler->filter('li[id="username_logged_in"] > div > a > span')->text(); + } + return $username_logged_in; + } + + /** + * Disable posting flood control + */ + protected function disable_flood_interval() + { + $relogin_back = false; + $logged_in_username = $this->get_logged_in_user(); + if ($logged_in_username && $logged_in_username !== 'admin') + { + $this->logout(); + $relogin_back = true; + } + + if (!$logged_in_username || $relogin_back) + { + $this->login(); + $this->admin_login(); + } + + $this->add_lang('acp/common'); + $crawler = self::request('GET', 'adm/index.php?i=acp_board&mode=post&sid=' . $this->sid); + $form = $crawler->selectButton('submit')->form([ + 'config[flood_interval]' => 0, + ]); + $crawler = self::submit($form); + $this->assertContainsLang('CONFIG_UPDATED', $crawler->text()); + + // Get logged out back or get logged in in user back if needed + if (!$logged_in_username) + { + $this->logout(); + } + + if ($relogin_back) + { + $this->logout(); + $this->login($logged_in_username); + } + } + + /** + * Check if a user exists by username(s) or user_id(s) + * + * @param array &$user_id_ary The user ids to check or empty if usernames used + * @param array &$username_ary The usernames to check or empty if user ids used + * + * @return bool Returns true if a user exists, false otherwise + */ + protected function user_exists($username, $user_id = null) + { + global $db; + + $db = $this->get_db(); + + if (!function_exists('utf_clean_string')) + { + require_once(__DIR__ . '/../../phpBB/includes/utf/utf_tools.php'); + } + if (!function_exists('user_get_id_name')) + { + require_once(__DIR__ . '/../../phpBB/includes/functions_user.php'); + } + + return user_get_id_name($user_id, $username) ? false : true; + } }