function EntityViewsDataTest::setUp

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/Entity/EntityViewsDataTest.php \Drupal\Tests\views\Kernel\Entity\EntityViewsDataTest::setUp()
  2. 8.9.x core/modules/views/tests/src/Unit/EntityViewsDataTest.php \Drupal\Tests\views\Unit\EntityViewsDataTest::setUp()
  3. 11.x core/modules/views/tests/src/Kernel/Entity/EntityViewsDataTest.php \Drupal\Tests\views\Kernel\Entity\EntityViewsDataTest::setUp()

Overrides KernelTestBase::setUp

File

core/modules/views/tests/src/Kernel/Entity/EntityViewsDataTest.php, line 62

Class

EntityViewsDataTest
Tests entity views data.

Namespace

Drupal\Tests\views\Kernel\Entity

Code

protected function setUp() : void {
    parent::setUp();
    $this->entityTypeManager = $this->container
        ->get('entity_type.manager');
    // A common entity type definition. Tests may change this prior to passing
    // it to setUpEntityType().
    $this->baseEntityType = new TestEntityType([
        // A normal entity type would have its class picked up during discovery,
        // but as we're mocking this without an annotation we have to specify it.
'class' => ViewsTestEntity::class,
        'base_table' => 'entity_test',
        'id' => 'entity_test',
        'label' => 'Entity test',
        'entity_keys' => [
            'uuid' => 'uuid',
            'id' => 'id',
            'langcode' => 'langcode',
            'bundle' => 'type',
            'revision' => 'revision_id',
        ],
        'handlers' => [
            'views_data' => EntityViewsData::class,
        ],
        'provider' => 'entity_test',
        'list_cache_contexts' => [
            'entity_test_list_cache_context',
        ],
    ]);
    // Base fields for the test entity types.
    $this->commonBaseFields['name'] = BaseFieldDefinition::create('string')->setLabel(t('Name'))
        ->setDescription(t('The name of the test entity.'))
        ->setTranslatable(TRUE)
        ->setSetting('max_length', 32);
    $this->commonBaseFields['created'] = BaseFieldDefinition::create('created')->setLabel(t('Authored on'))
        ->setDescription(t('Time the entity was created'))
        ->setTranslatable(TRUE);
    $this->commonBaseFields['user_id'] = BaseFieldDefinition::create('entity_reference')->setLabel(t('User ID'))
        ->setDescription(t('The ID of the associated user.'))
        ->setSetting('target_type', 'user')
        ->setSetting('handler', 'default')
        ->setDefaultValue([
        0 => [
            'target_id' => 1,
        ],
    ])
        ->setTranslatable(TRUE);
    // Add a description field. This example comes from the taxonomy Term
    // entity.
    $this->commonBaseFields['description'] = BaseFieldDefinition::create('text_long')->setLabel('Description')
        ->setDescription('A description of the term.')
        ->setTranslatable(TRUE);
    // Add a URL field; this example is from the Comment entity.
    $this->commonBaseFields['homepage'] = BaseFieldDefinition::create('uri')->setLabel('Homepage')
        ->setDescription("The comment author's home page address.")
        ->setTranslatable(TRUE)
        ->setSetting('max_length', 255);
    // A base field with cardinality > 1
    $this->commonBaseFields['string'] = BaseFieldDefinition::create('string')->setLabel('Strong')
        ->setTranslatable(TRUE)
        ->setCardinality(2);
    // Set up the basic 'entity_test' entity type. This is used by several
    // tests; others customize it and the base fields.
    $this->setUpEntityType($this->baseEntityType, $this->commonBaseFields);
}

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