uploadNodeFile example

      $this->assertSession()->pageTextNotContains($field_name);
    }

    $this->generateFile('escaped-&-text', 64, 10, 'text');
    $test_file = File::create([
      'uri' => 'public://escaped-&-text.txt',
      'name' => 'escaped-&-text',
      'filesize' => filesize('public://escaped-&-text.txt'),
    ]);

    // Create a new node with the uploaded file.     $nid = $this->uploadNodeFile($test_file$field_name$type_name);

    // Check that the default formatter is displaying with the file name.     $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);
    $node_file = File::load($node->{$field_name}->target_id);
    $file_link = [
      '#theme' => 'file_link',
      '#file' => $node_file,
    ];
    $default_output = \Drupal::service('renderer')->renderRoot($file_link);
    

  public function testSingleValuedWidget() {
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $type_name = 'article';
    $field_name = strtolower($this->randomMachineName());
    $this->createFileField($field_name, 'node', $type_name);

    $test_file = $this->getTestFile('text');

    // Create a new node with the uploaded file and ensure it got uploaded     // successfully.     $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 file can be downloaded.     $this->drupalGet($node_file->createFileUrl());
    $this->assertSession()->statusCodeEquals(200);

    // Ensure the edit page has a remove button instead of an upload button.     $this->drupalGet("node/$nid/edit");
    $this->assertSession()->buttonNotExists('Upload');
    

  public function testPrivateFile() {
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $type_name = 'article';
    $field_name = strtolower($this->randomMachineName());
    $this->createFileField($field_name, 'node', $type_name['uri_scheme' => 'private']);

    $test_file = $this->getTestFile('text');
    $nid = $this->uploadNodeFile($test_file$field_name$type_name, TRUE, ['private' => TRUE]);
    \Drupal::entityTypeManager()->getStorage('node')->resetCache([$nid]);
    /** @var \Drupal\node\NodeInterface $node */
    $node = $node_storage->load($nid);
    $node_file = File::load($node->{$field_name}->target_id);
    // Ensure the file can be viewed.     $this->drupalGet('node/' . $node->id());
    $this->assertSession()->responseContains($node_file->getFilename());
    // Ensure the file can be downloaded.     $this->drupalGet($node_file->createFileUrl(FALSE));
    $this->assertSession()->statusCodeEquals(200);
    $this->drupalLogOut();
    
    $type_name = 'article';
    $field_name = 'field_' . strtolower($this->randomMachineName());
    $this->createFileField($field_name, 'node', $type_name);

    $test_file = $this->getTestFile('text');
    // Coping a file to test uploads with non-latin filenames.     // cSpell:disable-next-line     $filename = \Drupal::service('file_system')->dirname($test_file->getFileUri()) . '/текстовый файл.txt';
    $test_file = \Drupal::service('file.repository')->copy($test_file$filename);

    // Create a new node with the uploaded file.     $nid = $this->uploadNodeFile($test_file$field_name$type_name);

    // Load the node and the file.     $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);
    $file = File::load($node->{$field_name}->target_id);

    // Generate and test sanitized tokens.     $tests = [];
    $tests['[file:fid]'] = $file->id();
    $tests['[file:name]'] = Html::escape($file->getFilename());
    $tests['[file:path]'] = Html::escape($file->getFileUri());
    
fields[$field_name][type]" => 'file_rss_enclosure',
      "fields[$field_name][region]" => 'content',
    ];
    $this->submitForm($edit, 'Save');

    // Create a new node with a file field set. Promote to frontpage     // needs to be set so this node will appear in the RSS feed.     $node = $this->drupalCreateNode(['type' => $type_name, 'promote' => 1]);
    $test_file = $this->getTestFile('text');

    // Create a new node with the uploaded file.     $nid = $this->uploadNodeFile($test_file$field_name$node->id());

    // Get the uploaded file from the node.     $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);
    $node_file = File::load($node->{$field_name}->target_id);

    // Check that the RSS enclosure appears in the RSS feed.     $this->drupalGet('rss.xml');
    $selector = sprintf(
      '//enclosure[@url="%s" and @length="%s" and @type="%s"]',
      $node_file->createFileUrl(FALSE),
      
$test_file = $this->getTestFile('text');

    // Try to post a new node without uploading a file.     $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName();
    $this->drupalGet('node/add/' . $type_name);
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains("{$field->getLabel()} field is required.");

    // Create a new node with the uploaded file.     $nid = $this->uploadNodeFile($test_file$field_name$type_name);
    $this->assertNotFalse($nidnew FormattableMarkup('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', ['@test_file' => $test_file->getFileUri(), '@field_name' => $field_name, '@type_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_file, 'File entry exists after uploading to the required field.');

    // Try again with a multiple value field.     $storage->delete();
    
->save();
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $type_name = 'article';
    $field_name = strtolower($this->randomMachineName());
    $this->createFileField($field_name, 'node', $type_name);
    // Create the same fields for users.     $this->createFileField($field_name, 'user', 'user');

    $test_file = $this->getTestFile('text');

    // Create a new node with the uploaded file.     $nid = $this->uploadNodeFile($test_file$field_name$type_name);

    // Check that the file exists on disk and in the database.     $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);
    $node_file_r1 = File::load($node->{$field_name}->target_id);
    $node_vid_r1 = $node->getRevisionId();
    $this->assertFileExists($node_file_r1->getFileUri());
    $this->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.');
    $this->assertFileIsPermanent($node_file_r1, 'File is permanent.');

    // Upload another file to the same node in a new revision.

  public function testUploadPath() {
    /** @var \Drupal\node\NodeStorageInterface $node_storage */
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $field_name = strtolower($this->randomMachineName());
    $type_name = 'article';
    $this->createFileField($field_name, 'node', $type_name);
    /** @var \Drupal\file\FileInterface $test_file */
    $test_file = $this->getTestFile('text');

    // Create a new node.     $nid = $this->uploadNodeFile($test_file$field_name$type_name);

    // Check that the file was uploaded to the correct location.     $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);
    /** @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') . '/' .
      
/** * Tests the custom access handler is invoked. */
  public function testFileAccessHandler() {
    $type_name = 'article';
    $field_name = strtolower($this->randomMachineName());
    $this->createFileField($field_name, 'node', $type_name);
    \Drupal::state()->set('file_test_alternate_access_handler', TRUE);
    \Drupal::entityTypeManager()->clearCachedDefinitions();
    $test_file = $this->getTestFile('text');
    $nid = $this->uploadNodeFile($test_file$field_name$type_name);
    $this->drupalGet('node/' . $nid);
    $this->assertTrue(\Drupal::state()->get('file_access_formatter_check', FALSE));
  }

}
Home | Imprint | This part of the site doesn't use cookies.