You are here

public function FileManagedFileElementTest::testManagedFile in Drupal 10

Same name in this branch
  1. 10 core/modules/file/tests/src/Functional/FileManagedFileElementTest.php \Drupal\Tests\file\Functional\FileManagedFileElementTest::testManagedFile()
  2. 10 core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php \Drupal\Tests\file\FunctionalJavascript\FileManagedFileElementTest::testManagedFile()
Same name and namespace in other branches
  1. 8 core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php \Drupal\Tests\file\FunctionalJavascript\FileManagedFileElementTest::testManagedFile()
  2. 9 core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php \Drupal\Tests\file\FunctionalJavascript\FileManagedFileElementTest::testManagedFile()

Tests the managed_file element type.

File

core/modules/file/tests/src/FunctionalJavascript/FileManagedFileElementTest.php, line 55

Class

FileManagedFileElementTest
Tests the 'managed_file' element type.

Namespace

Drupal\Tests\file\FunctionalJavascript

Code

public function testManagedFile() {

  // Perform the tests with all permutations of $form['#tree'],
  // $element['#extended'], and $element['#multiple'].
  $filename = \Drupal::service('file_system')
    ->tempnam('temporary://', "testManagedFile") . '.txt';
  file_put_contents($filename, $this
    ->randomString(128));
  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 . ']';

        // Now, test the Upload and Remove buttons, with Ajax.
        // Upload, then Submit.
        $last_fid_prior = $this
          ->getLastFileId();
        $this
          ->drupalGet($path);
        $this
          ->getSession()
          ->getPage()
          ->attachFileToField($file_field_name, $this->container
          ->get('file_system')
          ->realpath($filename));
        $uploaded_file = $this
          ->assertSession()
          ->waitForElement('css', '.file--mime-text-plain');
        $this
          ->assertNotEmpty($uploaded_file);
        $last_fid = $this
          ->getLastFileId();
        $this
          ->assertGreaterThan($last_fid_prior, $last_fid, 'New file got uploaded.');
        $this
          ->submitForm([], 'Save');

        // Remove, then Submit.
        $remove_button_title = $multiple ? 'Remove selected' : 'Remove';
        $this
          ->drupalGet($path . '/' . $last_fid);
        if ($multiple) {
          $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
          $this
            ->getSession()
            ->getPage()
            ->checkField($selected_checkbox);
        }
        $this
          ->getSession()
          ->getPage()
          ->pressButton($remove_button_title);
        $this
          ->assertSession()
          ->assertWaitOnAjaxRequest();
        $this
          ->submitForm([], 'Save');
        $this
          ->assertSession()
          ->pageTextContains('The file ids are .');

        // Upload, then Remove, then Submit.
        $this
          ->drupalGet($path);
        $this
          ->getSession()
          ->getPage()
          ->attachFileToField($file_field_name, $this->container
          ->get('file_system')
          ->realpath($filename));
        $uploaded_file = $this
          ->assertSession()
          ->waitForElement('css', '.file--mime-text-plain');
        $this
          ->assertNotEmpty($uploaded_file);
        if ($multiple) {
          $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this
            ->getLastFileId() . '][selected]';
          $this
            ->getSession()
            ->getPage()
            ->checkField($selected_checkbox);
        }
        $this
          ->getSession()
          ->getPage()
          ->pressButton($remove_button_title);
        $this
          ->assertSession()
          ->assertWaitOnAjaxRequest();
        $this
          ->submitForm([], 'Save');
        $this
          ->assertSession()
          ->pageTextContains('The file ids are .');
      }
    }
  }
}