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.
Problem/Motivation
From #749748: Contact, register and comment forms do not prefill with user info from browser
Looks like we're missing test coverage for anonymous contact info in comments.
// @todo Complete test coverage for:
//'comments' => array(CommentItemInterface::OPEN, CommentItemInterface::CLOSED, CommentInterface::_HIDDEN),
//// COMMENT_ANONYMOUS_MUST_CONTACT is irrelevant for this test.
//'contact ' => array(COMMENT_ANONYMOUS_MAY_CONTACT, COMMENT_ANONYMOUS_MAYNOT_CONTACT),
But whilst looking into it, CommentLinksTest is a behemoth.
Proposed resolution
Make comment links unit testable by
- Converting the logic to a service √
- Injecting the dependencies √
- Writing a data provider and adding PHP Unit tests. √
- Fixing failures√
Remaining tasks
Fix the tests (make the asserts smarter than just assertEquals on $expected)
Patch
User interface changes
None
API changes
New CommentLinkBuilderInterface and new CommentLinkBuilder service
COMMENT_FORM_BELOW and COMMENT_FORM_SEPARATE_PAGE moved to Drupal\comment\Plugin\Field\FieldType\CommentItem::FORM_BELOW and FORM_SEPARATE_PAGE
Comment | File | Size | Author |
---|---|---|---|
#31 | comment-links-2318251.31.patch | 47.99 KB | larowlan |
#31 | interdiff.txt | 4.07 KB | larowlan |
Comments
Comment #1
larowlanHaving a look
Comment #2
larowlanWow that is a pants test.
Took 20 minutes on my local.
So first order of business - convert it to PHPUnit.
Here's the guts of it.
Needs - fix the $expected values.
Needs - PHPCS/Docblocks.
There are 1920 combinations, so this function is way off the cyclic complexity chart.
PHPUnit tests them in no time.
Test will fail miserably cause I've not gone through to set up the expected output yet.
Will need to think about how to do that smarter.
Comment #3
larowlanComment #4
larowlanMoving in the right direction
Comment #5
larowlanFinished PHP Unit tests, cleaned up original web test to just make sure that the hook is invoked and basic links are output - (now takes 53 secs instead of 20 mins locally) - remainder of the scenarios now handled instead by the much-faster PHP Unit - where we have 100% test coverage.
Comment #7
larowlanwhoops, changed my mind on which item to put the form constants on
Comment #8
larowlanComment #9
larowlanDraft change record is https://www.drupal.org/node/2318879
Comment #10
thehong CreditAttribution: thehong commentedI think comment is no longer tied to to Node, can you explain why NodeInterface is used in CommentLinkBuilder? Thanks
Comment #11
larowlanAt the moment links are only shown for nodes.
Comment #12
larowlanRefactored away the Node bits, cause node module might not be enabled so those type hints aren't much use.
Also, we can make this generic and part of the formatter as planned.
Also fixed missing docblocks
Comment #14
larowlanYeah, $links was already being used » $entity_links
Comment #15
larowlanre-roll
Comment #16
larowlanComment #17
thehong CreditAttribution: thehong commentedI think we should inject 'node' string to this service, it's easier for developer to use it for other entity.
Comment #18
andypostComment #19
andypost@thehong "node" is a entity type argument, I can't get what you mean
Comment #20
thehong CreditAttribution: thehong commented@andypost Instead of
\Drupal::service('comment.link_builder')->buildLinks($node_links, $node, $context);
we should change it to:
\Drupal::service('comment.link_builder')->buildLinks('node', $node_links, $node, $context);
Comment #21
dawehnerThis doens't make sense. The entity itself ($node) already knows about everything which is needed. (it's entity type ID).
No need to fallback to Drupal 7 APIs.
Just an airport review.
<3
i really like to have a proper one line description which explains what is going on.
Always typehint the accountinterface and don't talk about the proxy. this is just an implementation detail.
I know this is out of scope but can't we somehow integrate these links properly into the entity display bits? These links should be for example be configurable per view mode.
Oh I nearly got tricked by the fact that status is a part of the comment field. Any reason to not use != CommentItemInterface::HIDDEN ?
Wait, this string does not contain any HTML, or does it?
Urgs, would be so much better to kinda make it more flexible and less hardcoded
You have to use $this->t() otherwise potx cannot find it. Out of scope of this issue.
Same question, this is not even HTML at all.
Can you use the getStringTranslationStub() method here? I could imagine that formatPlural is not supported? It would then be great to add support for it.
Comment #22
andypostOverall it's a big step forward to clean-up spaghetti legacy for links!
1) The order of arguments just mimics
hook_node_links_alter()
&hook_comment_links_alter()
2) In context of comment services I think that better follow patterns #2188287: Split CommentManager in two by placing all render funcs into one
CommentRenderHelper
service3) I'd suggest to extract conversion of constants to separate issue with own change record.
Also this patch just converts node links but I think comment links should be addressed here too.
Use CommentItemInterface for constants
Comment #23
larowlan#17
Correct, we should use $entity->getEntityTypeId and not 'node' hardcoded
21.3 will fix
21.4 #2271349: Node and Comment ops links should be display components (which can be disabled) should sort that but also in #1920044: Move comment field settings that relate to rendering to formatter options we were making it configurable in the formatter (there is a patch there)
21.5 good call, cause there are two non-zero constants in effect here
21.6, 21.9 I think you are right, there are data attributes but rendering should sort them
21.7 Welcome suggestions, ideally history module would have its own link builder service I guess
21.8 we have $this->t() in scope so will fix
21.10 ooh didn't know we had that, nice
22.2 agreed it would fit better on CommentRenderHelper, perhaps CommentRenderBuilder would be a more apt name (bikeshed!). So depends if this or #2188287: Split CommentManager in two goes in first?
22.3 (CommentItem » CommentItemInterface) will fix, sorry
thanks thehong, andypost, dawehner
@thehong, do you have an irc nick so I can send some review karma your way - can't see it in your profile.
Comment #24
larowlanFixes as promised in #23
@andypost - I'd like to tackle comment links separately as they are a different kettle of fish (post render cache callback)
Comment #25
andypostI think that only change record needed, +1 rtbc
@dawehner are you agree?
Comment #26
larowlanDraft change record above
Comment #27
andypostso let's go
Comment #28
Wim LeersAll I can say is:
.Comment #29
alexpottWhy not buildNodeLinks? To reflect the hook_node_links_alter() - also I think this should return the array of links and the alter hook should still add them - ie not pass $entity_links at all. That way if we ever move hook_node_links_alter to events we can have a build event and an alter event.
Comment #30
larowlanI think buildCommentedEntityLinks would be more appropriate - because longer term we want this to be used for more than just nodes. That would leave buildCommentLinks for the next step (to move the comment operation etc links over here).
Ok will fix
Comment #31
larowlanComment #32
andypostgreat
Comment #33
alexpottThanks. Committed b6ba35f and pushed to 8.0.x. Thanks!
Comment #35
larowlanPublished the change notice