You are here

function FileFieldWidgetTest::testPrivateFileSetting in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Tests/FileFieldWidgetTest.php \Drupal\file\Tests\FileFieldWidgetTest::testPrivateFileSetting()

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

File

core/modules/file/src/Tests/FileFieldWidgetTest.php, line 269
Contains \Drupal\file\Tests\FileFieldWidgetTest.

Class

FileFieldWidgetTest
Tests the file field widget, single and multi-valued, with and without AJAX, with public and private files.

Namespace

Drupal\file\Tests

Code

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

  // Grant the admin user required permissions.
  user_role_grant_permissions($this->adminUser->roles[0]->target_id, array(
    '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 = array(
    'settings[uri_scheme]' => 'private',
  );
  $this
    ->drupalPostForm("admin/structure/types/manage/{$type_name}/fields/{$field_id}/storage", $edit, t('Save field settings'));
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $type_name);
  $node_storage
    ->resetCache(array(
    $nid,
  ));
  $node = $node_storage
    ->load($nid);
  $node_file = File::load($node->{$field_name}->target_id);
  $this
    ->assertFileExists($node_file, '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
    ->getFileUri()));
  $this
    ->assertResponse(200, '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/{$field_id}/storage");
  $this
    ->assertFieldByXpath('//input[@id="edit-settings-uri-scheme-public" and @disabled="disabled"]', 'public', 'Upload destination setting disabled.');

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