function FileFieldWidgetTest::testPrivateFileSetting

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testPrivateFileSetting()
  2. 8.9.x core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testPrivateFileSetting()
  3. 11.x 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 249

Class

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

Namespace

Drupal\Tests\file\Functional

Code

public function testPrivateFileSetting() : void {
  $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 = $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 = [
    'field_storage[subform][settings][uri_scheme]' => 'private',
  ];
  $this->drupalGet("admin/structure/types/manage/{$type_name}/fields/{$field_id}");
  $this->submitForm($edit, 'Save');
  $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}");
  $this->assertSession()
    ->fieldDisabled("edit-field-storage-subform-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}");
  $this->assertSession()
    ->fieldEnabled("edit-field-storage-subform-settings-uri-scheme-public");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.