You are here

public function EntityDisplayTest::setUp in Field Group 8

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/EntityDisplayTest.php \Drupal\Tests\field_group\Functional\EntityDisplayTest::setUp()

Overrides BrowserTestBase::setUp

File

tests/src/Functional/EntityDisplayTest.php, line 46

Class

EntityDisplayTest
Tests for displaying entities.

Namespace

Drupal\Tests\field_group\Functional

Code

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

  // Create test user.
  $admin_user = $this
    ->drupalCreateUser([
    'access content',
    'administer content types',
    'administer node fields',
    'administer node form display',
    'administer node display',
    'bypass node access',
  ]);
  $this
    ->drupalLogin($admin_user);

  // Create content type, with underscores.
  $type_name = strtolower($this
    ->randomMachineName(8)) . '_test';
  $type = $this
    ->drupalCreateContentType(array(
    'name' => $type_name,
    'type' => $type_name,
  ));
  $this->type = $type
    ->id();

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
  $display = \Drupal::entityTypeManager()
    ->getStorage('entity_view_display')
    ->load('node' . '.' . $type_name . '.' . 'default');

  // Create a node.
  $node_values = array(
    'type' => $type_name,
  );

  // Create test fields.
  foreach ([
    'field_test',
    'field_test_2',
    'field_no_access',
  ] as $field_name) {
    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'node',
      'type' => 'test_field',
    ]);
    $field_storage
      ->save();
    $instance = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => $type_name,
      'label' => $this
        ->randomMachineName(),
    ]);
    $instance
      ->save();

    // Assign a test value for the field.
    $node_values[$field_name][0]['value'] = mt_rand(1, 127);

    // Set the field visible on the display object.
    $display_options = array(
      'label' => 'above',
      'type' => 'field_test_default',
      'settings' => array(
        'test_formatter_setting' => $this
          ->randomMachineName(),
      ),
    );
    $display
      ->setComponent($field_name, $display_options);
  }

  // Save display + create node.
  $display
    ->save();
  $this->node = $this
    ->drupalCreateNode($node_values);
}