1.20.x views_plugin_style.test ViewsPluginStyleTestCase::testGroupingLegacy()

Tests the grouping legacy features of styles.

File

modules/views/tests/styles/views_plugin_style.test, line 17
Definition of ViewsPluginStyleTestCase.

Class

ViewsPluginStyleTestCase
Tests some general style plugin related functionality.

Code

function testGroupingLegacy() {
  $view = $this->getBasicView();
  // Setup grouping by the job.
  $view->init_display();
  $view->init_style();
  $view->style_plugin->options['grouping'] = 'job';

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

  // Add the job field .
  $view->display['default']->handler->override_option('fields', array(
    'name' => array(
      'id' => 'name',
      'table' => 'views_test',
      'field' => 'name',
      'relationship' => 'none',
    ),
    'job' => array(
      'id' => 'job',
      'table' => 'views_test',
      '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_name = 'John';
  $expected['Job: Singer'][0]->views_test_job = 'Singer';
  $expected['Job: Singer'][0]->views_test_id = '1';
  $expected['Job: Singer'][1] = new StdClass();
  $expected['Job: Singer'][1]->views_test_name = 'George';
  $expected['Job: Singer'][1]->views_test_job = 'Singer';
  $expected['Job: Singer'][1]->views_test_id = '2';
  $expected['Job: Drummer'] = array();
  $expected['Job: Drummer'][2] = new StdClass();
  $expected['Job: Drummer'][2]->views_test_name = 'Ringo';
  $expected['Job: Drummer'][2]->views_test_job = 'Drummer';
  $expected['Job: Drummer'][2]->views_test_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_name = 'John';
  $expected['Job: Singer']['rows'][0]->views_test_job = 'Singer';
  $expected['Job: Singer']['rows'][0]->views_test_id = '1';
  $expected['Job: Singer']['rows'][1] = new StdClass();
  $expected['Job: Singer']['rows'][1]->views_test_name = 'George';
  $expected['Job: Singer']['rows'][1]->views_test_job = 'Singer';
  $expected['Job: Singer']['rows'][1]->views_test_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_name = 'Ringo';
  $expected['Job: Drummer']['rows'][2]->views_test_job = 'Drummer';
  $expected['Job: Drummer']['rows'][2]->views_test_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.'));
}