function StyleSerializerTest::testGroupRows

Same name and namespace in other branches
  1. 9 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testGroupRows()
  2. 8.9.x core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testGroupRows()
  3. 11.x core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testGroupRows()

Tests the "Grouped rows" functionality.

File

core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php, line 171

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\Tests\rest\Functional\Views

Code

public function testGroupRows() : void {
  $this->drupalCreateContentType([
    'type' => 'page',
  ]);
  // Create a text field with cardinality set to unlimited.
  $field_name = 'field_group_rows';
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => 'string',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ]);
  $field_storage->save();
  // Create an instance of the text field on the content type.
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'page',
  ]);
  $field->save();
  $grouped_field_values = [
    'a',
    'b',
    'c',
  ];
  $edit = [
    'title' => $this->randomMachineName(),
    $field_name => $grouped_field_values,
  ];
  $this->drupalCreateNode($edit);
  $view = Views::getView('test_serializer_node_display_field');
  $view->setDisplay('rest_export_1');
  // Override the view's fields to include the field_group_rows field, set the
  // group_rows setting to true.
  $fields = [
    $field_name => [
      'id' => $field_name,
      'table' => 'node__' . $field_name,
      'field' => $field_name,
      'type' => 'string',
      'group_rows' => TRUE,
    ],
  ];
  $view->displayHandlers
    ->get('default')
    ->overrideOption('fields', $fields);
  $build = $view->preview();
  // Get the serializer service.
  $serializer = $this->container
    ->get('serializer');
  // Check if the field_group_rows field is grouped.
  $expected = [];
  $expected[] = [
    $field_name => implode(', ', $grouped_field_values),
  ];
  $this->assertEquals($serializer->serialize($expected, 'json'), (string) $this->renderer
    ->renderRoot($build));
  // Set the group rows setting to false.
  $view = Views::getView('test_serializer_node_display_field');
  $view->setDisplay('rest_export_1');
  $fields[$field_name]['group_rows'] = FALSE;
  $view->displayHandlers
    ->get('default')
    ->overrideOption('fields', $fields);
  $build = $view->preview();
  // Check if the field_group_rows field is ungrouped and displayed per row.
  $expected = [];
  foreach ($grouped_field_values as $grouped_field_value) {
    $expected[] = [
      $field_name => $grouped_field_value,
    ];
  }
  $this->assertEquals($serializer->serialize($expected, 'json'), (string) $this->renderer
    ->renderRoot($build));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.