diff --git a/core/modules/file/src/Element/ManagedFile.php b/core/modules/file/src/Element/ManagedFile.php index 8e7843fcbb..06bf2e62a1 100644 --- a/core/modules/file/src/Element/ManagedFile.php +++ b/core/modules/file/src/Element/ManagedFile.php @@ -85,6 +85,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form // does for #element_validate and #process, this fills the missing // functionality to allow File fields to be extended through FAPI. if (isset($element['#file_value_callbacks'])) { + \Drupal::logger('file')->notice('#file_value_callbacks: '. serialize($element['#file_value_callbacks'])); foreach ($element['#file_value_callbacks'] as $callback) { $callback($element, $input, $form_state); } @@ -123,23 +124,32 @@ public static function valueCallback(&$element, $input, FormStateInterface $form $fids = []; } } + else { + \Drupal::logger('file')->notice('$input[\'fids\'] empty at line 95 in ManagedFile::valueCallback()'); + } } } + else { + \Drupal::logger('file')->notice('Input is FALSE in ManagedFile::valueCallback()'); + } // If there is no input or if the default value was requested above, use the // default value. if ($input === FALSE || $force_default) { if ($element['#extended']) { + \Drupal::logger('file')->notice('$element[\'#extended\']'); $default_fids = isset($element['#default_value']['fids']) ? $element['#default_value']['fids'] : []; $return = isset($element['#default_value']) ? $element['#default_value'] : ['fids' => []]; } else { + \Drupal::logger('file')->notice('$element[\'#extended\'] FALSE'); $default_fids = isset($element['#default_value']) ? $element['#default_value'] : []; $return = ['fids' => []]; } // Confirm that the file exists when used as a default value. if (!empty($default_fids)) { + \Drupal::logger('file')->notice('empty $default_fids'); $fids = []; foreach ($default_fids as $fid) { if ($file = File::load($fid)) { @@ -149,6 +159,11 @@ public static function valueCallback(&$element, $input, FormStateInterface $form } } + if (empty($fids)) { + $fids = []; + \Drupal::logger('file')->notice('fids empty, line 164 in ManagedFile'); + } + $return['fids'] = $fids; return $return; } diff --git a/core/modules/media_library/src/Form/FileUploadForm.php b/core/modules/media_library/src/Form/FileUploadForm.php index 81367821c2..2cb3e9d5be 100644 --- a/core/modules/media_library/src/Form/FileUploadForm.php +++ b/core/modules/media_library/src/Form/FileUploadForm.php @@ -253,6 +253,7 @@ public function processUploadElement(array $element, FormStateInterface $form_st * The form state. */ public function uploadButtonSubmit(array $form, FormStateInterface $form_state) { + \Drupal::logger('file')->notice('FileUploadForm::uploadButtonSubmit upload values:' . serialize($form_state->getValue('upload', []))); $files = $this->entityTypeManager ->getStorage('file') ->loadMultiple($form_state->getValue('upload', [])); diff --git a/core/modules/media_library/tests/src/FunctionalJavascript/UploadFail.php b/core/modules/media_library/tests/src/FunctionalJavascript/UploadFail.php index 6992a03624..a1a8a0c56e 100644 --- a/core/modules/media_library/tests/src/FunctionalJavascript/UploadFail.php +++ b/core/modules/media_library/tests/src/FunctionalJavascript/UploadFail.php @@ -4,9 +4,11 @@ use Behat\Mink\Exception\ElementNotFoundException; use Drupal\Core\File\Exception\FileNotExistsException; +use Drupal\Core\File\FileSystemInterface; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; use Drupal\media\Entity\Media; use Drupal\Tests\TestFileCreationTrait; +use PHPUnit\Framework\AssertionFailedError; /** * Checks if uploads fail after several attempts. @@ -23,6 +25,7 @@ class UploadFail extends WebDriverTestBase { protected static $modules = [ 'media_library_test', 'node', + 'dblog', ]; /** @@ -58,18 +61,41 @@ public function testManyConsecutiveUploads() { // Visit a node create page and open the media library. $this->drupalGet('node/add/basic_page'); + $database = $this->container->get('database'); + $count = 0; // Upload then remove an image 100x to see if/when it fails. while ($count < 100) { + \Drupal::logger('file')->notice("Starting iteration $count"); $this->assertElementExistsAfterWait('css', '.media-library-open-button[name^="field_twin_media"]', 10000, "Twin media open button not found on iteration $count")->click(); $this->assertElementExistsAfterWait('css', '.media-library-menu', 10000, "Media library menu did not appear on iteration $count"); $this->waitForText('Add or select media', 10000, "Text 'Add or select media' not appear on iteration $count"); $this->clickTypeTab('Four'); - $new_filename = uniqid() . '.jpg'; + $new_filename = uniqid(time()) . '.jpg'; $new_filepath = $file_system->realpath('public://') . '/' . $new_filename; - copy($file_system->realpath($jpg_image->uri), $new_filepath); + $file_system->copy($file_system->realpath($jpg_image->uri), $new_filepath); $this->waitForFieldExists('Add files', 10000, "Did not find 'Add files' on iteration $count"); + $media_count = $database->select('file_managed')->countQuery()->execute()->fetchField(); + $this->assertEquals($count, $media_count); $page->attachFileToField('Add files', $new_filepath); + $result = $page->waitFor(10, function () use ($database, $count) { + $media_count = $database->select('file_managed')->countQuery()->execute()->fetchField(); + return ($count + 1) === intval($media_count); + }); + if (!$result) { + $logs = $database + ->select('watchdog', 'w') + ->fields('w', ['message', 'variables']) + ->condition('type', 'file') + ->orderBy('wid', 'ASC') + ->execute() + ->fetchAll(); + $output = "The file_managed table not updated on iteration $count."; + foreach ($logs as $log) { + $output .= "\n" . $log->message; + } + throw new AssertionFailedError($output); + } $this->waitForText('Alternative text', 10000, "Alternative text field not found on iteration $count"); $field = $page->findField('Alternative text'); $this->assertTrue(!empty($field), "Alt text label found but not the field on iteration $count"); @@ -86,6 +112,7 @@ public function testManyConsecutiveUploads() { $this->assertElementExistsAfterWait('css', '[name="field_twin_media-0-media-library-remove-button"]', 10000, "Media remove button not found on iteration $count")->click(); $this->waitForText('2 media items remaining', 10000, "'2 media items remaining' not found on iteration $count"); $count++; + $this->resetAll(); usleep(10000); } @@ -211,8 +238,6 @@ protected function waitForFieldExists($field, $timeout = 10000, $message = 'did } while (microtime(TRUE) < $end); $this->htmlOutput(); - $filename = $this->htmlOutputDirectory . '/' . $this->htmlOutputClassName . '-' . $this->htmlOutputCounter . '-' . $this->htmlOutputTestId . '-waitFieExis.jpg'; $this->createScreenshot($filename); - $this->assertNotEmpty($node, $message); }