You are here

public function StyleSerializerTest::testFieldRawOutput in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testFieldRawOutput()
  2. 9 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testFieldRawOutput()

Tests the raw output options for row field rendering.

File

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

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\Tests\rest\Functional\Views

Code

public function testFieldRawOutput() {
  $this
    ->drupalLogin($this->adminUser);

  // Test the UI settings for adding field ID aliases.
  $this
    ->drupalGet('admin/structure/views/view/test_serializer_display_field/edit/rest_export_1');
  $row_options = 'admin/structure/views/nojs/display/test_serializer_display_field/rest_export_1/row_options';
  $this
    ->assertSession()
    ->linkByHrefExists($row_options);

  // Test an empty string for an alias, this should not be used. This also
  // tests that the form can be submitted with no aliases.
  $values = [
    'row_options[field_options][created][raw_output]' => '1',
    'row_options[field_options][name][raw_output]' => '1',
  ];
  $this
    ->drupalGet($row_options);
  $this
    ->submitForm($values, 'Apply');
  $this
    ->submitForm([], 'Save');
  $view = Views::getView('test_serializer_display_field');
  $view
    ->setDisplay('rest_export_1');
  $this
    ->executeView($view);
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_test');

  // Update the name for each to include a script tag.
  foreach ($storage
    ->loadMultiple() as $entity_test) {
    $name = $entity_test->name->value;
    $entity_test
      ->set('name', "<script>{$name}</script>");
    $entity_test
      ->save();
  }

  // Just test the raw 'created' value against each row.
  foreach (Json::decode($this
    ->drupalGet('test/serialize/field', [
    'query' => [
      '_format' => 'json',
    ],
  ])) as $index => $values) {
    $this
      ->assertSame($view->result[$index]->views_test_data_created, $values['created'], 'Expected raw created value found.');
    $this
      ->assertSame($view->result[$index]->views_test_data_name, $values['name'], 'Expected raw name value found.');
  }

  // Test result with an excluded field.
  $view
    ->setDisplay('rest_export_1');
  $view->displayHandlers
    ->get('rest_export_1')
    ->overrideOption('fields', [
    'name' => [
      'id' => 'name',
      'table' => 'views_test_data',
      'field' => 'name',
      'relationship' => 'none',
    ],
    'created' => [
      'id' => 'created',
      'exclude' => TRUE,
      'table' => 'views_test_data',
      'field' => 'created',
      'relationship' => 'none',
    ],
  ]);
  $view
    ->save();
  $this
    ->executeView($view);
  foreach (Json::decode($this
    ->drupalGet('test/serialize/field', [
    'query' => [
      '_format' => 'json',
    ],
  ])) as $index => $values) {
    $this
      ->assertTrue(!isset($values['created']), 'Excluded value not found.');
  }

  // Test that the excluded field is not shown in the row options.
  $this
    ->drupalGet('admin/structure/views/nojs/display/test_serializer_display_field/rest_export_1/row_options');
  $this
    ->assertSession()
    ->pageTextNotContains('created');
}