You are here

protected function FieldDefaultsTestBase::createField in Field Defaults 8

Creates a field on a content entity.

2 calls to FieldDefaultsTestBase::createField()
BooleanTest::testFieldBoolean in src/Tests/BooleanTest.php
Test updating a boolean.
StringTest::testFieldString in src/Tests/StringTest.php
Test updating a boolean.

File

src/Tests/FieldDefaultsTestBase.php, line 84

Class

FieldDefaultsTestBase
Class FieldDefaultsTestBase.

Namespace

Drupal\field_defaults\Tests

Code

protected function createField($type = 'boolean', $cardinality = '1', $contentType = 'page') {
  $this
    ->drupalGet('admin/structure/types/manage/' . $contentType . '/fields');

  // Go to the 'Add field' page.
  $this
    ->clickLink('Add field');

  // Make a name for this field.
  $field_name = strtolower($this
    ->randomMachineName(10));

  // Fill out the field form.
  $edit = [
    'new_storage_type' => $type,
    'field_name' => $field_name,
    'label' => $field_name,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save and continue'));

  // Fill out the $cardinality form as if we're not using an unlimited values.
  $edit = [
    'cardinality' => 'number',
    'cardinality_number' => (string) $cardinality,
  ];

  // -1 for $cardinality, we should change to 'Unlimited'.
  if (-1 == $cardinality) {
    $edit = [
      'cardinality' => '-1',
      'cardinality_number' => '1',
    ];
  }

  // And now we save the cardinality settings.
  $this
    ->drupalPostForm(NULL, $edit, t('Save field settings'));
  $this
    ->assertText(t('Updated field @name field settings.', [
    '@name' => $field_name,
  ]));

  // Save.
  $this
    ->drupalPostForm(NULL, [], t('Save settings'));
  $this
    ->assertText(t('Saved @name configuration.', [
    '@name' => $field_name,
  ]));
  return $field_name;
}