Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Merge requests
!3663
Issue
#2942160
: Rationalize image widget validation error messages
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#2942160
: Rationalize image widget validation error messages
issue/drupal-2942160:2942160-rationalize-image-widget
into
10.1.x
Overview
0
Commits
3
Pipelines
0
Changes
3
Open
mondrake
requested to merge
issue/drupal-2942160:2942160-rationalize-image-widget
into
10.1.x
2 years ago
Overview
0
Commits
3
Pipelines
0
Changes
3
Expand
0
0
Merge request reports
Compare
10.1.x
version 2
74c79f00
2 years ago
version 1
c8071ea2
2 years ago
10.1.x (HEAD)
and
latest version
latest version
78327020
3 commits,
2 years ago
version 2
74c79f00
2 commits,
2 years ago
version 1
c8071ea2
1 commit,
2 years ago
3 files
+
114
−
19
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
core/modules/file/tests/src/Kernel/ValidatorTest.php
+
87
−
8
Options
@@ -138,17 +138,96 @@ public function providerTestFileValidateExtensionsOnUri(): array {
];
}
/**
* Provides data for testFileValidateIsImage.
*
* @return array[]
* An associative array of simple arrays, with key the test scenario and
* value an array having the following elements:
* - the file path of the image file to be tested
* - the extensions restriction
* - the expected error message or NULL if no error expected.
*/
public
function
providerFileValidateIsImage
():
array
{
return
[
'Valid image, no extensions restriction'
=>
[
'core/misc/druplicon.png'
,
''
,
NULL
,
],
'Invalid image, no extensions restriction'
=>
[
'core/tests/fixtures/files/invalid-img-test.png'
,
''
,
'The image file is invalid or the image type is not allowed. Allowed types: png jpeg jpg jpe gif webp'
,
],
'Not an image, no extensions restriction'
=>
[
'core/assets/vendor/jquery/jquery.min.js'
,
''
,
'The image file is invalid or the image type is not allowed. Allowed types: png jpeg jpg jpe gif webp'
,
],
// If extension restrictions are specified, we expect that the
// validator returns an empty array if the test file extension is not in
// the list of the restricted extensions, as the extension validator will
// cope with that case.
'Valid image, extension included in restriction'
=>
[
'core/misc/druplicon.png'
,
'jpeg png'
,
NULL
,
],
'Invalid image, extension included in restriction'
=>
[
'core/tests/fixtures/files/invalid-img-test.png'
,
'jpeg png'
,
'The image file is invalid or the image type is not allowed. Allowed types: png jpeg'
,
],
'Valid png image, extensions restricted to jpeg'
=>
[
'core/misc/druplicon.png'
,
'jpeg'
,
NULL
,
],
'Invalid png image, extensions restricted to png'
=>
[
'core/tests/fixtures/files/invalid-img-test.png'
,
'png'
,
'The image file is invalid or the image type is not allowed. Allowed types: png'
,
],
'Not an image, extensions restricted to jpeg'
=>
[
'core/assets/vendor/jquery/jquery.min.js'
,
'jpeg'
,
NULL
,
],
// Even if we include a non-image file extension in the allowed
// extensions, still the toolkit will cut it.
'Not an image, but extension allowed in restriction'
=>
[
'core/assets/vendor/jquery/jquery.min.js'
,
'jpeg png js'
,
'The image file is invalid or the image type is not allowed. Allowed types: png jpeg'
,
],
];
}
/**
* This ensures a specific file is actually an image.
*
* @param string $image_path
* The file path of the image file to be tested.
* @param string $extensions
* The allowed extensions restriction.
* @param string|null $expected_error
* The expected error message or NULL if no error expected.
*
* @dataProvider providerFileValidateIsImage
*/
public
function
testFileValidateIsImage
()
{
$this
->
assertFileExists
(
$this
->
image
->
getFileUri
());
$errors
=
file_validate_is_image
(
$this
->
image
);
$this
->
assertCount
(
0
,
$errors
,
'No error reported for our image file.'
);
$this
->
assertFileExists
(
$this
->
nonImage
->
getFileUri
());
$errors
=
file_validate_is_image
(
$this
->
nonImage
);
$this
->
assertCount
(
1
,
$errors
,
'An error reported for our non-image file.'
);
public
function
testFileValidateIsImage
(
string
$image_path
,
string
$extensions
,
?string
$expected_error
):
void
{
$image
=
File
::
create
();
$image
->
setFileUri
(
$image_path
);
$image
->
setFilename
(
\Drupal
::
service
(
'file_system'
)
->
basename
(
$image_path
));
$this
->
assertFileExists
(
$image
->
getFileUri
());
$errors
=
file_validate_is_image
(
$image
,
$extensions
);
if
(
$expected_error
!==
NULL
)
{
$this
->
assertEquals
(
$expected_error
,
strip_tags
(
$errors
[
0
]));
}
else
{
$this
->
assertEmpty
(
$errors
);
}
}
/**
Loading