Unverified Commit 20ebfa43 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2884052 by zviryatko, Krzysztof Domański, alexpott, drclaw, vijaycs85,...

Issue #2884052 by zviryatko, Krzysztof Domański, alexpott, drclaw, vijaycs85, Madis, charlotte.b, mcdruid, Berdir, lauriii: Uploading a managed file on a custom form that allows multiple files also triggers the remove button which results in a duplicate temporary record and results in the file being deleted

(cherry picked from commit 982773de)
parent 4615f549
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -201,7 +201,7 @@
    triggerUploadButton(event) {
      $(event.target)
        .closest('.js-form-managed-file')
        .find('.js-form-submit')
        .find('.js-form-submit[data-drupal-selector$="upload-button"]')
        .trigger('mousedown');
    },

+1 −1
Original line number Diff line number Diff line
@@ -92,7 +92,7 @@
      }
    },
    triggerUploadButton: function triggerUploadButton(event) {
      $(event.target).closest('.js-form-managed-file').find('.js-form-submit').trigger('mousedown');
      $(event.target).closest('.js-form-managed-file').find('.js-form-submit[data-drupal-selector$="upload-button"]').trigger('mousedown');
    },
    disableFields: function disableFields(event) {
      var $clickedButton = $(this);
+49 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\file\FunctionalJavascript;

use Drupal\Core\Url;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\TestFileCreationTrait;

/**
 * Tests ajax upload to managed files.
 *
 * @group file
 */
class AjaxFileManagedMultipleTest extends WebDriverTestBase {

  use TestFileCreationTrait {
    getTestFiles as drupalGetTestFiles;
  }

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['file_test', 'file', 'file_module_test'];

  /**
   * Test if managed file form element works well with multiple files upload.
   */
  public function testMultipleFilesUpload() {
    $file_system = \Drupal::service('file_system');
    $file_storage = \Drupal::entityTypeManager()->getStorage('file');
    $page = $this->getSession()->getPage();

    $this->drupalGet(Url::fromRoute('file_module_test.managed_test', ['multiple' => TRUE]));

    $paths = [];
    foreach (array_slice($this->drupalGetTestFiles('image'), 0, 2) as $image) {
      $paths[] = $image->filename;
      $page->attachFileToField('files[nested_file][]', $file_system->realpath($image->uri));
      $this->assertSession()->assertWaitOnAjaxRequest();
    }

    // Save entire form.
    $page->pressButton('Save');

    $this->assertSession()->pageTextContains('The file ids are 1,2.');
    $this->assertCount(2, $file_storage->loadByProperties(['filename' => $paths]));
  }

}