function FileManagedFileElementTest::testManagedFile

Same name in this branch
  1. 10 core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php \Drupal\Tests\file\FunctionalJavascript\FileManagedFileElementTest::testManagedFile()
Same name in other branches
  1. 9 core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php \Drupal\Tests\file\FunctionalJavascript\FileManagedFileElementTest::testManagedFile()
  2. 9 core/modules/file/tests/src/Functional/FileManagedFileElementTest.php \Drupal\Tests\file\Functional\FileManagedFileElementTest::testManagedFile()
  3. 8.9.x core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php \Drupal\Tests\file\FunctionalJavascript\FileManagedFileElementTest::testManagedFile()
  4. 8.9.x core/modules/file/tests/src/Functional/FileManagedFileElementTest.php \Drupal\Tests\file\Functional\FileManagedFileElementTest::testManagedFile()
  5. 11.x core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php \Drupal\Tests\file\FunctionalJavascript\FileManagedFileElementTest::testManagedFile()
  6. 11.x core/modules/file/tests/src/Functional/FileManagedFileElementTest.php \Drupal\Tests\file\Functional\FileManagedFileElementTest::testManagedFile()

Tests the managed_file element type.

File

core/modules/file/tests/src/Functional/FileManagedFileElementTest.php, line 28

Class

FileManagedFileElementTest
Tests the 'managed_file' element type.

Namespace

Drupal\Tests\file\Functional

Code

public function testManagedFile() : void {
    // 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 . ']';
                $this->drupalGet($path);
                // Ensure the aria-describedby relationship works as expected.
                $input_id = Html::getId('edit_' . $input_base_name);
                $this->assertSession()
                    ->elementExists('css', '#' . $input_id . '--description');
                $this->assertSession()
                    ->elementExists('css', '[aria-describedby="' . $input_id . '--description"]');
                // Submit without a file.
                $this->submitForm([], 'Save');
                $this->assertSession()
                    ->pageTextContains("The file ids are .");
                // Submit with a file, but with an invalid form token. Ensure the file
                // was not saved.
                $last_fid_prior = $this->getLastFileId();
                $this->drupalGet($path);
                $form_token_field = $this->assertSession()
                    ->hiddenFieldExists('form_token');
                $form_token_field->setValue('invalid token');
                $edit = [
                    $file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri()),
                ];
                $this->submitForm($edit, 'Save');
                $this->assertSession()
                    ->pageTextContains('The form has become outdated.');
                $last_fid = $this->getLastFileId();
                $this->assertEquals($last_fid_prior, $last_fid, 'File was not saved when uploaded with an invalid form token.');
                // Submit a new file, without using the Upload button.
                $last_fid_prior = $this->getLastFileId();
                $edit = [
                    $file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri()),
                ];
                $this->drupalGet($path);
                $this->submitForm($edit, 'Save');
                $last_fid = $this->getLastFileId();
                $this->assertGreaterThan($last_fid_prior, $last_fid, 'New file got saved.');
                $this->assertSession()
                    ->pageTextContains("The file ids are {$last_fid}.");
                // Submit no new input, but with a default file.
                $this->drupalGet($path . '/' . $last_fid);
                $this->submitForm([], 'Save');
                $this->assertSession()
                    ->pageTextContains("The file ids are {$last_fid}.");
                // Upload, then Submit.
                $last_fid_prior = $this->getLastFileId();
                $this->drupalGet($path);
                $edit = [
                    $file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri()),
                ];
                $this->submitForm($edit, 'Upload');
                $last_fid = $this->getLastFileId();
                $this->assertGreaterThan($last_fid_prior, $last_fid, 'New file got uploaded.');
                $this->submitForm([], 'Save');
                $this->assertSession()
                    ->pageTextContains("The file ids are {$last_fid}.");
                // Remove, then Submit.
                $remove_button_title = $multiple ? 'Remove selected' : 'Remove';
                $remove_edit = [];
                if ($multiple) {
                    $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
                    $remove_edit = [
                        $selected_checkbox => '1',
                    ];
                }
                $this->drupalGet($path . '/' . $last_fid);
                $this->submitForm($remove_edit, $remove_button_title);
                $this->submitForm([], 'Save');
                $this->assertSession()
                    ->pageTextContains("The file ids are .");
                // Upload, then Remove, then Submit.
                $this->drupalGet($path);
                $edit = [
                    $file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri()),
                ];
                $this->submitForm($edit, 'Upload');
                $remove_edit = [];
                if ($multiple) {
                    $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this->getLastFileId() . '][selected]';
                    $remove_edit = [
                        $selected_checkbox => '1',
                    ];
                }
                $this->submitForm($remove_edit, $remove_button_title);
                $this->submitForm([], 'Save');
                $this->assertSession()
                    ->pageTextContains("The file ids are .");
            }
        }
    }
    // The multiple file upload has additional conditions that need checking.
    $path = 'file/test/1/1/1';
    $edit = [
        'files[nested_file][]' => \Drupal::service('file_system')->realpath($test_file->getFileUri()),
    ];
    $fid_list = [];
    $this->drupalGet($path);
    // Add a single file to the upload field.
    $this->submitForm($edit, 'Upload');
    $fid_list[] = $this->getLastFileId();
    $this->assertSession()
        ->fieldExists("nested[file][file_{$fid_list[0]}][selected]");
    // Add another file to the same upload field.
    $this->submitForm($edit, 'Upload');
    $fid_list[] = $this->getLastFileId();
    $this->assertSession()
        ->fieldExists("nested[file][file_{$fid_list[1]}][selected]");
    // Save the entire form.
    $this->submitForm([], 'Save');
    // Check that two files are saved into a single multiple file element.
    $this->assertSession()
        ->pageTextContains("The file ids are " . implode(',', $fid_list) . ".");
    // Delete only the first file.
    $edit = [
        'nested[file][file_' . $fid_list[0] . '][selected]' => '1',
    ];
    $this->drupalGet($path . '/' . implode(',', $fid_list));
    $this->submitForm($edit, 'Remove selected');
    // Check that the first file has been deleted but not the second.
    $this->assertSession()
        ->fieldNotExists("nested[file][file_{$fid_list[0]}][selected]");
    $this->assertSession()
        ->fieldExists("nested[file][file_{$fid_list[1]}][selected]");
}

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