Steps to reproduce

There are a couple, so bare with me.

  1. Download modules, provided in drupal.make.yml in attachments.
  2. Enable commerce_shipping, commerce_customer_ui, features.
  3. Go to admin/commerce/customer-profiles/types/billing/fields and add new field with label "Test" and field type List (Integer).
    Put two pairs of key|value to allowed values, e.g.:
    0|Set
    1|Ready

    Check Required field & set default value to Set (or any other you've provided). Set number of values to 1.

  4. Go to admin/commerce/customer-profiles/types/shipping/fields and add newly created field to Shipping customer profile.
    Make sure it also Required field and set to the same default value as for BIlling customer profile. Do not change number of values & allowed values settings.
  5. Create feature:
    • Go to admin/structure/features/create.
    • Select in field instances next fields:
      commerce_customer_profile-billing-field_test
      commerce_customer_profile-shipping-field_test
    • Export feature. Name it sample or whatever.
  6. Reinstall drupal.
    Enable commerce_shipping, sample(machine_name of newly created feature) , commerce_product_ui, commerce_product_reference, commerce_cart, commerce_ui.
  7. Go to admin/commerce/config/checkout and move Shipping information pane to Shipping page.
  8. Go to admin/commerce/config/checkout/form/pane/customer_profile_shipping and enable profile copying.
    Make sure Profile copied from Billing information and Make copying information from this profile the default action is checked.
  9. Go to admin/commerce/products and create product.
    Provide any SKU, Title and Price you want.
  10. Go to admin/structure/types/manage/page/fields.
    Create product reference field. Make sure that "add to cart" button will be displayed on node detail page.
    More about it on Drupal Commerce site.
  11. Create new node. Reference newly created product. Then add it to your cart. Proceed to the cart & checkout.
  12. Provide necessary info in billing information.
  13. Continue to the next step.
  14. Do not uncheck My Shipping information is the same as my Billing information checkbox and proceed to the next step.

Expected behavior

Checkout can proceed to the next step and eventually complete.

What i get instead is error:

Test field is required.

Problem/Motivation

Bug happens because of two things:

  1. Feature exports default value for this field instance as integer, rather than string:
    // Exported field_instance: 'commerce_customer_profile-billing-field_test'.
    $field_instances['commerce_customer_profile-billing-field_test'] = array(
      'bundle' => 'billing',
      'default_value' => array(
        0 => array(
          'value' => 0,
        ),
      ),
      'deleted' => 0,
      'description' => '',
      'display' => array(
        'default' => array(
          'label' => 'above',
          'module' => 'list',
          'settings' => array(),
          'type' => 'list_default',
          'weight' => -9,
        ),
      ),
      'entity_type' => 'commerce_customer_profile',
      'field_name' => 'field_test',
      'label' => 'Test',
      'required' => 1,
      'settings' => array(
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'active' => 1,
        'module' => 'options',
        'settings' => array(),
        'type' => 'options_buttons',
        'weight' => -9,
      ),
    );
    
  2. Because condition here on line 172 inside _options_properties() is evaluated as TRUE, whereas should be FALSE.
    The reason for that is $element['#value'] = 0 that comes as a parameter.

Proposed resolution

I suggest replace == operator with === within _options_properties(). This would compare not only value, but type.

Patch is provided.

Comments

Sardis created an issue. See original summary.

Status: Needs review » Needs work

The last submitted patch, options_validate_fix.patch, failed testing. View results

sardis’s picture

Status: Needs work » Needs review
StatusFileSize
new756 bytes

Oops, previous patch was created with IDE. Resubmitting patch made with git diff.

br0ken’s picture

Might be it's late hour but I didn't understand how type checking can resolve the problem between integers and strings. Also, I see the case why your changes break BC.

class A {
  public function __toString() {
    return '_none';
  }
}

var_dump(new A() == '_none'); // true
var_dump(new A() === '_none'); // false

In general, I doubt it is the bug in the core rather than in features.

Can you provide a test?

sardis’s picture

Well, i agree, it seems like a bad export of field instance via features.
But apparently, there could be cases, where $element['#value'] is not a string.
And here's what happens according to PHP manual when you compare Integer with String:

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

In our case:

$element['#value'] = 0;
var_dump($element['#value'] == '_none'); // 0 == 0 -> true

And this is not what we want our code to do.

However:

The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

That's why i decided to use === operator. And it did the trick.

So what should we do about it?

br0ken’s picture

By adding type comparison here you'll definitely break compatibility for some part of users which had this working for years. Not all code produced by Features. Someone may create fields programmatically and, to be honest, I saw too much PHP code for Drupal 7 without any thoughts about types (e.g. '0' or 0).

What do you think about casting a string type for this particular operand in condition?

sardis’s picture

Assigned: Unassigned » sardis
Status: Needs review » Needs work

I like this idea as well. I'll make a patch bit later so that you could review.

David_Rothstein’s picture

Issue tags: +Needs tests

I don't understand what the practical backwards compatibility concern would be with the above patch (#4 is interesting but seems theoretical). That said, the revised solution of casting to a string before checking sounds good too!

I agree that automated tests would be a good idea here. It should be possible to do something simple (not involving Drupal Commerce) that programmatically saves the field instance with a default value of integer zero in order to trigger the bug.

Thanks for a very detailed bug report!

br0ken’s picture

Actually core has these tests (in options.test) but they do not show the problem because of web requests which convert integers to strings (see \OptionsWidgetsTestCase::testRadioButtons()). So, the real good example is to create a custom form with the radios element having the options_field_widget_validate function for its #element_validate.

@David_Rothstein, as I've shown it in my previous comment, field value can be an object, which can be interpreted as a string (it's an edge case, I agree).

br0ken’s picture

Hm, I've tried to create that one and realized that I was wrong. We definitely need a test which is able to reproduce this because submitted values are always converted to strings. Might it be that some of the modules (there are a lot in "commerce" stack) modify the type?

sardis’s picture

From the start i've tried to reproduce bug in easier way. Simply by creating field instance programmatically with default value set in int and then go to page, where this widget is used (it was node add form), but i had the same result.
That's why i had to include whole Drupal Commerce stack in order to reproduce bug, which happens on my dev server.

So i think, you made a good point, @BR0kEN: this might be commerce. But it has to be proved. I'll try and find out.

br0ken’s picture

Both of these patches must lead tests to fail. But the problem is not self-generated, as you can see (I purposely added wrong validation function).

br0ken’s picture

StatusFileSize
new656 bytes
new4.53 KB
br0ken’s picture

@Sardis, you need to put a breakpoint at the first line of the options_field_widget_validate() and step by the stack trace until you find the point where data modifies.

The last submitted patch, 12: 2895721-11--expose-the-problem--initial-state--do-not-commit.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

The last submitted patch, 12: 2895721-11--expose-the-problem--strict-comparison--do-not-commit.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

br0ken’s picture

So now when tests finished you may see the "problem". Of course we can rectify the core a bit but in general, it isn't a problem of the core.

David_Rothstein’s picture

It's probably possible to have the tests force it to be an integer by submitting the form programmatically using drupal_form_submit(). However, I have no idea if that corresponds to anything the Commerce modules are doing.

What's also interesting is that if $element['#value'] ever does wind up set to integer 0 on a required field, the built-in form API validation should reject it also (see https://api.drupal.org/api/drupal/includes%21form.inc/function/_form_val... and in particular the $is_empty_value variable). So it's not obvious why fixing this in the Options module would actually improve things.

Definitely agree that some more debugging would be useful to understand what is going on.

anrikun’s picture

anrikun’s picture

Status: Needs review » Needs work

Applied patch #13
Get Notice : Array to string conversion in options_field_widget_validate() (line 172 in [...]\modules\field\modules\options\options.module). when cardinality is multiple.

anrikun’s picture

Status: Needs work » Needs review
StatusFileSize
new803 bytes

Here is a new patch (that needs tests).

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.