Problem/Motivation

In #2708411: [PP-1] editor.module's editor_file_reference filter not tracking file usage correctly for translations, @Leksat shows in comments #17.1 + #18.1 + #22 + #26 that there is another bug in editor.module's hook_entity_update(): not only does it fail in case of new/removed translations (which is what #2708411: [PP-1] editor.module's editor_file_reference filter not tracking file usage correctly for translations is about, it also fails when adding/removing images without creating a new revision (i.e. when editing a particular revision): if images are added/removed, then the file usage is never incremented/decremented accordingly.

Note that there is NO risk of data loss! There's only risk of incorrect counting. The only risk is that files that should be deleted, aren't, because it's possible that when 0 actual uses remain, file_usage still lists >0.

Examples:

  • Start with body=''. Now edit it to body='<img>'. Usage=1, which is correct. Now edit it to body=''. Usage=0, which is also correct.
  • Start with body=''. Now edit it to body='<img>'. Usage=1, which is correct. Now edit it to body='<img><img>'. Usage=1, which is incorrect (because it already was >0, it failed to count additional new uses). Now edit it to body=''. Usage=1, which is incorrect.
  • Start with body='<img><img>'. Usage=2. Now edit it to body='<img><img><img>'. Usage=2, which is incorrect (same reason as before). Now edit it to body='<img><img>'. Usage=2, which is incorrect. Now edit it to body=''. Usage=0, which is correct (because the delta is -2, which happened to be correct).
  • Start with body='<img><img>'. Usage=2. Now edit it to body='<img><img><img>'. Usage=2, which is incorrect (same reason as before). Now edit it to body='<img><img>'. Usage=2, which is correct (but only because it's not being updated…). Now edit it to body=''. Usage=0, which is correct (because the delta is -2, which happened to result in usage=0).
  • Start with body='<img><img>'. Usage=2. Now edit it to body='<img><img><img>'. Usage=2, which is incorrect (same reason as before). Now edit it to body=''. Usage=0, which is correct (because the delta is -3, which happened to result in usage=-1, which simply results in the row being deleted).
  • Start with body='<img><img>'. Usage=2. Now edit it to body='<img>'. Usage=2, which is incorrect (same reason as before). Now edit it to body=''. Usage=1, which is incorrect (because the delta is -1).

In other words: the number of times a file is referenced is only counted correctly when going from 0 to N or from N to zero. It's not counted correctly when going from >0 to N or from N to >0.

Proposed resolution

Add test coverage to ensure that adding/removing images to a body field without creating new revisions works as expected.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Issue fork drupal-2892368

Command icon 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:

Comments

Wim Leers created an issue. See original summary.

wim leers’s picture

Status: Active » Needs review
StatusFileSize
new1.85 KB
wim leers’s picture

StatusFileSize
new2.43 KB
new4.25 KB

This imports the fix from #2708411: [PP-1] editor.module's editor_file_reference filter not tracking file usage correctly for translations. Let's fix the "add new images" case only. This should then still fail, but it should fail later in the test.

wim leers’s picture

Issue tags: -Needs tests
StatusFileSize
new752 bytes
new4.63 KB

And now also fixing the "remove images" case. Now the new test coverage that was added in #2 should be passing!

wim leers’s picture

StatusFileSize
new1.89 KB
new6.11 KB

Except now some of the later test assertions are failing. They need to be updated: they're asserting behavior that we're only realizing now to be incorrect.

wim leers’s picture

Assigned: wim leers » Unassigned
Priority: Major » Normal
Issue summary: View changes

Note that unlike #2708411: [PP-1] editor.module's editor_file_reference filter not tracking file usage correctly for translations, this is not critical, because there's no risk of data loss. There's only risk of incorrect counting. The only risk is that files that should be deleted, aren't, because it's possible that when 0 actual uses remain, file_usage still lists >0. IS updated with concrete examples and detailed explanation.

Finally, it's crucial that @Leksat be credited on this issue!

The last submitted patch, 2: 2892368-2-test_only_FAIL.patch, failed testing. View results

The last submitted patch, 3: 2892368-3.patch, failed testing. View results

The last submitted patch, 4: 2892368-4.patch, failed testing. View results

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). 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.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.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.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.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.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Bug Smash Initiative, +Needs reroll
akashkumar07’s picture

StatusFileSize
new5.79 KB

Reroll added for patch #5.

akashkumar07’s picture

Status: Needs work » Needs review
StatusFileSize
new5.77 KB
new1.92 KB

Removed $this->assertIdentical() deprecated method with $this->assertSame().

smustgrave’s picture

Issue tags: -Needs reroll

Reroll looks good.

Looking at the code everything seems fine to me. (may need a second pair of eyes)

Going to upload a tests-only patch based on #22

smustgrave’s picture

StatusFileSize
new3.19 KB

If this fails then I'll move to RTBC

Status: Needs review » Needs work

The last submitted patch, 24: 2892368-24-tests-only.patch, failed testing. View results

smustgrave’s picture

Status: Needs work » Reviewed & tested by the community

Looks like the tests still cover

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 24: 2892368-24-tests-only.patch, failed testing. View results

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

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now 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: 10.1.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, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sokru made their first commit to this issue’s fork.

sokru’s picture

Status: Needs work » Needs review

Converted the patch from #22 to MR, but not sure if this (and many more entity/file usage) issue(s) should be postponed, based on @catch's comment on https://www.drupal.org/project/drupal/issues/1239558#comment-15415115

This should really be postponed on #3364744: Add a reliable entity-usage system to core, the file_usage system is broken beyond repair, there is no way to rebuild file usage, so as soon as it's off by one or more, it's like that forever.

smustgrave’s picture

Status: Needs review » Needs work

Solution mentions about just adding test coverage but MR contains other changes as well.

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.