You are here

protected function ConditionalFieldRadiosTest::setUp in Conditional Fields 4.x

Same name and namespace in other branches
  1. 8 tests/src/FunctionalJavascript/ConditionalFieldRadiosTest.php \Drupal\Tests\conditional_fields\FunctionalJavascript\ConditionalFieldRadiosTest::setUp()

Overrides ConditionalFieldTestBase::setUp

File

tests/src/FunctionalJavascript/ConditionalFieldRadiosTest.php, line 53

Class

ConditionalFieldRadiosTest
Test Conditional Fields States.

Namespace

Drupal\Tests\conditional_fields\FunctionalJavascript

Code

protected function setUp() {
  parent::setUp();

  // Create a vocabulary with random name.
  $this->taxonomyName = $this
    ->getRandomGenerator()
    ->word(8);
  $vocabulary = Vocabulary::create([
    'name' => $this->taxonomyName,
    'vid' => $this->taxonomyName,
  ]);
  $vocabulary
    ->save();

  // Create a random taxonomy terms for vocabulary.
  $this->termsCount = mt_rand(3, 5);
  for ($i = 1; $i <= $this->termsCount; $i++) {
    $termName = $this
      ->getRandomGenerator()
      ->word(8);
    Term::create([
      'parent' => [],
      'name' => $termName,
      'vid' => $this->taxonomyName,
    ])
      ->save();
  }

  // Add a custom field with taxonomy terms to 'Article'.
  // The field label is a machine name of created vocabulary.
  $handler_settings = [
    'target_bundles' => [
      $vocabulary
        ->id() => $vocabulary
        ->id(),
    ],
  ];
  $this
    ->createEntityReferenceField('node', 'article', 'field_' . $this->taxonomyName, $this->taxonomyName, 'taxonomy_term', 'default', $handler_settings);
  EntityFormDisplay::load('node.article.default')
    ->setComponent('field_' . $this->taxonomyName, [
    'type' => 'options_buttons',
  ])
    ->save();
}