assertAccess example

/** * @covers ::render * @covers ::doRender * * @dataProvider providerAccessValues */
  public function testRenderWithPresetAccess($access) {
    $build = [
      '#access' => $access,
    ];

    $this->assertAccess($build$access);
  }

  /** * @covers ::render * @covers ::doRender * * @dataProvider providerAccessValues */
  public function testRenderWithAccessCallbackCallable($access) {
    $build = [
      '#access_callback' => function D) use ($access) {
        
$entity_values += [
      'status' => FALSE,
      'uid' => $user->id(),
      'bundle' => $media_type->id(),
    ];

    $entity = Media::create($entity_values);
    $entity->save();
    /** @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface $access_handler */
    $access_handler = $this->container->get('entity_type.manager')->getAccessControlHandler('media');
    $this->assertAccess($expected_result$expected_cache_contexts$expected_cache_tags$access_handler->access($entity$operation$user, TRUE));
  }

  /** * @param string[] $permissions * User permissions. * @param \Drupal\Core\Access\AccessResultInterface $expected_result * Expected result. * @param string[] $expected_cache_contexts * Expected cache contexts. * @param string[] $expected_cache_tags * Expected cache tags. * * @covers ::checkCreateAccess * @dataProvider providerCreateAccess */
/** @var \Drupal\media_library\MediaLibraryUiBuilder $ui_builder */
    $ui_builder = $this->container->get('media_library.ui_builder');

    // Create a media library state to test access.     $state = MediaLibraryState::create('media_library.opener.field_widget', ['file', 'image'], 'file', 2, [
      'entity_type_id' => 'entity_test',
      'bundle' => 'test',
      'field_name' => 'field_test_media',
    ]);

    $access_result = $ui_builder->checkAccess($this->createUser()$state);
    $this->assertAccess($access_result, FALSE, "The following permissions are required: 'administer entity_test content' OR 'administer entity_test_with_bundle content' OR 'create test entity_test_with_bundle entities'.", []['url.query_args', 'user.permissions']);

    // Create a user with the appropriate permissions and assert that access is     // granted.     $account = $this->createUser([
      'create test entity_test_with_bundle entities',
      'view media',
    ]);
    $access_result = $ui_builder->checkAccess($account$state);
    $this->assertAccess($access_result, TRUE, NULL, Views::getView('media_library')->storage->getCacheTags()['url.query_args', 'user.permissions']);
  }

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