You are here

public function StyleSerializerTest::testUIFieldAlias 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::testUIFieldAlias()

Test the field ID alias functionality of the DataFieldRow plugin.

File

core/modules/rest/src/Tests/Views/StyleSerializerTest.php, line 371
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
    ->drupalPostForm($row_options, array(
    'row_options[field_options][name][alias]' => '',
  ), t('Apply'));
  $this
    ->drupalPostForm(NULL, array(), t('Save'));
  $view = Views::getView('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'), $this
    ->castSafeStrings($expected));

  // Test a random aliases for fields, they should be replaced.
  $alias_map = array(
    'name' => $this
      ->randomMachineName(),
    // Use # to produce an invalid character for the validation.
    'nothing' => '#' . $this
      ->randomMachineName(),
    'created' => 'created',
  );
  $edit = array(
    'row_options[field_options][name][alias]' => $alias_map['name'],
    'row_options[field_options][nothing][alias]' => $alias_map['nothing'],
  );
  $this
    ->drupalPostForm($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
    ->randomMachineName();
  $edit = array(
    'row_options[field_options][name][alias]' => $alias_map['name'],
    'row_options[field_options][nothing][alias]' => $alias_map['nothing'],
  );
  $this
    ->drupalPostForm($row_options, $edit, t('Apply'));
  $this
    ->drupalPostForm(NULL, array(), t('Save'));
  $view = Views::getView('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'), $this
    ->castSafeStrings($expected));
}