public function StyleSerializerTest::testUIFieldAlias

Test the field ID alias functionality of the DataFieldRow plugin.

File

drupal/core/modules/rest/lib/Drupal/rest/Tests/Views/StyleSerializerTest.php, line 176
Contains \Drupal\rest\Tests\Views\StyleSerializerTest.

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\rest\Tests\Views

Code

public function testUIFieldAlias() {
  $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
    ->assertLinkByHref($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.
  $this
    ->drupalPost($row_options, array(
    'row_options[field_options][name][alias]' => '',
  ), t('Apply'));
  $this
    ->drupalPost(NULL, array(), t('Save'));
  $view = views_get_view('test_serializer_display_field');
  $view
    ->setDisplay('rest_export_1');
  $this
    ->executeView($view);
  $expected = array();
  foreach ($view->result as $row) {
    $expected_row = array();
    foreach ($view->field as $id => $field) {
      $expected_row[$id] = $field
        ->render($row);
    }
    $expected[] = $expected_row;
  }
  $this
    ->assertIdentical($this
    ->drupalGetJSON('test/serialize/field'), $expected);

  // Test a random aliases for fields, they should be replaced.
  $alias_map = array(
    'name' => $this
      ->randomName(),
    // Use # to produce an invalid character for the validation.
    'nothing' => '#' . $this
      ->randomName(),
    'created' => 'created',
  );
  $edit = array(
    'row_options[field_options][name][alias]' => $alias_map['name'],
    'row_options[field_options][nothing][alias]' => $alias_map['nothing'],
  );
  $this
    ->drupalPost($row_options, $edit, t('Apply'));
  $this
    ->assertText(t('The machine-readable name must contain only letters, numbers, dashes and underscores.'));

  // Change the map alias value to a valid one.
  $alias_map['nothing'] = $this
    ->randomName();
  $edit = array(
    'row_options[field_options][name][alias]' => $alias_map['name'],
    'row_options[field_options][nothing][alias]' => $alias_map['nothing'],
  );
  $this
    ->drupalPost($row_options, $edit, t('Apply'));
  $this
    ->drupalPost(NULL, array(), t('Save'));
  $view = views_get_view('test_serializer_display_field');
  $view
    ->setDisplay('rest_export_1');
  $this
    ->executeView($view);
  $expected = array();
  foreach ($view->result as $row) {
    $expected_row = array();
    foreach ($view->field as $id => $field) {
      $expected_row[$alias_map[$id]] = $field
        ->render($row);
    }
    $expected[] = $expected_row;
  }
  $this
    ->assertIdentical($this
    ->drupalGetJSON('test/serialize/field'), $expected);
}