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 tobody='<img>'. Usage=1, which is correct. Now edit it tobody=''. Usage=0, which is also correct. - Start with
body=''. Now edit it tobody='<img>'. Usage=1, which is correct. Now edit it tobody='<img><img>'. Usage=1, which is incorrect (because it already was >0, it failed to count additional new uses). Now edit it tobody=''. Usage=1, which is incorrect. - Start with
body='<img><img>'. Usage=2. Now edit it tobody='<img><img><img>'. Usage=2, which is incorrect (same reason as before). Now edit it tobody='<img><img>'. Usage=2, which is incorrect. Now edit it tobody=''. 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 tobody='<img><img><img>'. Usage=2, which is incorrect (same reason as before). Now edit it tobody='<img><img>'. Usage=2, which is correct (but only because it's not being updated…). Now edit it tobody=''. 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 tobody='<img><img><img>'. Usage=2, which is incorrect (same reason as before). Now edit it tobody=''. 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 tobody='<img>'. Usage=2, which is incorrect (same reason as before). Now edit it tobody=''. 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.
| Comment | File | Size | Author |
|---|---|---|---|
| #24 | 2892368-24-tests-only.patch | 3.19 KB | smustgrave |
| #22 | interdiff_21-22.txt | 1.92 KB | akashkumar07 |
| #22 | 2892368-22.patch | 5.77 KB | akashkumar07 |
Issue fork drupal-2892368
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
Comment #2
wim leersComment #3
wim leersThis 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.
Comment #4
wim leersAnd now also fixing the "remove images" case. Now the new test coverage that was added in #2 should be passing!
Comment #5
wim leersExcept 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.
Comment #6
wim leersNote 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_usagestill lists >0. IS updated with concrete examples and detailed explanation.Finally, it's crucial that @Leksat be credited on this issue!
Comment #20
smustgrave commentedComment #21
akashkumar07 commentedReroll added for patch #5.
Comment #22
akashkumar07 commentedRemoved $this->assertIdentical() deprecated method with $this->assertSame().
Comment #23
smustgrave commentedReroll 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
Comment #24
smustgrave commentedIf this fails then I'll move to RTBC
Comment #26
smustgrave commentedLooks like the tests still cover
Comment #32
sokru commentedConverted 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
Comment #33
smustgrave commentedSolution mentions about just adding test coverage but MR contains other changes as well.