Issue #3276965 by guiu.rocafort.ferrer, andregp, roberttabigue, quietone, rootwork, smustgrave: generate sample values get dimensions wrong when min_resolution is bigger than 600x600
parent
7334f69920
commit
4578bbd605
|
@ -344,6 +344,17 @@ class ImageItem extends FileItem {
|
||||||
$max_resolution = empty($settings['max_resolution']) ? '600x600' : $settings['max_resolution'];
|
$max_resolution = empty($settings['max_resolution']) ? '600x600' : $settings['max_resolution'];
|
||||||
$extensions = array_intersect(explode(' ', $settings['file_extensions']), ['png', 'gif', 'jpg', 'jpeg']);
|
$extensions = array_intersect(explode(' ', $settings['file_extensions']), ['png', 'gif', 'jpg', 'jpeg']);
|
||||||
$extension = array_rand(array_combine($extensions, $extensions));
|
$extension = array_rand(array_combine($extensions, $extensions));
|
||||||
|
|
||||||
|
$min = explode('x', $min_resolution);
|
||||||
|
$max = explode('x', $max_resolution);
|
||||||
|
if (intval($min[0]) > intval($max[0])) {
|
||||||
|
$max[0] = $min[0];
|
||||||
|
}
|
||||||
|
if (intval($min[1]) > intval($max[1])) {
|
||||||
|
$max[1] = $min[1];
|
||||||
|
}
|
||||||
|
$max_resolution = "$max[0]x$max[1]";
|
||||||
|
|
||||||
// Generate a max of 5 different images.
|
// Generate a max of 5 different images.
|
||||||
if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) <= 5) {
|
if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) <= 5) {
|
||||||
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
|
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
|
||||||
|
|
|
@ -63,6 +63,13 @@ class ImageItemTest extends FieldKernelTestBase {
|
||||||
'type' => 'image',
|
'type' => 'image',
|
||||||
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
|
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
|
||||||
])->save();
|
])->save();
|
||||||
|
FieldStorageConfig::create([
|
||||||
|
'entity_type' => 'entity_test',
|
||||||
|
'field_name' => 'image_test_generation',
|
||||||
|
'type' => 'image',
|
||||||
|
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
|
||||||
|
])->save();
|
||||||
|
|
||||||
FieldConfig::create([
|
FieldConfig::create([
|
||||||
'entity_type' => 'entity_test',
|
'entity_type' => 'entity_test',
|
||||||
'field_name' => 'image_test',
|
'field_name' => 'image_test',
|
||||||
|
@ -71,6 +78,15 @@ class ImageItemTest extends FieldKernelTestBase {
|
||||||
'file_extensions' => 'jpg',
|
'file_extensions' => 'jpg',
|
||||||
],
|
],
|
||||||
])->save();
|
])->save();
|
||||||
|
FieldConfig::create([
|
||||||
|
'entity_type' => 'entity_test',
|
||||||
|
'field_name' => 'image_test_generation',
|
||||||
|
'bundle' => 'entity_test',
|
||||||
|
'settings' => [
|
||||||
|
'min_resolution' => '800x800',
|
||||||
|
],
|
||||||
|
])->save();
|
||||||
|
|
||||||
\Drupal::service('file_system')->copy($this->root . '/core/misc/druplicon.png', 'public://example.jpg');
|
\Drupal::service('file_system')->copy($this->root . '/core/misc/druplicon.png', 'public://example.jpg');
|
||||||
$this->image = File::create([
|
$this->image = File::create([
|
||||||
'uri' => 'public://example.jpg',
|
'uri' => 'public://example.jpg',
|
||||||
|
@ -136,11 +152,25 @@ class ImageItemTest extends FieldKernelTestBase {
|
||||||
$properties = $entity->getFieldDefinition('image_test')->getFieldStorageDefinition()->getPropertyDefinitions();
|
$properties = $entity->getFieldDefinition('image_test')->getFieldStorageDefinition()->getPropertyDefinitions();
|
||||||
$this->assertEquals($expected, array_keys($properties));
|
$this->assertEquals($expected, array_keys($properties));
|
||||||
|
|
||||||
// Test the generateSampleValue() method.
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests generateSampleItems() method under different resolutions.
|
||||||
|
*/
|
||||||
|
public function testImageItemSampleValueGeneration() {
|
||||||
|
|
||||||
|
// Default behaviour. No resolution configuration.
|
||||||
$entity = EntityTest::create();
|
$entity = EntityTest::create();
|
||||||
$entity->image_test->generateSampleItems();
|
$entity->image_test->generateSampleItems();
|
||||||
$this->entityValidateAndSave($entity);
|
$this->entityValidateAndSave($entity);
|
||||||
$this->assertEquals('image/jpeg', $entity->image_test->entity->get('filemime')->value);
|
$this->assertEquals('image/jpeg', $entity->image_test->entity->get('filemime')->value);
|
||||||
|
|
||||||
|
// Max resolution bigger than 600x600.
|
||||||
|
$entity->image_test_generation->generateSampleItems();
|
||||||
|
$this->entityValidateAndSave($entity);
|
||||||
|
$imageItem = $entity->image_test_generation->first()->getValue();
|
||||||
|
$this->assertEquals('800', $imageItem['width']);
|
||||||
|
$this->assertEquals('800', $imageItem['height']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue