function ImageFieldValidateTest::testValid
Same name in other branches
- 9 core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testValid()
- 8.9.x core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testValid()
- 11.x core/modules/image/tests/src/Functional/ImageFieldValidateTest.php \Drupal\Tests\image\Functional\ImageFieldValidateTest::testValid()
Tests image validity.
File
-
core/
modules/ image/ tests/ src/ Functional/ ImageFieldValidateTest.php, line 31
Class
- ImageFieldValidateTest
- Tests validation functions such as min/max dimensions.
Namespace
Drupal\Tests\image\FunctionalCode
public function testValid() : void {
$file_system = $this->container
->get('file_system');
$image_files = $this->drupalGetTestFiles('image');
$field_name = $this->randomMachineName();
$this->createImageField($field_name, 'node', 'article', [], [
'file_directory' => 'test-upload',
]);
$expected_path = 'public://test-upload';
// Create alt text for the image.
$alt = $this->randomMachineName();
// Create a node with a valid image.
$node = $this->uploadNodeImage($image_files[0], $field_name, 'article', $alt);
$this->assertFileExists($expected_path . '/' . $image_files[0]->filename);
// Remove the image.
$this->drupalGet('node/' . $node . '/edit');
$this->submitForm([], 'Remove');
$this->submitForm([], 'Save');
// Get invalid image test files.
$dir = 'core/tests/fixtures/files';
$files = [];
if (is_dir($dir)) {
$files = $file_system->scanDirectory($dir, '/invalid-img-.*/');
}
$invalid_image_files = [];
foreach ($files as $file) {
$invalid_image_files[$file->filename] = $file;
}
// Try uploading a zero-byte image.
$zero_size_image = $invalid_image_files['invalid-img-zero-size.png'];
$edit = [
'files[' . $field_name . '_0]' => $file_system->realpath($zero_size_image->uri),
];
$this->drupalGet('node/' . $node . '/edit');
$this->submitForm($edit, 'Upload');
$this->assertFileDoesNotExist($expected_path . '/' . $zero_size_image->filename);
// Try uploading an invalid image.
$invalid_image = $invalid_image_files['invalid-img-test.png'];
$edit = [
'files[' . $field_name . '_0]' => $file_system->realpath($invalid_image->uri),
];
$this->drupalGet('node/' . $node . '/edit');
$this->submitForm($edit, 'Upload');
$this->assertFileDoesNotExist($expected_path . '/' . $invalid_image->filename);
// Upload a valid image again.
$valid_image = $image_files[0];
$edit = [
'files[' . $field_name . '_0]' => $file_system->realpath($valid_image->uri),
];
$this->drupalGet('node/' . $node . '/edit');
$this->submitForm($edit, 'Upload');
$this->assertFileExists($expected_path . '/' . $valid_image->filename);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.