This error is likely be repro'd out of tests only, but reporting as I see it.
Using toUrl within a Kernel test results in the following uncaught exception.
Drupal\Component\Plugin\Exception\ContextException: The 'entity:user' context is required and not present.
This is causing the tests for Linky project to fail. This may be caused by new code introduced this year in #2942907: Entity system does not provide an API for retrieving an entity variant that is safe for editing, specifically the changes introduced to \Drupal\Core\ParamConverter\EntityConverter::convert
| Comment | File | Size | Author |
|---|---|---|---|
| #44 | interdiff-43-44.txt | 642 bytes | herved |
| #44 | 3056234-44.patch | 1.95 KB | herved |
| #43 | interdiff-10-43.txt | 545 bytes | herved |
| #43 | 3056234-43.patch | 1.86 KB | herved |
| #10 | interdiff-5-10.txt | 2.52 KB | subhojit777 |
Comments
Comment #2
dpiTests for fail
Comment #3
dpiComment #4
dpiComment #5
dpiComment #6
m4oliveiRan into this in our test suite today upgrading to 8.7.7 from 8.6. I found this was a suitable workaround, simply defining the anonymous user in the setUp method:
Comment #7
hchonovLet's rename it to something meaningful. EntityUrlTest maybe?
Let's make this an EntityKernelTest.
The trait and the modules are included in EntityKernelTestBase.
Let's assert something :)
Comment #8
berdirwe can combine the two if conditions into if (isset() && .. hasContextValue() IMHO, then it's a single-line change.
Comment #10
subhojit777It was also failing for me in tests after 8.6 -> 8.7.8 upgrade and the patch in #5 fixed it. Here is another patch containing the changes suggested in #7 and #8
Comment #12
berdirWe're seeing this in our D9 tests, 300 or so behat tests in, this exceptions happens in a long backtrace as part of installing a module and after that, behat is completely messed up and the remaining 60 scenarios fail with an exception. I can't reproduce when just running that test.
Will see if this helps, but I kind of suspect that this indicates a problem elsewhere. My guess is that in my case, there is still a current user ID set, but the user was already deleted. In other cases, it could be that the entity for user 0 was deleted from the database.
No idea what D8/D9 change could cause this.
Comment #15
acbramley commentedJust hit this in a test that created (and subsequently deleted) an entity type that admin_toolbar_tools triggered a menu rebuild from. Here's the trace:
The patch fixed it, cheers!
Comment #17
rgpublicOur whole log is inundated with this error happening every 15 seconds. Applying the patch (which perfectly works on Drupal 9.2.2) stops it. Still wonder why it happens. The website makes heavy use of Drupal's/Symfony's Session capabilities (Session-Manager service, currentUser()->setAccount). Still I have a hard time understanding what the origin of all this is.
Comment #18
codersrini commentedThe patch given on #5 fixed the issue for me in Drupal 9.2.6. Thanks
Comment #20
arnaud-brugnon commentedPatch #10 works for me
Comment #21
dpiComment #22
acbramley commentedStarted getting this post 9.3 upgrade. Creating the anonymous user didn't fix it, but the patch did :)
Comment #23
renguer0 commentedPatch #10 works but we lost 'Anonymous user' group.
It just has no effect when selecting it on page view conditionals on content. There is a workaround for that? Cleaned all caches.
Comment #24
frank.dev commentedFor me the patch only solves part of the problem.
The website is accessible again for anonymous visitors. But when calling the DB log, the error message remains with a white screen.
Comment #25
troybthompson commentedI'm running 9.3.9 and started getting site down errors after adding a link field that seemed to didn't fully get created. Restored a backup and would still crash whenever I cleared my cache, removed modules until it started working but then wouldn't work for anonymous users.
I used #10 and now it loads for anonymous but still not clear what the original cause was.
When viewing logs, I still get a white screen with:
Comment #26
frank.dev commentedI was able to fix the problem described here:
The data record with the uid = "0" was missing in the "users" database table. However, the record was not simply deleted beforehand, instead the uid's seem to have simply "increased" by one place - uid "0" became "1", "1" became "2" and so on.
I solved the problem by resetting all existing uid's by 1 - 1 to 0, 2 to 1 and so on.
I was then able to take patch #10 back again, it's no longer needed.
Comment #27
frank.dev commentedAddendum:
The uid "0" stands for the anonymous user and is therefore urgently needed.
Comment #28
troybthompson commentedI just looked and my uids are 1, 7, 8, 9 with 9 being blank so I assume anonymous. So what would cause that?
There was a user 0 in my backup from December so I'm guessing a Drupal update since then? Possibly a module but I don't think I've added much.
Comment #29
frank.dev commentedI don't know exactly what caused the error either.
All I know is that for the last few months it has been observed whenever I import a full Drupal database backup into a new empty database. At this point, the uid 0 "disappeared" and even before I connected this new database to a Drupal installation.
If I then only import the "users" table into the database created in this way, uid 0 is retained.
troybthompson, has the problem been solved for you now?
Comment #30
rgpublic@Frank.dev: Did you check your backup/restore method? If this is reproducible, perhaps it has sth. to do with type of export/import method that this line is lost. Perhaps it's a bug/configuration issue in PHPMyAdmin / mysqldump... Just a thought. In my case, the line was definitely there, so there might be different issues here that lead to the symptom.
Comment #31
frank.dev commentedrgpublic, thanks for the tip. However, I have been using the same tool to import the database for 5 years now:
https://www.ozerov.de/bigdump/
Also with the same PHP version.
The error does not actually occur with every database import, but for the first time in the last few months and quite often.
Addendum:
When importing the backup file, no line / data record was deleted. The modified "users" table contained the same number of records as the backup file and the original database.
Comment #32
dpiIf you are exporting/importing your database and you're finding you have missing anonymous user, you can try setting the SQL mode before your import with
SET sql_mode =NO_AUTO_VALUE_ON_ZERO. Otherwise MySQL, etc, will instead treat the zero ID as autoincrementable and append it to the end of your users table with a new ID.If you use Drush SQL export you already get this mode set, see
\Drupal\Core\Command\DbDumpCommand::getTemplate. Im guessing you are not using Drush for these exports, perhaps they are done with themysql/mysqldumpCLI tool or some higher level tool.Comment #33
frank.dev commented@dpi, thanks for the tip. However, "SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";" is always set in my backup files,
Comment #34
troybthompson commentedThat may be the problem in my case, it looks like the original backup when my site was broken had UID 0. The error initially was "Drupal\Component\Plugin\Exception\PluginNotFoundException: The "" entity type does not exist. in Drupal\Core\Entity\EntityTypeManager->getDefinition() (line 143 of core/lib/Drupal/Core/Entity/EntityTypeManager.php)." and then after backing up and restoring I got the error message in this issue.
I use Navicat for database work and haven't had an issue before but this backup was created by my hosting place for me. In their sql file, the only reference to auto value is:
which I assume means commented out?
Manually changing it back to 0 solved the problem, let me view the error log page again, and allowed me to remove patch #10. So thank you for the help even if this is unrelated to this issue.
But now that means I still have the cause of my original issue to figure out. =^)
Comment #37
anybodyComment #38
veder commentedI was experiencing the same error in my Drupal site, core ver. 9.1.0 after importing a database from one of my team member's localhost. That was occurring because of the same problem as is described in #28. However my fix was different. I opened the database file-I had received from my colleague and imported into my localhost-and observed that an insert statement for the anonymous user (uid, 0) was present in the SQL script. For some reason the uid of that row got changed to 186 in my site's database in my localhost; I found this out by executing a select query with a where condition for the uuid column with the value copied and pasted from the database script. I just changed 186 to 0.
Comment #40
dpiClosed #3357354: The 'entity:user' context is required and not present as duplicate of this issue.
Comment #41
dave reidI just experienced this in a kernel test in some custom code as well. I believe when we had code running Url::fromUri('internal:/taxonomy/term/1').
Comment #42
herved commentedCross-posting #3357354-16: The 'entity:user' context is required and not present here.
The patch solves my issue on 2 projects caused by image styles as explained there. Few, what an absolute horror to debug. Thank you so much!
@Berdir from #12: Could the introduction of image lazy loading in core be directly related to the emergence of this issue?
Edit: I will reroll the patch for 10.1.x
Comment #43
herved commentedComment #44
herved commentedOk the assert is wrong there it seems.
Not exactly sure what to assert tbh, we mainly need to ensure that there are no exceptions thrown even though the current user context is NULL (because the anonymous user is used).
We could also do this but it's not really what we are trying to test:
PS: Very interesting to see that such a simple URL causes the issue as anonymous. My case in #42 was hitting the same issue but from
\Drupal\Core\Routing\AccessAwareRouter::matchComment #45
berdirFWIW, #3158130: Many calls to ContextRepository::getAvailableContexts() due to entity upcasting removes that whole chunk completely, I'd expect that fixes the issue as well.
Comment #46
herved commentedIndeed it does, and I see no side effects. I'll use that instead then. Thanks for pointing it out.
I guess we could close this one then?
Cheers
Comment #47
berdirI'm not sure, maybe wait until it's actually resolved. I'd love to get it done, but no idea how feasible it is to get it done soon. Hard to move it forward as it's related to several other language upcasting issues.
Comment #48
gabesulliceFor those looking for a simple workaround to fix their kernel tests, the following additions to my kernel test class suppressed the exception.
I imported
Drupal\Tests\user\Traits\UserCreationTraitand added that trait to my kernel test class withuse UserCreationTest. Then I added$this->setUpCurrentUser()in my class'ssetUpmethod. In my case, I didn't need to call that method any optional arguments.Comment #49
yurg commentedThere are a lot of
mentions across the versions. For me the #44 is a life-saver. Thanks.
Comment #50
joachim commentedIf you don't want to make a user current, this workaround creates the anon user in a kernel test:
Note that AFAICT the next call to createUser() will create user 1, so call it again if you don't want you actual test users to be that.
Comment #51
mstrelan commentedAs per #45 this is probably no longer needed as per #3158130: Many calls to ContextRepository::getAvailableContexts() due to entity upcasting which has landed. Marking PMNMI for someone to verify if there is anything left to do here.
Comment #52
smustgrave commentedSince there's been no follow up to #51 going to close out, if still a bug in D11 please re-open.