You are here

public function EntityViewsDataTest::testBaseTableFields in Drupal 9

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 471
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('ID')
      ->setDescription('The ID of the user entity.')
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE),
  ];
  $this->entityFieldManager
    ->expects($this
    ->any())
    ->method('getBaseFieldDefinitions')
    ->will($this
    ->returnValueMap([
    [
      'user',
      $user_base_field_definitions,
    ],
    [
      'entity_test',
      $base_field_definitions,
    ],
  ]));
  $this->entityFieldManager
    ->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
    ->getMockBuilder(DefaultTableMapping::class)
    ->disableOriginalConstructor()
    ->getMock();
  $table_mapping
    ->expects($this
    ->any())
    ->method('getTableNames')
    ->willReturn([
    'entity_test',
    'entity_test__string',
  ]);
  $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',
      ],
    ],
    [
      'string',
      [
        'value' => 'string_value',
      ],
    ],
  ]);
  $table_mapping
    ->expects($this
    ->any())
    ->method('getFieldNames')
    ->willReturnMap([
    [
      'entity_test',
      [
        'id',
        'uuid',
        'type',
        'langcode',
        'name',
        'description',
        'homepage',
        'user_id',
      ],
    ],
    [
      'entity_test__string',
      [
        'string',
      ],
    ],
  ]);
  $table_mapping
    ->expects($this
    ->any())
    ->method('requiresDedicatedTableStorage')
    ->willReturnCallback(function (BaseFieldDefinition $base_field) {
    return $base_field
      ->getName() === 'string';
  });
  $table_mapping
    ->expects($this
    ->any())
    ->method('getDedicatedDataTableName')
    ->willReturnCallback(function (BaseFieldDefinition $base_field) {
    if ($base_field
      ->getName() === 'string') {
      return 'entity_test__string';
    }
  });
  $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']);

  // The string field name should be used as the 'entity field' but the actual
  // field should reflect what the column mapping is using for multi-value
  // base fields NOT just the field name. The actual column name returned from
  // mappings in the test mocks is 'value'.
  $this
    ->assertStringField($data['entity_test__string']['string_value']);
  $this
    ->assertField($data['entity_test__string']['string_value'], 'string');
  $this
    ->assertEquals([
    'left_field' => 'id',
    'field' => 'entity_id',
    'extra' => [
      [
        'field' => 'deleted',
        'value' => 0,
        'numeric' => TRUE,
      ],
    ],
  ], $data['entity_test__string']['table']['join']['entity_test']);
}