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
Contact module's mail sending logic is wired into the form submission handler.
Let's abstract it out into a testable service.
Proposed resolution
Move the logic into a service.
Add unit tests.
Consider sending for other scenarios through which contact messages could be created, eg REST
Remaining tasks
Patch
Tests
Review
User interface changes
None
API changes
None, only additions - new service.
Comment | File | Size | Author |
---|---|---|---|
#34 | contact-mail-delivery-2325801-2.34.patch | 26.8 KB | larowlan |
#34 | interdiff.txt | 11.23 KB | larowlan |
Comments
Comment #1
larowlanComment #2
larowlanding ding
Comment #3
larowlanFirst patch - refactoring.
To come - unit tests.
Comment #4
larowlanComment #5
jibranI'll try to write some phpunit test for this.
Comment #6
andypostThis trait uses setter injection
so do not use argument.
Should be something like
calls:
- [setStringTranslation, ['@string_translation']]
Comment #7
larowlanIt supports both - see StringTranslationTrait::getStringTranslation : so if you set $this->stringTranslation in constructor, you're good to go
Comment #8
dawehnerI really like it!
+1 for using the argument, even if it uses the trait.
Does anyone know why we clone the user?
Comment #9
jibranHere we go added test haven't fixed #8 or #6
This is blocked on #2030591: Expand ContactForm with methods.
Thank you very much @larowlan with all the phpunit help and fixes.
Comment #10
jibranReroll after #2030591: Expand ContactForm with methods and fixed #6 and #8.1
@dawehner I have no idea about #8.2
Comment #11
larowlan8.2 would be because we override the email of anon user with that provided in form values. I guess clone to prevent that being saved for uid 0?
Comment #12
dawehnerThis patch looks really great!
Is there a reason we don't inject via constructor injection. Just wondering ...
Not for this issue but in general for example here, the logging could be moved into a decorator. This allows the actual main class to be more thin.
one empty line too much :(
Did you considered to use the flood control into the service as well?
nitpick: It can't be twice the same logger service
wow!
I really like this new mock methods.
I wonder whether this is too specific and would limit us from future refactoring.
Comment #13
jibranFor #12.1 see #8.1 :D
#8.2 Nice idea thank you.
#8.3 I'll fix it.
#8.4 Can we move it to follow up?
#8.5 I'll fix it.
#8.6 Kudos to @larowlan.
#8.7 Thanks.
#8.8 I think so to I'll change them to any.
Comment #14
jibrannot
#8it is #12.Comment #15
tim.plunkettAll regular services (except plugin.manager.mail!) define a service for their logger channel. It might worth doing here.
We don't use this for any services, usually opting for constructor injection alongside the trait.
Do not translate exception messages, and do not use full stops in them.
Also, is there any reason to not define a custom exception class?
What does this cover?
Please don't use () on @covers
EDIT: opened #2328919: Remove () from a bunch of @covers definitions in PHPUnit
Comment #16
jibranThat was some review thanks for the very nice review.
Comment #18
jibranmeh
Comment #19
andypost+1 rtbc
Comment #20
tim.plunkettYep, looks good to me. Great work @jibran and @larowlan!
Comment #21
olli CreditAttribution: olli commented#8.2/#11: Could that
$sender = clone ...
also move to MailHandler::sendMailMessages() with the lines that override name/email?Comment #25
larowlanRe-roll after #2260121: PHPUnit Tests namespace of modules is ambiguous with regular runtime namespace (+ Simpletest tests)
Comment #26
alexpottI agree with @olli in #21 I think the clone and user load should go into the MailManager since this is where the properties on the anonymous user get set.
OTOH:
If we leave the load in MessageForm should this be UserInterface or AccountInterface?
Comment #27
alexpottI agree with @olli in #21 I think the clone and user load should go into the MailManager since this is where the properties on the anonymous user get set.
OTOH:
If we leave the load in MessageForm should this be UserInterface or AccountInterface?
Comment #28
alexpottNot needed
Comment #29
jibranI made the changes but didn't fix the test yet. If the changes are good then I can fix the test.
Comment #31
larowlanI don't like this - we're limiting our options here.
Comment #32
jibranIt's a valid point. Service becomes useless with this change.
Please elaborate how you want to proceed here.
Comment #33
larowlanat #27 @alexpott asked us to do the clone and load in the send method.
I think retaining the ability to nominate the sender as an argument gives us additional functionality.
We should be able to satisfy @alexpott's request without loosing that functionality/flexibility.
Lee
Comment #34
larowlanFixes #27 and #28 but without removing the $sender argument from the MailHandler::sendMailMessages method, as mentioned in #33 I think leaving that gives the interface more flexibility.
Comment #35
andypostLooks rtbc
I think better to leave
$account
as argument name and$sender
as variableComment #36
jibranwhy not 2?
Comment #37
jibranBut anyways i think @alexpott points are addressed so back to RTBC
Comment #38
alexpottCommitted e5921e7 and pushed to 8.0.x. Thanks!