Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The "Authored on" field on the comment edit form doesn't actually show the date the comment was created, only the date it was modified.
Usually these are one and the same, but if you edit the date, save the comment, and then re-edit the comment, your date change won't be there, because the field displays the modification date instead. The field writes to $comment->created just fine but the form doesn't display that field for editing. To reproduce:
- Create a comment.
- Edit the date of the comment to be one month ago, or whatever. Save. See that the change does show when you view the comment.
- Edit the comment. See how the Authored on field shows the modification date (today) rather than the month-ago date.
Comment | File | Size | Author |
---|---|---|---|
#24 | comment-1005004-24.patch | 645 bytes | tmctighe |
#19 | 1005004-19-tests-only.patch | 4.11 KB | markabur |
#19 | 1005004-19-test-and-fix.patch | 4.81 KB | markabur |
#20 | 1005004-19-tests-only.patch | 4.16 KB | markabur |
#20 | 1005004-19-test-and-fix.patch | 4.86 KB | markabur |
Comments
Comment #1
markabur CreditAttribution: markabur commentedHere's the test that shows the problem.
Comment #2
markabur CreditAttribution: markabur commentedHere's the patch that fixes the problem. It doesn't include the test patch above, so they both need to be applied.
The fix has two parts, because the test fails for two reasons.
First part. Testing for $comment->date is not correct here, as that's the name of the form field, not the database field.
Second part. Saving the comment is timezone-sensitive, making the test fail if you're in any timezone other than +0300. These changes force the system to be UTC for this test, so that the field doesn't change unexpectedly:
Comment #3
markabur CreditAttribution: markabur commentedCool, testbot actually ran the test in #1. Here's a patch that combines #1 and #2, should still be green...
Comment #4
markabur CreditAttribution: markabur commentedHere's where the bug came from; it's not present in d6: #537062: Remove unneeded comment_edit() page callback
Comment #5
markabur CreditAttribution: markabur commentedThought about this and realized that this bug causes a type of data loss. If we add one more "save" step to the test then the bad date would be written to the database.
Comment #6
markabur CreditAttribution: markabur commentedSetting this to major as it's a regression from d6, and it does cause data corruption (wrong date written to database) 100% of the time a comment is edited.
Comment #7
bfroehle CreditAttribution: bfroehle commentedAre these changes necessary? Why have they been made?
I agree that $date needs to be set differently. However, is there really a situation where $comment->created wouldn't exist (but $comment->changed would)? Something feels wrong about this.
Comment #8
bfroehle CreditAttribution: bfroehle commentedseems more appropriate to me...
Comment #9
bfroehle CreditAttribution: bfroehle commentedTwo new patches (tests only & tests+fix) which include revised tests (to eliminate my objections in #7) and the fix I think is proper (from #8).
The bug really originated in #122098: Split comment.timestamp into 'created' and 'updated' columns (the split of the previous timestamp into created and changed).
From
comment_submit()
, we see that the form element 'date' corresponds to the created timestamp:And therefore we should use $comment->created, not $comment->changed, to generate the form element 'date.'
Comment #11
bfroehle CreditAttribution: bfroehle commented#9: 1005004-9-test-and-fix.patch queued for re-testing.
Comment #12
markabur CreditAttribution: markabur commentedThanks for looking at this. I agree that it makes sense to do:
instead of messing with the timezone. On my machine that results in the field looking like "2008-03-02 06:23 -0800" rather than the "2008-03-02 17:23 +0300" that was input, but they are equivalent. With my way, the input value of "2008-03-02 06:23 -0000" is exactly what you see if you look at the loaded form, i.e. there's no timezone conversion. But, the system needs to take care of timezones anyway, and this change is simpler.
That makes more sense too. I don't know if $comment->date is ever really non-empty here, but yeah there's no point in pre-filling the field with $comment->changed in any case.
I've beefed up the tests to check the saved/loaded comment data against what it was originally created with. But I'm confused because the subject test should pass but it fails, and the date test should fail but it passes. Is this because comment_load($comment->id) isn't really loading the comment from the database?
The attached "fix" patch is good (same as above) but that subject test will still fail as we're not trying to fix that..
Comment #14
bfroehle CreditAttribution: bfroehle commented@markabur: In #9 the 'date test' is passing because when you preview the comment it's just returning what you originally typed in. This is exactly the behavior of
$date = (!empty($comment->date) ? $comment->date : format_date($comment->created, 'custom', 'Y-m-d H:i O'));
The subject test is failing because
$edit['subject'] = $edit['subject'] . ' edited';
and then you do the comparison to
$edit['subject'] . 'edited'
.You should really just assert that the new subject is equal to $edit['subject'].
Lastly, if we're adding routines for verification after we save, we should probably change the name of the test (to something like testCommentEditPreviewSave) and the generic comments for it.
Comment #15
markabur CreditAttribution: markabur commentedDoh, I see the trouble with
$edit['subject'] = $edit['subject'] . ' edited';
. Silly mistake.But I'm still trying to understand why the final date test is passing in the tests-only patch; when I go through the sequence manually the date does change, which means the test should fail, since we just saved the comment while it was displaying the wrong date in the edit form.
In other words if the first test below fails, then we save the comment, then the second test below should fail as well, but it doesn't.
Comment #16
bfroehle CreditAttribution: bfroehle commented@markabur in #15: The second test is passing because the form save mechanism is fine. Since you aren't changing the date in
$edit
, $comment->created will still be set correctly. What was broken is the form generation mechanism, and this test does fail.Comment #17
markabur CreditAttribution: markabur commented@bfroehle thanks, I just realized that -- $edit['date'] wasn't getting picked up from the form like I assumed. I'd like the test to demonstrate that saving the form in the mis-generated state results in bad data in the database. Does that make sense?
Is there a command for reading the value of the form field that's actually displayed? E.g. I want to add a step like "$edit['date'] = DrupalGetFormField('date')" before saving the form.
Comment #18
markabur CreditAttribution: markabur commented$this->xpath() is what I was looking for..
Comment #19
markabur CreditAttribution: markabur commentedHere's the revised test. It includes a section that uses $this->xpath() to retrieve the form fields from the page and then saves the comment using those values. So, the test-only patch will fail twice: once for displaying the wrong date after loading the comment, and once for saving that date to the database.
The fix causes the form to display the correct date, which causes the the correct date to get into the database when the form is saved.
Comment #20
markabur CreditAttribution: markabur commentedSame thing but with the comment updated to reflect the renamed function (testCommentEditPreviewSave()).
Comment #21
moshe weitzman CreditAttribution: moshe weitzman commentedIMO it is a critical. Editing a comment is quite routine. One line fix, plus tests.
Comment #22
webchickWow, what a horrible problem! Thanks a lot for the fix, and for the test.
Committed to HEAD.
Comment #24
tmctighe CreditAttribution: tmctighe commentedSo this fix never solved the problem for users given the ability to edit their own comments.
In short: a user with the permission "Can edit own comments" edit's their comment, the original creation date is destroyed.
I've attached the fix.
Comment #25
andypostThe follow-up with test should be ported in #1374090: Editing a comment still changes creation date
Comment #26
xanderol CreditAttribution: xanderol commentedWill this fix be making it into core?
7.24 still has the bug.
I applied the patch from #24 and it seems to take care of the problem.
Comment #27
andypostPlease do not use this issue to report.
Patch #24 nearly he same that was commited to 8.x in #1374090: Editing a comment still changes creation date so just backport the change with test