function FileFieldDisplayTest::testNodeDisplay

Same name and namespace in other branches
  1. 8.9.x core/modules/file/tests/src/Functional/FileFieldDisplayTest.php \Drupal\Tests\file\Functional\FileFieldDisplayTest::testNodeDisplay()
  2. 10 core/modules/file/tests/src/Functional/FileFieldDisplayTest.php \Drupal\Tests\file\Functional\FileFieldDisplayTest::testNodeDisplay()
  3. 11.x core/modules/file/tests/src/Functional/FileFieldDisplayTest.php \Drupal\Tests\file\Functional\FileFieldDisplayTest::testNodeDisplay()

Tests normal formatter display on node display.

File

core/modules/file/tests/src/Functional/FileFieldDisplayTest.php, line 24

Class

FileFieldDisplayTest
Tests the display of file fields in node and views.

Namespace

Drupal\Tests\file\Functional

Code

public function testNodeDisplay() {
    $field_name = strtolower($this->randomMachineName());
    $type_name = 'article';
    $field_storage_settings = [
        'display_field' => '1',
        'display_default' => '1',
        '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);
    // Create a new node *without* the file field set, and check that the field
    // is not shown for each node display.
    $node = $this->drupalCreateNode([
        'type' => $type_name,
    ]);
    // Check file_default last as the assertions below assume that this is the
    // case.
    $file_formatters = [
        'file_table',
        'file_url_plain',
        'hidden',
        'file_default',
    ];
    foreach ($file_formatters as $formatter) {
        if ($formatter === 'hidden') {
            $edit = [
                "fields[{$field_name}][region]" => 'hidden',
            ];
        }
        else {
            $edit = [
                "fields[{$field_name}][type]" => $formatter,
                "fields[{$field_name}][region]" => 'content',
            ];
        }
        $this->drupalGet("admin/structure/types/manage/{$type_name}/display");
        $this->submitForm($edit, 'Save');
        $this->drupalGet('node/' . $node->id());
        // Verify that the field label is hidden when no file is attached.
        $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);
    $this->assertSession()
        ->responseContains($default_output);
    // Turn the "display" option off and check that the file is no longer displayed.
    $edit = [
        $field_name . '[0][display]' => FALSE,
    ];
    $this->drupalGet('node/' . $nid . '/edit');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->responseNotContains($default_output);
    // Add a description and make sure that it is displayed.
    $description = $this->randomMachineName();
    $edit = [
        $field_name . '[0][description]' => $description,
        $field_name . '[0][display]' => TRUE,
    ];
    $this->drupalGet('node/' . $nid . '/edit');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains($description);
    // Ensure the filename in the link's title attribute is escaped.
    $this->assertSession()
        ->responseContains('title="escaped-&-text.txt"');
    // Test that fields appear as expected after during the preview.
    // Add a second file.
    $name = 'files[' . $field_name . '_1][]';
    $edit_upload[$name] = \Drupal::service('file_system')->realpath($test_file->getFileUri());
    $this->drupalGet("node/{$nid}/edit");
    $this->submitForm($edit_upload, 'Upload');
    // Uncheck the display checkboxes and go to the preview.
    $edit[$field_name . '[0][display]'] = FALSE;
    $edit[$field_name . '[1][display]'] = FALSE;
    $this->submitForm($edit, 'Preview');
    $this->clickLink('Back to content editing');
    // First file.
    $this->assertSession()
        ->responseContains($field_name . '[0][display]');
    // Second file.
    $this->assertSession()
        ->responseContains($field_name . '[1][display]');
    $this->assertSession()
        ->responseContains($field_name . '[1][description]');
    // Check that the file fields don't contain duplicate HTML IDs.
    $this->assertSession()
        ->pageContainsNoDuplicateId();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.