View source  
  <?php
namespace Drupal\Tests\social_simple\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
abstract class TestSocialSimpleTestBase extends BrowserTestBase {
  use TaxonomyTestTrait;
  use EntityReferenceTestTrait;
  
  public static $modules = [
    'system',
    'user',
    'block',
    'taxonomy',
    'node',
    'field',
    'field_ui',
    'taxonomy',
    'social_simple',
  ];
  
  protected $adminUser;
  
  protected $advancedUser;
  
  protected $normalUser;
  
  protected $viewDisplay;
  
  protected $formDisplay;
  
  protected $article;
  
  protected $vocabulary;
  
  protected $term1;
  
  protected $term2;
  
  protected function setUp() {
    parent::setUp();
    
    $this->vocabulary = $this
      ->createVocabulary();
    $this->term1 = $this
      ->createTerm($this->vocabulary);
    $this->term2 = $this
      ->createTerm($this->vocabulary);
    if ($this->profile != 'standard') {
      $this
        ->createContentType([
        'type' => 'article',
        'name' => 'Article',
      ]);
      $field_name = 'field_tags';
      $handler_settings = [
        'target_bundles' => [
          $this->vocabulary
            ->id() => $this->vocabulary
            ->id(),
        ],
        'auto_create' => TRUE,
      ];
      $this
        ->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
      $this
        ->setComponentFormDisplay('node.article.default', 'node', 'article', $field_name, 'entity_reference_autocomplete', []);
    }
    $title = 'Article1';
    $values = [
      'type' => 'article',
      'title' => $title,
      'body' => [
        'value' => 'Content body for ' . $title,
      ],
    ];
    $this->article = $this
      ->createNode($values);
  }
  
  protected function setComponentViewDisplay($form_display_id, $entity_type, $bundle, $mode, $field_name) {
    
    $this->viewDisplay = EntityViewDisplay::load($form_display_id);
    if (!$this->viewDisplay) {
      EntityViewDisplay::create([
        'targetEntityType' => $entity_type,
        'bundle' => $bundle,
        'mode' => $mode,
        'status' => TRUE,
      ])
        ->save();
      $this->viewDisplay = EntityViewDisplay::load($form_display_id);
    }
    if ($this->viewDisplay instanceof EntityViewDisplayInterface) {
      $this->viewDisplay
        ->setComponent($field_name)
        ->save();
    }
  }
  
  protected function removeComponentViewDisplay($form_display_id, $entity_type, $bundle, $mode, $field_name) {
    
    $this->viewDisplay = EntityViewDisplay::load($form_display_id);
    if (!$this->viewDisplay) {
      EntityViewDisplay::create([
        'targetEntityType' => $entity_type,
        'bundle' => $bundle,
        'mode' => $mode,
        'status' => TRUE,
      ])
        ->save();
      $this->viewDisplay = EntityViewDisplay::load($form_display_id);
    }
    if ($this->viewDisplay instanceof EntityViewDisplayInterface) {
      $this->viewDisplay
        ->removeComponent($field_name)
        ->save();
    }
  }
  
  protected function setComponentFormDisplay($form_display_id, $entity_type, $bundle, $field_name, $widget_id, $settings, $mode = 'default') {
    
    $this->formDisplay = EntityFormDisplay::load($form_display_id);
    if (!$this->formDisplay) {
      EntityFormDisplay::create([
        'targetEntityType' => $entity_type,
        'bundle' => $bundle,
        'mode' => $mode,
        'status' => TRUE,
      ])
        ->save();
      $this->formDisplay = EntityFormDisplay::load($form_display_id);
    }
    if ($this->formDisplay instanceof EntityFormDisplayInterface) {
      $this->formDisplay
        ->setComponent($field_name, [
        'type' => $widget_id,
        'settings' => $settings,
      ])
        ->save();
    }
  }
}