function 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\KernelCode
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'));
}Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.
