Problem/Motivation
Timestamp fields have a range constraint so that no value outside of the range of the database storage can be saved. The changed and created field types extend the timestamp field type but are missing that constraint. That means that invalid created or changed values that enter the system via a migration or JSON:API, for example, are not properly validated and may cause exceptions when being saved.
Steps to reproduce
$node = Node::create([
'title' => 'Back to the future',
'type' => 'article',
// This is not a 32-bit integer.
'created' => 2147483649
]);
// This yields 0, thus no violation is recorded.
$node->validate()->count();
// Brace for SQL exception.
$node->save();
Proposed resolution
Copy the constraints over from TimestampItem to CreatedItem and ChangedItem.
Remaining tasks
User interface changes
-
Introduced terminology
-
API changes
-
Data model changes
-
Release notes snippet
-
| Comment | File | Size | Author |
|---|
Issue fork drupal-3521088
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3521088-createditem-and-changeditem
changes, plain diff MR !11970
Comments
Comment #2
annmarysruthy commentedComment #4
annmarysruthy commentedComment #5
tstoecklerThanks for picking this up @annmarysruthy!
Looks perfect.
This will need some test coverage to go in, though. I looked around a bit and found
\Drupal\KernelTests\Core\Entity\EntityValidationTest::checkValidation. I think it would make sense to add some checks to that as that already tests various field-type-specific constraints. All the tested entity types should also have a created field, so just adding a test for that should work fine. And for the changed field you can check if the entity type implementsEntityChangedInterfaceas only a couple entity types have that.Marking needs work for that.
Comment #6
prabha1997 commentedI am working on this issue
Comment #7
prabha1997 commentedI tried to add validation for the ChangedItem field as shown below, but I am not getting the expected validation failure for the invalid changed timestamp.
Comment #8
tstoecklerNice work with the test coverage.
Ahh, how annoying. I think that is because one of the test entities uses the
changed_testitem (i.e.\Drupal\entity_test\Plugin\Field\FieldType\ChangedTestItem) instead of the regularchanged. I had missed that initially when looking into this. I guess we could also add the constraint to the test field type one, but that then kind of feels like testing the test coverage. Alternatively we could check that$entity->get('changed')->getFieldDefinition()->getType()is in factchanged(and notchanged_test). I guess I like the latter solution a bit better, but also open to what you think.Comment #9
prabha1997 commentedI've updated the test to include a check for the actual field type, so it now only runs if the changed field uses the real 'changed' type (not the changed_test).
Comment #10
tstoecklerThanks for sticking with this @prabha1997!
I tried it out locally and I guess my suggestion above was not really great. Turns out the inside of the if is reached. I had seen that the
entity_test_constraintsentity type has an actualchangedfield, but unfortunately that is not one of the entity types used in the test. (See\Drupal\entity_test\EntityTestHelper::getEntityTypes().) All of this is a bit of a mess, but not one we will have to fix here, I hope. So I looked into how to get an actualchangedfield into the entity types being tested without rewriting the entire test and found that the test entity types support adding base field definitions via state. I wasn't sure whether that actually worked as intended so I tried it out to be sure. And since I already had it then I went ahead and pushed it to the branch, I hope that was OK.Maybe you can take a look and see if you agree with those changes, thanks!
Comment #11
smustgrave commentedRan the test only job here https://git.drupalcode.org/issue/drupal-3521088/-/jobs/5700854 and got
The use 2147483648 makes sense as mentioned it's what Timestamp uses.
So no objections.
Comment #12
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #13
tstoecklerRebased (and squashed). Back to RTBC per #11
Comment #15
quietone commentedTriaging the RTBC queue.
All questions are answered here and credit is updated.
I did think that the wording is the comments in the test could be improved and simplified a bit. Plus one was not wrapped correctly. Also, there were 2 @see lines in the method. I thought one was not necessary and the other should be in the method doc block. Given those changes are only to comments and did not change the intent I am leaving at RTBC.
Comment #19
catchThis looks good, I originally mis-read #15 as needing fixes but then saw the commit to improve the comments and agreed with leaving RTBC. Committed/pushed to 11.x, thanks!
Comment #22
wim leersWow! I just ran into this while working on #3533675: Avoid suggesting UNIX timestamp integers for `type: integer` props! So nice to see this fixed already 🤩