Using Drupal 7.38 with no additional modules

Steps to reproduce:
1. Use search block and search for 2 letter string
2. Use search block again to search for a longer string. This should be done while still being in search results page

Expected behaviour:
1. Use search block with 2 letter string - get error
2. Use search block in search results page with a longer string - get new results

Current behaviour:
1. Use search block with 2 letter string - get error
2. Use search block in search results page with a longer string - get the same error (You must include at least one positive keyword with 3 characters or more.)

Note that the following scenario works fine:
1. Use search block with 3 letter string - get results
2. Use search block in search results page with another string - get new results

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cilefen’s picture

Version: 7.38 » 7.x-dev

I have confirmed this. I checked and Drupal 8 does not have this issue.

jhodgdon’s picture

That's a weird one. Thanks for reporting this!

Da_Cloud’s picture

I recently received the same situation as a bug report on one of my sites and I think I've found a solution to this problem. I've added an patch and in this comment you can find the thought process behind the patch.

In short this problem occurs because the submit handler of the search block (which redirects the user to the new search request) is never triggered duo to an error with the old search request.

Say you have the following situation:

  1. You've searched on the string "a". This will result in the url "search/node/a".
  2. You'll then try to search on the string "test" in the search box.
  3. The html submit function will first re-open the current page "search/node/a", with the new search parameters in the $_POST variable.
  4. This page load will trigger an search action on the old string "a". Which will result in an error
  5. Drupal will then run drupal_process_form() [form.inc line 864] to process the searchbox form, but will get stuck because form_get_errors() will return an error, meaning the submit handler will never be called.

Looking through the code I noticed that the search form itself ignores step 4 of the above situation by checking if the $_POST['form_id'] variable doesn't contain the string 'search_form';

  // Process the search form. Note that if there is $_POST data,
  // search_form_submit() will cause a redirect to search/[module path]/[keys],
  // which will get us back to this page callback. In other words, the search
  // form submits with POST but redirects to GET. This way we can keep
  // the search query URL clean as a whistle.
  if (empty($_POST['form_id']) || $_POST['form_id'] != 'search_form') {

The quickest way to solve this issue would be to extend this statement to also check on the form_id 'search_block_form'.

if (empty($_POST['form_id']) || ($_POST['form_id'] != 'search_form' && $_POST['form_id'] != 'search_block_form')) {
Da_Cloud’s picture

Status: Active » Needs review
jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! That looks like the right fix.

I also tested manually. Without the patch, the described behavior happens if you use the Search block, but not if you use the Search form on the search results page. With the patch, the Search block and Search form on the search results page both work. This simple patch makes this happen. Let's get it in when we can.

I guess we should technically have an automated test for it though... but maybe not necessary?

David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests, +Novice

The fix looks very nice, but yeah I think we should have tests. Mostly because after looking through Git history, they already exist from when this was fixed in Drupal 8, so all we really have to do is backport them. See:
#1366020-29: Overhaul SearchQuery; make search redirects use GET query params for keywords
#2017095: After searching for a too-short word, original search keyword is retained in subsequent searches (an earlier issue which reported the same thing as this one)

Drupal 8 tests can be found towards the bottom of SearchBlockTest::testSearchFormBlock.

It might be considered a "Novice" task to backport those and make sure the Drupal 7 patch here makes them pass.

Da_Cloud’s picture

Status: Needs work » Needs review
FileSize
2.2 KB

I've given it a shot to try and backport the tests back to Drupal 7. Its my first hands-on experience with simpletest so please take a good look at them.

Da_Cloud’s picture

My bad. Completely forgot to check if the new simpletests would fail without the patch, so I found a minor issue in the tests. Below patch should be correct though.

cilefen’s picture

@da_cloud One way to be sure is to post a tests-only patch along with a full patch in a single comment. Attach the tests-only patch first.

Da_Cloud’s picture

Thanks for the tip. I've added both a test-only patch containing just the simpletests and the real patch which also includes the fix below.

The test-only patch should fail on the SearchBlockTestCase specifically line 677 where it picks up an error that shouldn't be there.

The last submitted patch, 10: drupal-test-only-patch-2530652.patch, failed testing.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

Nice work! This looks perfect to me, and the test bot confirms that the test fails in the expected way without the patch.

David_Rothstein’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 7.x - thanks!

  • David_Rothstein committed 111d400 on 7.x
    Issue #2530652 by da_cloud, jhodgdon, cilefen, marbard: Cannot use the...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.