function QueryGroupByTest::testGroupByFieldWithCardinality

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/QueryGroupByTest.php \Drupal\Tests\views\Kernel\QueryGroupByTest::testGroupByFieldWithCardinality()
  2. 10 core/modules/views/tests/src/Kernel/QueryGroupByTest.php \Drupal\Tests\views\Kernel\QueryGroupByTest::testGroupByFieldWithCardinality()
  3. 11.x 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->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->assertCount(5, $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->assertCount(6, $view->result);
    $this->assertEqual('3', $view->getStyle()
        ->getField(5, 'id'));
    $this->assertEqual('6', $view->getStyle()
        ->getField(5, 'field_test'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.