You are here

public function MediaLibraryAccessTest::testEditorOpenerAccess in Drupal 10

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

@covers \Drupal\media_library\MediaLibraryEditorOpener::checkAccess

@dataProvider editorOpenerAccessProvider

Parameters

bool $media_embed_enabled: Whether to test with media_embed filter enabled on the text format.

bool $can_use_format: Whether the logged in user is allowed to use the text format.

File

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

Class

MediaLibraryAccessTest
Tests the media library access.

Namespace

Drupal\Tests\media_library\Kernel

Code

public function testEditorOpenerAccess($media_embed_enabled, $can_use_format) {
  $format = $this->container
    ->get('entity_type.manager')
    ->getStorage('filter_format')
    ->create([
    'format' => $this
      ->randomMachineName(),
    'name' => $this
      ->randomString(),
    'filters' => [
      'media_embed' => [
        'status' => $media_embed_enabled,
      ],
    ],
  ]);
  $format
    ->save();
  $permissions = [
    'access media overview',
    'view media',
  ];
  if ($can_use_format) {
    $permissions[] = $format
      ->getPermissionName();
  }
  $state = MediaLibraryState::create('media_library.opener.editor', [
    'image',
  ], 'image', 1, [
    'filter_format_id' => $format
      ->id(),
  ]);
  $access_result = $this->container
    ->get('media_library.ui_builder')
    ->checkAccess($this
    ->createUser($permissions), $state);
  if ($media_embed_enabled && $can_use_format) {
    $this
      ->assertAccess($access_result, TRUE, NULL, Views::getView('media_library')->storage
      ->getCacheTags(), [
      'user.permissions',
    ]);
  }
  else {
    $this
      ->assertAccess($access_result, FALSE, NULL, [], [
      'user.permissions',
    ]);
  }
}