function AccessTest::testCheckFieldAccess

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Kernel/AccessTest.php \Drupal\Tests\file\Kernel\AccessTest::testCheckFieldAccess()
  2. 8.9.x core/modules/file/tests/src/Kernel/AccessTest.php \Drupal\Tests\file\Kernel\AccessTest::testCheckFieldAccess()
  3. 10 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 123

Class

AccessTest
Tests for the File access control.

Namespace

Drupal\Tests\file\Kernel

Code

public function testCheckFieldAccess() : void {
    $this->setUpCurrentUser();
    
    /** @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.