You are here

trait DevelGenerateSetupTrait in Devel 8.2

Same name and namespace in other branches
  1. 8.3 devel_generate/tests/src/Traits/DevelGenerateSetupTrait.php \Drupal\Tests\devel_generate\Traits\DevelGenerateSetupTrait
  2. 4.x devel_generate/tests/src/Traits/DevelGenerateSetupTrait.php \Drupal\Tests\devel_generate\Traits\DevelGenerateSetupTrait

Hierarchy

2 files declare their use of DevelGenerateSetupTrait
DevelGenerateBrowserTest.php in devel_generate/tests/src/Functional/DevelGenerateBrowserTest.php
DevelGenerateCommandsTest.php in devel_generate/tests/src/Functional/DevelGenerateCommandsTest.php

File

devel_generate/tests/src/Traits/DevelGenerateSetupTrait.php, line 11

Namespace

Drupal\Tests\devel_generate\Traits
View source
trait DevelGenerateSetupTrait {
  use CommentTestTrait;
  use EntityReferenceTestTrait;

  /**
   * Vocabulary for testing.
   *
   * @var \Drupal\taxonomy\VocabularyInterface
   */
  protected $vocabulary;
  public function setUpData() {
    $entity_type_manager = $this->container
      ->get('entity_type.manager');

    // Create Basic page and Article node types.
    if ($this->profile != 'standard') {
      $this
        ->drupalCreateContentType(array(
        'type' => 'page',
        'name' => 'Basic Page',
      ));
      $this
        ->drupalCreateContentType(array(
        'type' => 'article',
        'name' => 'Article',
      ));
      $this
        ->addDefaultCommentField('node', 'article');
    }

    // Creating a vocabulary to associate taxonomy terms generated.
    $this->vocabulary = Vocabulary::create([
      'name' => $this
        ->randomMachineName(),
      'description' => $this
        ->randomMachineName(),
      'vid' => mb_strtolower($this
        ->randomMachineName()),
      'langcode' => Language::LANGCODE_NOT_SPECIFIED,
      'weight' => mt_rand(0, 10),
    ]);
    $this->vocabulary
      ->save();

    // Creates a field of an entity reference field storage on article.
    $field_name = 'taxonomy_' . $this->vocabulary
      ->id();
    $handler_settings = array(
      'target_bundles' => array(
        $this->vocabulary
          ->id() => $this->vocabulary
          ->id(),
      ),
      'auto_create' => TRUE,
    );
    $this
      ->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $entity_type_manager
      ->getStorage('entity_form_display')
      ->load('node.article.default')
      ->setComponent($field_name, array(
      'type' => 'options_select',
    ))
      ->save();
    $entity_type_manager
      ->getStorage('entity_view_display')
      ->load('node.article.default')
      ->setComponent($field_name, array(
      'type' => 'entity_reference_label',
    ))
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommentTestTrait::addDefaultCommentField public function Adds the default comment field to an entity.
DevelGenerateSetupTrait::$vocabulary protected property Vocabulary for testing.
DevelGenerateSetupTrait::setUpData public function
EntityReferenceTestTrait::createEntityReferenceField protected function Creates a field of an entity reference field storage on the specified bundle.