uploadNodeFiles example


  public function uploadNodeFile(FileInterface $file$field_name$nid_or_type$new_revision = TRUE, array $extras = []) {
    return $this->uploadNodeFiles([$file]$field_name$nid_or_type$new_revision$extras);
  }

  /** * Uploads multiple files to a node. * * @param \Drupal\file\FileInterface[] $files * The files to be uploaded. * @param string $field_name * The name of the field on which the files should be saved. * @param $nid_or_type * A numeric node id to upload files to an existing node, or a string * indicating the desired bundle for a new node. * @param bool $new_revision * The revision number. * @param array $extras * Additional values when a new node is created. * * @return int * The node id. */
$this->assertSession()->fieldExists('files[test_file_field_1_0][]');
    $submit_xpath = $this->assertSession()->buttonExists('Save')->getXpath();
    $client = $this->getSession()->getDriver()->getClient();
    $form = $client->getCrawler()->filterXPath($submit_xpath)->form();
    $client->request($form->getMethod()$form->getUri()$form->getPhpValues()$edit);

    $node = $node_storage->loadUnchanged($nid);
    $this->assertCount($cardinality$node->{$field_name}, 'More files than allowed could not be saved to node.');

    $upload_files_node_creation = [$test_file$test_file];
    // Try to upload multiple files, but fewer than the maximum.     $nid = $this->uploadNodeFiles($upload_files_node_creation$field_name$type_name, TRUE, []);
    $node = $node_storage->loadUnchanged($nid);
    $this->assertSameSize($upload_files_node_creation$node->{$field_name}, 'Node was successfully saved with multiple files.');

    // Try to upload exactly the allowed number of files on revision.     $this->uploadNodeFile($test_file$field_name$node->id(), 1);
    $node = $node_storage->loadUnchanged($nid);
    $this->assertCount($cardinality$node->{$field_name}, 'Node was successfully revised to maximum number of files.');

    // Try to upload exactly the allowed number of files, new node.     $upload_files = [$test_file$test_file$test_file];
    $nid = $this->uploadNodeFiles($upload_files$field_name$type_name, TRUE, []);
    
Home | Imprint | This part of the site doesn't use cookies.