You are here

public function EntityViewsDataTest::testBaseTableFields in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Unit/EntityViewsDataTest.php \Drupal\Tests\views\Unit\EntityViewsDataTest::testBaseTableFields()

Tests fields on the base table.

File

core/modules/views/tests/src/Unit/EntityViewsDataTest.php, line 408
Contains \Drupal\Tests\views\Unit\EntityViewsDataTest.

Class

EntityViewsDataTest
@coversDefaultClass \Drupal\views\EntityViewsData @group Views

Namespace

Drupal\Tests\views\Unit

Code

public function testBaseTableFields() {
  $base_field_definitions = $this
    ->setupBaseFields(EntityTest::baseFieldDefinitions($this->baseEntityType));
  $user_base_field_definitions = [
    'uid' => BaseFieldDefinition::create('integer')
      ->setLabel(t('ID'))
      ->setDescription(t('The ID of the user entity.'))
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE),
  ];
  $this->entityManager
    ->expects($this
    ->any())
    ->method('getBaseFieldDefinitions')
    ->will($this
    ->returnValueMap([
    [
      'user',
      $user_base_field_definitions,
    ],
    [
      'entity_test',
      $base_field_definitions,
    ],
  ]));

  // Setup the table mapping.
  $table_mapping = $this
    ->getMock('Drupal\\Core\\Entity\\Sql\\TableMappingInterface');
  $table_mapping
    ->expects($this
    ->any())
    ->method('getTableNames')
    ->willReturn([
    'entity_test',
  ]);
  $table_mapping
    ->expects($this
    ->any())
    ->method('getColumnNames')
    ->willReturnMap([
    [
      'id',
      [
        'value' => 'id',
      ],
    ],
    [
      'uuid',
      [
        'value' => 'uuid',
      ],
    ],
    [
      'type',
      [
        'value' => 'type',
      ],
    ],
    [
      'langcode',
      [
        'value' => 'langcode',
      ],
    ],
    [
      'name',
      [
        'value' => 'name',
      ],
    ],
    [
      'description',
      [
        'value' => 'description__value',
        'format' => 'description__format',
      ],
    ],
    [
      'homepage',
      [
        'value' => 'homepage',
      ],
    ],
    [
      'user_id',
      [
        'target_id' => 'user_id',
      ],
    ],
  ]);
  $table_mapping
    ->expects($this
    ->any())
    ->method('getFieldNames')
    ->willReturnMap([
    [
      'entity_test',
      [
        'id',
        'uuid',
        'type',
        'langcode',
        'name',
        'description',
        'homepage',
        'user_id',
      ],
    ],
  ]);
  $this->entityStorage
    ->expects($this
    ->once())
    ->method('getTableMapping')
    ->willReturn($table_mapping);
  $this
    ->setupFieldStorageDefinition();
  $data = $this->viewsData
    ->getViewsData();
  $this
    ->assertNumericField($data['entity_test']['id']);
  $this
    ->assertField($data['entity_test']['id'], 'id');
  $this
    ->assertUuidField($data['entity_test']['uuid']);
  $this
    ->assertField($data['entity_test']['uuid'], 'uuid');
  $this
    ->assertStringField($data['entity_test']['type']);
  $this
    ->assertEquals('type', $data['entity_test']['type']['entity field']);
  $this
    ->assertLanguageField($data['entity_test']['langcode']);
  $this
    ->assertField($data['entity_test']['langcode'], 'langcode');
  $this
    ->assertEquals('Original language', $data['entity_test']['langcode']['title']);
  $this
    ->assertStringField($data['entity_test']['name']);
  $this
    ->assertField($data['entity_test']['name'], 'name');
  $this
    ->assertLongTextField($data['entity_test'], 'description');
  $this
    ->assertField($data['entity_test']['description__value'], 'description');
  $this
    ->assertField($data['entity_test']['description__format'], 'description');
  $this
    ->assertUriField($data['entity_test']['homepage']);
  $this
    ->assertField($data['entity_test']['homepage'], 'homepage');
  $this
    ->assertEntityReferenceField($data['entity_test']['user_id']);
  $this
    ->assertField($data['entity_test']['user_id'], 'user_id');
  $relationship = $data['entity_test']['user_id']['relationship'];
  $this
    ->assertEquals('users_field_data', $relationship['base']);
  $this
    ->assertEquals('uid', $relationship['base field']);
}