Issue #3057070 by joachim, Pancho, nicxvan: Refactor ImageFieldCreationTrait to support entity types other than nodes

merge-requests/7474/merge
xjm 2024-04-26 15:12:10 -05:00
parent dd84445aea
commit cfe99268a5
No known key found for this signature in database
GPG Key ID: 206B0B8743BDF4C2
16 changed files with 49 additions and 48 deletions

View File

@ -72,7 +72,7 @@ class ContentTranslationLanguageChangeTest extends NodeTestBase {
$this->rebuildContainer();
$this->createImageField('field_image_field', 'article');
$this->createImageField('field_image_field', 'node', 'article');
}
/**

View File

@ -317,7 +317,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Create an image field that uses the new style.
$field_name = $this->randomMachineName(10);
$this->createImageField($field_name, 'article');
$this->createImageField($field_name, 'node', 'article');
\Drupal::service('entity_display.repository')
->getViewDisplay('node', 'article')
->setComponent($field_name, [
@ -470,7 +470,7 @@ class ImageAdminStylesTest extends ImageFieldTestBase {
// Create an image field that uses the new style.
$field_name = $this->randomMachineName(10);
$this->createImageField($field_name, 'article');
$this->createImageField($field_name, 'node', 'article');
\Drupal::service('entity_display.repository')
->getViewDisplay('node', 'article')
->setComponent($field_name, [

View File

@ -81,7 +81,7 @@ class ImageFieldDefaultImagesTest extends ImageFieldTestBase {
$widget_settings = [
'preview_image_style' => 'medium',
];
$field = $this->createImageField($field_name, 'article', $storage_settings, $field_settings, $widget_settings);
$field = $this->createImageField($field_name, 'node', 'article', $storage_settings, $field_settings, $widget_settings);
// The field default image id should be 2.
$this->assertEquals($default_images['field']->uuid(), $field->getSetting('default_image')['uuid']);

View File

@ -64,7 +64,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$node_storage = $this->container->get('entity_type.manager')->getStorage('node');
$field_name = $this->randomMachineName();
$field_settings = ['alt_field_required' => 0];
$instance = $this->createImageField($field_name, 'article', ['uri_scheme' => $scheme], $field_settings);
$instance = $this->createImageField($field_name, 'node', 'article', ['uri_scheme' => $scheme], $field_settings);
// Go to manage display page.
$this->drupalGet("admin/structure/types/manage/article/display");
@ -261,7 +261,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$widget_settings = [
'preview_image_style' => 'medium',
];
$field = $this->createImageField($field_name, 'article', [], $field_settings, $widget_settings);
$field = $this->createImageField($field_name, 'node', 'article', [], $field_settings, $widget_settings);
// Verify that the min/max dimensions set on the field are properly
// extracted, and displayed, on the image field's configuration form.
@ -369,7 +369,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$node_storage = $this->container->get('entity_type.manager')->getStorage('node');
$field_name = $this->randomMachineName();
$field_settings = ['alt_field_required' => 0];
$instance = $this->createImageField($field_name, 'article', [], $field_settings);
$instance = $this->createImageField($field_name, 'node', 'article', [], $field_settings);
// Go to manage display page.
$this->drupalGet("admin/structure/types/manage/article/display");
@ -484,7 +484,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$node_storage = $this->container->get('entity_type.manager')->getStorage('node');
// Create a new image field.
$field_name = $this->randomMachineName();
$this->createImageField($field_name, 'article');
$this->createImageField($field_name, 'node', 'article');
// Create a new node, with no images and verify that no images are
// displayed.
@ -573,7 +573,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
// Create an image field that uses the private:// scheme and test that the
// default image works as expected.
$private_field_name = $this->randomMachineName();
$this->createImageField($private_field_name, 'article', ['uri_scheme' => 'private']);
$this->createImageField($private_field_name, 'node', 'article', ['uri_scheme' => 'private']);
// Add a default image to the new field.
$edit = [
// Get the path of the 'image-test.gif' file.

View File

@ -33,7 +33,7 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
$image_files = $this->drupalGetTestFiles('image');
$field_name = $this->randomMachineName();
$this->createImageField($field_name, 'article', [], ['file_directory' => 'test-upload']);
$this->createImageField($field_name, 'node', 'article', [], ['file_directory' => 'test-upload']);
$expected_path = 'public://test-upload';
// Create alt text for the image.
@ -125,9 +125,9 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
1 => $this->getFieldSettings($no_height_min_resolution, $no_height_max_resolution),
2 => $this->getFieldSettings($no_width_min_resolution, $no_width_max_resolution),
];
$this->createImageField($field_names[0], 'article', [], $field_settings[0]);
$this->createImageField($field_names[1], 'article', [], $field_settings[1]);
$this->createImageField($field_names[2], 'article', [], $field_settings[2]);
$this->createImageField($field_names[0], 'node', 'article', [], $field_settings[0]);
$this->createImageField($field_names[1], 'node', 'article', [], $field_settings[1]);
$this->createImageField($field_names[2], 'node', 'article', [], $field_settings[2]);
// We want a test image that is too small, and a test image that is too
// big, so cycle through test image files until we have what we need.
@ -174,7 +174,7 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
'title_field_required' => 1,
'required' => 1,
];
$instance = $this->createImageField($field_name, 'article', [], $field_settings);
$instance = $this->createImageField($field_name, 'node', 'article', [], $field_settings);
$images = $this->drupalGetTestFiles('image');
// Let's just use the first image.
$image = $images[0];
@ -223,7 +223,7 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
* @dataProvider providerTestEmpty
*/
public function testEmpty($field_name, $required, $cardinality, $form_element_name, $expected_page_text_when_edit_access_allowed, $expected_page_text_when_edit_access_forbidden) {
$this->createImageField($field_name, 'article', ['cardinality' => $cardinality], ['required' => $required]);
$this->createImageField($field_name, 'node', 'article', ['cardinality' => $cardinality], ['required' => $required]);
// Test with field edit access allowed.
$this->drupalGet('node/add/article');

View File

@ -31,7 +31,7 @@ class ImageFieldWidgetTest extends ImageFieldTestBase {
'min_resolution' => $min_resolution . 'x' . $min_resolution,
'alt_field' => 0,
];
$this->createImageField($field_name, 'article', [], $field_settings, [], [], 'Image test on [site:name]');
$this->createImageField($field_name, 'node', 'article', [], $field_settings, [], [], 'Image test on [site:name]');
$this->drupalGet('node/add/article');
// Verify that the image field widget is found on add/node page.
$this->assertSession()->elementExists('xpath', '//div[contains(@class, "field--widget-image-image")]');

View File

@ -56,7 +56,7 @@ class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
// Create an image field on the "Basic page" node type.
$this->fieldName = $this->randomMachineName();
$this->createImageField($this->fieldName, 'basic_page', [], ['title_field' => 1]);
$this->createImageField($this->fieldName, 'node', 'basic_page', [], ['title_field' => 1]);
// Create and log in user.
$permissions = [

View File

@ -26,7 +26,7 @@ class ImageStyleDeleteTest extends ImageFieldTestBase {
parent::setUp();
// Create an image field 'foo' having the image style 'medium' as widget
// preview and as formatter.
$this->createImageField('foo', 'page', [], [], ['preview_image_style' => 'medium'], ['image_style' => 'medium']);
$this->createImageField('foo', 'node', 'page', [], [], ['preview_image_style' => 'medium'], ['image_style' => 'medium']);
}
/**

View File

@ -24,7 +24,7 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
*/
public function testAJAXValidationMessage() {
$field_name = $this->randomMachineName();
$this->createImageField($field_name, 'article', ['cardinality' => -1]);
$this->createImageField($field_name, 'node', 'article', ['cardinality' => -1]);
$this->drupalGet('node/add/article');
/** @var \Drupal\file\FileInterface[] $text_files */
@ -73,7 +73,7 @@ class ImageFieldValidateTest extends ImageFieldTestBase {
->save();
// Then, add an image field.
$this->createImageField('field_dummy_image', 'article');
$this->createImageField('field_dummy_image', 'node', 'article');
// Open an article and trigger the AJAX handler.
$this->drupalGet('node/add/article');

View File

@ -42,7 +42,7 @@ class ImageFieldWidgetMultipleTest extends WebDriverTestBase {
$field_name = 'images';
$storage_settings = ['cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED];
$field_settings = ['alt_field_required' => 0];
$this->createImageField($field_name, 'article', $storage_settings, $field_settings);
$this->createImageField($field_name, 'node', 'article', $storage_settings, $field_settings);
$this->drupalLogin($this->drupalCreateUser(['access content', 'create article content']));
$this->drupalGet('node/add/article');
$this->assertSession()->fieldExists('title[0][value]')->setValue('Test');

View File

@ -15,11 +15,13 @@ trait ImageFieldCreationTrait {
/**
* Create a new image field.
*
* @param string $name
* @param string $field_name
* The name of the new field (all lowercase). The Field UI 'field_' prefix
* is not added to the field name.
* @param string $type_name
* The node type that this field will be added to.
* @param string $entity_type
* The entity type that this field will be added to.
* @param string $bundle
* The bundle this field will be added to.
* @param array $storage_settings
* (optional) A list of field storage settings that will be added to the
* defaults.
@ -33,20 +35,20 @@ trait ImageFieldCreationTrait {
* @param string $description
* (optional) A description for the field. Defaults to ''.
*/
protected function createImageField($name, $type_name, $storage_settings = [], $field_settings = [], $widget_settings = [], $formatter_settings = [], $description = '') {
protected function createImageField($field_name, $entity_type, $bundle, $storage_settings = [], $field_settings = [], $widget_settings = [], $formatter_settings = [], $description = '') {
FieldStorageConfig::create([
'field_name' => $name,
'entity_type' => 'node',
'field_name' => $field_name,
'entity_type' => $entity_type,
'type' => 'image',
'settings' => $storage_settings,
'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
])->save();
$field_config = FieldConfig::create([
'field_name' => $name,
'label' => $name,
'entity_type' => 'node',
'bundle' => $type_name,
'field_name' => $field_name,
'label' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'required' => !empty($field_settings['required']),
'settings' => $field_settings,
'description' => $description,
@ -55,15 +57,14 @@ trait ImageFieldCreationTrait {
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
$display_repository->getFormDisplay('node', $type_name)
->setComponent($name, [
$display_repository->getFormDisplay($entity_type, $bundle)
->setComponent($field_name, [
'type' => 'image_image',
'settings' => $widget_settings,
])
->save();
$display_repository->getViewDisplay('node', $type_name)
->setComponent($name, [
$display_repository->getViewDisplay($entity_type, $bundle)
->setComponent($field_name, [
'type' => 'image',
'settings' => $formatter_settings,
])

View File

@ -130,8 +130,8 @@ abstract class JsonApiFunctionalTestBase extends BrowserTestBase {
],
FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
);
$this->createImageField('field_image', 'article');
$this->createImageField('field_no_hero', 'article');
$this->createImageField('field_image', 'node', 'article');
$this->createImageField('field_no_hero', 'node', 'article');
}
FieldStorageConfig::create([

View File

@ -158,7 +158,7 @@ class JsonApiDocumentTopLevelNormalizerTest extends JsonapiKernelTestBase {
);
$this->createTextField('node', 'article', 'body', 'Body');
$this->createImageField('field_image', 'article');
$this->createImageField('field_image', 'node', 'article');
$this->user = User::create([
'name' => 'user1',

View File

@ -297,7 +297,7 @@ class FilterTest extends JsonapiKernelTestBase {
'shapes', 'Shapes',
FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
);
$this->createImageField('photo', 'painting');
$this->createImageField('photo', 'node', 'painting');
}
/**

View File

@ -319,7 +319,7 @@ class LayoutBuilderDefaultValuesTest extends BrowserTestBase {
$widget_settings = [
'preview_image_style' => 'medium',
];
$this->createImageField($field_name, 'test_node_type', $storage_settings, $field_settings, $widget_settings);
$this->createImageField($field_name, 'node', 'test_node_type', $storage_settings, $field_settings, $widget_settings);
$field_name = 'field_image_instance_default';
$storage_settings['default_image'] = [
@ -339,7 +339,7 @@ class LayoutBuilderDefaultValuesTest extends BrowserTestBase {
$widget_settings = [
'preview_image_style' => 'medium',
];
$this->createImageField($field_name, 'test_node_type', $storage_settings, $field_settings, $widget_settings);
$this->createImageField($field_name, 'node', 'test_node_type', $storage_settings, $field_settings, $widget_settings);
$field_name = 'field_image_both_defaults';
$storage_settings['default_image'] = [
@ -359,7 +359,7 @@ class LayoutBuilderDefaultValuesTest extends BrowserTestBase {
$widget_settings = [
'preview_image_style' => 'medium',
];
$this->createImageField($field_name, 'test_node_type', $storage_settings, $field_settings, $widget_settings);
$this->createImageField($field_name, 'node', 'test_node_type', $storage_settings, $field_settings, $widget_settings);
$field_name = 'field_image_no_default';
$storage_settings = [];
@ -367,7 +367,7 @@ class LayoutBuilderDefaultValuesTest extends BrowserTestBase {
$widget_settings = [
'preview_image_style' => 'medium',
];
$this->createImageField($field_name, 'test_node_type', $storage_settings, $field_settings, $widget_settings);
$this->createImageField($field_name, 'node', 'test_node_type', $storage_settings, $field_settings, $widget_settings);
}
/**

View File

@ -193,7 +193,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
$renderer = $this->container->get('renderer');
$node_storage = $this->container->get('entity_type.manager')->getStorage('node');
$field_name = $this->randomMachineName();
$this->createImageField($field_name, 'article', ['uri_scheme' => $scheme]);
$this->createImageField($field_name, 'node', 'article', ['uri_scheme' => $scheme]);
// Create a new node with an image attached. Make sure we use a large image
// so the scale effects of the image styles always have an effect.
$test_image = current($this->getTestFiles('image', 39325));
@ -389,7 +389,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
->save();
$node_storage = $this->container->get('entity_type.manager')->getStorage('node');
$field_name = $this->randomMachineName();
$this->createImageField($field_name, 'article', ['uri_scheme' => 'public']);
$this->createImageField($field_name, 'node', 'article', ['uri_scheme' => 'public']);
// Create a new node with an image attached.
$test_image = current($this->getTestFiles('image'));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
@ -464,7 +464,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
->save();
$node_storage = $this->container->get('entity_type.manager')->getStorage('node');
$field_name = $this->randomMachineName();
$this->createImageField($field_name, 'article', ['uri_scheme' => 'public']);
$this->createImageField($field_name, 'node', 'article', ['uri_scheme' => 'public']);
// Create a new node with an image attached.
$test_image = current($this->getTestFiles('image'));
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
@ -520,7 +520,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
private function assertResponsiveImageFieldFormattersLink(string $link_type): void {
$field_name = $this->randomMachineName();
$field_settings = ['alt_field_required' => 0];
$this->createImageField($field_name, 'article', ['uri_scheme' => 'public'], $field_settings);
$this->createImageField($field_name, 'node', 'article', ['uri_scheme' => 'public'], $field_settings);
// Create a new node with an image attached.
$test_image = current($this->getTestFiles('image'));