grantPermission example


    $host->save();

    $translation = $host->addTranslation('fr');
    // cSpell:disable-next-line     $translation->title = 'Animaux avec des noms étranges';
    $translation->body->value = $host->body->value;
    $translation->body->format = $host->body->format;
    $translation->save();

    Role::load(RoleInterface::AUTHENTICATED_ID)
      ->grantPermission('translate any entity')
      ->save();

    $page = $this->getSession()->getPage();
    $assert_session = $this->assertSession();
    $this->drupalGet('/fr/node/' . $host->id() . '/edit');
    $this->waitForEditor();

    // Test that the default alt attribute displays without an override.     // cSpell:disable-next-line     $this->assertNotEmpty($assert_session->waitForElementVisible('xpath', '//img[contains(@alt, "texte alternatif par défaut")]'));
    // Test `aria-label` attribute appears on the preview wrapper.
protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Give anonymous users permission to customize shortcut links, so that we     // can verify the cache tags of cached versions of shortcuts.     $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
    $user_role->grantPermission('customize shortcut links');
    $user_role->grantPermission('access shortcuts');
    $user_role->save();
  }

  /** * {@inheritdoc} */
  protected function createEntity() {
    // Create a "Llama" shortcut.     $shortcut = Shortcut::create([
      'shortcut_set' => 'default',
      

}

/** * Grant all non-anonymous roles the 'delete own files' permission. */
function file_post_update_add_permissions_to_roles(?array &$sandbox = NULL): void {
  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'user_role', function DRoleInterface $role): bool {
    if ($role->id() === RoleInterface::ANONYMOUS_ID || $role->isAdmin()) {
      return FALSE;
    }
    $role->grantPermission('delete own files');
    return TRUE;
  });
}
$manager->getDefinitions();
    /** @var \Drupal\filter\FilterFormatInterface $filter_test_format */
    $filter_test_format = FilterFormat::load('filter_test');
    $full_html_format = FilterFormat::load('full_html');
    $filtered_html_format = FilterFormat::load('filtered_html');

    /** @var \Drupal\user\RoleInterface $role */
    $role = Role::create([
      '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);
  }

  
    $assert_session->elementExists('css', 'td.views-field-operations li a:contains("Edit")', $row1);
    $assert_session->linkByHrefExists('/media/' . $media1->id() . '/edit');
    $assert_session->elementExists('css', 'td.views-field-operations li a:contains("Delete")', $row1);
    $assert_session->linkByHrefExists('/media/' . $media1->id() . '/delete');

    // Make the user the owner of the unpublished media item and assert the     // media item is only visible with the 'view own unpublished media'     // permission.     $media2->setOwner($this->nonAdminUser)->save();
    $this->getSession()->reload();
    $assert_session->pageTextNotContains($media2->label());
    $role->grantPermission('view own unpublished media')->save();
    $this->getSession()->reload();
    $row = $assert_session->elementExists('css', 'table tbody tr:nth-child(2)');
    $name = $assert_session->elementExists('css', 'td.views-field-name a', $row);
    $this->assertSame($media2->label()$name->getText());
    $status_element = $assert_session->elementExists('css', 'td.views-field-status', $row);
    $this->assertSame('Unpublished', $status_element->getText());

    // Assert the admin user can always view all media.     $this->drupalLogin($this->adminUser);
    $this->drupalGet('/admin/content/media');
    $row1 = $assert_session->elementExists('css', 'table tbody tr:nth-child(1)');
    
$this->rebuildContainer();
    \Drupal::service('module_installer')->install(['editor']);
    /** @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');
    

  protected function setUp(): void {
    parent::setUp();

    // Use Stark theme for testing markup output.     \Drupal::service('theme_installer')->install(['stark']);
    $this->config('system.theme')->set('default', 'stark')->save();
    $this->installEntitySchema('entity_test');
    // Grant the 'view test entity' permission.     $this->installConfig(['user']);
    Role::load(RoleInterface::ANONYMOUS_ID)
      ->grantPermission('view test entity')
      ->save();

    $this->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType, 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);

    // Set up a field, so that the entity that'll be referenced bubbles up a     // cache tag when rendering it entirely.     FieldStorageConfig::create([
      'field_name' => 'body',
      'entity_type' => $this->entityType,
      'type' => 'text',
      'settings' => [],
    ])
$this->setUpCurrentUser(['uid' => 1]);

    $this->installEntitySchema('entity_test_mul');
    $this->installEntitySchema('entity_test_admin_routes');

    /** @var \Drupal\user\RoleInterface $role */
    $role = Role::create([
      'id' => RoleInterface::ANONYMOUS_ID,
      'label' => 'Anonymous',
    ]);
    $role
      ->grantPermission('administer entity_test content')
      ->grantPermission('view test entity');
    $role->save();
  }

  protected function httpKernelHandle($url) {
    $request = Request::create($url);
    /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel */
    $http_kernel = \Drupal::service('http_kernel');
    return $http_kernel->handle($request, HttpKernelInterface::SUB_REQUEST)->getContent();
  }

  

    $this->assertSame(204, $response->getStatusCode());
  }

  /** * Ensures that Drupal's page cache is effective. * * @see https://www.drupal.org/project/drupal/issues/3009596 */
  public function testPageCacheFromIssue3009596() {
    $anonymous_role = Role::load(RoleInterface::ANONYMOUS_ID);
    $anonymous_role->grantPermission('access content');
    $anonymous_role->trustData()->save();

    NodeType::create(['type' => 'emu_fact'])->save();
    \Drupal::service('router.builder')->rebuildIfNeeded();

    $node = Node::create([
      'type' => 'emu_fact',
      'title' => "Emus don't say moo!",
    ]);
    $node->save();

    

  protected function grantUserPermissionToCreateContentOfType(AccountInterface $account$content_type_id) {
    $role_ids = $account->getRoles(TRUE);
    /** @var \Drupal\user\RoleInterface $role */
    $role_id = reset($role_ids);
    $role = Role::load($role_id);
    $role->grantPermission(sprintf('create %s content', $content_type_id));
    $role->grantPermission(sprintf('edit any %s content', $content_type_id));
    $role->grantPermission(sprintf('delete any %s content', $content_type_id));
    $role->save();
  }

}
protected $nonReferencingEntity;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Give anonymous users permission to view test entities, so that we can     // verify the cache tags of cached versions of test entity pages.     $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
    $user_role->grantPermission('view test entity');
    $user_role->save();

    // Create an entity.     $this->entity = $this->createEntity();

    // If this is an entity with field UI enabled, then add a configurable     // field. We will use this configurable field in later tests to ensure that     // field configuration invalidate render cache entries.     if ($this->entity->getEntityType()->get('field_ui_base_route')) {
      // Add field, so we can modify the field storage and field entities to       // verify that changes to those indeed clear cache tags.
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $this->config('statistics.settings')
      ->set('count_content_views', 1)
      ->save();

    Role::load(AccountInterface::ANONYMOUS_ROLE)
      ->grantPermission('view post access counter')
      ->save();

    // Add another language to enable multilingual path processor.     ConfigurableLanguage::create(['id' => 'xx', 'label' => 'Test language'])->save();
    $this->config('language.negotiation')->set('url.prefixes.en', 'en')->save();

    $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
    $this->node = $this->drupalCreateNode();
  }

  /** * Tests that statistics works with different addressing variants. */
/** * Grant permissions to a user role. * * @param \Drupal\user\RoleInterface $role * The user role entity to alter. * @param array $permissions * (optional) A list of permission names to grant. */
  protected function grantPermissions(RoleInterface $role, array $permissions) {
    foreach ($permissions as $permission) {
      $role->grantPermission($permission);
    }
    $role->trustData()->save();
  }

}
$this->installConfig(['user']);

    // Create some test entities.     for ($i = 0; $i < 5; $i++) {
      EntityTest::create(['name' => $this->randomString()])->save();
    }

    // Create and admin user.     $this->adminUser = $this->createUser(['view test entity'], FALSE, TRUE);

    Role::load(AccountInterface::ANONYMOUS_ROLE)
      ->grantPermission('view test entity')
      ->save();
  }

  /** * Tests entity link fields. */
  public function testEntityLink() {
    // Anonymous users cannot see edit/delete links.     $expected_results = ['canonical' => TRUE, 'edit-form' => FALSE, 'delete-form' => FALSE, 'canonical_raw' => TRUE, 'canonical_raw_absolute' => TRUE];
    $this->doTestEntityLink(\Drupal::currentUser()$expected_results);

    

  protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Give anonymous users permission to view nodes, so that we can verify the     // cache tags of cached versions of node pages.     Role::load(RoleInterface::ANONYMOUS_ID)->grantPermission('access tour')
      ->save();
  }

  /** * Tests cache tags presence and invalidation of the Tour entity. * * Tests the following cache tags: * - 'tour:<tour ID>' */
  public function testRenderedTour() {
    $url = Url::fromRoute('tour_test.1');

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