Follow-up to #2320157: Generate placeholder content for Field types

Problem/Motivation

Now that #2238085: [regression] options_allowed_values() signature doesn't allow for Views filter configuration has landed, we should be able to implement the ListItemBase::generateSampleValue() method.

Steps to reproduce

  1. Install Drupal standard
  2. Download and install Devel Generate (part of Devel)
  3. Add a field on standard pages
    • Name: whatever
    • Type: any type that starts with "List" eg "List (text)"
    • Allowed values list: at least one value
    • Allowed number of values: whatever
  4. Generate standard pages using Devel Generate
  5. Edit one of those pages and see that your field has not been filled

Proposed resolution

Adapt the code from Devel Generate 7.x

function list_devel_generate($object, $field, $instance, $bundle) {
  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
    return devel_generate_multiple('_list_devel_generate', $object, $field, $instance, $bundle);
  }
  else {
    return _list_devel_generate($object, $field, $instance, $bundle);
  }
}

function _list_devel_generate($object, $field, $instance, $bundle) {
  $object_field = array();
  if ($allowed_values = list_allowed_values($field)) {
    $keys = array_keys($allowed_values);
    $object_field['value'] = $keys[mt_rand(0, count($allowed_values) - 1)];
  }
  return $object_field;
}

Remaining tasks

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Create a patch Instructions Done
Update the issue summary noting if allowed during the beta Instructions
Manually test the patch Novice Instructions
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards Instructions

User interface changes

None.

API changes

None.

Why this should be an RC target

That code is never directly used in Core except for tests. It's not disruptive at all. It's going to be used by Devel and enlight #2590993: Create stub entities with proper default values (see #7)

Comments

duaelfr’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new793 bytes
duaelfr’s picture

Issue summary: View changes

Added steps to reproduce.

duaelfr’s picture

I tried to find out how to test it.

Looking in OptionsFieldTest::testUpdateAllowedValues() I found a call to ListItemBase::generateSampleItems() followed by a call to FieldUnitTestBase::entityValidateAndSave() that's supposed to test if generated values are OK. That function seems to work well to validate if a given value is valid but it does not seem to check if there is a value in the field or not, even if the FieldConfig entity has the required setting.

The attached patch is supposed to fail because it makes the test_options field on entity_test required and the ListItemBase::generateSampleItems() cannot generate values. Unless the testbot has different results than my local tests, that path is not going to fail.

Forget that comment. I didn't saw the FieldConfig settings were replaced just before the call to FieldUnitTestBase::entityValidateAndSave().

duaelfr’s picture

I finally found out how to improve tests for that tiny feature.

The last submitted patch, 4: ListItemBase_generateSampleValue-2536374-4-tests-only.patch, failed testing.

mikeryan’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me (and will enable me to remove a kludge from #2590993: Create stub entities with proper default values).

duaelfr’s picture

Issue summary: View changes
Issue tags: +rc target triage

Let's try to get this one in the RC :)

alexpott’s picture

Issue tags: -rc target triage +rc eligible

This is rc eligible as it only changes test code.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed cbef10d and pushed to 8.0.x. Thanks!

  • alexpott committed cbef10d on
    Issue #2536374 by DuaelFr: Generate placeholder content for ListItemBase...

Status: Fixed » Closed (fixed)

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