You are here

public function FieldTest::testQueryWithGroupByForBaseField in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php \Drupal\Tests\views\Unit\Plugin\field\FieldTest::testQueryWithGroupByForBaseField()
  2. 9 core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php \Drupal\Tests\views\Unit\Plugin\field\FieldTest::testQueryWithGroupByForBaseField()

@covers ::query

File

core/modules/views/tests/src/Unit/Plugin/field/FieldTest.php, line 455
Contains \Drupal\Tests\views\Unit\Plugin\field\FieldTest.

Class

FieldTest
@coversDefaultClass \Drupal\views\Plugin\views\field\EntityField @group views

Namespace

Drupal\Tests\views\Unit\Plugin\field

Code

public function testQueryWithGroupByForBaseField() {
  $definition = [
    'entity_type' => 'test_entity',
    'field_name' => 'title',
  ];
  $handler = new EntityField([], 'field', $definition, $this->entityTypeManager, $this->formatterPluginManager, $this->fieldTypePluginManager, $this->languageManager, $this->renderer, $this->entityRepository, $this->entityFieldManager);
  $handler->view = $this->executable;
  $handler->view->field = [
    $handler,
  ];
  $this
    ->setupLanguageRenderer($handler, $definition);
  $field_storage = $this
    ->getBaseFieldStorage();
  $this->entityFieldManager
    ->expects($this
    ->any())
    ->method('getFieldStorageDefinitions')
    ->with('test_entity')
    ->willReturn([
    'title' => $field_storage,
  ]);
  $table_mapping = $this
    ->createMock('Drupal\\Core\\Entity\\Sql\\TableMappingInterface');
  $table_mapping
    ->expects($this
    ->any())
    ->method('getFieldColumnName')
    ->with($field_storage, 'value')
    ->willReturn('title');
  $entity_storage = $this
    ->createMock('Drupal\\Core\\Entity\\Sql\\SqlEntityStorageInterface');
  $entity_storage
    ->expects($this
    ->any())
    ->method('getTableMapping')
    ->willReturn($table_mapping);
  $this->entityTypeManager
    ->expects($this
    ->any())
    ->method('getStorage')
    ->with('test_entity')
    ->willReturn($entity_storage);
  $options = [
    'group_column' => 'value',
    'group_columns' => [],
    'table' => 'test_entity_table',
  ];
  $handler
    ->init($this->executable, $this->display, $options);
  $query = $this
    ->getMockBuilder('Drupal\\views\\Plugin\\views\\query\\Sql')
    ->disableOriginalConstructor()
    ->getMock();
  $query
    ->expects($this
    ->once())
    ->method('ensureTable')
    ->with('test_entity_table', NULL)
    ->willReturn('test_entity_table');

  // Ensure that we add the title field to the query, if we group by some
  // other field in the view.
  $query
    ->expects($this
    ->once())
    ->method('addField')
    ->with('test_entity_table', 'title');
  $this->executable->query = $query;
  $handler
    ->query(TRUE);
}