function FileFieldWidgetTest::testPrivateFileSetting

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

File

drupal/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php, line 206
Definition of Drupal\file\Tests\FileFieldWidgetTest.

Class

FileFieldWidgetTest
Tests file field widget.

Namespace

Drupal\file\Tests

Code

function testPrivateFileSetting() {
  $type_name = 'article';
  $field_name = strtolower($this
    ->randomName());
  $this
    ->createFileField($field_name, $type_name);
  $instance = field_info_instance('node', $field_name, $type_name);
  $test_file = $this
    ->getTestFile('text');

  // Change the field setting to make its files private, and upload a file.
  $edit = array(
    'field[settings][uri_scheme]' => 'private',
  );
  $this
    ->drupalPost("admin/structure/types/manage/{$type_name}/fields/{$instance->id}/field", $edit, t('Save field settings'));
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $type_name);
  $node = node_load($nid, TRUE);
  $node_file = file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid']);
  $this
    ->assertFileExists($node_file, t('New file saved to disk on node creation.'));

  // Ensure the private file is available to the user who uploaded it.
  $this
    ->drupalGet(file_create_url($node_file->uri));
  $this
    ->assertResponse(200, t('Confirmed that the generated URL is correct by downloading the shipped file.'));

  // 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/{$instance->id}/field");
  $this
    ->assertFieldByXpath('//input[@id="edit-field-settings-uri-scheme-public" and @disabled="disabled"]', 'public', t('Upload destination setting disabled.'));

  // Delete node and confirm that setting could be changed.
  $node
    ->delete();
  $this
    ->drupalGet("admin/structure/types/manage/{$type_name}/fields/{$instance->id}/field");
  $this
    ->assertFieldByXpath('//input[@id="edit-field-settings-uri-scheme-public" and not(@disabled)]', 'public', t('Upload destination setting enabled.'));
}