From 37b9e45ac06b5d2e90af60a931cd0fc548756d1b Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Wed, 11 Apr 2018 22:05:27 +0100 Subject: [PATCH] Issue #2956814 by tim.plunkett: FileValidationConstraintValidator assumes that the file exists --- .../Constraint/FileValidationConstraintValidator.php | 7 ++++++- .../file/tests/src/Kernel/FileItemValidationTest.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php b/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php index 972aa69767dd..a6da920c6697 100644 --- a/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php +++ b/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php @@ -15,7 +15,12 @@ class FileValidationConstraintValidator extends ConstraintValidator { */ public function validate($value, Constraint $constraint) { // Get the file to execute validators. - $file = $value->get('entity')->getTarget()->getValue(); + $target = $value->get('entity')->getTarget(); + if (!$target) { + return; + } + + $file = $target->getValue(); // Get the validators. $validators = $value->getUploadValidators(); // Checks that a file meets the criteria specified by the validators. diff --git a/core/modules/file/tests/src/Kernel/FileItemValidationTest.php b/core/modules/file/tests/src/Kernel/FileItemValidationTest.php index c9a247bf4029..a0e96c2082fe 100644 --- a/core/modules/file/tests/src/Kernel/FileItemValidationTest.php +++ b/core/modules/file/tests/src/Kernel/FileItemValidationTest.php @@ -104,6 +104,18 @@ public function testFileValidationConstraint($file_type) { $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()); + + // Refer to a file that does not exist. + $entity_test = EntityTest::create([ + 'uid' => $this->user->id(), + 'field_test_file' => [ + 'target_id' => 2, + ], + ]); + $result = $entity_test->validate(); + $this->assertCount(1, $result); + $this->assertEquals('field_test_file.0.target_id', $result->get(0)->getPropertyPath()); + $this->assertEquals('The referenced entity (<em class="placeholder">file</em>: <em class="placeholder">2</em>) does not exist.', (string) $result->get(0)->getMessage()); } /** -- GitLab