Problem/Motivation
Comment types do not declare a dependency on the module that provides the target entity type. Thus, if that module is installed, the comment type will remain and using it will lead to fatal errors.
Proposed resolution
Declare the dependency on the provider of the target entity type. In particular implement calculateDependencies()
in CommentType
. EntityDisplayModeBase::calculateDependencies()
can be used as inspiration.
Remaining tasks
Task | Novice task? | Contributor instructions | Complete? |
---|---|---|---|
Create a patch | Instructions | ||
Add automated tests | Instructions | ||
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards | Instructions |
User interface changes
None.
API changes
None.
The fact that comment types now get appropriately deleted when a module is uninstalled is a change in behavior, but since the comment types that were previously left in the system were completely broken, I don't see how anyone can currently rely on that behavior. Thus, I do not see the need for a change record either.
Data model changes
Comment types have a new entry in their exported configuration dependencies.
Comment | File | Size | Author |
---|---|---|---|
#76 | interdiff-2717673-74-76.txt | 848 bytes | mohit_aghera |
#76 | commenttype-dependencies-2717673-76.patch | 8.28 KB | mohit_aghera |
#74 | commenttype-dependencies-2717673-74.patch | 7.46 KB | mohit_aghera |
#63 | interdiff-2717673-62-63.txt | 1.43 KB | tetranz |
#63 | commenttype-dependencies-2717673-63.patch | 7.21 KB | tetranz |
Comments
Comment #2
tetranz CreditAttribution: tetranz as a volunteer commentedComment #3
tetranz CreditAttribution: tetranz as a volunteer commentedComment #4
tetranz CreditAttribution: tetranz as a volunteer commentedComment #5
tetranz CreditAttribution: tetranz as a volunteer commentedComment #8
tstoecklerAwesome work and thanks for including a test!
Yay, test coverage! The failing tests mean we need to update our default config to include this dependency I guess.
The test-only patch fails as expected so otherwise should be good to go.
Comment #9
tetranz CreditAttribution: tetranz as a volunteer commentedThanks. I'll take a look at the config later today.
Comment #10
tstoecklerOh, if you're already rerolling this: Please remove the empty newline at the beginning of the test method (the one inside the method) and add a newline after the test method before the closing brace of the test class.
Comment #11
tetranz CreditAttribution: tetranz as a volunteer commentedI'm still feeling my way a little with this.
I can see that the default comment type and forum comment type both have dependencies on the node module. I don't quite understand the other errors but let's see what happens with this first.
Comment #13
tetranz CreditAttribution: tetranz as a volunteer commentedWell ... I thought I understood what the failed tests are saying but obviously not. I'll take another look tomorrow but I'm probably going to need some help or at least a point in the right direction.
Comment #14
tstoecklerTry adding the dependencies outside of the "enforced" section (i.e. just move them one level up). Enforced dependencies are for those dependencies that Drupal can't figure out automatically. I.e. Drupal has no way of knowing that it should delete the forum node type when Forum module is being uninstalled - it's just another node type. Therefore Forum has to add itself as an enforced dependency. In this case, though, Drupal should figure out the dependency on e.g. Node automatically, so it should not be inside enforced. You can also try saving the respective config with the patch applied and then exporting it. That should also have the dependencies properly set then.
Comment #15
tetranz CreditAttribution: tetranz as a volunteer commentedThanks. I just figured that out minutes ago by digging deeper into the test that compares the active and default configs.
I think this is what it needs.
And similar for the standard profile comment.
Making a new patch now.
Comment #16
tetranz CreditAttribution: tetranz as a volunteer commentedComment #18
claudiu.cristeaThere's always a
getTargetEntityTypeId()
method available to get the$this->target_entity_type_id
. Let's use it.Should end with a dot. Try to shorten the entire phrase.
Naming: Let's be more specific.
$comment_type
?We don't need to (re)calculate the dependencies here. They are already computed when the comment type is created/saved. This is enough:
There's no need for assertion messages. They make the tests hard to be followed/debugged. Consider removing the message. Also, what if there's no 'module' key in $dependencies? The test will not fail softly (as we prefer). Shouldn't we assure an empty array? For example:
...and then use $modules in assertion.
Comment #19
claudiu.cristea#18.5 can be solved more clever by getting the dependencies in this way:
In this way we always have a $dependencies['module'], even it's empty.
Comment #20
tetranz CreditAttribution: tetranz as a volunteer commentedThanks @claudiu.cristea. My first patch so I have a lot to learn :)
I haven't looked into the upgrade path yet.
Comment #21
claudiu.cristea@tetranz, it's a very promising start :)
Is still missing the upgrade path and a corresponding test. What you'll need to do is to create in the module root a file comment.post_update.php and add an update function with the naming pattern
comment_post_update_SOME_NAME()
. See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension...In this function you should only iterate through all comment type entities and re-save them. Something like this should work:
This would automatically add the dependency to the entity.
But we need also to test this. You'll find some source of inspiration in other update tests:
Comment #22
tetranz CreditAttribution: tetranz as a volunteer commentedSomehow I messed up the interdiff.
Comment #24
tetranz CreditAttribution: tetranz as a volunteer commentedI've been trying to understand the final failed test but I just noticed https://www.drupal.org/node/2724871 which I think explains it.
I'll move on to the upgrade path.
Comment #26
tetranz CreditAttribution: tetranz as a volunteer commentedComment #28
tetranz CreditAttribution: tetranz as a volunteer commentedTrying the test on 8.2.x to see if the Migrate test still fails.
Comment #29
tetranz CreditAttribution: tetranz as a volunteer commentedComment #30
tstoecklerWow, this looks great now. I hadn't even thought about the upgrade path.
This looks good to go for me but I'll let @claudiu.cristea take another look.
Comment #31
claudiu.cristea@tetranz,
Looks nice to me too. Only few nits.
We need to group post update functions by the minor Drupal version to which they are referring. See
core/modules/field/field.post_update.php
for an example.Here, probably we add
and close that.
Un-needed extra empty line.
Comment #32
tetranz CreditAttribution: tetranz as a volunteer commentedThanks to both of you for the help and guidance. It was a good learning experience. Just what I needed to get started. I'll go find another issue now. :)
Comment #33
claudiu.cristeaFor me this is RTBC if green. When the other issue is fixed we'll need to run the test again againts 8.1.x.
Comment #35
tetranz CreditAttribution: tetranz as a volunteer commentedI think something changed in the node module. I'll have to take a look later.
Comment #36
tstoecklerThe actual failure is
which seems strange. Not sure what is happening.
(See https://dispatcher.drupalci.org/job/default/136781/console )
Comment #37
tetranz CreditAttribution: tetranz as a volunteer commentedNodeLoadMultipleTest.php doesn't appear to have a use statement for Node. That's why it's trying to find it in the same namespace, i.e., Drupal\node\Tests\Node
Comment #38
alexpottNodeLoadMultipleTest was failing due to a borked commit which has been reverted.
Comment #39
tetranz CreditAttribution: tetranz as a volunteer commentedRunning test again.
Comment #40
tstoecklerYay, thanks @alexpott for the clarification.
Comment #42
tstoecklerThat was #2724871: Random failure in \Drupal\migrate_drupal_ui\Tests\d7\MigrateUpgrade7Test
Comment #43
claudiu.cristea+1 for RTBC. Looks clean now.
Comment #44
catchI was about to commit this, but then realised I have a question:
If you've already uninstalled node module, but have comments missing the dependency, what happens to them with the update? This could possibly be handled in a new issue adding either an update and/or test coverage - since this at least stops the issue on sites that are only missing the dependency itself.
Comment #45
catchComment #46
tetranz CreditAttribution: tetranz as a volunteer commentedI see what you mean. Should I change the post_update and related test to only do something if the node module exists? Without that I think the $comment_type->save is going to crash or do something meaningless.
Comment #47
catchI think that's a good idea here.
We should then open up a follow-up for sites that are already affected - and that could start with test coverage for the case to see how exactly it breaks.
Comment #48
tetranz CreditAttribution: tetranz as a volunteer commentedComment #49
claudiu.cristeaNo, sorry. It's not only node. A comment type can be defined for any other arbitrary entity type. I think, inside array_walk() we need to:
Then in test we need to test the same, probably using $this->config.
Also, what if there's no definition for this target entity type? As is called now it will throw an exception. Do we want that? I'm not sure, I thin we most probably want to avid the dependency. This is open for discussion.
Comment #50
tetranz CreditAttribution: tetranz as a volunteer commentedYes, of course, that makes sense for the post_update.
I'm a bit confused about the update test. I thought drupal-8.filled.standard.php.gz gives us a known set of data which only has the default comment type and the forum comment type. We know that both of those depend on the node entity provided by the node module.
Are you saying that it would be cleaner to not make that assumption in the update test and instead iterate over what we really do have and use the configuration properly rather than hard coding node? That sounds better and more generic.
Comment #51
catchThe existing update test looks OK to me, we know what data there is. Although I think we could drop the early return for node being installed - it should always be installed no?
What I meant is in follow-up issue, we'd want a test where the database has node module uninstalled and without the early return - to validate whatever we end up doing for comment types where the provider module has already been uninstalled. But there not here.
Comment #52
tetranz CreditAttribution: tetranz as a volunteer commentedpost_update now checks properly that the entity type definition exists before attempting to save and therefore recalculate dependencies.
I implemented @claudiu.cristea's suggested definition check in calculateDependencies then I changed my mind and removed it. I happy to put it back if it seems necessary.
I will open another for the situation where the provider module does not exist.
Comment #53
tetranz CreditAttribution: tetranz as a volunteer commentedI created a related issue here. https://www.drupal.org/node/2733819. The wording could probably be improved a bit.
I'll have a go at it if nobody else grabs it in the next day or so.
Comment #54
tetranz CreditAttribution: tetranz as a volunteer commentedComment #55
tetranz CreditAttribution: tetranz as a volunteer commentedI guess this needs to be tested on 8.1.x.
Comment #58
tetranz CreditAttribution: tetranz at Third and Grove commentedTrying to bring this back to life. Redone it for 8.3.x.
Forum now sets its comment type as a dependency so I've removed any reference to the forum module.
Node still does not set the default comment type as a dependency.
Comment #60
claudiu.cristea@tetranz, for the first failure, you probably need to add 'target_entity_type_id' => 'entity_test' in:
inside \Drupal\Tests\comment\Kernel\CommentIntegrationTest::setUp()
Also check the coding standards messages
Comment #61
claudiu.cristeaThe first line of comment should be continued. From https://www.drupal.org/docs/develop/coding-standards/api-documentation-a...
Comment #62
tetranz CreditAttribution: tetranz at Third and Grove commentedThanks @claudiu.cristea. I think this might do it.
Comment #63
tetranz CreditAttribution: tetranz at Third and Grove commentedI shouldn't have removed the forum test.
Comment #64
tetranz CreditAttribution: tetranz at Third and Grove commentedComment #65
claudiu.cristeaLooks perfect to me, I have nothing more to comment here.
Comment #67
claudiu.cristeaComment #68
alexpottThese should be removed. New policy - see #2860096: Remove api doc groups for updates eg. updates-8.2.x-to-8.3.x.
So in the update we defend against missing target entity providers and in calculateDependencies() we don't. Should we be deleting comment types in this position? Or should calculateDependencies() be similarly defensive. Not sure.
Comment #69
claudiu.cristea@alexpott, in update we deal with the case where a comment type was created for a target entity whose module has been disabled in the meantime. For such cases we decided not to delete them but, in #2733819: Minimize the effect of a comment type's target entity provider being uninstalled., we raise the red flag.
In
calculateDependecies()
we don't want to defend, we want to crash because that should be a very edge case, happening in very special circumstances like creating comment types through API and not ensuring a reliable target entity type. Most of the time the comment types are created through the UI where the user cannot select invalid target entity types. So, I don't think we want to mute such events.Comment #70
alexpott@claudiu.cristea hmmm... this doesn't make sense to me - we're leaving around invalid config that can't be saved - because of this change. That's just not right. That means we couldn't ever again write a generic update all the config thing like system_post_update_recalculate_configuration_entity_dependencies() because something might fail. It also means that on these sites config import is completely broken.
Comment #71
claudiu.cristea@alexpott, you propose to delete them in update?
Comment #73
apadernoComment #74
mohit_aghera CreditAttribution: mohit_aghera as a volunteer and at Axelerant commentedRe-rolling patch against 8.6.x branch.
I have also fixed couple of issues mentioned by @alexpott
Fixed usage of DI
We still needs some inputs from @alexpott about implementation approach as mentioned by @claudiu.cristea
Comment #76
mohit_aghera CreditAttribution: mohit_aghera as a volunteer and at Axelerant commentedAttempt to fix test case failures.
Comment #86
smustgrave CreditAttribution: smustgrave at Mobomo commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
At this time we will need a D10 MR of this issue.
Since it's been 5 years tagged for IS update if any of the remaining tasks have changed as the module has been updated some.