You are here

public function QueryGroupByTest::testGroupByFieldWithCardinality in Zircon Profile 8

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

Tests grouping a field with cardinality > 1.

File

core/modules/views/src/Tests/QueryGroupByTest.php, line 218
Contains \Drupal\views\Tests\QueryGroupByTest.

Class

QueryGroupByTest
Tests aggregate functionality of views, for example count.

Namespace

Drupal\views\Tests

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
    ->assertEqual(2, count($view->result));
  $this
    ->assertEqual('3', $view
    ->getStyle()
    ->getField(0, 'id'));
  $this
    ->assertEqual('1', $view
    ->getStyle()
    ->getField(0, 'field_test'));
  $this
    ->assertEqual('6', $view
    ->getStyle()
    ->getField(1, 'id'));
  $this
    ->assertEqual('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
    ->assertEqual(5, count($view->result));
  $this
    ->assertEqual('3', $view
    ->getStyle()
    ->getField(0, 'id'));
  $this
    ->assertEqual('1', $view
    ->getStyle()
    ->getField(0, 'field_test'));
  $this
    ->assertEqual('3', $view
    ->getStyle()
    ->getField(1, 'id'));
  $this
    ->assertEqual('2', $view
    ->getStyle()
    ->getField(1, 'field_test'));
  $this
    ->assertEqual('1', $view
    ->getStyle()
    ->getField(2, 'id'));
  $this
    ->assertEqual('3', $view
    ->getStyle()
    ->getField(2, 'field_test'));
  $this
    ->assertEqual('1', $view
    ->getStyle()
    ->getField(3, 'id'));
  $this
    ->assertEqual('4', $view
    ->getStyle()
    ->getField(3, 'field_test'));
  $this
    ->assertEqual('1', $view
    ->getStyle()
    ->getField(4, 'id'));
  $this
    ->assertEqual('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
    ->assertEqual(6, count($view->result));
  $this
    ->assertEqual('3', $view
    ->getStyle()
    ->getField(5, 'id'));
  $this
    ->assertEqual('6', $view
    ->getStyle()
    ->getField(5, 'field_test'));
}