You are here

public function StyleSerializerTest::testGroupRows in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/rest/src/Tests/Views/StyleSerializerTest.php \Drupal\rest\Tests\Views\StyleSerializerTest::testGroupRows()

Tests the "Grouped rows" functionality.

File

core/modules/rest/src/Tests/Views/StyleSerializerTest.php, line 553
Contains \Drupal\rest\Tests\Views\StyleSerializerTest.

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\rest\Tests\Views

Code

public function testGroupRows() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $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
    ->assertEqual($serializer
    ->serialize($expected, 'json'), (string) $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
    ->assertEqual($serializer
    ->serialize($expected, 'json'), (string) $renderer
    ->renderRoot($build));
}