This is wrong:

$this->container->set('current_user', $user);

and should be replaced with

$this->container->get('request')->attributes->set('_account', $user);

Comments

tstoeckler’s picture

Actually, I'm not sure there's anything wrong with the current code. See line 606 in core.services.yml

  current_user:
    class: Drupal\Core\Session\AccountInterface
    factory_method: authenticate
    factory_service: authentication
    arguments: ['@request']
    scope: request
klausi’s picture

Status: Active » Closed (works as designed)

Oh, some people said on IRC that the current user service is not supposed to be set ... I'm confused now. Please re-open if you think this needs fixing.