function LinkFieldTest::testLinkFormatterQueryParametersDuplication

Same name in other branches
  1. 10 core/modules/link/tests/src/Functional/LinkFieldTest.php \Drupal\Tests\link\Functional\LinkFieldTest::testLinkFormatterQueryParametersDuplication()

Tests the default 'link' formatter with complex query parameters.

File

core/modules/link/tests/src/Functional/LinkFieldTest.php, line 555

Class

LinkFieldTest
Tests link field widgets and formatters.

Namespace

Drupal\Tests\link\Functional

Code

public function testLinkFormatterQueryParametersDuplication() : void {
    $test_urls = $this->getUrlWithComplexQuery();
    $field_name = $this->randomMachineName();
    // Create a field with settings to validate.
    $this->fieldStorage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'entity_test',
        'type' => 'link',
        'cardinality' => count($test_urls),
    ]);
    $this->fieldStorage
        ->save();
    FieldConfig::create([
        'field_storage' => $this->fieldStorage,
        'label' => 'Read more about this entity',
        'bundle' => 'entity_test',
        'settings' => [
            'title' => DRUPAL_OPTIONAL,
            'link_type' => LinkItemInterface::LINK_GENERIC,
        ],
    ])
        ->save();
    
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getFormDisplay('entity_test', 'entity_test', 'default')
        ->setComponent($field_name, [
        'type' => 'link_default',
    ])
        ->save();
    $display_options = [
        'type' => 'link',
        'label' => 'hidden',
    ];
    $display_repository->getViewDisplay('entity_test', 'entity_test', 'full')
        ->setComponent($field_name, $display_options)
        ->save();
    // Create an entity with link field values provided
    // by $this->getUrlWithComplexQuery().
    $entity = EntityTest::create();
    $links = [];
    // Prepare values for field.
    foreach ($test_urls as $key => $test_url) {
        $links[$key] = [
            'uri' => 'internal:' . $test_url['inputByUser'],
            'title' => $test_url['inputByUser'],
        ];
    }
    $entity->{$field_name}
        ->setValue($links);
    $entity->save();
    // Verify that the link is output according to the formatter settings.
    // Not using generatePermutations(), since that leads to 32 cases, which
    // would not test actual link field formatter functionality but rather
    // the link generator and options/attributes. Only 'url_plain' has a
    // dependency on 'url_only'.
    $options = [
        'trim_length' => [
            NULL,
            6,
        ],
        'rel' => [
            NULL,
            'nofollow',
        ],
        'target' => [
            NULL,
            '_blank',
        ],
        'url_only' => [
            [
                'url_only' => FALSE,
            ],
            [
                'url_only' => FALSE,
                'url_plain' => TRUE,
            ],
            [
                'url_only' => TRUE,
            ],
            [
                'url_only' => TRUE,
                'url_plain' => TRUE,
            ],
        ],
    ];
    foreach ($options as $setting => $values) {
        foreach ($values as $new_value) {
            // Update the field formatter settings.
            if (!is_array($new_value)) {
                $display_options['settings'] = [
                    $setting => $new_value,
                ];
            }
            else {
                $display_options['settings'] = $new_value;
            }
            $display_repository->getViewDisplay('entity_test', 'entity_test', 'full')
                ->setComponent($field_name, $display_options)
                ->save();
            $output = $this->renderTestEntity($entity->id());
            foreach ($test_urls as $test_url) {
                $url = $test_url['renderedHref'];
                $title = $test_url['inputByUser'];
                switch ($setting) {
                    case 'trim_length':
                        $title = isset($new_value) ? Unicode::truncate($title, $new_value, FALSE, TRUE) : $title;
                        $this->assertStringContainsString('<a href="' . $url . '">' . Html::escape($title) . '</a>', $output);
                        break;
                    case 'rel':
                        $rel = isset($new_value) ? ' rel="' . $new_value . '"' : '';
                        $this->assertStringContainsString('<a href="' . $url . '"' . $rel . '>' . Html::escape($title) . '</a>', $output);
                        break;
                    case 'target':
                        $target = isset($new_value) ? ' target="' . $new_value . '"' : '';
                        $this->assertStringContainsString('<a href="' . $url . '"' . $target . '>' . Html::escape($title) . '</a>', $output);
                        break;
                    case 'url_only':
                        // In this case, $new_value is an array.
                        if (!$new_value['url_only']) {
                            $this->assertStringContainsString('<a href="' . $url . '">' . Html::escape($title) . '</a>', $output);
                            break;
                        }
                        if (empty($new_value['url_plain'])) {
                            $this->assertStringContainsString('<a href="' . $url . '">' . $url . '</a>', $output);
                            break;
                        }
                        $this->assertStringNotContainsString('<a href="' . $url . '">' . $url . '</a>', $output);
                        $this->assertStringContainsString($url, $output);
                        break;
                }
            }
        }
    }
}

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