You are here

function ViewsQueryGroupByTest::GroupByTestHelper in Views (for Drupal 7) 7.3

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.

6 calls to ViewsQueryGroupByTest::GroupByTestHelper()
ViewsQueryGroupByTest::testGroupByAverage in tests/views_groupby.test
ViewsQueryGroupByTest::testGroupByCount in tests/views_groupby.test
ViewsQueryGroupByTest::testGroupByMax in tests/views_groupby.test
ViewsQueryGroupByTest::testGroupByMin in tests/views_groupby.test
ViewsQueryGroupByTest::testGroupByNone in tests/views_groupby.test

... See full list

File

tests/views_groupby.test, line 205
Definitions of ViewsQueryGroupByTest and ViewsUiGroupbyTestCase.

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
    ->drupalCreateContentType();
  $type2 = $this
    ->drupalCreateContentType();
  $node_1 = array(
    'type' => $type1->type,
  );

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

  // Nids from 5 to 7.
  $this
    ->drupalCreateNode($node_2);
  $this
    ->drupalCreateNode($node_2);
  $this
    ->drupalCreateNode($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 nodetype 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]);
}