summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-10-03 14:01:46 +0100
committerAlex Pott2016-10-03 14:01:46 +0100
commit66741e368b1bbb2ab2348a0028dbc9dc2b749f25 (patch)
tree1fe56b6e2ce660b6bbb15fb69800a8539d00bca0
parentdd6d5868185a8333f59b8d4aa919da3fc1e871ab (diff)
Issue #2782309 by samuel.mortenson, martin107: Refactor File and Image related image field creation logic into a new trait
-rw-r--r--core/modules/image/src/Tests/ImageFieldTestBase.php61
-rw-r--r--core/modules/image/tests/src/Kernel/ImageFieldCreationTrait.php70
2 files changed, 73 insertions, 58 deletions
diff --git a/core/modules/image/src/Tests/ImageFieldTestBase.php b/core/modules/image/src/Tests/ImageFieldTestBase.php
index 6081d32..bb976f9 100644
--- a/core/modules/image/src/Tests/ImageFieldTestBase.php
+++ b/core/modules/image/src/Tests/ImageFieldTestBase.php
@@ -2,9 +2,8 @@
namespace Drupal\image\Tests;
-use Drupal\field\Entity\FieldConfig;
+use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
use Drupal\simpletest\WebTestBase;
-use Drupal\field\Entity\FieldStorageConfig;
/**
* TODO: Test the following functions.
@@ -24,6 +23,8 @@ use Drupal\field\Entity\FieldStorageConfig;
*/
abstract class ImageFieldTestBase extends WebTestBase {
+ use ImageFieldCreationTrait;
+
/**
* Modules to enable.
*
@@ -52,62 +53,6 @@ abstract class ImageFieldTestBase extends WebTestBase {
}
/**
- * Create a new image field.
- *
- * @param string $name
- * The name of the new field (all lowercase), exclude the "field_" prefix.
- * @param string $type_name
- * The node type that this field will be added to.
- * @param array $storage_settings
- * A list of field storage settings that will be added to the defaults.
- * @param array $field_settings
- * A list of instance settings that will be added to the instance defaults.
- * @param array $widget_settings
- * Widget settings to be added to the widget defaults.
- * @param array $formatter_settings
- * Formatter settings to be added to the formatter defaults.
- * @param string $description
- * A description for the field.
- */
- function createImageField($name, $type_name, $storage_settings = array(), $field_settings = array(), $widget_settings = array(), $formatter_settings = array(), $description = '') {
- FieldStorageConfig::create(array(
- 'field_name' => $name,
- 'entity_type' => 'node',
- '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,
- 'required' => !empty($field_settings['required']),
- 'settings' => $field_settings,
- 'description' => $description,
- ]);
- $field_config->save();
-
- entity_get_form_display('node', $type_name, 'default')
- ->setComponent($name, array(
- 'type' => 'image_image',
- 'settings' => $widget_settings,
- ))
- ->save();
-
- entity_get_display('node', $type_name, 'default')
- ->setComponent($name, array(
- 'type' => 'image',
- 'settings' => $formatter_settings,
- ))
- ->save();
-
- return $field_config;
-
- }
-
- /**
* Preview an image in a node.
*
* @param \Drupal\Core\Image\ImageInterface $image
diff --git a/core/modules/image/tests/src/Kernel/ImageFieldCreationTrait.php b/core/modules/image/tests/src/Kernel/ImageFieldCreationTrait.php
new file mode 100644
index 0000000..a8fe589
--- /dev/null
+++ b/core/modules/image/tests/src/Kernel/ImageFieldCreationTrait.php
@@ -0,0 +1,70 @@
+<?php
+
+namespace Drupal\Tests\image\Kernel;
+
+use Drupal\field\Entity\FieldConfig;
+use Drupal\field\Entity\FieldStorageConfig;
+
+/**
+ * Provides a helper method for creating Image fields.
+ */
+trait ImageFieldCreationTrait {
+
+ /**
+ * Create a new image field.
+ *
+ * @param string $name
+ * The name of the new field (all lowercase), exclude the "field_" prefix.
+ * @param string $type_name
+ * The node type that this field will be added to.
+ * @param array $storage_settings
+ * (optional) A list of field storage settings that will be added to the
+ * defaults.
+ * @param array $field_settings
+ * (optional) A list of instance settings that will be added to the instance
+ * defaults.
+ * @param array $widget_settings
+ * (optional) Widget settings to be added to the widget defaults.
+ * @param array $formatter_settings
+ * (optional) Formatter settings to be added to the formatter defaults.
+ * @param string $description
+ * (optional) A description for the field. Defaults to ''.
+ */
+ protected function createImageField($name, $type_name, $storage_settings = array(), $field_settings = array(), $widget_settings = array(), $formatter_settings = array(), $description = '') {
+ FieldStorageConfig::create(array(
+ 'field_name' => $name,
+ 'entity_type' => 'node',
+ '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,
+ 'required' => !empty($field_settings['required']),
+ 'settings' => $field_settings,
+ 'description' => $description,
+ ]);
+ $field_config->save();
+
+ entity_get_form_display('node', $type_name, 'default')
+ ->setComponent($name, array(
+ 'type' => 'image_image',
+ 'settings' => $widget_settings,
+ ))
+ ->save();
+
+ entity_get_display('node', $type_name, 'default')
+ ->setComponent($name, array(
+ 'type' => 'image',
+ 'settings' => $formatter_settings,
+ ))
+ ->save();
+
+ return $field_config;
+ }
+
+}