diff --git a/core/modules/file/src/Plugin/Validation/Constraint/FileSizeLimitConstraintValidator.php b/core/modules/file/src/Plugin/Validation/Constraint/FileSizeLimitConstraintValidator.php index f2b52328667c72b018fcc6cf147d32d8bd4e40f2..dc66d7e5e0eeacf098efe3f074a16b42ac525117 100644 --- a/core/modules/file/src/Plugin/Validation/Constraint/FileSizeLimitConstraintValidator.php +++ b/core/modules/file/src/Plugin/Validation/Constraint/FileSizeLimitConstraintValidator.php @@ -49,7 +49,7 @@ public function validate(mixed $value, Constraint $constraint): void { $fileLimit = $constraint->fileLimit; - if ($fileLimit && $file->getSize() > $fileLimit) { + if ($file->isNew() && $fileLimit && $file->getSize() > $fileLimit) { $this->context->addViolation($constraint->maxFileSizeMessage, [ '%filesize' => ByteSizeMarkup::create($file->getSize()), '%maxsize' => ByteSizeMarkup::create($fileLimit), diff --git a/core/modules/file/tests/src/Kernel/FileItemValidationTest.php b/core/modules/file/tests/src/Kernel/FileItemValidationTest.php index 6358163c866359ff3dc0e72fc89c6d4260abd86d..2b843d6d3452c5dba97cc9cf3def3425f6456c11 100644 --- a/core/modules/file/tests/src/Kernel/FileItemValidationTest.php +++ b/core/modules/file/tests/src/Kernel/FileItemValidationTest.php @@ -106,14 +106,23 @@ public function testFileValidationConstraint($file_type): void { 'target_id' => $file->id(), ], ]); + + // Enforce the file to be new as file size is checked only for new files. + $entity_test->field_test_file->entity->enforceIsNew(); $result = $entity_test->validate(); $this->assertCount(2, $result); - $this->assertEquals('field_test_file.0', $result->get(0)->getPropertyPath()); $this->assertEquals('The file is <em class="placeholder">2.93 KB</em> exceeding the maximum file size of <em class="placeholder">2 KB</em>.', (string) $result->get(0)->getMessage()); $this->assertEquals('field_test_file.0', $result->get(1)->getPropertyPath()); $this->assertEquals('Only files with the following extensions are allowed: <em class="placeholder">jpg|png</em>.', (string) $result->get(1)->getMessage()); + // File size is not checked for already existing files. + $entity_test->field_test_file->entity->enforceIsNew(FALSE); + $result = $entity_test->validate(); + $this->assertCount(1, $result); + $this->assertEquals('field_test_file.0', $result->get(0)->getPropertyPath()); + $this->assertEquals('Only files with the following extensions are allowed: <em class="placeholder">jpg|png</em>.', (string) $result->get(0)->getMessage()); + // Refer to a file that does not exist. $entity_test = EntityTest::create([ 'uid' => $this->user->id(),