You are here

public function FileFieldWidgetTest::testPrivateFileSetting in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testPrivateFileSetting()
  2. 9 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testPrivateFileSetting()

Tests a file field with a "Private files" upload destination setting.

File

core/modules/file/tests/src/Functional/FileFieldWidgetTest.php, line 247

Class

FileFieldWidgetTest
Tests the file field widget with public and private files.

Namespace

Drupal\Tests\file\Functional

Code

public function testPrivateFileSetting() {
  $node_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');

  // Grant the admin user required permissions.
  user_role_grant_permissions($this->adminUser->roles[0]->target_id, [
    'administer node fields',
  ]);
  $type_name = 'article';
  $field_name = strtolower($this
    ->randomMachineName());
  $this
    ->createFileField($field_name, 'node', $type_name);
  $field = FieldConfig::loadByName('node', $type_name, $field_name);
  $field_id = $field
    ->id();
  $test_file = $this
    ->getTestFile('text');

  // Change the field setting to make its files private, and upload a file.
  $edit = [
    'settings[uri_scheme]' => 'private',
  ];
  $this
    ->drupalGet("admin/structure/types/manage/{$type_name}/fields/{$field_id}/storage");
  $this
    ->submitForm($edit, 'Save field settings');
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $type_name);
  $node = $node_storage
    ->loadUnchanged($nid);
  $node_file = File::load($node->{$field_name}->target_id);
  $this
    ->assertFileExists($node_file
    ->getFileUri());

  // Ensure the private file is available to the user who uploaded it.
  $this
    ->drupalGet($node_file
    ->createFileUrl());
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Ensure we can't change 'uri_scheme' field settings while there are some
  // entities with uploaded files.
  $this
    ->drupalGet("admin/structure/types/manage/{$type_name}/fields/{$field_id}/storage");
  $this
    ->assertSession()
    ->fieldDisabled("edit-settings-uri-scheme-public");

  // Delete node and confirm that setting could be changed.
  $node
    ->delete();
  $this
    ->drupalGet("admin/structure/types/manage/{$type_name}/fields/{$field_id}/storage");
  $this
    ->assertSession()
    ->fieldEnabled("edit-settings-uri-scheme-public");
}