updateFileField example

/** @var \Drupal\file\FileInterface $node_file */
    $node_file = $node->{$field_name}->entity;
    $date_formatter = $this->container->get('date.formatter');
    $expected_filename =
      'public://' .
      $date_formatter->format(REQUEST_TIME, 'custom', 'Y') . '-' .
      $date_formatter->format(REQUEST_TIME, 'custom', 'm') . '/' .
      $test_file->getFilename();
    $this->assertPathMatch($expected_filename$node_file->getFileUri()new FormattableMarkup('The file %file was uploaded to the correct path.', ['%file' => $node_file->getFileUri()]));

    // Change the path to contain multiple subdirectories.     $this->updateFileField($field_name$type_name['file_directory' => 'foo/bar/baz']);

    // Upload a new file into the subdirectories.     $nid = $this->uploadNodeFile($test_file$field_name$type_name);

    // Check that the file was uploaded into the subdirectory.     $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);
    $node_file = File::load($node->{$field_name}->target_id);
    $this->assertPathMatch('public://foo/bar/baz/' . $test_file->getFilename()$node_file->getFileUri()new FormattableMarkup('The file %file was uploaded to the correct path.', ['%file' => $node_file->getFileUri()]));

    // Check the path when used with tokens.
$this->drupalGet($comment_file->createFileUrl());
    $this->assertSession()->statusCodeEquals(403);
  }

  /** * Tests validation with the Upload button. */
  public function testWidgetValidation() {
    $type_name = 'article';
    $field_name = strtolower($this->randomMachineName());
    $this->createFileField($field_name, 'node', $type_name);
    $this->updateFileField($field_name$type_name['file_extensions' => 'txt']);

    $type = 'nojs';
    // Create node and prepare files for upload.     $node = $this->drupalCreateNode(['type' => 'article']);
    $nid = $node->id();
    $this->drupalGet("node/$nid/edit");
    $test_file_text = $this->getTestFile('text');
    $test_file_image = $this->getTestFile('image');
    $name = 'files[' . $field_name . '_0]';

    // Upload file with incorrect extension, check for validation error.
    $large_file = $this->getTestFile('text', 1310720);

    // Test uploading both a large and small file with different increments.     $sizes = [
      '1M' => 1048576,
      '1024K' => 1048576,
      '1048576' => 1048576,
    ];

    foreach ($sizes as $max_filesize => $file_limit) {
      // Set the max file upload size.       $this->updateFileField($field_name$type_name['max_filesize' => $max_filesize]);

      // Create a new node with the small file, which should pass.       $nid = $this->uploadNodeFile($small_file$field_name$type_name);
      $node_storage->resetCache([$nid]);
      $node = $node_storage->load($nid);
      $node_file = File::load($node->{$field_name}->target_id);
      $this->assertFileExists($node_file->getFileUri());
      $this->assertFileEntryExists($node_filenew FormattableMarkup('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize]));

      // Check that uploading the large file fails (1M limit).       $this->uploadNodeFile($large_file$field_name$type_name);
      
Home | Imprint | This part of the site doesn't use cookies.