comparison core/modules/quickedit/tests/src/Kernel/QuickEditTestBase.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\Tests\quickedit\Kernel;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\field\Entity\FieldStorageConfig;
8
9 /**
10 * Base class for testing Quick Edit functionality.
11 */
12 abstract class QuickEditTestBase extends KernelTestBase {
13
14 /**
15 * Modules to enable.
16 *
17 * @var array
18 */
19 public static $modules = ['system', 'entity_test', 'field', 'field_test', 'filter', 'user', 'text', 'quickedit'];
20
21 /**
22 * Bag of created fields.
23 *
24 * Allows easy access to test field names/IDs/objects via:
25 * - $this->fields->{$field_name}_field_storage
26 * - $this->fields->{$field_name}_instance
27 *
28 * @see \Drupal\quickedit\Tests\QuickEditTestBase::createFieldWithStorage()
29 *
30 * @var \ArrayObject
31 */
32 protected $fields;
33
34 /**
35 * Sets the default field storage backend for fields created during tests.
36 */
37 protected function setUp() {
38 parent::setUp();
39
40 $this->fields = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS);
41
42 $this->installEntitySchema('user');
43 $this->installEntitySchema('entity_test');
44 $this->installConfig(['field', 'filter']);
45 }
46
47 /**
48 * Creates a field.
49 *
50 * @param string $field_name
51 * The field name.
52 * @param string $type
53 * The field type.
54 * @param int $cardinality
55 * The field's cardinality.
56 * @param string $label
57 * The field's label (used everywhere: widget label, formatter label).
58 * @param array $field_settings
59 * @param string $widget_type
60 * The widget type.
61 * @param array $widget_settings
62 * The widget settings.
63 * @param string $formatter_type
64 * The formatter type.
65 * @param array $formatter_settings
66 * The formatter settings.
67 */
68 protected function createFieldWithStorage($field_name, $type, $cardinality, $label, $field_settings, $widget_type, $widget_settings, $formatter_type, $formatter_settings) {
69 $field_storage = $field_name . '_field_storage';
70 $this->fields->$field_storage = FieldStorageConfig::create([
71 'field_name' => $field_name,
72 'entity_type' => 'entity_test',
73 'type' => $type,
74 'cardinality' => $cardinality,
75 ]);
76 $this->fields->$field_storage->save();
77
78 $field = $field_name . '_field';
79 $this->fields->$field = FieldConfig::create([
80 'field_storage' => $this->fields->$field_storage,
81 'bundle' => 'entity_test',
82 'label' => $label,
83 'description' => $label,
84 'weight' => mt_rand(0, 127),
85 'settings' => $field_settings,
86 ]);
87 $this->fields->$field->save();
88
89 entity_get_form_display('entity_test', 'entity_test', 'default')
90 ->setComponent($field_name, [
91 'type' => $widget_type,
92 'settings' => $widget_settings,
93 ])
94 ->save();
95
96 entity_get_display('entity_test', 'entity_test', 'default')
97 ->setComponent($field_name, [
98 'label' => 'above',
99 'type' => $formatter_type,
100 'settings' => $formatter_settings
101 ])
102 ->save();
103 }
104
105 }