In #1821420: Convert mail variables to cmi the following change was made to simpletest.module:
// Use the test mail class instead of the default mail handler class.
- variable_set('mail_system', array('default-system' => 'Drupal\Core\Mail\VariableLog'));
+ config('system.mail')->set('interface.default', 'Drupal\Core\Mail\VariableLog')->save();
A subtle difference was introduced here when a custom module provides a different mail system implementation. Previously, the variable_set() would overwrite all mail system settings, so all test mails would be sent to VariableLog. Now, only the default implementation is changed, so any custom mail system implementations that might be set in e.g. hook_install() are left intact.
It is possible to work around this in tests by explicitly changing custom mail system settings in setUp(), as per http://drupalcode.org/project/ubercart.git/commitdiff/2139e13, but this should not be required.
Comment | File | Size | Author |
---|---|---|---|
#48 | 2146971-48.patch | 7.66 KB | rpayanm |
#48 | 2146971-interdiff.txt | 977 bytes | rpayanm |
#35 | 2146971-simpletest-mail-handler-34.patch | 7.17 KB | piyuesh23 |
#33 | 2146971-simpletest-mail-handler-33.patch | 7.34 KB | piyuesh23 |
#29 | 2146971_29.patch | 2.88 KB | ravi.khetri |
Comments
Comment #1
longwaveThe attached patch restores the previous behaviour by overwriting all mail interfaces with the default.
Comment #2
TR CreditAttribution: TR commentedYes, I think this is a very good idea.
The mail.system variable was dysfunctional in D7 - read my explanation at https://drupal.org/comment/4160574#comment-4160574. So one could argue that the D7 behavior was a bug and that D8 uses the improved config system to correct that. Which is true, but only for live sites. SimpleTest is another matter entirely.
The whole point of resetting this variable in SimpleTest is so that SimpleTest can capture any outgoing mail in order to test the mail functionality. Withoug this change, we cannot ever test a module that implements its own mail system.
So a big +1 for this change - it doesn't impact any tests unless you have your own mail system defined, and if you do it allows you to test your module, which is currently impossible without workarounds.
Comment #3
webchickNice catch. This looks like a typo; the system.mail.yml file defines this as:
... so I don't really see how the old code would've ever worked.
My only question is whether it's possible to add a test for this?
Comment #4
longwaveThe old code worked fine except when alternative mail system implementations were involved, and core does not use these.
The attached patches subvert the existing SimpleTest e-mail capture test to use an alternative implementation and demonstrate the bug, without needing any additional assertions.
Comment #6
longwave4: 2146971-simpletest-mail-handler-4-PASS.patch queued for re-testing.
Comment #8
longwaveRerolled.
Comment #9
longwaveComment #10
longwaveLess confusing title
Comment #11
BerdirFollowing, let's do this after #2187495: Use plugin system for MailInterface classes. There is a test module but I can't see a test that would verify it? Also, the comment in the test module still references the old class name, but all of that should switch to plugins anyway after the pluginification.
Comment #12
longwave@Berdir the test works as follows: MailCaptureTest installs mailsystem_test module, which sets the simpletest mail handler to PhpMail. With the existing bug, this causes MailCaptureTest to fail as the mails are not actually captured, instead PhpMail sends them as normal. After the bug is patched, mailsystem_test's setting is overridden inside the test environment so the mails are correctly captured. See the FAIL patch and results in #4 for full details.
Comment #13
Les LimThis will also need a reroll because of #2171683: Remove all Simpletest overrides and rely on native multi-site functionality instead, which substantially rewrote WebTestBase.
There are tests that manipulate
system.mail
settings beyond what WebTestBase does; it would probably be a good idea to change those, as well. Here's what I found on a grep:\Drupal\user\Tests\UserCreateFailMailTest:
\Drupal\system\Tests\InstallerTest:
\Drupal\system_mail_failure_test\TestPhpMailFailure:
\Drupal\system\Tests\Mail\MailTest does this as well, but the body of that test will change a lot if #2187495: Use plugin system for MailInterface classes lands.
Comment #14
longwaveWebTestBase and InstallerTestBase are expected to be extended by contrib which might provide alternative mail handlers, so this patch enforces the use of test_mail_collector for all mail handlers during testing. Other tests that previously changed the default mail handler only used the default mail handler anyway, so I don't think they need to be changed here.
I had to rearrange some code in WebTestBase so the mail handler is enforced after modules are installed; this means that modules that set up mail handlers in their hook_install are overridden during testing.
Comment #17
longwaveComment #18
jhedstromComment #19
rpayanmComment #21
ankitgarg CreditAttribution: ankitgarg commentedrerolled
Comment #22
ankitgarg CreditAttribution: ankitgarg commentedrerolled
Comment #23
ankitgarg CreditAttribution: ankitgarg commentedrerolled
Comment #24
ankitgarg CreditAttribution: ankitgarg commentedrerolled
Comment #26
ankitgarg CreditAttribution: ankitgarg commentedUpdated Rerole.
Comment #28
ankitgarg CreditAttribution: ankitgarg commentedComment #29
ravi.khetri CreditAttribution: ravi.khetri commentedReRolled.
Comment #31
jhedstromIt looks like something has been lost in the last 2 rerolls compared to the patches in #14 and #19.
Comment #32
piyuesh23 CreditAttribution: piyuesh23 commentedComment #33
piyuesh23 CreditAttribution: piyuesh23 commentedRe-rolled patch. Added back missing elements.
Comment #35
piyuesh23 CreditAttribution: piyuesh23 commentedFixed the PHP error. Uploading the patch again.
Comment #37
kerby70 CreditAttribution: kerby70 at Blink Reaction (now part of FFW) commentedReroll attached.
Removing empty .module file per (.module and .profile files are no longer required; ModuleHandler::getModuleList() now returns Extension objects).
Comment #38
kerby70 CreditAttribution: kerby70 commentedComment #40
TR CreditAttribution: TR commentedIt's failing because the patch doesn't take into account the changes from #2392319: Config objects (but not config entities) should by default be immutable
Comment #41
kerby70 CreditAttribution: kerby70 at Blink Reaction (now part of FFW) commentedAdding the module file back in, to see if that is needed here. See what simpletest says.
Comment #42
kerby70 CreditAttribution: kerby70 commentedComment #44
kerby70 CreditAttribution: kerby70 at Blink Reaction (now part of FFW) commentedCorrecting because of #40, and re-removing the empty .module file (see #37).
Comment #45
kerby70 CreditAttribution: kerby70 at Blink Reaction (now part of FFW) commentedIs the following desirable still from #14, 19, 26, 28 and 29 but not in 33 or 35?
Comment #46
kerby70 CreditAttribution: kerby70 commentedComment #47
BerdirNitpicks:
There should be an empty line after the class and the docblock.
Trailing spaces after the $modules lines.
make sure there's a newline at the end of those files, git doesn't like it if isn't.
Comment #48
rpayanmComment #49
Mile23Maybe a duplicate of #2609680: Add an AssertMailTrait to allow mail testing in Kernel tests and fix odd stdout not found in test output
Comment #50
andyposttrailing whitespace
(
Comment #51
ArlaAgreed. (Although I think they had the wrong solution, as I stated there.)