When running tests involving flags, calls to flag_get_flags() return nothing due to a static caching bug in simpletest - see #851036: Simpletest has static caching bugs. The way around this is to call drupal_static_reset() in one's tests, but of course this only works for static variables that use Drupal's centralized static variable cache. Flag currently uses plain old static variable declarations. The attached patch converts them all to use drupal_static().

Comments

corbacho’s picture

I was searching for other stuff... but I found this nice patch, it deserves a little attention... bumping

jaydub’s picture

Status: Active » Needs review
StatusFileSize
new4.47 KB

The patch doesn't apply likely due to the extra text in the header. Here is a re-rolled patch to apply cleanly to current d7.

I didn't apply the drupal_static to the static variables defined inside the flag class as I'm unsure that the behavior would work correctly in a class method vs a function.

Whether or not it's important to convert the static to drupal_static remains to be seen as per this mention on the drupal_static api page:

"A guideline is that if a function's static variable does not depend on any information outside of the function that might change during a single page request, then it's ok to use the "static" keyword instead of the drupal_static() function."

If however converting to drupal_static helps with testing as the original poster suggests then this patch can help in that regard.

quicksketch’s picture

This patch is a good idea. I think we would accidentally be modifying the return structure in some situations though. For example this change:

 function flag_views_flag_default($content_type) {
-  static $default_flag = array();
+  $default_flag = &drupal_static(__FUNCTION__);

The drupal_static() approach doesn't initialize the value to array() for us, so $default_flag would be NULL instead of an array. Looks like it just needs to be fixed to:

 function flag_views_flag_default($content_type) {
-  static $default_flag = array();
+  $default_flag = &drupal_static(__FUNCTION__, array());
quicksketch’s picture

Status: Needs review » Needs work
joachim’s picture

Version: 7.x-2.x-dev » 7.x-3.x-dev
Issue tags: -Simpletest, -testing +Needs backport to D7
joachim’s picture

Status: Needs work » Needs review
StatusFileSize
new4.11 KB

Rerolled and made the changes from #3.

Note that one of the changes to drupal_static() has already been handled by #1395034: Stale data as static cache isn't cleared on flag.

Testbot!

joachim’s picture

Status: Needs review » Fixed

Tests pass. Committed.

Issue #1113904 by katbailey, jaydub, joachim: Changed use of static to drupal_static().

joachim’s picture

Version: 7.x-3.x-dev » 7.x-2.x-dev
Status: Fixed » Patch (to be ported)
Issue tags: +Novice

Should be backported to 7.x-2.x. The patch in #2 would be the one to start from, with corrections from #3.

socketwench’s picture

StatusFileSize
new4.1 KB

Here you go!

socketwench’s picture

Status: Patch (to be ported) » Needs review
joachim’s picture

Status: Needs review » Reviewed & tested by the community

Tests pass and looks good to me.

socketwench’s picture

Status: Reviewed & tested by the community » Fixed

And committed.

Status: Fixed » Closed (fixed)
Issue tags: -Novice, -Needs backport to D7

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