You are here

public function ViewsQueryGroupByTest::testAggregateCount in Views (for Drupal 7) 7.3

Tests aggregate count feature.

File

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

Class

ViewsQueryGroupByTest
Tests aggregate functionality of views, for example count.

Code

public function testAggregateCount() {

  // Create 2 nodes of type1 and 3 nodes of type2
  $type1 = $this
    ->drupalCreateContentType();
  $type2 = $this
    ->drupalCreateContentType();
  $node_1 = array(
    'type' => $type1->type,
  );
  $this
    ->drupalCreateNode($node_1);
  $this
    ->drupalCreateNode($node_1);
  $this
    ->drupalCreateNode($node_1);
  $this
    ->drupalCreateNode($node_1);
  $node_2 = array(
    'type' => $type2->type,
  );
  $this
    ->drupalCreateNode($node_2);
  $this
    ->drupalCreateNode($node_2);
  $this
    ->drupalCreateNode($node_2);
  $view = $this
    ->viewsAggregateCountView();
  $output = $view
    ->execute_display();
  $this
    ->assertEqual(count($view->result), 2, 'Make sure the count of items is right.');
  $types = array();
  foreach ($view->result as $item) {

    // 'num_records' is a alias for nid.
    $types[$item->node_type] = $item->num_records;
  }
  $this
    ->assertEqual($types[$type1->type], 4);
  $this
    ->assertEqual($types[$type2->type], 3);
}