Problem/Motivation

Working on #2590993: Create stub entities with proper default values, I'm using FieldItemInterface::generateSampleValue() to generate values for a user entity stub. This fails for user entities - generateSampleValue() returns a string too long for the 60 characters (USERNAME_MAX_LENGTH) that are stored. That's based on the max_length from the base field definition for 'name', which is not set and thus defaults to 255. There is a comment "No Length constraint here because the UserName constraint also covers that.", but the constraint is not applied in generateSampleValue().

Proposed resolution

Set a max length of USERNAME_MAX_LENGTH in User::baseFieldDefinitions().

Remaining tasks

Write a test.

User interface changes

None

API changes

None

Data model changes

None

Comments

mikeryan created an issue. See original summary.

mikeryan’s picture

Status: Active » Needs review
Issue tags: +Needs tests
StatusFileSize
new685 bytes
mikeryan’s picture

Issue tags: -Needs tests
StatusFileSize
new1.93 KB

With a test... But, a little hitch here, adding the length creates redundant violations. Removing the length check from the UserName constraint causes other errors, which I haven't time to figure out, but I guess that means there was a good reason to make that check in the explicit constraint. Not quite sure how to resolve this...

The last submitted patch, 2: user_name_max_length-2602066-2.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 3: user_name_max_length-2602066-3.patch, failed testing.

mikeryan’s picture

So, in addition to the error I expected based on my local testing (redundant reports of the length violation):

testValidation

fail: [Other] Line 90 of core/modules/user/src/Tests/UserValidationTest.php:
Violation found when name is too long.
Value 2 is equal to value 1.

there's a whole bunch like

runUpdates

fail: [Other] Line 273 of core/modules/system/src/Tests/Update/UpdatePathTestBase.php:
After all updates ran, entity schema is up to date.

So, I guess there needs to be an update function for the entity schema, although looking at user.entity_schema_data in key_value I don't see why that would have changed?

mikeryan’s picture

Oh, I see it now, user.field_schema_data.name is the place...

mikeryan’s picture

Status: Needs work » Needs review
StatusFileSize
new1.15 KB

Capturing my attempts to update the field schema, added an update function that does:

  $fields = \Drupal::entityManager()->getFieldDefinitions('user', 'user');
  \Drupal::service('entity.definition_update_manager')
    ->updateFieldStorageDefinition($fields['name']);

Stepping through updateFieldStorageDefinition, I see that $original->itemDefinition->definition['settings']['max_length'] is 255, and $storage_definition->itemDefinition->definition['settings']['max_length'] is 60 - perfect! But, $original->schema['columns']['value']['length'] is 255, even though the actual SQL schema in use is 60 (forced in UserStorageSchema::getSharedTableFieldSchema()). So, the update fails with a complaint that you can't change the schema for a field that is already populated, even though there is no actual schema change. I can see in key_value that the entity.definitions.installed collection user.field_storage_definitions has the wrong column length, so it seems that there is an underlying bug in saving the field_storage_definitions that it doesn't necessarily reflect the true SQL schema. Right now, for the purposes of performing this update, I don't see how to fix that - anyone with a deeper understanding of the field internals have a thought?

mikeryan’s picture

Status: Needs review » Needs work

Oops, ignore that patch.

mikeryan’s picture

StatusFileSize
new3.18 KB

The last submitted patch, 8: user_name_max_length-2602066-8.patch, failed testing.

mikeryan’s picture

Status: Needs work » Needs review

#10 didn't get tested...

mikeryan’s picture

Priority: Normal » Major
Issue tags: +rc target triage

Thinking through a bit about what I discovered, I believe that this discrepancy in the stored username schema length doesn't just get in the way of my migration-related change, but that any future attempt to change user field definitions will get blocked - raising to Major.

Status: Needs review » Needs work

The last submitted patch, 10: user_name_max_length-2602066-10.patch, failed testing.

berdir’s picture

xjm’s picture

Issue tags: -rc target triage

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

boobaa’s picture

Please keep in mind that the length of the usernames are not only stored in actually TWO constants (USERNAME_MAX_LENGTH in the module file and UserInterface::USERNAME_MAX_LENGTH), but also in about 8+ other files, too. Here's a list of files I came across containing the magic number of 60 (or 61 for tests; this list might be incomplete):

  • core/modules/comment/comment.install
  • core/modules/comment/src/CommentForm.php
  • core/modules/comment/src/Entity/Comment.php
  • core/modules/comment/tests/src/Kernel/CommentValidationTest.php
  • core/modules/content_translation/src/ContentTranslationHandler.php
  • core/modules/migrate/tests/src/Kernel/MigrateEntityContentValidationTest.php
  • core/modules/node/src/Plugin/Action/AssignOwnerNode.php
  • core/modules/user/tests/src/Kernel/UserValidationTest.php

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.