function LinkFormatterDisplayTest::testLinkFormatter

Tests that links are rendered correctly.

Run tests without dataProvider to improve speed.

See also

\Drupal\Tests\link\Traits\LinkInputValuesTraits::getLinkInputValues()

self::getTestCases()

File

core/modules/link/tests/src/Kernel/LinkFormatterDisplayTest.php, line 64

Class

LinkFormatterDisplayTest
Tests the default 'link' field formatter.

Namespace

Drupal\Tests\link\Kernel

Code

public function testLinkFormatter() : 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 default 'link' formatter and custom
        // display settings. Hide field label.
        $render_array = $entity->field_test
            ->view([
            'label' => 'hidden',
            'settings' => $display_settings,
        ]);
        $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]);
            $message = "Test case failed. Case name: '{$case_name}'. Delta: '{$delta}'. Uri: '{$this->getLinkInputValues()[$delta]['uri']}'";
            $this->assertEquals($expected_result, $rendered_delta, $message);
        }
    }
}

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