View source
<?php
namespace Drupal\Tests\media\Kernel;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\media\Entity\Media;
use Drupal\Tests\user\Traits\UserCreationTrait;
class MediaAccessControlHandlerTest extends MediaKernelTestBase {
use UserCreationTrait;
public function testAccess(array $permissions, array $entity_values, $operation, AccessResultInterface $expected_result, array $expected_cache_contexts, array $expected_cache_tags) {
$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();
$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));
}
public function testCreateAccess(array $permissions, AccessResultInterface $expected_result, array $expected_cache_contexts, array $expected_cache_tags) {
$user = $this
->createUser($permissions);
$access_handler = $this->container
->get('entity_type.manager')
->getAccessControlHandler('media');
$this
->assertAccess($expected_result, $expected_cache_contexts, $expected_cache_tags, $access_handler
->createAccess('test', $user, [], TRUE));
}
protected function assertAccess(AccessResultInterface $expected_access_result, array $expected_cache_contexts, array $expected_cache_tags, AccessResultInterface $actual) : void {
$this
->assertSame($expected_access_result
->isAllowed(), $actual
->isAllowed());
$this
->assertSame($expected_access_result
->isForbidden(), $actual
->isForbidden());
$this
->assertSame($expected_access_result
->isNeutral(), $actual
->isNeutral());
$actual_cache_contexts = $actual
->getCacheContexts();
sort($expected_cache_contexts);
sort($actual_cache_contexts);
$this
->assertSame($expected_cache_contexts, $actual_cache_contexts);
$actual_cache_tags = $actual
->getCacheTags();
sort($expected_cache_tags);
sort($actual_cache_tags);
$this
->assertSame($expected_cache_tags, $actual_cache_tags);
}
public function providerAccess() {
$test_data = [];
$test_data['owner, no permissions / published / view'] = [
[],
[
'status' => TRUE,
],
'view',
AccessResult::neutral(),
[
'user.permissions',
],
[
'media:1',
],
];
$test_data['owner, no permissions / published / update'] = [
[],
[
'status' => TRUE,
],
'update',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['owner, no permissions / published / delete'] = [
[],
[
'status' => TRUE,
],
'delete',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['owner, no permissions / unpublished / view'] = [
[],
[],
'view',
AccessResult::neutral(),
[
'user.permissions',
],
[
'media:1',
],
];
$test_data['owner, no permissions / unpublished / update'] = [
[],
[],
'update',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['owner, no permissions / unpublished / delete'] = [
[],
[],
'delete',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['not owner, no permissions / published / view'] = [
[],
[
'uid' => 0,
'status' => TRUE,
],
'view',
AccessResult::neutral(),
[
'user.permissions',
],
[
'media:1',
],
];
$test_data['not owner, no permissions / published / update'] = [
[],
[
'uid' => 0,
'status' => TRUE,
],
'update',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['not owner, no permissions / published / delete'] = [
[],
[
'uid' => 0,
'status' => TRUE,
],
'delete',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['not owner, no permissions / unpublished / view'] = [
[],
[
'uid' => 0,
],
'view',
AccessResult::neutral(),
[
'user.permissions',
],
[
'media:1',
],
];
$test_data['not owner, no permissions / unpublished / update'] = [
[],
[
'uid' => 0,
],
'update',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['not owner, no permissions / unpublished / delete'] = [
[],
[
'uid' => 0,
],
'delete',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['owner, can view media / published / view'] = [
[
'view media',
],
[
'status' => TRUE,
],
'view',
AccessResult::allowed(),
[
'user.permissions',
],
[
'media:1',
],
];
$test_data['owner, can view media / published / update'] = [
[
'view media',
],
[
'status' => TRUE,
],
'update',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['owner, can view media / published / delete'] = [
[
'view media',
],
[
'status' => TRUE,
],
'delete',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['owner, can view media / unpublished / view'] = [
[
'view media',
],
[],
'view',
AccessResult::neutral(),
[
'user.permissions',
],
[
'media:1',
],
];
$test_data['owner, can view media / unpublished / update'] = [
[
'view media',
],
[],
'update',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['owner, can view media / unpublished / delete'] = [
[
'view media',
],
[],
'delete',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['not owner, can view media / published / view'] = [
[
'view media',
],
[
'uid' => 0,
'status' => TRUE,
],
'view',
AccessResult::allowed(),
[
'user.permissions',
],
[
'media:1',
],
];
$test_data['not owner, can view media / published / update'] = [
[
'view media',
],
[
'uid' => 0,
'status' => TRUE,
],
'update',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['not owner, can view media / published / delete'] = [
[
'view media',
],
[
'uid' => 0,
'status' => TRUE,
],
'delete',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['not owner, can view media / unpublished / view'] = [
[
'view media',
],
[
'uid' => 0,
],
'view',
AccessResult::neutral(),
[
'user.permissions',
],
[
'media:1',
],
];
$test_data['not owner, can view media / unpublished / update'] = [
[
'view media',
],
[
'uid' => 0,
],
'update',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['not owner, can view media / unpublished / delete'] = [
[
'view media',
],
[
'uid' => 0,
],
'delete',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['owner, can view own unpublished media / published / view'] = [
[
'view media',
'view own unpublished media',
],
[
'status' => TRUE,
],
'view',
AccessResult::allowed(),
[
'user.permissions',
],
[
'media:1',
],
];
$test_data['owner, can view own unpublished media / published / update'] = [
[
'view media',
'view own unpublished media',
],
[
'status' => TRUE,
],
'update',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['owner, can view own unpublished media / published / delete'] = [
[
'view media',
'view own unpublished media',
],
[
'status' => TRUE,
],
'delete',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['owner, can view own unpublished media / unpublished / view'] = [
[
'view media',
'view own unpublished media',
],
[],
'view',
AccessResult::allowed(),
[
'user.permissions',
'user',
],
[
'media:1',
],
];
$test_data['owner, can view own unpublished media / unpublished / update'] = [
[
'view media',
'view own unpublished media',
],
[],
'update',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['owner, can view own unpublished media / unpublished / delete'] = [
[
'view media',
'view own unpublished media',
],
[],
'delete',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['not owner, can view own unpublished media / published / view'] = [
[
'view media',
'view own unpublished media',
],
[
'uid' => 0,
'status' => TRUE,
],
'view',
AccessResult::allowed(),
[
'user.permissions',
],
[
'media:1',
],
];
$test_data['not owner, can view own unpublished media / published / update'] = [
[
'view media',
'view own unpublished media',
],
[
'uid' => 0,
'status' => TRUE,
],
'update',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['not owner, can view own unpublished media / published / delete'] = [
[
'view media',
'view own unpublished media',
],
[
'uid' => 0,
'status' => TRUE,
],
'delete',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['not owner, can view own unpublished media / unpublished / view'] = [
[
'view media',
'view own unpublished media',
],
[
'uid' => 0,
],
'view',
AccessResult::neutral(),
[
'user.permissions',
'user',
],
[
'media:1',
],
];
$test_data['not owner, can view own unpublished media / unpublished / update'] = [
[
'view media',
'view own unpublished media',
],
[
'uid' => 0,
],
'update',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
$test_data['not owner, can view own unpublished media / unpublished / delete'] = [
[
'view media',
'view own unpublished media',
],
[
'uid' => 0,
],
'delete',
AccessResult::neutral(),
[
'user.permissions',
],
[],
];
return $test_data;
}
public function providerCreateAccess() {
$test_data = [];
$test_data['user, no permissions / create'] = [
[],
AccessResult::neutral()
->setReason("The following permissions are required: 'administer media' OR 'create media'."),
[
'user.permissions',
],
[],
];
$test_data['user, can view media / create'] = [
[
'view media',
],
AccessResult::neutral("The following permissions are required: 'administer media' OR 'create media'."),
[
'user.permissions',
],
[],
];
$test_data['user, can view own unpublished media / create'] = [
[
'view media',
'view own unpublished media',
],
AccessResult::neutral("The following permissions are required: 'administer media' OR 'create media'."),
[
'user.permissions',
],
[],
];
$test_data['user, can view own unpublished media and update or delete any media / create'] = [
[
'view media',
'view own unpublished media',
'update any media',
'delete any media',
],
AccessResult::neutral("The following permissions are required: 'administer media' OR 'create media'."),
[
'user.permissions',
],
[],
];
$test_data['user, can view own unpublished media and update or delete own media / create'] = [
[
'view media',
'view own unpublished media',
'update media',
'delete media',
],
AccessResult::neutral("The following permissions are required: 'administer media' OR 'create media'."),
[
'user.permissions',
],
[],
];
$test_data['user, can view own unpublished media and update or delete all media / create'] = [
[
'view media',
'view own unpublished media',
'update any media',
'delete any media',
'update media',
'delete media',
],
AccessResult::neutral("The following permissions are required: 'administer media' OR 'create media'."),
[
'user.permissions',
],
[],
];
$test_data['user, can not create or administer media / create'] = [
[
'access media overview',
'view media',
'view own unpublished media',
'update any media',
'delete any media',
'update media',
'delete media',
],
AccessResult::neutral("The following permissions are required: 'administer media' OR 'create media'."),
[
'user.permissions',
],
[],
];
$test_data['user, can create media / create'] = [
[
'create media',
],
AccessResult::allowed(),
[
'user.permissions',
],
[],
];
$test_data['user, can administer media / create'] = [
[
'administer media',
],
AccessResult::allowed(),
[
'user.permissions',
],
[],
];
return $test_data;
}
}