function StyleSerializerTest::testUIFieldAlias

Same name in other branches
  1. 8.9.x core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testUIFieldAlias()

Tests the field ID alias functionality of the DataFieldRow plugin.

File

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

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\Tests\rest\Functional\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->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.
    $this->drupalGet($row_options);
    $this->submitForm([
        'row_options[field_options][name][alias]' => '',
    ], 'Apply');
    $this->submitForm([], 'Save');
    $view = Views::getView('test_serializer_display_field');
    $view->setDisplay('rest_export_1');
    $this->executeView($view);
    $expected = [];
    foreach ($view->result as $row) {
        $expected_row = [];
        foreach ($view->field as $id => $field) {
            $expected_row[$id] = $field->render($row);
        }
        $expected[] = $expected_row;
    }
    $this->assertEquals($expected, Json::decode($this->drupalGet('test/serialize/field', [
        'query' => [
            '_format' => 'json',
        ],
    ])));
    // Test a random aliases for fields, they should be replaced.
    $alias_map = [
        'name' => $this->randomMachineName(),
        // Use # to produce an invalid character for the validation.
'nothing' => '#' . $this->randomMachineName(),
        'created' => 'created',
    ];
    $edit = [
        'row_options[field_options][name][alias]' => $alias_map['name'],
        'row_options[field_options][nothing][alias]' => $alias_map['nothing'],
    ];
    $this->drupalGet($row_options);
    $this->submitForm($edit, 'Apply');
    $this->assertSession()
        ->pageTextContains('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 = [
        'row_options[field_options][name][alias]' => $alias_map['name'],
        'row_options[field_options][nothing][alias]' => $alias_map['nothing'],
    ];
    $this->drupalGet($row_options);
    $this->submitForm($edit, 'Apply');
    $this->submitForm([], 'Save');
    $view = Views::getView('test_serializer_display_field');
    $view->setDisplay('rest_export_1');
    $this->executeView($view);
    $expected = [];
    foreach ($view->result as $row) {
        $expected_row = [];
        foreach ($view->field as $id => $field) {
            $expected_row[$alias_map[$id]] = $field->render($row);
        }
        $expected[] = $expected_row;
    }
    $this->assertEquals($expected, Json::decode($this->drupalGet('test/serialize/field', [
        'query' => [
            '_format' => 'json',
        ],
    ])));
}

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