function MediaLibraryAccessTest::testFieldWidgetEntityCreateAccess

Same name in other branches
  1. 9 core/modules/media_library/tests/src/Kernel/MediaLibraryAccessTest.php \Drupal\Tests\media_library\Kernel\MediaLibraryAccessTest::testFieldWidgetEntityCreateAccess()
  2. 8.9.x core/modules/media_library/tests/src/Kernel/MediaLibraryAccessTest.php \Drupal\Tests\media_library\Kernel\MediaLibraryAccessTest::testFieldWidgetEntityCreateAccess()
  3. 11.x 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 91

Class

MediaLibraryAccessTest
Tests the media library access.

Namespace

Drupal\Tests\media_library\Kernel

Code

public function testFieldWidgetEntityCreateAccess() : void {
    
    /** @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_with_bundle',
        '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',
    ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.