You are here

public function EntityViewsDataTest::testDataTableFields 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::testDataTableFields()

Tests fields on the data table.

File

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

Class

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

Namespace

Drupal\Tests\views\Unit

Code

public function testDataTableFields() {
  $entity_type = $this->baseEntityType
    ->set('data_table', 'entity_test_mul_property_data')
    ->set('base_table', 'entity_test_mul')
    ->set('id', 'entity_test_mul')
    ->setKey('bundle', 'type');
  $base_field_definitions = $this
    ->setupBaseFields(EntityTestMul::baseFieldDefinitions($this->baseEntityType));
  $base_field_definitions['type'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel('entity test type')
    ->setSetting('target_type', 'entity_test_bundle')
    ->setTranslatable(TRUE);
  $base_field_definitions = $this
    ->setupBaseFields($base_field_definitions);
  $user_base_field_definitions = [
    'uid' => BaseFieldDefinition::create('integer')
      ->setLabel('ID')
      ->setDescription('The ID of the user entity.')
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE),
  ];
  $entity_test_type = new ConfigEntityType([
    'id' => 'entity_test_bundle',
  ]);
  $this->entityFieldManager
    ->expects($this
    ->any())
    ->method('getBaseFieldDefinitions')
    ->will($this
    ->returnValueMap([
    [
      'user',
      $user_base_field_definitions,
    ],
    [
      'entity_test_mul',
      $base_field_definitions,
    ],
  ]));
  $this->entityFieldManager
    ->expects($this
    ->any())
    ->method('getBaseFieldDefinitions')
    ->will($this
    ->returnValueMap([
    [
      'user',
      $user_base_field_definitions,
    ],
    [
      'entity_test_mul',
      $base_field_definitions,
    ],
  ]));
  $this->viewsData
    ->setEntityType($entity_type);

  // Setup the table mapping.
  $table_mapping = $this
    ->getMockBuilder(DefaultTableMapping::class)
    ->disableOriginalConstructor()
    ->getMock();
  $table_mapping
    ->expects($this
    ->any())
    ->method('getTableNames')
    ->willReturn([
    'entity_test_mul',
    'entity_test_mul_property_data',
    'entity_test_mul__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_mul',
      [
        'uuid',
      ],
    ],
    [
      'entity_test_mul_property_data',
      [
        'id',
        'type',
        'langcode',
        'name',
        'description',
        'homepage',
        'user_id',
      ],
    ],
    [
      'entity_test_mul__string',
      [
        'string',
      ],
    ],
  ]);
  $table_mapping
    ->expects($this
    ->any())
    ->method('getFieldTableName')
    ->willReturnCallback(function ($field) {
    if ($field == 'uuid') {
      return 'entity_test_mul';
    }
    return 'entity_test_mul_property_data';
  });
  $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_mul__string';
    }
  });
  $this->entityStorage
    ->expects($this
    ->once())
    ->method('getTableMapping')
    ->willReturn($table_mapping);
  $this
    ->setupFieldStorageDefinition();
  $user_entity_type = static::userEntityInfo();
  $this->entityTypeManager
    ->expects($this
    ->any())
    ->method('getDefinition')
    ->will($this
    ->returnValueMap([
    [
      'user',
      TRUE,
      $user_entity_type,
    ],
    [
      'entity_test_bundle',
      TRUE,
      $entity_test_type,
    ],
  ]));
  $data = $this->viewsData
    ->getViewsData();

  // Check the base fields.
  $this
    ->assertFalse(isset($data['entity_test_mul']['id']));
  $this
    ->assertFalse(isset($data['entity_test_mul']['type']));
  $this
    ->assertUuidField($data['entity_test_mul']['uuid']);
  $this
    ->assertField($data['entity_test_mul']['uuid'], 'uuid');
  $this
    ->assertFalse(isset($data['entity_test_mul']['type']['relationship']));

  // Also ensure that field_data only fields don't appear on the base table.
  $this
    ->assertFalse(isset($data['entity_test_mul']['name']));
  $this
    ->assertFalse(isset($data['entity_test_mul']['description']));
  $this
    ->assertFalse(isset($data['entity_test_mul']['description__value']));
  $this
    ->assertFalse(isset($data['entity_test_mul']['description__format']));
  $this
    ->assertFalse(isset($data['entity_test_mul']['user_id']));
  $this
    ->assertFalse(isset($data['entity_test_mul']['homepage']));

  // Check the data fields.
  $this
    ->assertNumericField($data['entity_test_mul_property_data']['id']);
  $this
    ->assertField($data['entity_test_mul_property_data']['id'], 'id');
  $this
    ->assertBundleField($data['entity_test_mul_property_data']['type']);
  $this
    ->assertField($data['entity_test_mul_property_data']['type'], 'type');
  $this
    ->assertLanguageField($data['entity_test_mul_property_data']['langcode']);
  $this
    ->assertField($data['entity_test_mul_property_data']['langcode'], 'langcode');
  $this
    ->assertEquals('Translation language', $data['entity_test_mul_property_data']['langcode']['title']);
  $this
    ->assertStringField($data['entity_test_mul_property_data']['name']);
  $this
    ->assertField($data['entity_test_mul_property_data']['name'], 'name');
  $this
    ->assertLongTextField($data['entity_test_mul_property_data'], 'description');
  $this
    ->assertField($data['entity_test_mul_property_data']['description__value'], 'description');
  $this
    ->assertField($data['entity_test_mul_property_data']['description__format'], 'description');
  $this
    ->assertUriField($data['entity_test_mul_property_data']['homepage']);
  $this
    ->assertField($data['entity_test_mul_property_data']['homepage'], 'homepage');
  $this
    ->assertEntityReferenceField($data['entity_test_mul_property_data']['user_id']);
  $this
    ->assertField($data['entity_test_mul_property_data']['user_id'], 'user_id');
  $relationship = $data['entity_test_mul_property_data']['user_id']['relationship'];
  $this
    ->assertEquals('users_field_data', $relationship['base']);
  $this
    ->assertEquals('uid', $relationship['base field']);
  $this
    ->assertStringField($data['entity_test_mul__string']['string_value']);
  $this
    ->assertField($data['entity_test_mul__string']['string_value'], 'string');
  $this
    ->assertEquals([
    'left_field' => 'id',
    'field' => 'entity_id',
    'extra' => [
      [
        'field' => 'deleted',
        'value' => 0,
        'numeric' => TRUE,
      ],
    ],
  ], $data['entity_test_mul__string']['table']['join']['entity_test_mul']);
}