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
In #2328645: Remove remaining global $user, I moved that call to setAccount(). Which is nice, because it means that changing the current user also automatically sets the currect timezone. However, it means it's never called for anonymous users, because then the account is created on-demand in getAccount().
This should also be the proper fix for #2446859: Installer warning: date_default_timezone_get(): It is not safe to rely on the system's timezone settings and might allow us to remove the @ there again.
Proposed resolution
Add that line in getAccount() too, after initializing the AnonymousUserSession object.
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#37 | user-date-timezone-2510150-37.patch | 9.51 KB | tduong |
#37 | interdiff-2510150-31-37.txt | 1.14 KB | tduong |
#31 | user-date-timezone-2510150-31-test_only.patch | 2.67 KB | tduong |
#19 | user-date-timezone-2510150-8.patch | 2.85 KB | Berdir |
#8 | user-date-timezone-2510150-8.patch | 2.85 KB | Berdir |
Comments
Comment #1
BerdirHere's a patch, needs tests.
Comment #2
BerdirComment #4
BerdirDrupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[HY000]: General error: failed to instantiate user-supplied statement class: CREATE TABLE {cache_config} error in Drupal\dblog\Tests\ConnectionFailureTest.
Could actually be a real error since that test sounds like it might be doing something funky.
Comment #7
BerdirYeah, fun test. This fixes it, not very pretty, though.
Comment #8
BerdirAnd here are tests.
Comment #10
znerol CreditAttribution: znerol commentedLikely also is the proper fix of #2471405: When not logged Drupal\Core\EventSubscriber\FinishResponseSubscriber->setExpiresNoCache() causes a PHP warning because of an invalid timezone
Comment #11
BerdirThat's possible, should we consider reverting that as well then?
Comment #12
dysrama CreditAttribution: dysrama commentedPatch works for me, encountered the issue when trying to access the site as anon after turning on page cache.
Don't know enough about d8 to review the code though.
Comment #13
znerol CreditAttribution: znerol commentedI failed to reproduce the message mentioned in #2471405: When not logged Drupal\Core\EventSubscriber\FinishResponseSubscriber->setExpiresNoCache() causes a PHP warning because of an invalid timezone even when evaluating the offending line directly:
My environment:
Comment #14
BerdirThis is also not correct :(
As I just experienced, in a custom controller, it is possible to create a date() without accessing the current user at all. Then this code isn't executed and the timezone is wrong.
Comment #15
BerdirComment #16
dawehnermhhh, its tricky, given that we should not authenticate too early,
so what about running it after authenciation?
Comment #18
mpdonadioIs this Needs Review or Needs Work? It looks like the retest got stuck.
Comment #19
BerdirJust re-uploading the patch.
Comment #20
BerdirI think we need a test that replicates #14.
Somewhere in a test module, add a route and a controller that does nothing but return the current date in a Response() object. _access: 'true', so that there are no access restrictions.
Then access that in the test, similar to the existing test assertions there.
Comment #22
tduong CreditAttribution: tduong at MD Systems GmbH commentedUploaded patch:
Comment #24
almaudoh CreditAttribution: almaudoh commentedThis means an exception may be thrown in subsequent lines if
$context['user']
or$context['uid']
is referenced since they may not be defined.Comment #25
BerdirThose two keys are by design optional. It's already wrapped in an if ($this->currentUser) and might sometimes not exist. This just means it might not exist in more cases.
Comment #26
tduong CreditAttribution: tduong at MD Systems GmbH commentedTo fix these tests we need to explicitly call
date_default_timezone_set('Australia/Sydney');
in setUp(), so that the timezone is consistent while the installer runs. The proper fix is already discussed in #2611082: Inconsistent time zones in web tests issue, so the change in the patch #2 is the solution for both issues. Both changes are needed at the same time.I'll close the other issue as we will now solve the bug here.
Comment #27
tduong CreditAttribution: tduong at MD Systems GmbH commentedPatch.
Comment #28
mpdonadioThere were other bits in the patch in #2611082: Inconsistent time zones in web tests that also need to be added. I'll add them over my lunch break and upload.
Comment #29
BerdirI'm not sure. Maybe the test but we already are adding test coverage for this here and have failing tests without that, so...
We have a passing patch here now, we already had the rdf test fix here too and the other tests are passing with both changes combined.
Comment #30
mpdonadioOK, just talked with @berdir on IRC, @tduong is going to add a few bit from the other issue in, and I'll do final review.
Comment #31
tduong CreditAttribution: tduong at MD Systems GmbH commentedAdded these few bit from the other issue in.
Comment #33
mpdonadioI think the code here can warrant a better comment as to who the try/catch is needed and expand a bit why the exception can be ignored.
Otherwise, I think the change is reasonable and the test coverage is adequate.
Some of the code is cherry-picked from one of my patches on #2611082: Inconsistent time zones in web tests (mainly the new TimeZoneTest), if anyone objects to me doing the final review.
Going to do some manual testing to double check this before I RTBC it.
Comment #34
mpdonadioJust ran some manual tests w/ a bad TZ in php.ini, and this does appear to fix the original issue reported in #2638678: date.timezone not being set in php.ini results in exceptions (which is mostly an untestable situation).
I'm not hung up on the comment for the try/catch I mentioned above, so I am setting RTBC, and a committer can the have final call on that.
Comment #35
alexpottThis needs a comment - why is the
if($this->currentUser)
not working? Or to ask the other way around how do we get into the situation where $this->currentUser == TRUE but ->id() throws an exception?Unnecessary blank line
Comment #36
Berdir$this->currentUser is just the account proxy. loading the user and especially calling drupal_get_user_timezone() could fail. See the test fail in the first issue https://qa.drupal.org/pifr/test/1077288
We could add something like that not sure. But while we're adding it here, for that specific thing, I think this try/catch is more generic. Whatever happens, we should do everything we can to not throw exceptions or die while trying to log something. and since we're already in the logger, I think we also shouldn't try to log that.
Comment #37
tduong CreditAttribution: tduong at MD Systems GmbH commentedUpdated exception comment and deleted unnecessary blank line.
Comment #38
swentel CreditAttribution: swentel commentedBack to RTBC then.
Comment #39
alexpottThanks for addressing my feedback. Committed 9970cf7 and pushed to 8.0.x and 8.1.x. Thanks!