You are here

public function AccessTest::testCheckFieldAccess in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Kernel/AccessTest.php \Drupal\Tests\file\Kernel\AccessTest::testCheckFieldAccess()

Tests file entity field access.

See also

\Drupal\file\FileAccessControlHandler::checkFieldAccess()

File

core/modules/file/tests/src/Kernel/AccessTest.php, line 92

Class

AccessTest
Tests for the File access control.

Namespace

Drupal\Tests\file\Kernel

Code

public function testCheckFieldAccess() {
  \Drupal::currentUser()
    ->setAccount($this->user1);

  /** @var \Drupal\file\FileInterface $file */
  $file = File::create([
    'uri' => 'public://test.png',
  ]);

  // While creating a file entity access will be allowed for create-only
  // fields.
  $this
    ->assertTrue($file
    ->get('uri')
    ->access('edit'));
  $this
    ->assertTrue($file
    ->get('filemime')
    ->access('edit'));
  $this
    ->assertTrue($file
    ->get('filesize')
    ->access('edit'));

  // Access to the status field is denied whilst creating a file entity.
  $this
    ->assertFalse($file
    ->get('status')
    ->access('edit'));
  $file
    ->save();

  // After saving the entity is no longer new and, therefore, access to
  // create-only fields and the status field will be denied.
  $this
    ->assertFalse($file
    ->get('uri')
    ->access('edit'));
  $this
    ->assertFalse($file
    ->get('filemime')
    ->access('edit'));
  $this
    ->assertFalse($file
    ->get('filesize')
    ->access('edit'));
  $this
    ->assertFalse($file
    ->get('status')
    ->access('edit'));
}