function LinkSeparateFormatterDisplayTest::testLinkSeparateFormatter
Tests that links are rendered correctly.
Run tests without dataProvider to improve speed.
See also
\Drupal\Tests\link\Traits\LinkInputValuesTraits::getLinkInputValues()
File
-
core/
modules/ link/ tests/ src/ Kernel/ LinkSeparateFormatterDisplayTest.php, line 64
Class
- LinkSeparateFormatterDisplayTest
- Tests the 'link_separate' field formatter.
Namespace
Drupal\Tests\link\KernelCode
public function testLinkSeparateFormatter() : void {
// Create an entity with link field values provided.
$entity = EntityTest::create();
$entity->field_test
->setValue($this->getLinkInputValues());
foreach ($this->getTestCases() as $case_name => $case_options) {
[
$display_settings,
$expected_results,
] = array_values($case_options);
$this->assertEquals(count($this->getLinkInputValues()), count($expected_results), "Each field delta have expected result. Case name: '{$case_name}'");
// Render link field with 'link_separate' formatter and custom
// display settings. Hide field label.
$render_array = $entity->field_test
->view([
'label' => 'hidden',
'settings' => $display_settings,
'type' => 'link_separate',
]);
$output = (string) \Drupal::service('renderer')->renderRoot($render_array);
// Convert each field delta value to separate array item.
$field_deltas_display = explode("\n ", trim($output));
// Check results.
foreach ($expected_results as $delta => $expected_result) {
$rendered_delta = trim($field_deltas_display[$delta]);
$this->assertEquals($expected_result, $rendered_delta, "Test case failed. Case name: '{$case_name}'. Delta: '{$delta}'. Uri: '{$this->getLinkInputValues()[$delta]['uri']}'");
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.