function MediaAccessControlHandlerTest::testAccess

Same name in other branches
  1. 9 core/modules/media/tests/src/Kernel/MediaAccessControlHandlerTest.php \Drupal\Tests\media\Kernel\MediaAccessControlHandlerTest::testAccess()
  2. 8.9.x core/modules/media/tests/src/Kernel/MediaAccessControlHandlerTest.php \Drupal\Tests\media\Kernel\MediaAccessControlHandlerTest::testAccess()
  3. 11.x core/modules/media/tests/src/Kernel/MediaAccessControlHandlerTest.php \Drupal\Tests\media\Kernel\MediaAccessControlHandlerTest::testAccess()

Tests the media access control handler.

@covers ::checkAccess @dataProvider providerAccess

Parameters

string[] $permissions: The permissions that the user should be given.

array $entity_values: Initial values from which to create the media entity.

string $operation: The operation, one of 'view', 'update' or 'delete'.

\Drupal\Core\Access\AccessResultInterface $expected_result: Expected result.

string[] $expected_cache_contexts: Expected cache contexts.

string[] $expected_cache_tags: Expected cache tags.

bool $is_latest_revision: If FALSE, the media is historic revision.

File

core/modules/media/tests/src/Kernel/MediaAccessControlHandlerTest.php, line 45

Class

MediaAccessControlHandlerTest
Tests the media access control handler.

Namespace

Drupal\Tests\media\Kernel

Code

public function testAccess(array $permissions, array $entity_values, string $operation, AccessResultInterface $expected_result, array $expected_cache_contexts, array $expected_cache_tags, bool $is_latest_revision) : void {
    
    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $entityStorage $entity_storage */
    $entity_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('media');
    // Set a fixed ID so the type specific permissions match.
    $media_type = $this->createMediaType('test', [
        'id' => 'test',
    ]);
    $user = $this->createUser($permissions);
    $entity_values += [
        'status' => FALSE,
        'uid' => $user->id(),
        'bundle' => $media_type->id(),
    ];
    $entity = Media::create($entity_values);
    $entity->save();
    $load_revision_id = NULL;
    if (!$is_latest_revision) {
        $load_revision_id = $entity->getRevisionId();
        // Set up for a new revision to be saved.
        $entity = $entity_storage->createRevision($entity);
    }
    $entity->save();
    // Reload a previous revision.
    if ($load_revision_id !== NULL) {
        $entity = $entity_storage->loadRevision($load_revision_id);
    }
    
    /** @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));
}

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