getTestFile example

protected $defaultTheme = 'stark';

  /** * 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));
  }

}


  /** * Tests upload and remove buttons for a single-valued File field. */
  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);

    
$type = 'Article';
    $title = 'Test page';
    $this->createFileField('field_image', 'node', 'article', []['file_extensions' => 'txt png']);

    // Load the node form.     $this->drupalLogout();
    $this->drupalGet('node/add/article');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains("Create $type");

    // Generate an image file.     $image = $this->getTestFile('image');

    // Submit the form.     $edit = [
      'title[0][value]' => $title,
      'body[0][value]' => 'Test article',
      'files[field_image_0]' => $this->container->get('file_system')->realpath($image->getFileUri()),
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains("$type $title has been created.");
    $matches = [];
    
/** * Tests the normal formatter display on node display. */
  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 =
      
$this->assertSession()->statusCodeEquals(200);

    // Create a file with no usage.     $file = $this->createFile();

    $this->drupalGet('admin/content/files/usage/' . $file->id());
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->titleEquals('File usage information for ' . $file->getFilename() . ' | Drupal');

    foreach ($nodes as &$node) {
      $this->drupalGet('node/' . $node->id() . '/edit');
      $file = $this->getTestFile('image');

      $edit = [
        'files[file_0]' => \Drupal::service('file_system')->realpath($file->getFileUri()),
      ];
      $this->submitForm($edit, 'Save');
      $node = Node::load($node->id());
    }

    $this->drupalGet('admin/content/files');

    foreach ($nodes as $node) {
      
    // and then deleted up by file_cron().     $this->config('file.settings')
      ->set('make_unused_managed_files_temporary', TRUE)
      ->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.');
    
// Change the format to 'RSS enclosure'.     $this->drupalGet("admin/structure/types/manage/$type_name/display/rss");
    $edit = [
      "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');
    
/** * Tests file access for file uploaded to a private node. */
  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);
    
/** * Tests the required property on file fields. */
  public function testRequired() {
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $type_name = 'article';
    $field_name = strtolower($this->randomMachineName());
    $storage = $this->createFileField($field_name, 'node', $type_name[]['required' => '1']);
    $field = FieldConfig::loadByName('node', $type_name$field_name);

    $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]));

    
$field_storage_settings = [
      'display_field' => '1',
      'display_default' => '0',
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ];
    $field_settings = [
      'description_field' => '1',
    ];
    $widget_settings = [];
    $this->createFileField($field_name, 'node', $type_name$field_storage_settings$field_settings$widget_settings);

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

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

    $this->drupalGet('node/' . $nid . '/edit');
    $this->assertSession()->fieldExists($field_name . '[0][display]');
    $this->assertSession()->checkboxNotChecked($field_name . '[0][display]');
  }

  /** * Tests description toggle for field instance configuration. */
/** * Tests the managed_file element type. */
  public function testManagedFile() {
    // Check that $element['#size'] is passed to the child upload element.     $this->drupalGet('file/test');
    $field = $this->assertSession()->fieldExists("files[nested_file]");
    $this->assertEquals(13, $field->getAttribute('size'));

    // Perform the tests with all permutations of $form['#tree'],     // $element['#extended'], and $element['#multiple'].     $test_file = $this->getTestFile('text');
    foreach ([0, 1] as $tree) {
      foreach ([0, 1] as $extended) {
        foreach ([0, 1] as $multiple) {
          $path = 'file/test/' . $tree . '/' . $extended . '/' . $multiple;
          $input_base_name = $tree ? 'nested_file' : 'file';
          $file_field_name = $multiple ? 'files[' . $input_base_name . '][]' : 'files[' . $input_base_name . ']';

          // Submit without a file.           $this->drupalGet($path);
          $this->submitForm([], 'Save');
          $this->assertSession()->pageTextContains("The file ids are .");

          
$node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $token_service = \Drupal::token();
    $language_interface = \Drupal::languageManager()->getCurrentLanguage();
    /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
    $date_formatter = $this->container->get('date.formatter');

    // Create file field.     $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);
    
Home | Imprint | This part of the site doesn't use cookies.