Problem/Motivation
There doesn't appear to be an easy or consistent way for tests to assert that a message has been logged (via a logger channel, not to the screen).
The workaround is to enable the dblog module for any test needing to assert that a message is logged, but this seems overly complicated.
Proposed resolution
Add a test logger channel for kernel and browser tests, and a method assertLog() to the base classes (via a trait). Since log messages need to persist past page requests (for Browser tests anyway), either the state backend, or a cache backend, can be used to store the logs for the duration of the test.
Comments
Comment #2
dagmarI like this idea. I will allow to isolate a lot of tests.
There are fundamentally 3 operations regarding tests that I have seen while working on the dblog.module issue queue:
I will post others use cases if I found them.
There is a plan to swap the storage backend for dblog here: #2458191: Provide a storage backend for dblog module it would be nice to have the same (or at least most of the) functionality that implements the LogStorageInterface
Comment #3
jhedstromI'd like to see this be independent of the dblog module, but if we moved that storage interface into core, we could use it here. However, that might be overkill here, as I'm thinking this would work more similarly to the
AssertMailTraitused for tests (this just captures emails in an array).I'm going to work on a proof-of-concept.
Comment #4
jhedstromI realized we can't use in-memory (except for kernel tests), since log messages will be generated on page requests. We can use the state backend (that's what email testing system uses) , or perhaps just the cache backend system...
Comment #5
cilefen commentedComment #6
dagmarWell, in that case we just need to wait until #2458191: Provide a storage backend for dblog module provide the consistent way to fetch a log. I don't see the need to re-create the dblog functionality using another table. @jhedstrom do you see any other benefit of doing this that I'm not seeing?
Comment #7
jhedstrom@dagmar that makes sense to me. My proof-of-concept stalled out because re-creating dblog in a php-backend (eg, memory, or cache table) was non-trivial.
Comment #8
claudiu.cristeaI think this should be available also for KernelTestBase. Often we do workarounds to test logged messages in kernel tests.
Comment #9
dawehnerThe only way we have to ensure is that you don't have to install the dblog modue to use this piece of functionality ...
Comment #14
mile23It seems like the solution here would be to create a test logging service that keeps the log in memory. Then you assert on its accessors.
Given that, we'd need to add it to the container in
CoreServiceProvider::registerTest().Comment #18
joachim commentedSimilar issue with a wider scope: #2903456: Allow kernel tests to fail or expect logged errors.
Part of the plan there is to add a logger that logs to site state, which would work for the use cases here.
Comment #21
joachim commentedGiven the lack of feedback, I'm closing this as a duplicate.
Comment #22
mile23FTR: drupal/core-dev now requires colinodell/psr-testlogger, which meets these needs.
Comment #23
mile23