From #478982: Wondering how to test a drupal_http_request() I have discovered what a PITA it is to try and use SimpleTest on Drupal 6.x when testing drupal_http_request for a request to your own site. Basically, in Drupal 7, we added a check to see if the db_prefix global matches SimpleTest, and pass that as the user agent to drupal_http_request instead of the default Drupal user agent. This allows page executions made with drupal_http_request() will not be using the testing db. We can't check if watchdog calls are made, etc.

You can read all the details in #478982: Wondering how to test a drupal_http_request() but what I'm wondering is if it would be ok to backport this change to D6. It does not break APIs and enables easier testing for contrib modules. Patch attached for review.

If this is not accepted, any modules that want to test running a request on a local path will need to do the following in their own code every time they call drupal_http_request();

...
  // Since drupal_http_request() does not pass the SimpleTest user agent 'flag'
  // in Drupal 6 core, we need to manually set the user agent header.
$headers = array();
if (preg_match("/simpletest\d+/", $GLOBALS['db_prefix'], $matches)) {
  $headers['User-Agent'] = $matches[0];
}
...
drupal_http_request($url, $headers);
...

Comments

boombatower’s picture

+1

boombatower’s picture

Status: Needs review » Reviewed & tested by the community

Might as well bump to RTBC, this is a backport and works fine. We just need a decision on it since it isn't a bug per say.

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Helping tests work is important, so committed.

boombatower’s picture

Great! Thanks.

dave reid’s picture

Agreed! Thanks very much Gabor!

Status: Fixed » Closed (fixed)

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

hctom’s picture

Category: task » bug
Status: Closed (fixed) » Active

drupal_http_request() is producing an error with this add-on when using an array for $GLOBALS['db_prefix']

Line 492:

preg_match("/simpletest\d+/", $GLOBALS['db_prefix'], $matches)

preg_match expects the second argument to be a string, not an array... Perhaps the if statement should be extended with an is_string() call like this:

if (is_string($GLOBALS['db_prefix']) && preg_match("/simpletest\d+/", $GLOBALS['db_prefix'], $matches)) {

Thanx in advance & cheers

hctom

dave reid’s picture

Version: 6.x-dev » 7.x-dev
Status: Active » Needs work

Since Drupal 7 allows an array for db_prefix seems like this would need to be fixed in HEAD first now. Possibly other SimpleTest code is affected?

chawl’s picture

Version: 7.x-dev » 6.x-dev
Status: Needs work » Active

@hctom: Line 492 causes "string expected" problem for us even without the "simpletest" module on Drupal 6.13.

I am not sure if another broken module causes this, but if we use "Check manually" on Available Updates page, Drupal 6.13 gives series of

warning: preg_match() expects parameter 2 to be string, array given in /includes/common.inc on line 492

warnings after a remarkably long pause.

With your modified "if", warning disappears but simpletest module does not work anyway.

Tx.

dave reid’s picture

Version: 6.x-dev » 7.x-dev
Status: Active » Needs work

Please do not change the version and issue information.

dave reid’s picture

Status: Needs work » Needs review
StatusFileSize
new2.26 KB

Followup patch for D7 that checks the $_SERVER['HTTP_USER_AGENT'] for the SimpleTest user agent instead of the db_prefix.

Status: Needs review » Needs work

The last submitted patch failed testing.

chawl’s picture

@Dave Reid: Sorry, post form default values have set that.

hctom’s picture

@chawl: Of course the error occures without the simpletest module installed... the mentioned line of code is code form the core common.inc file, so no other module may be in charge :)

I guess the only way to solve this is something like this:

if (is_string($GLOBALS['db_prefix']) && preg_match("/simpletest\d+/", $GLOBALS['db_prefix'], $matches)) {
  $defaults['User-Agent'] = 'User-Agent: ' . $matches[0];
}
elseif (is_array($GLOBALS['db_prefix'])) {
  foreach ($GLOBALS['db_prefix'] as $prefix) {
    if (preg_match("/simpletest\d+/", $prefix, $matches)) {
      $defaults['User-Agent'] = 'User-Agent: ' . $matches[0];
      break;
    }
  }
}

@Dave: what do you think about this solution?

Cheers

hctom

chawl’s picture

@hctom: Tx :) I was really confused even I might suspect a rainy day.

Our problem turns out to be a remnant from Faceted Search module which needs $db_prefix to be defined as an array in the settings.php. But there were no problems before 6.13 strangely.

Anyway, sorry for bloating the thread.

hctom’s picture

I realized the bug during a multisite install preparation where some tables are shared... and so the $GLOBALS['db_prefix'] also has to be an array...

... but you are right... this code fragment is new in 6.13

cheers

hctom

antiorario’s picture

Will we find solutions to the 6.13 problem here?

dave reid’s picture

Status: Needs work » Needs review
StatusFileSize
new2.24 KB
new1.12 KB

Here are the followup patches for D7 and D6 that should fix things of db_prefix is an array. Please test and report back.

dries’s picture

Version: 7.x-dev » 6.x-dev
Status: Needs review » Reviewed & tested by the community

Committed to CVS HEAD. Thanks!

sethcohn’s picture

+1, please commit to D6 ASAP. 6.13 currently generates errors for any http request it generates (when module updates are checked, for example) on any site (as above) with db_prefix setup as an array, ie any multi-site, or using faceted search, for just a few common examples.

amedee-1’s picture

+1
Used the D6 patch on a 6.13 multisite, the errors are gone with this patch.

roball’s picture

I have also been affected with on multisite install of D6.13. The D6 patch from #18 should really be committed before releasing D6.14.

damienmckenna’s picture

The patch in #18 resolved the problem for me in 6.13.

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Thanks, committed to Drupal 6 too.

EvanDonovan’s picture

Subscribing. (I know it's fixed, but I wanted to remind myself I needed to patch this in my Drupal 6.x installs until 6.14 is released.)

Status: Fixed » Closed (fixed)

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

Breakerandi’s picture

Status: Closed (fixed) » Needs work

In a multisite setup with shared tables, $db_prefix is *not* a string, but an array (see http://drupal.org/node/2622). The patch for this issue that made it into D6.13, however, doesn't handle this case ($db_prefix an array) correctly, since line 492 in common.inc

if (preg_match("/simpletest\d+/", $GLOBALS['db_prefix'], $matches)) {

now assumes $db_prefix to be a string. Therefore, on certain requests/actions an error/warning occurs, like:

warning: preg_match() expects parameter 2 to be string, array given in /srv/www/vhosts/freerunning.net/httpdocs/includes/common.inc on line 492.

Is there any solution in the making?
(like an "foreach($db_prefix ...)" loop?)

dave reid’s picture

Status: Needs work » Fixed

@Breakerandi The patch was committed to the Drupal 6 branch and will be included in the next release (6.14). It is not included in 6.13.

Breakerandi’s picture

So in Drupal 6.14 the case that $db_prefix is an array will be handled correctly, right?

dave reid’s picture

@Breakerandi: Correct. When 6.14 is released, everything will work just fine.

hctom’s picture

@Everybody, who helped with this issue: Thanx for all the work :)

I'm looking for ward to the new Drupal release

Cheers

hctom

boombatower’s picture

Yea, again!

Breakerandi’s picture

Great, Thank you all, too :-)

Breakerandi’s picture

Now everything works fine as Dave Reid hase prognosed in #30. Thank you very much! Drupal 6.14 rulz :-)

Status: Fixed » Closed (fixed)

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