annotate core/modules/jsonapi/tests/src/Kernel/JsonapiKernelTestBase.php @ 19:fa3358dc1485 tip

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