function PagePreviewTest::setUp

Same name in other branches
  1. 9 core/modules/node/tests/src/Functional/PagePreviewTest.php \Drupal\Tests\node\Functional\PagePreviewTest::setUp()
  2. 8.9.x core/modules/node/tests/src/Functional/PagePreviewTest.php \Drupal\Tests\node\Functional\PagePreviewTest::setUp()
  3. 11.x core/modules/node/tests/src/Functional/PagePreviewTest.php \Drupal\Tests\node\Functional\PagePreviewTest::setUp()

Overrides NodeTestBase::setUp

File

core/modules/node/tests/src/Functional/PagePreviewTest.php, line 73

Class

PagePreviewTest
Tests the node entity preview functionality.

Namespace

Drupal\Tests\node\Functional

Code

protected function setUp() : void {
    parent::setUp();
    $this->addDefaultCommentField('node', 'page');
    $web_user = $this->drupalCreateUser([
        'edit own page content',
        'create page content',
        'administer menu',
    ]);
    $this->drupalLogin($web_user);
    // Add a vocabulary so we can test different view modes.
    $vocabulary = Vocabulary::create([
        'name' => $this->randomMachineName(),
        'description' => $this->randomMachineName(),
        'vid' => $this->randomMachineName(),
        'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
        'help' => '',
    ]);
    $vocabulary->save();
    // Add a term to the vocabulary.
    $term = Term::create([
        'name' => $this->randomMachineName(),
        'description' => $this->randomMachineName(),
        'vid' => $vocabulary->id(),
        'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $term->save();
    $this->term = $term;
    // Create an image field.
    FieldStorageConfig::create([
        'field_name' => 'field_image',
        'entity_type' => 'node',
        'type' => 'image',
        'settings' => [],
        'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
    ])->save();
    $field_config = FieldConfig::create([
        'field_name' => 'field_image',
        'label' => 'Images',
        'entity_type' => 'node',
        'bundle' => 'page',
        'required' => FALSE,
        'settings' => [],
    ]);
    $field_config->save();
    // Create a field.
    $this->fieldName = $this->randomMachineName();
    $handler_settings = [
        'target_bundles' => [
            $vocabulary->id() => $vocabulary->id(),
        ],
        'auto_create' => TRUE,
    ];
    $this->createEntityReferenceField('node', 'page', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getFormDisplay('node', 'page')
        ->setComponent($this->fieldName, [
        'type' => 'entity_reference_autocomplete_tags',
    ])
        ->save();
    // Show on default display and teaser.
    $display_repository->getViewDisplay('node', 'page')
        ->setComponent($this->fieldName, [
        'type' => 'entity_reference_label',
    ])
        ->save();
    $display_repository->getViewDisplay('node', 'page', 'teaser')
        ->setComponent($this->fieldName, [
        'type' => 'entity_reference_label',
    ])
        ->save();
    $display_repository->getFormDisplay('node', 'page')
        ->setComponent('field_image', [
        'type' => 'image_image',
        'settings' => [],
    ])
        ->save();
    $display_repository->getViewDisplay('node', 'page')
        ->setComponent('field_image')
        ->save();
    // Create a multi-value text field.
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'field_test_multi',
        'entity_type' => 'node',
        'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
        'type' => 'text',
        'settings' => [
            'max_length' => 50,
        ],
    ]);
    $field_storage->save();
    FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'page',
    ])->save();
    $display_repository->getFormDisplay('node', 'page')
        ->setComponent('field_test_multi', [
        'type' => 'text_textfield',
    ])
        ->save();
    $display_repository->getViewDisplay('node', 'page')
        ->setComponent('field_test_multi', [
        'type' => 'string',
    ])
        ->save();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.