You are here

public function MediaLibraryAccessTest::testFieldWidgetEntityCreateAccess in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media_library/tests/src/Kernel/MediaLibraryAccessTest.php \Drupal\Tests\media_library\Kernel\MediaLibraryAccessTest::testFieldWidgetEntityCreateAccess()

Tests that the field widget opener respects entity creation permissions.

File

core/modules/media_library/tests/src/Kernel/MediaLibraryAccessTest.php, line 107

Class

MediaLibraryAccessTest
Tests the media library access.

Namespace

Drupal\Tests\media_library\Kernel

Code

public function testFieldWidgetEntityCreateAccess() {

  /** @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',
  ]);
}