- <?php
- * @file
- * Tests aggregate functionality of Views.
- */
-
- require_once BACKDROP_ROOT . '/core/modules/views/tests/views_query.test';
-
- * Tests aggregate functionality of views, for example count.
- */
- class ViewsQueryGroupByTest extends ViewsSqlTest {
- protected $profile = 'testing';
-
-
- * Tests aggregate count feature.
- */
- public function testAggregateCount() {
-
- $type1 = $this->backdropCreateContentType();
- $type2 = $this->backdropCreateContentType();
-
- $node_1 = array(
- 'type' => $type1->type,
- );
- $this->backdropCreateNode($node_1);
- $this->backdropCreateNode($node_1);
- $this->backdropCreateNode($node_1);
- $this->backdropCreateNode($node_1);
-
- $node_2 = array(
- 'type' => $type2->type,
- );
- $this->backdropCreateNode($node_2);
- $this->backdropCreateNode($node_2);
- $this->backdropCreateNode($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) {
-
- $types[$item->node_type] = $item->num_records;
- }
-
- $this->assertEqual($types[$type1->type], 4);
- $this->assertEqual($types[$type2->type], 3);
- }
-
- public function viewsAggregateCountView() {
- $view = new view;
- $view->name = 'aggregate_count';
- $view->description = '';
- $view->tag = '';
- $view->base_table = 'node';
- $view->human_name = '';
- $view->core = 7;
- $view->api_version = '3.0';
- $view->disabled = FALSE;
-
-
- $handler = $view->new_display('default', 'Default', 'default');
- $handler->display->display_options['group_by'] = TRUE;
- $handler->display->display_options['access']['type'] = 'none';
- $handler->display->display_options['cache']['type'] = 'none';
- $handler->display->display_options['query']['type'] = 'views_query';
- $handler->display->display_options['query']['options']['query_comment'] = FALSE;
- $handler->display->display_options['exposed_form']['type'] = 'basic';
- $handler->display->display_options['pager']['type'] = 'some';
- $handler->display->display_options['style_plugin'] = 'default';
- $handler->display->display_options['row_plugin'] = 'fields';
-
- $handler->display->display_options['fields']['nid']['id'] = 'nid';
- $handler->display->display_options['fields']['nid']['table'] = 'node';
- $handler->display->display_options['fields']['nid']['field'] = 'title';
- $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
- $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
- $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
- $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
- $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
- $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
- $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
- $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
- $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
- $handler->display->display_options['fields']['nid']['link_to_node'] = 0;
-
- $handler->display->display_options['arguments']['type']['id'] = 'type';
- $handler->display->display_options['arguments']['type']['table'] = 'node';
- $handler->display->display_options['arguments']['type']['field'] = 'type';
- $handler->display->display_options['arguments']['type']['default_action'] = 'summary';
- $handler->display->display_options['arguments']['type']['default_argument_type'] = 'fixed';
- $handler->display->display_options['arguments']['type']['summary']['format'] = 'default_summary';
-
- return $view;
- }
-
-
- * @param 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.
- * @param array|null $values
- * (optional) Expected values.
- */
- function GroupByTestHelper($group_by = NULL, $values = NULL) {
-
- $type1 = $this->backdropCreateContentType();
- $type2 = $this->backdropCreateContentType();
-
- $node_1 = array(
- 'type' => $type1->type,
- );
-
- $this->backdropCreateNode($node_1);
- $this->backdropCreateNode($node_1);
- $this->backdropCreateNode($node_1);
- $this->backdropCreateNode($node_1);
- $node_2 = array(
- 'type' => $type2->type,
- );
-
- $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();
-
- 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);
-
- return;
- }
-
-
- 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]);
- }
-
- function viewsGroupByViewHelper($group_by = NULL) {
- $view = new view;
- $view->name = 'group_by_count';
- $view->description = '';
- $view->tag = '';
- $view->view_php = '';
- $view->base_table = 'node';
- $view->is_cacheable = FALSE;
- $view->api_version = 2;
- $view->disabled = FALSE;
-
-
- $handler = $view->new_display('default', 'Default', 'default');
- $handler->display->display_options['group_by'] = TRUE;
- $handler->display->display_options['access']['type'] = 'none';
- $handler->display->display_options['cache']['type'] = 'none';
- $handler->display->display_options['exposed_form']['type'] = 'basic';
- $handler->display->display_options['pager']['type'] = 'some';
- $handler->display->display_options['style_plugin'] = 'default';
- $handler->display->display_options['row_plugin'] = 'fields';
-
-
-
- if (!empty($group_by)) {
-
- $handler->display->display_options['fields']['nid']['id'] = 'nid';
- $handler->display->display_options['fields']['nid']['table'] = 'node';
- $handler->display->display_options['fields']['nid']['field'] = 'nid';
- $handler->display->display_options['fields']['nid']['group_type'] = $group_by;
- $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
- $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
- $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
- $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
- $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
- $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
- $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
- $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
- $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
- $handler->display->display_options['fields']['nid']['link_to_node'] = 0;
- }
-
-
- $handler->display->display_options['fields']['type']['id'] = 'type';
- $handler->display->display_options['fields']['type']['table'] = 'node';
- $handler->display->display_options['fields']['type']['field'] = 'type';
- $handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
- $handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
- $handler->display->display_options['fields']['type']['alter']['trim'] = 0;
- $handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1;
- $handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1;
- $handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
- $handler->display->display_options['fields']['type']['alter']['html'] = 0;
- $handler->display->display_options['fields']['type']['hide_empty'] = 0;
- $handler->display->display_options['fields']['type']['empty_zero'] = 0;
- $handler->display->display_options['fields']['type']['link_to_node'] = 0;
-
- return $view;
- }
-
- public function testGroupByCount() {
- $this->GroupByTestHelper('count', array(4, 3));
- }
-
- function testGroupBySum() {
- $this->GroupByTestHelper('sum', array(10, 18));
- }
-
-
- function testGroupByAverage() {
- $this->GroupByTestHelper('avg', array(2.5, 6));
- }
-
- function testGroupByMin() {
- $this->GroupByTestHelper('min', array(1, 5));
- }
-
- function testGroupByMax() {
- $this->GroupByTestHelper('max', array(4, 7));
- }
-
- function testGroupByNone() {
- $this->GroupByTestHelper();
- }
-
- public function testGroupByCountOnlyFilters() {
-
-
-
- $type1 = $this->backdropCreateContentType();
-
- $node_1 = array(
- 'type' => $type1->type,
- );
- for ($x = 0; $x < 10; $x++) {
- $this->backdropCreateNode($node_1);
- }
-
- $view = $this->viewsGroupByCountViewOnlyFilters();
- $output = $view->execute_display();
-
- $this->assertTrue(strpos($view->build_info['query'], 'GROUP BY'), t('Make sure that GROUP BY is in the query'));
- $this->assertTrue(strpos($view->build_info['query'], 'HAVING'), t('Make sure that HAVING is in the query'));
- }
-
- function viewsGroupByCountViewOnlyFilters() {
- $view = new view;
- $view->name = 'group_by_in_filters';
- $view->description = '';
- $view->tag = '';
- $view->view_php = '';
- $view->base_table = 'node';
- $view->is_cacheable = FALSE;
- $view->api_version = 2;
- $view->disabled = FALSE;
-
-
- $handler = $view->new_display('default', 'Default', 'default');
- $handler->display->display_options['group_by'] = TRUE;
- $handler->display->display_options['access']['type'] = 'none';
- $handler->display->display_options['cache']['type'] = 'none';
- $handler->display->display_options['exposed_form']['type'] = 'basic';
- $handler->display->display_options['pager']['type'] = 'some';
- $handler->display->display_options['style_plugin'] = 'default';
- $handler->display->display_options['row_plugin'] = 'fields';
-
- $handler->display->display_options['fields']['type']['id'] = 'type';
- $handler->display->display_options['fields']['type']['table'] = 'node';
- $handler->display->display_options['fields']['type']['field'] = 'type';
- $handler->display->display_options['fields']['type']['alter']['alter_text'] = 0;
- $handler->display->display_options['fields']['type']['alter']['make_link'] = 0;
- $handler->display->display_options['fields']['type']['alter']['trim'] = 0;
- $handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1;
- $handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1;
- $handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0;
- $handler->display->display_options['fields']['type']['alter']['html'] = 0;
- $handler->display->display_options['fields']['type']['hide_empty'] = 0;
- $handler->display->display_options['fields']['type']['empty_zero'] = 0;
- $handler->display->display_options['fields']['type']['link_to_node'] = 0;
-
- $handler->display->display_options['filters']['nid']['id'] = 'nid';
- $handler->display->display_options['filters']['nid']['table'] = 'node';
- $handler->display->display_options['filters']['nid']['field'] = 'nid';
- $handler->display->display_options['filters']['nid']['group_type'] = 'count';
- $handler->display->display_options['filters']['nid']['operator'] = '>';
- $handler->display->display_options['filters']['nid']['value']['value'] = '3';
-
- return $view;
- }
- }
-
- * Tests UI of aggregate functionality..
- */
- class viewsUiGroupbyTestCase extends BackdropWebTestCase {
- function setUp() {
-
- parent::setUp('views_ui', 'views_test');
-
-
- $views_admin = $this->backdropCreateUser(array('administer views', 'administer blocks', 'bypass node access', 'access user profiles', 'view revisions'));
- $this->backdropLogin($views_admin);
- }
-
-
- * Tests whether basic saving works.
- *
- * @todo: this should check the change of the settings as well.
- */
- function testGroupBySave() {
- $this->backdropGet('admin/structure/views/view/test_views_groupby_save/configure');
-
- $edit = array(
- 'group_by' => TRUE,
- );
- $this->backdropPost('admin/structure/views/nojs/display/test_views_groupby_save/default/group_by', $edit, t('Apply'));
-
- $this->backdropGet('admin/structure/views/view/test_views_groupby_save/configure');
- $this->backdropPost('admin/structure/views/view/test_views_groupby_save/configure', array(), t('Save'));
-
- $this->backdropGet('admin/structure/views/nojs/display/test_views_groupby_save/default/group_by');
- }
- }