1 views_groupby.test ViewsQueryGroupByTest::GroupByTestHelper($group_by = NULL, $values = NULL)

Parameters

string|null $group_by: (optional) Which group_by function should be used, for example sum or count. If omitted, the aggregation is tested with no group function.

array|null $values: (optional) Expected values.

File

core/modules/views/tests/views_groupby.test, line 107
Tests aggregate functionality of Views.

Class

ViewsQueryGroupByTest
Tests aggregate functionality of views, for example count.

Code

function GroupByTestHelper($group_by = NULL, $values = NULL) {
  // Create 4 nodes of type1 and 3 nodes of type2.
  $type1 = $this->backdropCreateContentType();
  $type2 = $this->backdropCreateContentType();

  $node_1 = array(
    'type' => $type1->type,
  );
  // Nids from 1 to 4.
  $this->backdropCreateNode($node_1);
  $this->backdropCreateNode($node_1);
  $this->backdropCreateNode($node_1);
  $this->backdropCreateNode($node_1);
  $node_2 = array(
    'type' => $type2->type,
  );
  // Nids from 5 to 7.
  $this->backdropCreateNode($node_2);
  $this->backdropCreateNode($node_2);
  $this->backdropCreateNode($node_2);

  $view = $this->viewsGroupByViewHelper($group_by);
  $output = $view->execute_display();

  $this->assertEqual(count($view->result), 2, 'Make sure the count of items is right.');

  $results = array();
  // There's no need for a function in order to have aggregation.
  if (empty($group_by)) {
    $types = array($type1->type, $type2->type);
    $results = array_map(function($item) {
      return $item->node_type;
    }, $view->result);
    sort($types);
    sort($results);
    $this->assertIdentical($results, $types);
    // Exit here with no aggregation function.
    return;
  }

  // Group by node type to identify the right count.
  foreach ($view->result as $item) {
    $results[$item->node_type] = $item->nid;
  }
  $this->assertEqual($results[$type1->type], $values[0]);
  $this->assertEqual($results[$type2->type], $values[1]);
}