You are here

public function QueryGroupByTest::groupByTestHelper 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::groupByTestHelper()

Provides a test helper which runs a view with some aggregation function.

Parameters

string|null $aggregation_function: Which aggregation function should be used, for example sum or count. If NULL is passed the aggregation will be tested with no function.

array $values: The expected views result.

6 calls to QueryGroupByTest::groupByTestHelper()
QueryGroupByTest::testGroupByAverage in core/modules/views/tests/src/Kernel/QueryGroupByTest.php
Tests the average aggregation function.
QueryGroupByTest::testGroupByCount in core/modules/views/tests/src/Kernel/QueryGroupByTest.php
Tests the count aggregation function.
QueryGroupByTest::testGroupByMax in core/modules/views/tests/src/Kernel/QueryGroupByTest.php
Tests the max aggregation function.
QueryGroupByTest::testGroupByMin in core/modules/views/tests/src/Kernel/QueryGroupByTest.php
Tests the min aggregation function.
QueryGroupByTest::testGroupByNone in core/modules/views/tests/src/Kernel/QueryGroupByTest.php
Tests aggregation with no specific function.

... See full list

File

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

Class

QueryGroupByTest
Tests aggregate functionality of views, for example count.

Namespace

Drupal\Tests\views\Kernel

Code

public function groupByTestHelper($aggregation_function, $values) {
  $this
    ->setupTestEntities();
  $view = Views::getView('test_group_by_count');
  $view
    ->setDisplay();

  // There is no need for a function in order to have aggregation.
  if (empty($aggregation_function)) {

    // The test table has 2 fields ('id' and 'name'). We'll remove 'id'
    // because it's unique and will test aggregation on 'name'.
    unset($view->displayHandlers
      ->get('default')->options['fields']['id']);
  }
  else {
    $view->displayHandlers
      ->get('default')->options['fields']['id']['group_type'] = $aggregation_function;
  }
  $this
    ->executeView($view);
  $this
    ->assertCount(2, $view->result, 'Make sure the count of items is right.');

  // Group by name to identify the right count.
  $results = [];
  foreach ($view->result as $item) {
    $results[$item->entity_test_name] = $item->id;
  }
  $this
    ->assertEquals($values[0], $results['name1'], new FormattableMarkup('Aggregation with @aggregation_function and groupby name: name1 returned the expected amount of results', [
    '@aggregation_function' => $aggregation_function,
  ]));
  $this
    ->assertEquals($values[1], $results['name2'], new FormattableMarkup('Aggregation with @aggregation_function and groupby name: name2 returned the expected amount of results', [
    '@aggregation_function' => $aggregation_function,
  ]));
}