createUserSession example

return $user;
  }

  /** * Tests the method getRoles exclude or include locked roles based in param. * * @see \Drupal\user\Entity\User::getRoles() * @covers ::getRoles */
  public function testUserGetRoles() {
    // Anonymous user.     $user = $this->createUserSession([]);
    $this->assertEquals([RoleInterface::ANONYMOUS_ID]$user->getRoles());
    $this->assertEquals([]$user->getRoles(TRUE));

    // Authenticated user.     $user = $this->createUserSession([], TRUE);
    $this->assertEquals([RoleInterface::AUTHENTICATED_ID]$user->getRoles());
    $this->assertEquals([]$user->getRoles(TRUE));
  }

}


    $entity_type_manager = $this->createMock('Drupal\Core\Entity\EntityTypeManagerInterface');
    $entity_type_manager->expects($this->any())
      ->method('getStorage')
      ->with($this->equalTo('user_role'))
      ->willReturn($role_storage);
    $container = new ContainerBuilder();
    $container->set('entity_type.manager', $entity_type_manager);
    \Drupal::setContainer($container);

    $this->users['user_one'] = $this->createUserSession(['role_one']);
    $this->users['user_two'] = $this->createUserSession(['role_one', 'role_two']);
    $this->users['user_three'] = $this->createUserSession(['role_two'], TRUE);
    $this->users['user_last'] = $this->createUserSession();
  }

  /** * Tests the has permission method. * * @param string $permission * The permission to check. * @param \Drupal\Core\Session\AccountInterface[] $sessions_with_access * The users with access. * @param \Drupal\Core\Session\AccountInterface[] $sessions_without_access * The users without access. * * @dataProvider providerTestHasPermission * * @see \Drupal\Core\Session\UserSession::hasPermission() */
Home | Imprint | This part of the site doesn't use cookies.