addRole example


  public function testAccessUnpublished($allowed_to_view_unpublished$expected_rendered, CacheableMetadata $expected_cacheability, array $expected_attachments) {
    // Unpublish the embedded entity so we can test variations in behavior.     $this->embeddedEntity->setUnpublished()->save();

    // Are we testing as a user who is allowed to view the embedded entity?     if ($allowed_to_view_unpublished) {
      $this->container->get('current_user')
        ->addRole($this->drupalCreateRole(['view own unpublished media']));
    }

    $content = $this->createEmbedCode([
      'data-entity-type' => 'media',
      'data-entity-uuid' => static::EMBEDDED_ENTITY_UUID,
    ]);
    $result = $this->applyFilter($content);

    if (!$expected_rendered) {
      $this->assertEmpty($this->getRawContent());
    }
    
// Test that the cancel user page has admin fields.     $cancel_user = $this->createUser();
    $this->drupalGet('user/' . $cancel_user->id() . '/cancel');
    $this->assertSession()->responseContains('Are you sure you want to cancel the account ' . $cancel_user->getAccountName() . '?');
    $this->assertSession()->responseContains('Disable the account and keep its content.');

    // Test that cancel confirmation gives an admin style message.     $this->submitForm([], 'Confirm');
    $this->assertSession()->pageTextContains('Account ' . $cancel_user->getAccountName() . ' has been disabled.');

    // Repeat with permission to select account cancellation method.     $user->addRole($this->drupalCreateRole(['select account cancellation method']));
    $user->save();
    $cancel_user = $this->createUser();
    $this->drupalGet('user/' . $cancel_user->id() . '/cancel');
    $this->assertSession()->pageTextContains('Cancellation method');
  }

}
$user1->save();
    $user2 = User::create([
      'name' => 'username2',
      'status' => 1,
    ]);
    $user2->save();
    $role = Role::create([
      'id' => 'role1',
      'label' => 'role1',
    ]);
    $role->grantPermission('view media')->trustData()->save();
    $user2->addRole($role->id());
    $this->assertFalse($test_media_type->access('view label', $user1));
    $this->assertTrue($test_media_type->access('view label', $user2));
  }

  /** * Tests creating a media item programmatically. */
  public function testMediaEntityCreation() {
    $media = Media::create([
      'bundle' => $this->testMediaType->id(),
      'name' => 'Unnamed',
      
$values = [
      'uid' => 1,
      'roles' => ['test_role_one'],
    ];
    $user = User::create($values);

    $this->assertTrue($user->hasRole('test_role_one'));
    $this->assertFalse($user->hasRole('test_role_two'));
    $this->assertEquals([RoleInterface::AUTHENTICATED_ID, 'test_role_one']$user->getRoles());

    $account = $user->addRole('test_role_one');
    $this->assertSame($user$account);
    $this->assertTrue($user->hasRole('test_role_one'));
    $this->assertFalse($user->hasRole('test_role_two'));
    $this->assertEquals([RoleInterface::AUTHENTICATED_ID, 'test_role_one']$user->getRoles());

    $user->addRole('test_role_two');
    $this->assertTrue($user->hasRole('test_role_one'));
    $this->assertTrue($user->hasRole('test_role_two'));
    $this->assertEquals([RoleInterface::AUTHENTICATED_ID, 'test_role_one', 'test_role_two']$user->getRoles());

    $account = $user->removeRole('test_role_three');
    
$admin_role->grantPermission('access comments');
    $admin_role->grantPermission('post comments');
    $admin_role->grantPermission('view test entity');
    $admin_role->save();

    /** @var \Drupal\user\RoleInterface $anonymous_role */
    $anonymous_role = Role::load(Role::ANONYMOUS_ID);
    $anonymous_role->grantPermission('access comments');
    $anonymous_role->save();

    $this->adminUser = $this->userStorage->create(['name' => $this->randomMachineName()]);
    $this->adminUser->addRole('admin');
    $this->adminUser->save();
  }

}

  public function testWorkspaceAccess($operation$permission) {
    $user = $this->createUser();
    $this->setCurrentUser($user);
    $workspace = Workspace::create(['id' => 'oak']);
    $workspace->save();

    $this->assertFalse($workspace->access($operation$user));

    \Drupal::entityTypeManager()->getAccessControlHandler('workspace')->resetCache();
    $role = $this->createRole([$permission]);
    $user->addRole($role);
    $this->assertTrue($workspace->access($operation$user));
  }

  /** * Tests workspace publishing access. */
  public function testPublishWorkspaceAccess() {
    $user = $this->createUser([
      'view own workspace',
      'edit own workspace',
    ]);
    

  protected $defaultTheme = 'stark';

  /** * Tests the generated title of a user: roles argument. */
  public function testArgumentTitle() {
    $role_id = $this->createRole([], 'markup_role_name', '<em>Role name with markup</em>');
    $this->createRole([], 'second_role_name', 'Second role name');
    $user = $this->createUser([], 'User with role one');
    $user->addRole($role_id);
    $user->save();
    $second_user = $this->createUser([], 'User with role two');
    $second_user->addRole('second_role_name');
    $second_user->save();

    $this->drupalGet('/user_roles_rid_test/markup_role_name');
    $this->assertSession()->assertEscaped('<em>Role name with markup</em>');

    $views_user = $this->drupalCreateUser(['administer views']);
    $this->drupalLogin($views_user);

    
    $rolename_a = 'a' . $this->randomMachineName(8);
    $this->drupalCreateRole(['access content']$rolename_a, '<em>' . $rolename_a . '</em>', 9);

    $rolename_b = 'b' . $this->randomMachineName(8);
    $this->drupalCreateRole(['access content']$rolename_b$rolename_b, 8);

    $rolename_not_assigned = $this->randomMachineName(8);
    $this->drupalCreateRole(['access content']$rolename_not_assigned$rolename_not_assigned);

    // Add roles to user 1.     $user = User::load(1);
    $user->addRole($rolename_a);
    $user->addRole($rolename_b);
    $user->save();

    $this->drupalLogin($this->createUser(['access user profiles']));
    $this->drupalGet('/test-views-handler-field-role');
    // Verify that the view test_views_handler_field_role renders role assigned     // to user in the correct order and markup in role names is escaped.     $this->assertSession()->responseContains($rolename_b . Html::escape('<em>' . $rolename_a . '</em>'));
    // Verify that the view test_views_handler_field_role does not render a role     // not assigned to a user.     $this->assertSession()->pageTextNotContains($rolename_not_assigned);
  }

    $this->anonymous->save();
    // Loading the anonymous user adds the correct role.     $this->anonymous = User::load($this->anonymous->id());

    // Setup an authenticated user for our tests.     $this->authenticated = User::create([
      'name' => $this->randomMachineName(),
    ]);
    $this->authenticated->save();
    // Add the custom role.     $this->authenticated->addRole($this->role->id());
  }

  /** * Tests the user_role condition. */
  public function testConditions() {
    // Grab the user role condition and configure it to check against     // authenticated user roles.     /** @var \Drupal\Core\Condition\ConditionInterface $condition */
    $condition = $this->manager->createInstance('user_role')
      ->setConfig('roles', [RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID])
      
    $site_configuration_role = $this->drupalCreateRole([
      'access toolbar',
      'access shortcuts',
      'administer site configuration',
      'access administration pages',
    ]);

    // Create two different users with the same role to assert that the second     // user has a cache hit despite the user cache context, as     // the returned cache contexts include those from lazy-builder content.     $site_configuration_user1 = $this->drupalCreateUser();
    $site_configuration_user1->addRole($site_configuration_role);
    $site_configuration_user1->save();
    $site_configuration_user2 = $this->drupalCreateUser();
    $site_configuration_user2->addRole($site_configuration_role);
    $site_configuration_user2->save();

    $this->drupalLogin($site_configuration_user1);
    $this->verifyDynamicPageCache($test_page_url, 'MISS');
    $this->verifyDynamicPageCache($test_page_url, 'HIT');
    $this->assertCacheContexts(['user', 'url.query_args:_wrapper_format']);
    $this->assertSession()->linkExists('Shortcuts');
    $this->assertSession()->linkExists('Cron');

    
// Create a user at a certain timestamp.     $account = $this->drupalCreateUser();
    $account->created = 1363219200;
    $account->save();
    $accounts[$account->label()] = $account;
    $timestamp_user = $account->label();

    $rid_1 = $this->drupalCreateRole([], 'custom_role_1', 'custom_role_1');
    $rid_2 = $this->drupalCreateRole([], 'custom_role_2', 'custom_role_2');

    $account = $this->drupalCreateUser();
    $account->addRole($rid_1);
    $account->addRole($rid_2);
    $account->save();
    $accounts[$account->label()] = $account;
    $role_account_name = $account->label();

    // Create an admin user and look at the listing.     $admin_user = $this->drupalCreateUser(['administer users']);
    $accounts[$admin_user->label()] = $admin_user;

    $accounts['admin'] = User::load(1);

    
/** * Tests the Who's Online block. */
  public function testWhosOnlineBlock() {
    $request_time = \Drupal::time()->getRequestTime();
    // Generate users.     $user1 = User::create([
      'name' => 'user1',
      'mail' => 'user1@example.com',
    ]);
    $user1->addRole('administrator');
    $user1->activate();
    $user1->setLastAccessTime($request_time);
    $user1->save();

    $user2 = User::create([
      'name' => 'user2',
      'mail' => 'user2@example.com',
    ]);
    $user2->activate();
    $user2->setLastAccessTime($request_time + 1);
    $user2->save();

    
'id' => 'admin',
      'label' => 'admin',
    ]);
    $role->grantPermission($filter_test_format->getPermissionName());
    $role->grantPermission($full_html_format->getPermissionName());
    $role->grantPermission($filtered_html_format->getPermissionName());
    $role->save();
    $this->testUser = User::create([
      'name' => 'foobar',
      'mail' => 'foobar@example.com',
    ]);
    $this->testUser->addRole($role->id());
    $this->testUser->save();
    \Drupal::service('current_user')->setAccount($this->testUser);
  }

  /** * {@inheritdoc} */
  public function getFormId() {
    return 'test_text_area_element';
  }

  
/** @var \Drupal\contact\ContactFormInterface $contact_form */
    $contact_form = ContactForm::load('feedback');
    $recipients = $contact_form->getRecipients();
    $this->assertEquals(['simpletest@example.com']$recipients);

    $role = Role::create([
      'id' => 'admin_theme',
      'label' => 'Admin theme',
    ]);
    $role->grantPermission('view the administration theme');
    $role->save();
    $this->adminUser->addRole($role->id());
    $this->adminUser->save();
    $this->drupalGet('node/add');
    $this->assertSession()->statusCodeEquals(200);

    // Ensure that there are no pending updates after installation.     $this->drupalLogin($this->rootUser);
    $this->drupalGet('update.php/selection');
    $this->updateRequirementsProblem();
    $this->drupalGet('update.php/selection');
    $this->assertSession()->pageTextContains('No pending updates.');

    
$this->enableViewsTestModule();

    $this->webUser = $this->drupalCreateUser();
    $roles = $this->webUser->getRoles();
    $this->webRole = $roles[0];

    $this->normalRole = $this->drupalCreateRole([]);
    $this->normalUser = $this->drupalCreateUser([
      'views_test_data test permission',
    ]);
    $this->normalUser->addRole($this->normalRole);
    $this->normalUser->save();
    // @todo when all the plugin information is cached make a reset function and     // call it here.   }

}
Home | Imprint | This part of the site doesn't use cookies.