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