comparison core/modules/jsonapi/tests/src/Kernel/JsonapiKernelTestBase.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php
2
3 namespace Drupal\Tests\jsonapi\Kernel;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10 * Contains shared test utility methods.
11 *
12 * @internal
13 */
14 abstract class JsonapiKernelTestBase extends KernelTestBase {
15
16 /**
17 * {@inheritdoc}
18 */
19 public static $modules = ['jsonapi'];
20
21 /**
22 * Creates a field of an entity reference field storage on the bundle.
23 *
24 * @param string $entity_type
25 * The type of entity the field will be attached to.
26 * @param string $bundle
27 * The bundle name of the entity the field will be attached to.
28 * @param string $field_name
29 * The name of the field; if it exists, a new instance of the existing.
30 * field will be created.
31 * @param string $field_label
32 * The label of the field.
33 * @param string $target_entity_type
34 * The type of the referenced entity.
35 * @param string $selection_handler
36 * The selection handler used by this field.
37 * @param array $handler_settings
38 * An array of settings supported by the selection handler specified above.
39 * (e.g. 'target_bundles', 'sort', 'auto_create', etc).
40 * @param int $cardinality
41 * The cardinality of the field.
42 *
43 * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
44 */
45 protected function createEntityReferenceField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', array $handler_settings = [], $cardinality = 1) {
46 // Look for or add the specified field to the requested entity bundle.
47 if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
48 FieldStorageConfig::create([
49 'field_name' => $field_name,
50 'type' => 'entity_reference',
51 'entity_type' => $entity_type,
52 'cardinality' => $cardinality,
53 'settings' => [
54 'target_type' => $target_entity_type,
55 ],
56 ])->save();
57 }
58 if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
59 FieldConfig::create([
60 'field_name' => $field_name,
61 'entity_type' => $entity_type,
62 'bundle' => $bundle,
63 'label' => $field_label,
64 'settings' => [
65 'handler' => $selection_handler,
66 'handler_settings' => $handler_settings,
67 ],
68 ])->save();
69 }
70 }
71
72 /**
73 * Creates a field of an entity reference field storage on the bundle.
74 *
75 * @param string $entity_type
76 * The type of entity the field will be attached to.
77 * @param string $bundle
78 * The bundle name of the entity the field will be attached to.
79 * @param string $field_name
80 * The name of the field; if it exists, a new instance of the existing.
81 * field will be created.
82 * @param string $field_label
83 * The label of the field.
84 * @param int $cardinality
85 * The cardinality of the field.
86 *
87 * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
88 */
89 protected function createTextField($entity_type, $bundle, $field_name, $field_label, $cardinality = 1) {
90 // Look for or add the specified field to the requested entity bundle.
91 if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
92 FieldStorageConfig::create([
93 'field_name' => $field_name,
94 'type' => 'text',
95 'entity_type' => $entity_type,
96 'cardinality' => $cardinality,
97 ])->save();
98 }
99 if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
100 FieldConfig::create([
101 'field_name' => $field_name,
102 'entity_type' => $entity_type,
103 'bundle' => $bundle,
104 'label' => $field_label,
105 ])->save();
106 }
107 }
108
109 }