You are here

protected function FieldExampleBrowserTestBase::setUp in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/field_example/tests/src/Functional/FieldExampleBrowserTestBase.php \Drupal\Tests\field_example\Functional\FieldExampleBrowserTestBase::setUp()

Once installed, a content type with the desired field is created.

Overrides ExamplesBrowserTestBase::setUp

File

field_example/tests/src/Functional/FieldExampleBrowserTestBase.php, line 61

Class

FieldExampleBrowserTestBase
Class FieldExampleBrowserTestBase.

Namespace

Drupal\Tests\field_example\Functional

Code

protected function setUp() {

  // Install Drupal.
  parent::setUp();

  // Create and login a user that creates the content type.
  $permissions = [
    'administer content types',
    'administer node fields',
    'administer node form display',
    'administer node display',
  ];
  $this->administratorAccount = $this
    ->drupalCreateUser($permissions);
  parent::drupalLogin($this->administratorAccount);

  // Prepare a new content type where the field will be added.
  $this->contentTypeName = strtolower($this
    ->randomMachineName(10));
  $this
    ->drupalGet('admin/structure/types/add');
  $edit = [
    'name' => $this->contentTypeName,
    'type' => $this->contentTypeName,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save and manage fields');
  $this
    ->assertText((string) new FormattableMarkup('The content type @name has been added.', [
    '@name' => $this->contentTypeName,
  ]));

  // Reset the permission cache.
  $create_permission = 'create ' . $this->contentTypeName . ' content';
  $this
    ->checkPermissions([
    $create_permission,
  ]);

  // Now that we have a new content type, create a user that has privileges
  // on the content type.
  $this->authorAccount = $this
    ->drupalCreateUser([
    $create_permission,
  ]);
}