Issue #1946208 by dawehner: Rewrite QueryGroupByTest to use ViewUnitTestBase.

8.0.x
Alex Pott 2013-06-15 11:40:18 +02:00
parent 757d39e2dd
commit 1ea2d228c4
6 changed files with 132 additions and 163 deletions

View File

@ -74,64 +74,7 @@ function entity_test_views_data() {
);
}
$data['entity_test_property_data']['table']['join']['entity_test'] = array(
'left_field' => 'id',
'field' => 'id',
);
$data['entity_test_property_data']['id'] = array(
'title' => t('ID'),
'help' => t('The {entity_test}.id of the test entity.'),
'argument' => array(
'id' => 'numeric',
),
'field' => array(
'id' => 'numeric',
),
'filter' => array(
'id' => 'numeric',
),
'sort' => array(
'id' => 'standard',
),
);
if (module_exists('langcode')) {
$data['entity_test_property_data']['langcode'] = array(
'title' => t('Language'),
'help' => t('The {language}.langcode of this variant of this test entity.'),
'field' => array(
'id' => 'language',
),
'filter' => array(
'id' => 'language',
),
'argument' => array(
'id' => 'language',
),
'sort' => array(
'id' => 'standard',
),
);
$data['entity_test_property_data']['default_langcode'] = array(
'title' => t('Default langcode'),
'help' => t('Boolean indicating whether the current variant is in the original entity language.'),
'field' => array(
'id' => 'boolean',
),
'filter' => array(
'id' => 'boolean',
),
'argument' => array(
'id' => 'numeric',
),
'sort' => array(
'id' => 'standard',
),
);
}
$data['entity_test_property_data']['name'] = array(
$data['entity_test']['name'] = array(
'title' => t('Name'),
'help' => t('The name of the test entity.'),
'field' => array(
@ -148,7 +91,7 @@ function entity_test_views_data() {
),
);
$data['entity_test_property_data']['user_id'] = array(
$data['entity_test']['user_id'] = array(
'title' => t('Name'),
'help' => t('The name of the test entity.'),
'field' => array(

View File

@ -2,7 +2,7 @@
/**
* @file
* Definition of Drupal\views\Tests\QueryGroupByTest.
* Contains \Drupal\views\Tests\QueryGroupByTest.
*/
namespace Drupal\views\Tests;
@ -10,7 +10,7 @@ namespace Drupal\views\Tests;
/**
* Tests aggregate functionality of views, for example count.
*/
class QueryGroupByTest extends ViewTestBase {
class QueryGroupByTest extends ViewUnitTestBase {
/**
* Views used by this test.
@ -19,6 +19,20 @@ class QueryGroupByTest extends ViewTestBase {
*/
public static $testViews = array('test_group_by_in_filters', 'test_aggregate_count', 'test_group_by_count');
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_test', 'system', 'field');
/**
* The storage controller for the test entity type.
*
* @var \Drupal\Core\Entity\DatabaseStorageController
*/
public $storageController;
public static function getInfo() {
return array(
'name' => 'Groupby',
@ -27,28 +41,23 @@ class QueryGroupByTest extends ViewTestBase {
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('entity_test', array('entity_test'));
$this->storageController = $this->container->get('plugin.manager.entity')->getStorageController('entity_test');
}
/**
* Tests aggregate count feature.
*/
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);
$this->setupTestEntities();
$view = views_get_view('test_aggregate_count');
$this->executeView($view);
@ -57,87 +66,106 @@ class QueryGroupByTest extends ViewTestBase {
$types = array();
foreach ($view->result as $item) {
// num_records is a alias for nid.
$types[$item->node_field_data_type] = $item->num_records;
// num_records is a alias for id.
$types[$item->entity_test_name] = $item->num_records;
}
$this->assertEqual($types[$type1->type], 4);
$this->assertEqual($types[$type2->type], 3);
$this->assertEqual($types['name1'], 4, 'Groupby the name: name1 returned the expected amount of results.');
$this->assertEqual($types['name2'], 3, 'Groupby the name: name2 returned the expected amount of results.');
}
//public function testAggregateSum() {
//}
/**
* @param $group_by
* Which group_by function should be used, for example sum or count.
* Provides a test helper which runs a view with some aggregation function.
*
* @param string $aggregation_function
* Which aggregation function should be used, for example sum or count.
* @param array $values
* The expected views result.
*/
function GroupByTestHelper($group_by, $values) {
// Create 2 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);
public function groupByTestHelper($aggregation_function, $values) {
$this->setupTestEntities();
$view = views_get_view('test_group_by_count');
$view->setDisplay();
$view->displayHandlers->get('default')->options['fields']['nid']['group_type'] = $group_by;
$view->displayHandlers->get('default')->options['fields']['id']['group_type'] = $aggregation_function;
$this->executeView($view);
$this->assertEqual(count($view->result), 2, 'Make sure the count of items is right.');
// Group by nodetype to identify the right count.
// Group by name to identify the right count.
$results = array();
foreach ($view->result as $item) {
$results[$item->node_field_data_type] = $item->nid;
$results[$item->entity_test_name] = $item->id;
}
$this->assertEqual($results[$type1->type], $values[0]);
$this->assertEqual($results[$type2->type], $values[1]);
$this->assertEqual($results['name1'], $values[0], format_string('Aggregation with @aggregation_function and groupby name: name1 returned the expected amount of results', array('@aggregation_function' => $aggregation_function)));
$this->assertEqual($results['name2'], $values[1], format_string('Aggregation with @aggregation_function and groupby name: name2 returned the expected amount of results', array('@aggregation_function' => $aggregation_function)));
}
/**
* Helper method that creates some test entities.
*/
protected function setupTestEntities() {
// Create 4 entities with name1 and 3 nodes with name2.
$entity_1 = array(
'name' => 'name1',
);
$this->storageController->create($entity_1)->save();
$this->storageController->create($entity_1)->save();
$this->storageController->create($entity_1)->save();
$this->storageController->create($entity_1)->save();
$entity_2 = array(
'name' => 'name2',
);
$this->storageController->create($entity_2)->save();
$this->storageController->create($entity_2)->save();
$this->storageController->create($entity_2)->save();
}
/**
* Tests the count aggregation function.
*/
public function testGroupByCount() {
$this->GroupByTestHelper('count', array(4, 3));
$this->groupByTestHelper('count', array(4, 3));
}
function testGroupBySum() {
$this->GroupByTestHelper('sum', array(10, 18));
/**
* Tests the sum aggregation function.
*/
public function testGroupBySum() {
$this->groupByTestHelper('sum', array(10, 18));
}
function testGroupByAverage() {
$this->GroupByTestHelper('avg', array(2.5, 6));
/**
* Tests the average aggregation function.
*/
public function testGroupByAverage() {
$this->groupByTestHelper('avg', array(2.5, 6));
}
function testGroupByMin() {
$this->GroupByTestHelper('min', array(1, 5));
/**
* Tests the min aggregation function.
*/
public function testGroupByMin() {
$this->groupByTestHelper('min', array(1, 5));
}
function testGroupByMax() {
$this->GroupByTestHelper('max', array(4, 7));
/**
* Tests the max aggregation function.
*/
public function testGroupByMax() {
$this->groupByTestHelper('max', array(4, 7));
}
/**
* Tests groupby with filters.
*/
public function testGroupByCountOnlyFilters() {
// Check if GROUP BY and HAVING are included when a view
// Doesn't display SUM, COUNT, MAX... functions in SELECT statment
$type1 = $this->drupalCreateContentType();
$node_1 = array(
'type' => $type1->type,
);
for ($x = 0; $x < 10; $x++) {
$this->drupalCreateNode($node_1);
$this->storageController->create(array('name' => 'name1'))->save();
}
$view = views_get_view('test_group_by_in_filters');

View File

@ -222,6 +222,7 @@ abstract class ViewUnitTestBase extends DrupalUnitTestBase {
$view->setDisplay();
$view->preExecute($args);
$view->execute();
$this->verbose('<pre>Executed view: ' . ((string) $view->build_info['query']) . '</pre>');
}
/**

View File

@ -1,4 +1,4 @@
base_table: node
base_table: entity_test
core: '8'
description: ''
status: '1'
@ -8,21 +8,21 @@ display:
access:
type: none
arguments:
type:
name:
default_action: summary
default_argument_type: fixed
field: type
id: type
field: name
id: name
summary:
format: default_summary
table: node_field_data
plugin_id: node_type
table: entity_test
plugin_id: string
cache:
type: none
exposed_form:
type: basic
fields:
nid:
id:
alter:
alter_text: '0'
ellipsis: '1'
@ -32,12 +32,12 @@ display:
trim: '0'
word_boundary: '1'
empty_zero: '0'
field: title
field: id
hide_empty: '0'
id: nid
id: id
link_to_node: '0'
table: node
plugin_id: node
table: entity_test
plugin_id: numeric
group_by: '1'
pager:
type: some

View File

@ -1,4 +1,4 @@
base_table: node
base_table: entity_test
core: '8'
description: ''
status: '1'
@ -12,7 +12,7 @@ display:
exposed_form:
type: basic
fields:
nid:
id:
alter:
alter_text: '0'
ellipsis: '1'
@ -22,14 +22,13 @@ display:
trim: '0'
word_boundary: '1'
empty_zero: '0'
field: nid
field: id
group_type: { }
hide_empty: '0'
id: nid
link_to_node: '0'
table: node
plugin_id: node
type:
id: id
table: entity_test
plugin_id: numeric
name:
alter:
alter_text: '0'
ellipsis: '1'
@ -39,12 +38,11 @@ display:
trim: '0'
word_boundary: '1'
empty_zero: '0'
field: type
field: name
hide_empty: '0'
id: type
link_to_node: '0'
table: node_field_data
plugin_id: node_type
id: name
table: entity_test
plugin_id: standard
group_by: '1'
pager:
type: some

View File

@ -1,4 +1,4 @@
base_table: node
base_table: entity_test
core: '8'
description: ''
status: '1'
@ -12,7 +12,7 @@ display:
exposed_form:
type: basic
fields:
type:
name:
alter:
alter_text: '0'
ellipsis: '1'
@ -22,19 +22,18 @@ display:
trim: '0'
word_boundary: '1'
empty_zero: '0'
field: type
field: name
hide_empty: '0'
id: type
link_to_node: '0'
table: node_field_data
plugin_id: node_type
id: name
table: entity_test
plugin_id: standard
filters:
nid:
field: nid
id:
field: id
group_type: count
id: nid
id: id
operator: '>'
table: node
table: entity_test
value:
value: '3'
plugin_id: numeric