You are here

function StyleTest::testGroupingLegacy in Views (for Drupal 7) 8.3

Tests the grouping legacy features of styles.

File

lib/Drupal/views/Tests/Plugin/StyleTest.php, line 69
Definition of Drupal\views\Tests\Plugin\StyleTest.

Class

StyleTest
Tests some general style plugin related functionality.

Namespace

Drupal\views\Tests\Plugin

Code

function testGroupingLegacy() {
  $view = $this->view
    ->cloneView();

  // Setup grouping by the job.
  $view
    ->initDisplay();
  $view
    ->initStyle();
  $view->style_plugin->options['grouping'] = 'job';

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

  // Add the job field .
  $view->displayHandlers['default']
    ->overrideOption('fields', array(
    'name' => array(
      'id' => 'name',
      'table' => 'views_test_data',
      'field' => 'name',
      'relationship' => 'none',
    ),
    'job' => array(
      'id' => 'job',
      'table' => 'views_test_data',
      'field' => 'job',
      'relationship' => 'none',
    ),
  ));

  // Now run the query and groupby the result.
  $this
    ->executeView($view);

  // This is the old way to call it.
  $sets = $view->style_plugin
    ->render_grouping($view->result, $view->style_plugin->options['grouping']);
  $expected = array();

  // Use Job: as label, so be sure that the label is used for groupby as well.
  $expected['Job: Singer'] = array();
  $expected['Job: Singer'][0] = new \stdClass();
  $expected['Job: Singer'][0]->views_test_data_name = 'John';
  $expected['Job: Singer'][0]->views_test_data_job = 'Singer';
  $expected['Job: Singer'][0]->views_test_data_id = '1';
  $expected['Job: Singer'][1] = new \stdClass();
  $expected['Job: Singer'][1]->views_test_data_name = 'George';
  $expected['Job: Singer'][1]->views_test_data_job = 'Singer';
  $expected['Job: Singer'][1]->views_test_data_id = '2';
  $expected['Job: Drummer'] = array();
  $expected['Job: Drummer'][2] = new \stdClass();
  $expected['Job: Drummer'][2]->views_test_data_name = 'Ringo';
  $expected['Job: Drummer'][2]->views_test_data_job = 'Drummer';
  $expected['Job: Drummer'][2]->views_test_data_id = '3';
  $this
    ->assertEqual($sets, $expected, t('The style plugin should proper group the results with grouping by the rendered output.'));
  $expected = array();
  $expected['Job: Singer'] = array();
  $expected['Job: Singer']['group'] = 'Job: Singer';
  $expected['Job: Singer']['rows'][0] = new \stdClass();
  $expected['Job: Singer']['rows'][0]->views_test_data_name = 'John';
  $expected['Job: Singer']['rows'][0]->views_test_data_job = 'Singer';
  $expected['Job: Singer']['rows'][0]->views_test_data_id = '1';
  $expected['Job: Singer']['rows'][1] = new \stdClass();
  $expected['Job: Singer']['rows'][1]->views_test_data_name = 'George';
  $expected['Job: Singer']['rows'][1]->views_test_data_job = 'Singer';
  $expected['Job: Singer']['rows'][1]->views_test_data_id = '2';
  $expected['Job: Drummer'] = array();
  $expected['Job: Drummer']['group'] = 'Job: Drummer';
  $expected['Job: Drummer']['rows'][2] = new \stdClass();
  $expected['Job: Drummer']['rows'][2]->views_test_data_name = 'Ringo';
  $expected['Job: Drummer']['rows'][2]->views_test_data_job = 'Drummer';
  $expected['Job: Drummer']['rows'][2]->views_test_data_id = '3';

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

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