You are here

protected function StyleTest::doTestGrouping in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/Plugin/StyleTest.php \Drupal\Tests\views\Kernel\Plugin\StyleTest::doTestGrouping()
  2. 9 core/modules/views/tests/src/Kernel/Plugin/StyleTest.php \Drupal\Tests\views\Kernel\Plugin\StyleTest::doTestGrouping()

Provides reusable code for ::testGrouping().

1 call to StyleTest::doTestGrouping()
StyleTest::testGrouping in core/modules/views/tests/src/Kernel/Plugin/StyleTest.php
Tests the grouping features of styles.

File

core/modules/views/tests/src/Kernel/Plugin/StyleTest.php, line 86

Class

StyleTest
Tests general style functionality.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

protected function doTestGrouping($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'] = [
    [
      'field' => 'job',
    ],
    [
      'field' => 'age',
    ],
  ];

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

  // Add the job and age field.
  $fields = [
    'name' => [
      'id' => 'name',
      'table' => 'views_test_data',
      'field' => 'name',
      'relationship' => 'none',
      'label' => 'Name',
    ],
    'job' => [
      'id' => 'job',
      'table' => 'views_test_data',
      'field' => 'job',
      'relationship' => 'none',
      'label' => 'Job',
    ],
    'age' => [
      '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 = [];
  $expected['Job: Singer'] = [];
  $expected['Job: Singer']['group'] = 'Job: Singer';
  $expected['Job: Singer']['level'] = 0;
  $expected['Job: Singer']['rows']['Age: 25'] = [];
  $expected['Job: Singer']['rows']['Age: 25']['group'] = 'Age: 25';
  $expected['Job: Singer']['rows']['Age: 25']['level'] = 1;
  $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'] = [];
  $expected['Job: Singer']['rows']['Age: 27']['group'] = 'Age: 27';
  $expected['Job: Singer']['rows']['Age: 27']['level'] = 1;
  $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'] = [];
  $expected['Job: Drummer']['group'] = 'Job: Drummer';
  $expected['Job: Drummer']['level'] = 0;
  $expected['Job: Drummer']['rows']['Age: 28'] = [];
  $expected['Job: Drummer']['rows']['Age: 28']['group'] = 'Age: 28';
  $expected['Job: Drummer']['rows']['Age: 28']['level'] = 1;
  $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] = [
      'field' => 'job',
      'rendered' => TRUE,
      'rendered_strip' => TRUE,
    ];
    $view->style_plugin->options['grouping'][1] = [
      '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
    ->assertEquals($expected, $sets_new_rendered);

  // 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
      ->assertEquals($new_expected, $sets_new_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'] = [
    [
      'field' => 'job',
    ],
    [
      '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] = [
      'field' => 'job',
      'rendered' => TRUE,
      'rendered_strip' => TRUE,
    ];
    $view->style_plugin->options['grouping'][1] = [
      'field' => 'age',
      'rendered' => TRUE,
      'rendered_strip' => TRUE,
    ];
  }
  $sets_new_rendered = $view->style_plugin
    ->renderGrouping($view->result, $view->style_plugin->options['grouping'], TRUE);
  $no_label_expected = $expected;

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

  // Test that grouping works on fields having no colon after the label.
  $fields['job']['label'] = 'Job';
  $fields['job']['element_label_colon'] = FALSE;
  $view
    ->destroy();
  $view
    ->setDisplay();
  $view
    ->initStyle();
  $view->displayHandlers
    ->get('default')
    ->overrideOption('fields', $fields);
  $view->style_plugin->options['grouping'] = [
    [
      'field' => 'job',
    ],
    [
      '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] = [
      'field' => 'job',
      'rendered' => TRUE,
      'rendered_strip' => TRUE,
    ];
    $view->style_plugin->options['grouping'][1] = [
      'field' => 'age',
      'rendered' => TRUE,
      'rendered_strip' => TRUE,
    ];
  }
  $sets_new_rendered = $view->style_plugin
    ->renderGrouping($view->result, $view->style_plugin->options['grouping'], TRUE);

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