You are here

public function QueryGroupByTest::testGroupByFieldWithCardinality in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/QueryGroupByTest.php \Drupal\Tests\views\Kernel\QueryGroupByTest::testGroupByFieldWithCardinality()

Tests grouping a field with cardinality > 1.

File

core/modules/views/tests/src/Kernel/QueryGroupByTest.php, line 219

Class

QueryGroupByTest
Tests aggregate functionality of views, for example count.

Namespace

Drupal\Tests\views\Kernel

Code

public function testGroupByFieldWithCardinality() {
  $field_storage = FieldStorageConfig::create([
    'type' => 'integer',
    'field_name' => 'field_test',
    'cardinality' => 4,
    'entity_type' => 'entity_test_mul',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_name' => 'field_test',
    'entity_type' => 'entity_test_mul',
    'bundle' => 'entity_test_mul',
  ]);
  $field
    ->save();
  $entities = [];
  $entity = EntityTestMul::create([
    'field_test' => [
      1,
      1,
      1,
    ],
  ]);
  $entity
    ->save();
  $entities[] = $entity;
  $entity = EntityTestMul::create([
    'field_test' => [
      2,
      2,
      2,
    ],
  ]);
  $entity
    ->save();
  $entities[] = $entity;
  $entity = EntityTestMul::create([
    'field_test' => [
      2,
      2,
      2,
    ],
  ]);
  $entity
    ->save();
  $entities[] = $entity;
  $view = Views::getView('test_group_by_count_multicardinality');
  $this
    ->executeView($view);
  $this
    ->assertCount(2, $view->result);
  $this
    ->assertEquals('3', $view
    ->getStyle()
    ->getField(0, 'id'));
  $this
    ->assertEquals('1', $view
    ->getStyle()
    ->getField(0, 'field_test'));
  $this
    ->assertEquals('6', $view
    ->getStyle()
    ->getField(1, 'id'));
  $this
    ->assertEquals('2', $view
    ->getStyle()
    ->getField(1, 'field_test'));
  $entities[2]->field_test[0]->value = 3;
  $entities[2]->field_test[1]->value = 4;
  $entities[2]->field_test[2]->value = 5;
  $entities[2]
    ->save();
  $view = Views::getView('test_group_by_count_multicardinality');
  $this
    ->executeView($view);
  $this
    ->assertCount(5, $view->result);
  $this
    ->assertEquals('3', $view
    ->getStyle()
    ->getField(0, 'id'));
  $this
    ->assertEquals('1', $view
    ->getStyle()
    ->getField(0, 'field_test'));
  $this
    ->assertEquals('3', $view
    ->getStyle()
    ->getField(1, 'id'));
  $this
    ->assertEquals('2', $view
    ->getStyle()
    ->getField(1, 'field_test'));
  $this
    ->assertEquals('1', $view
    ->getStyle()
    ->getField(2, 'id'));
  $this
    ->assertEquals('3', $view
    ->getStyle()
    ->getField(2, 'field_test'));
  $this
    ->assertEquals('1', $view
    ->getStyle()
    ->getField(3, 'id'));
  $this
    ->assertEquals('4', $view
    ->getStyle()
    ->getField(3, 'field_test'));
  $this
    ->assertEquals('1', $view
    ->getStyle()
    ->getField(4, 'id'));
  $this
    ->assertEquals('5', $view
    ->getStyle()
    ->getField(4, 'field_test'));

  // Check that translated values are correctly retrieved and are not grouped
  // into the original entity.
  $translation = $entity
    ->addTranslation('it');
  $translation->field_test = [
    6,
    6,
    6,
  ];
  $translation
    ->save();
  $view = Views::getView('test_group_by_count_multicardinality');
  $this
    ->executeView($view);
  $this
    ->assertCount(6, $view->result);
  $this
    ->assertEquals('3', $view
    ->getStyle()
    ->getField(5, 'id'));
  $this
    ->assertEquals('6', $view
    ->getStyle()
    ->getField(5, 'field_test'));
}