User context missing when using toUrl in some circumstances

Created on 22 May 2019, over 5 years ago
Updated 23 May 2024, 7 months ago

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

🐛 Bug report
Status

Postponed: needs info

Version

11.0 🔥

Component
Entity 

Last updated about 3 hours ago

Created by

🇦🇺Australia dpi Perth, Australia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India rugvedmandrekar

    I 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; 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.

  • 🇦🇺Australia dpi Perth, Australia

    Closed 🐛 The 'entity:user' context is required and not present Closed: duplicate as duplicate of this issue.

  • 🇺🇸United States dave reid Nebraska USA

    I 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').

  • Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    39:06
    39:06
    Queueing
  • 🇧🇪Belgium herved

    Cross-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

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    29,471 pass, 2 fail
  • last update over 1 year ago
    30,146 pass, 2 fail
  • 🇧🇪Belgium herved

    Ok 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:

    $context_repository = $this->container->get('context.repository');
    $contexts = $context_repository->getAvailableContexts();
    $this->assertArrayHasKey('@user.current_user_context:current_user', $contexts);
    $this->assertFalse($contexts['@user.current_user_context:current_user']->hasContextValue());
    

    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::match

  • last update over 1 year ago
    30,147 pass
  • last update over 1 year ago
    29,472 pass
  • 🇨🇭Switzerland berdir Switzerland

    FWIW, 🐛 Many calls to ContextRepository::getAvailableContexts() due to entity upcasting Needs review removes that whole chunk completely, I'd expect that fixes the issue as well.

  • 🇧🇪Belgium herved

    Indeed 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

  • 🇨🇭Switzerland berdir Switzerland

    I'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.

  • For 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\UserCreationTrait and added that trait to my kernel test class with use UserCreationTest. Then I added $this->setUpCurrentUser() in my class's setUp method. In my case, I didn't need to call that method any optional arguments.

  • 🇸🇰Slovakia yurg

    There are a lot of
    "

    Drupal\Component\Plugin\Exception\ContextException: The 'entity:user' context is required and not present. in Drupal\Core\Plugin\Context\Context->getContextValue()

    " mentions across the versions.

    For me the #44 🐛 User context missing when using toUrl in some circumstances Needs work is a life-saver. Thanks.

  • 🇬🇧United Kingdom joachim

    If you don't want to make a user current, this workaround creates the anon user in a kernel test:

    use UserCreationTest
    
        $values = [
          'uid' => 0,
          'status' => 0,
          'name' => '',
        ];
        $this->createUser(values: $values);
    

    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.

  • Status changed to Postponed: needs info 7 months ago
  • 🇦🇺Australia mstrelan

    As per #45 this is probably no longer needed as per 🐛 Many calls to ContextRepository::getAvailableContexts() due to entity upcasting Needs review which has landed. Marking PMNMI for someone to verify if there is anything left to do here.

Production build 0.71.5 2024