class FieldAccessCheckTest in FileField Sources 8
@coversDefaultClass \Drupal\filefield_sources\Access\FieldAccessCheck @group Access
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\filefield_sources\Unit\FieldAccessCheckTest
Expanded class hierarchy of FieldAccessCheckTest
File
- tests/
src/ Unit/ FieldAccessCheckTest.php, line 18
Namespace
Drupal\Tests\filefield_sources\UnitView source
class FieldAccessCheckTest extends UnitTestCase {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
$field_definition = $this
->createMock(FieldDefinitionInterface::class);
$field_storage = $this
->createMock(EntityStorageInterface::class);
$field_storage
->expects($this
->any())
->method('load')
->willReturn($field_definition);
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$this->entityTypeManager
->expects($this
->any())
->method('getStorage')
->willReturn($field_storage);
$container = new Container();
\Drupal::setContainer($container);
}
/**
* Provides test data for testAccess().
*
* Only will return true only if the field is editable.
*
* @see \Drupal\Tests\filefield_sources\Unit\FieldAccessCheckTest::testAccess()
*/
public function providerTestAccess() {
$data = [];
$data[] = [
TRUE,
AccessResult::allowed(),
];
$data[] = [
FALSE,
AccessResult::forbidden(),
];
return $data;
}
/**
* Tests the FileFieldAccessCheck.
*
* This check is pretty straightforward if the user is not allowed to edit
* the field value then is not allowed then the access check is not allowed
* either.
*
* This is used for instance in the reference FilefieldSource if the user
* cannot use the field then the route for the autocomplete is now allowed
* either.
*
* @param bool $field_storage_is_accessible
* Whether the user has access to the field storage entity.
* @param \Drupal\Core\Access\AccessResult $expected_result
* The expected result of the access call.
*
* @dataProvider providerTestAccess
*/
public function testAccess(bool $field_storage_is_accessible, AccessResult $expected_result) {
$access_result = AccessResult::forbidden();
if ($field_storage_is_accessible) {
$access_result = AccessResult::allowed();
}
$access_control = $this
->createMock(EntityAccessControlHandlerInterface::class);
$access_control
->expects($this
->any())
->method('fieldAccess')
->willReturn($access_result);
$this->entityTypeManager
->expects($this
->any())
->method('getAccessControlHandler')
->willReturn($access_control);
$editAccessCheck = new FieldAccessCheck($this->entityTypeManager);
$account = $this
->createMock('Drupal\\Core\\Session\\AccountInterface');
$access = $editAccessCheck
->access('edit', 'bundle', 'field', $account);
$this
->assertEquals($expected_result, $access);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FieldAccessCheckTest:: |
protected | property | The entity type manager service. | |
FieldAccessCheckTest:: |
public | function | Provides test data for testAccess(). | |
FieldAccessCheckTest:: |
protected | function |
Overrides UnitTestCase:: |
|
FieldAccessCheckTest:: |
public | function | Tests the FileFieldAccessCheck. | |
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. |