You are here

class FieldAccessCheckTest in FileField Sources 8

@coversDefaultClass \Drupal\filefield_sources\Access\FieldAccessCheck @group Access

Hierarchy

Expanded class hierarchy of FieldAccessCheckTest

File

tests/src/Unit/FieldAccessCheckTest.php, line 18

Namespace

Drupal\Tests\filefield_sources\Unit
View 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

Namesort descending Modifiers Type Description Overrides
FieldAccessCheckTest::$entityTypeManager protected property The entity type manager service.
FieldAccessCheckTest::providerTestAccess public function Provides test data for testAccess().
FieldAccessCheckTest::setUp protected function Overrides UnitTestCase::setUp
FieldAccessCheckTest::testAccess public function Tests the FileFieldAccessCheck.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.