function StyleSerializerEntityTest::testUIFieldAlias
Same name in other branches
- 10 core/modules/rest/tests/src/Functional/Views/StyleSerializerEntityTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerEntityTest::testUIFieldAlias()
Tests the field ID alias functionality of the DataFieldRow plugin.
File
-
core/
modules/ rest/ tests/ src/ Functional/ Views/ StyleSerializerEntityTest.php, line 387
Class
- StyleSerializerEntityTest
- Tests the serializer style plugin.
Namespace
Drupal\Tests\rest\Functional\ViewsCode
public function testUIFieldAlias() : void {
$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.