You are here

function StyleTest::_testGrouping in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Plugin/StyleTest.php \Drupal\views\Tests\Plugin\StyleTest::_testGrouping()

Tests the grouping features of styles.

1 call to StyleTest::_testGrouping()
StyleTest::testGrouping in core/modules/views/src/Tests/Plugin/StyleTest.php

File

core/modules/views/src/Tests/Plugin/StyleTest.php, line 103
Contains \Drupal\views\Tests\Plugin\StyleTest.

Class

StyleTest
Tests general style functionality.

Namespace

Drupal\views\Tests\Plugin

Code

function _testGrouping($stripped = FALSE) {
  $view = Views::getView('test_view');
  $view
    ->setDisplay();

  // Setup grouping by the job and the age field.
  $view
    ->initStyle();
  $view->style_plugin->options['grouping'] = array(
    array(
      'field' => 'job',
    ),
    array(
      'field' => 'age',
    ),
  );

  // Reduce the amount of items to make the test a bit easier.
  // Set up the pager.
  $view->displayHandlers
    ->get('default')
    ->overrideOption('pager', array(
    'type' => 'some',
    'options' => array(
      'items_per_page' => 3,
    ),
  ));

  // Add the job and age field.
  $fields = array(
    'name' => array(
      'id' => 'name',
      'table' => 'views_test_data',
      'field' => 'name',
      'relationship' => 'none',
      'label' => 'Name',
    ),
    'job' => array(
      'id' => 'job',
      'table' => 'views_test_data',
      'field' => 'job',
      'relationship' => 'none',
      'label' => 'Job',
    ),
    'age' => array(
      'id' => 'age',
      'table' => 'views_test_data',
      'field' => 'age',
      'relationship' => 'none',
      'label' => 'Age',
    ),
  );
  $view->displayHandlers
    ->get('default')
    ->overrideOption('fields', $fields);

  // Now run the query and groupby the result.
  $this
    ->executeView($view);
  $expected = array();
  $expected['Job: Singer'] = array();
  $expected['Job: Singer']['group'] = 'Job: Singer';
  $expected['Job: Singer']['rows']['Age: 25'] = array();
  $expected['Job: Singer']['rows']['Age: 25']['group'] = 'Age: 25';
  $expected['Job: Singer']['rows']['Age: 25']['rows'][0] = new ResultRow([
    'index' => 0,
  ]);
  $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_name = 'John';
  $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_job = 'Singer';
  $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_age = '25';
  $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_id = '1';
  $expected['Job: Singer']['rows']['Age: 27'] = array();
  $expected['Job: Singer']['rows']['Age: 27']['group'] = 'Age: 27';
  $expected['Job: Singer']['rows']['Age: 27']['rows'][1] = new ResultRow([
    'index' => 1,
  ]);
  $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_name = 'George';
  $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_job = 'Singer';
  $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_age = '27';
  $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_id = '2';
  $expected['Job: Drummer'] = array();
  $expected['Job: Drummer']['group'] = 'Job: Drummer';
  $expected['Job: Drummer']['rows']['Age: 28'] = array();
  $expected['Job: Drummer']['rows']['Age: 28']['group'] = 'Age: 28';
  $expected['Job: Drummer']['rows']['Age: 28']['rows'][2] = new ResultRow([
    'index' => 2,
  ]);
  $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_name = 'Ringo';
  $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_job = 'Drummer';
  $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_age = '28';
  $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_id = '3';

  // Alter the results to support the stripped case.
  if ($stripped) {

    // Add some html to the result and expected value.
    $rand1 = '<a data="' . $this
      ->randomMachineName() . '" />';
    $view->result[0]->views_test_data_job .= $rand1;
    $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_data_job = 'Singer' . $rand1;
    $expected['Job: Singer']['group'] = 'Job: Singer';
    $rand2 = '<a data="' . $this
      ->randomMachineName() . '" />';
    $view->result[1]->views_test_data_job .= $rand2;
    $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_data_job = 'Singer' . $rand2;
    $rand3 = '<a data="' . $this
      ->randomMachineName() . '" />';
    $view->result[2]->views_test_data_job .= $rand3;
    $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_data_job = 'Drummer' . $rand3;
    $expected['Job: Drummer']['group'] = 'Job: Drummer';
    $view->style_plugin->options['grouping'][0] = array(
      'field' => 'job',
      'rendered' => TRUE,
      'rendered_strip' => TRUE,
    );
    $view->style_plugin->options['grouping'][1] = array(
      'field' => 'age',
      'rendered' => TRUE,
      'rendered_strip' => TRUE,
    );
  }

  // The newer api passes the value of the grouping as well.
  $sets_new_rendered = $view->style_plugin
    ->renderGrouping($view->result, $view->style_plugin->options['grouping'], TRUE);
  $this
    ->assertEqual($sets_new_rendered, $expected, 'The style plugins should properly group the results with grouping by the rendered output.');

  // Don't test stripped case, because the actual value is not stripped.
  if (!$stripped) {
    $sets_new_value = $view->style_plugin
      ->renderGrouping($view->result, $view->style_plugin->options['grouping'], FALSE);

    // Reorder the group structure to grouping by value.
    $new_expected = $expected;
    $new_expected['Singer'] = $expected['Job: Singer'];
    $new_expected['Singer']['rows']['25'] = $expected['Job: Singer']['rows']['Age: 25'];
    $new_expected['Singer']['rows']['27'] = $expected['Job: Singer']['rows']['Age: 27'];
    $new_expected['Drummer'] = $expected['Job: Drummer'];
    $new_expected['Drummer']['rows']['28'] = $expected['Job: Drummer']['rows']['Age: 28'];
    unset($new_expected['Job: Singer']);
    unset($new_expected['Singer']['rows']['Age: 25']);
    unset($new_expected['Singer']['rows']['Age: 27']);
    unset($new_expected['Job: Drummer']);
    unset($new_expected['Drummer']['rows']['Age: 28']);
    $this
      ->assertEqual($sets_new_value, $new_expected, 'The style plugins should proper group the results with grouping by the value.');
  }

  // Test that grouping works on fields having no label.
  $fields['job']['label'] = '';
  $view
    ->destroy();
  $view
    ->setDisplay();
  $view
    ->initStyle();
  $view->displayHandlers
    ->get('default')
    ->overrideOption('fields', $fields);
  $view->style_plugin->options['grouping'] = array(
    array(
      'field' => 'job',
    ),
    array(
      'field' => 'age',
    ),
  );
  $this
    ->executeView($view);
  if ($stripped) {
    $view->result[0]->views_test_data_job .= $rand1;
    $view->result[1]->views_test_data_job .= $rand2;
    $view->result[2]->views_test_data_job .= $rand3;
    $view->style_plugin->options['grouping'][0] = array(
      'field' => 'job',
      'rendered' => TRUE,
      'rendered_strip' => TRUE,
    );
    $view->style_plugin->options['grouping'][1] = array(
      'field' => 'age',
      'rendered' => TRUE,
      'rendered_strip' => TRUE,
    );
  }
  $sets_new_rendered = $view->style_plugin
    ->renderGrouping($view->result, $view->style_plugin->options['grouping'], TRUE);

  // Remove labels from expected results.
  foreach ($expected as $job => $data) {
    unset($expected[$job]);
    $job = str_replace('Job: ', '', $job);
    $data['group'] = $job;
    $expected[$job] = $data;
  }
  $this
    ->assertEqual($expected, $sets_new_rendered);
}