You are here

public function FieldTest::setUp in Token 8

Overrides KernelTestBase::setUp

File

tests/src/Kernel/FieldTest.php, line 54

Class

FieldTest
Tests field tokens.

Namespace

Drupal\Tests\token\Kernel

Code

public function setUp() {
  parent::setUp();
  $this
    ->installEntitySchema('user');
  $this
    ->installEntitySchema('node');
  $this
    ->installEntitySchema('taxonomy_term');

  // Create the article content type with a text field.
  $node_type = NodeType::create([
    'type' => 'article',
  ]);
  $node_type
    ->save();
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'test_field',
    'entity_type' => 'node',
    'type' => 'text',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_name' => 'test_field',
    'entity_type' => 'node',
    'bundle' => 'article',
    'label' => 'Test field',
  ]);
  $field
    ->save();

  // Create a reference field with the same name on user.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'test_field',
    'entity_type' => 'user',
    'type' => 'entity_reference',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_name' => 'test_field',
    'entity_type' => 'user',
    'bundle' => 'user',
    'label' => 'Test field',
  ]);
  $field
    ->save();
  $this->testFormat = FilterFormat::create([
    'format' => 'test',
    'weight' => 1,
    'filters' => [
      'filter_html_escape' => [
        'status' => TRUE,
      ],
    ],
  ]);
  $this->testFormat
    ->save();

  // Create a multi-value list_string field.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'test_list',
    'entity_type' => 'node',
    'type' => 'list_string',
    'cardinality' => 2,
    'settings' => [
      'allowed_values' => [
        'key1' => 'value1',
        'key2' => 'value2',
      ],
    ],
  ]);
  $field_storage
    ->save();
  $this->field = FieldConfig::create([
    'field_name' => 'test_list',
    'entity_type' => 'node',
    'bundle' => 'article',
  ])
    ->save();

  // Add an untranslatable node reference field.
  FieldStorageConfig::create([
    'field_name' => 'test_reference',
    'type' => 'entity_reference',
    'entity_type' => 'node',
    'settings' => [
      'target_type' => 'node',
    ],
    'translatable' => FALSE,
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'test_reference',
    'entity_type' => 'node',
    'bundle' => 'article',
    'label' => 'Test reference',
  ])
    ->save();

  // Add an untranslatable taxonomy term reference field.
  $this->vocabulary = $this
    ->createVocabulary();
  FieldStorageConfig::create([
    'field_name' => 'test_term_reference',
    'type' => 'entity_reference',
    'entity_type' => 'node',
    'settings' => [
      'target_type' => 'taxonomy_term',
    ],
    'translatable' => FALSE,
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'test_term_reference',
    'entity_type' => 'node',
    'bundle' => 'article',
    'label' => 'Test term reference',
    'settings' => [
      'handler' => 'default:taxonomy_term',
      'handler_settings' => [
        'target_bundles' => [
          $this->vocabulary
            ->id() => $this->vocabulary
            ->id(),
        ],
      ],
    ],
  ])
    ->save();

  // Add a field to terms of the created vocabulary.
  $storage = FieldStorageConfig::create([
    'field_name' => 'term_field',
    'entity_type' => 'taxonomy_term',
    'type' => 'text',
  ]);
  $storage
    ->save();
  $field = FieldConfig::create([
    'field_name' => 'term_field',
    'entity_type' => 'taxonomy_term',
    'bundle' => $this->vocabulary
      ->id(),
  ]);
  $field
    ->save();

  // Add a second language.
  $language = ConfigurableLanguage::create([
    'id' => 'de',
    'label' => 'German',
  ]);
  $language
    ->save();

  // Add a datetime field.
  $field_datetime_storage = FieldStorageConfig::create([
    'field_name' => 'field_datetime',
    'type' => 'datetime',
    'entity_type' => 'node',
    'settings' => [
      'datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME,
    ],
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ]);
  $field_datetime_storage
    ->save();
  $field_datetime = FieldConfig::create([
    'field_storage' => $field_datetime_storage,
    'bundle' => 'article',
  ]);
  $field_datetime
    ->save();

  // Add a daterange field.
  $field_daterange_storage = FieldStorageConfig::create([
    'field_name' => 'field_daterange',
    'type' => 'daterange',
    'entity_type' => 'node',
    'settings' => [
      'datetime_type' => DateRangeItem::DATETIME_TYPE_DATETIME,
    ],
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ]);
  $field_daterange_storage
    ->save();
  $field_daterange = FieldConfig::create([
    'field_storage' => $field_daterange_storage,
    'bundle' => 'article',
  ]);
  $field_daterange
    ->save();

  // Add a timestamp field.
  $field_timestamp_storage = FieldStorageConfig::create([
    'field_name' => 'field_timestamp',
    'type' => 'timestamp',
    'entity_type' => 'node',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ]);
  $field_timestamp_storage
    ->save();
  $field_timestamp = FieldConfig::create([
    'field_storage' => $field_timestamp_storage,
    'bundle' => 'article',
  ]);
  $field_timestamp
    ->save();
}