class FieldPermissionsServiceTest in Field Permissions 8
Same name and namespace in other branches
- 8.2 tests/src/Unit/FieldPermissionsServiceTest.php \Drupal\Tests\field_permissions\Unit\FieldPermissionsServiceTest
Tests the field permissions service.
@group field_permissions
@coversDefaultClass \Drupal\field_permissions\FieldPermissionsService
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\field_permissions\Unit\FieldPermissionsServiceTest
Expanded class hierarchy of FieldPermissionsServiceTest
File
- tests/
src/ Unit/ FieldPermissionsServiceTest.php, line 25
Namespace
Drupal\Tests\field_permissions\UnitView source
class FieldPermissionsServiceTest extends UnitTestCase {
/**
* Mock entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Mock permission type manager.
*
* @var \Drupal\field_permissions\Plugin\FieldPermissionType\Manager
*/
protected $permissionTypeManager;
/**
* The field permissions service.
*
* @var \Drupal\field_permissions\FieldPermissionsServiceInterface
*/
protected $fieldPermissionsService;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$entity_type_manager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->entityTypeManager = $entity_type_manager
->reveal();
$permission_type_manager = $this
->prophesize(Manager::class);
$this->permissionTypeManager = $permission_type_manager
->reveal();
$this->fieldPermissionsService = new FieldPermissionsService($this->entityTypeManager, $this->permissionTypeManager);
}
/**
* Test field access method.
*
* @covers ::getFieldAccess
*
* @dataProvider providerTestGetFieldAccess
*/
public function testGetFieldAccess($operation, FieldItemListInterface $items, AccountInterface $account, FieldDefinitionInterface $field_definition, $expected_access) {
$this
->assertEquals($expected_access, $this->fieldPermissionsService
->getFieldAccess($operation, $items, $account, $field_definition));
}
/**
* Data provider for ::testGetFieldAccess.
*/
public function providerTestGetFieldAccess() {
$cases = [];
$field_item_list = $this
->prophesize(FieldItemListInterface::class)
->reveal();
// Administrator role.
$account = $this
->prophesize(AccountInterface::class);
$account
->getRoles()
->willReturn([
'administrator',
]);
$field_definition = $this
->prophesize(FieldDefinitionInterface::class);
$storage = $this
->prophesize(FieldStorageConfigInterface::class);
$storage
->getThirdPartySetting('field_permissions', 'permission_type', FieldPermissionTypeInterface::ACCESS_PUBLIC)
->willReturn('foo');
$field_definition
->getFieldStorageDefinition()
->willReturn($storage
->reveal());
$cases[] = [
'view',
$field_item_list,
$account
->reveal(),
$field_definition
->reveal(),
TRUE,
];
// No admin roles, but public access.
$account = $this
->prophesize(AccountInterface::class);
$account
->getRoles()
->willReturn([
'blah',
]);
$field_definition = $this
->prophesize(FieldDefinitionInterface::class);
$storage = $this
->prophesize(FieldStorageConfigInterface::class);
$storage
->getThirdPartySetting('field_permissions', 'permission_type', FieldPermissionTypeInterface::ACCESS_PUBLIC)
->willReturn(FieldPermissionTypeInterface::ACCESS_PUBLIC);
$field_definition
->getFieldStorageDefinition()
->willReturn($storage
->reveal());
$cases[] = [
'view',
$field_item_list,
$account
->reveal(),
$field_definition
->reveal(),
TRUE,
];
return $cases;
}
/**
* Test the comment field method.
*
* @covers ::isCommentField
*/
public function testIsCommentField() {
$field_definition = $this
->prophesize(FieldDefinitionInterface::class);
// Comment module not enabled.
$container = new Container();
\Drupal::setContainer($container);
$this
->assertFalse($this->fieldPermissionsService
->isCommentField($field_definition
->reveal()));
// Comment module enabled, no comment fields.
$comment_manager = $this
->prophesize(CommentManagerInterface::class);
$comment_manager
->getFields(Argument::any())
->willReturn([]);
$container
->set('comment.manager', $comment_manager
->reveal());
$this
->assertFalse($this->fieldPermissionsService
->isCommentField($field_definition
->reveal()));
// Comment module enabled, no matching fields.
$field_definition
->getName()
->willReturn('foo_field');
$field_definition
->getTargetEntityTypeId()
->willReturn('foo');
$comment_manager
->getFields('foo')
->willReturn([
'bar_field' => 'bar',
]);
$container
->set('comment.manager', $comment_manager
->reveal());
$this
->assertFalse($this->fieldPermissionsService
->isCommentField($field_definition
->reveal()));
// A comment field!
$comment_manager
->getFields('foo')
->willReturn([
'bar_field' => 'bar',
'foo_field' => 'foo',
]);
$container
->set('comment.manager', $comment_manager
->reveal());
$this
->assertTrue($this->fieldPermissionsService
->isCommentField($field_definition
->reveal()));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FieldPermissionsServiceTest:: |
protected | property | Mock entity type manager. | |
FieldPermissionsServiceTest:: |
protected | property | The field permissions service. | |
FieldPermissionsServiceTest:: |
protected | property | Mock permission type manager. | |
FieldPermissionsServiceTest:: |
public | function | Data provider for ::testGetFieldAccess. | |
FieldPermissionsServiceTest:: |
public | function |
Overrides UnitTestCase:: |
|
FieldPermissionsServiceTest:: |
public | function | Test field access method. | |
FieldPermissionsServiceTest:: |
public | function | Test the comment field method. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |