function FieldPluginBaseTest::providerTestRenderAsLinkWithUrlAndOptions

Same name in other branches
  1. 8.9.x core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTest::providerTestRenderAsLinkWithUrlAndOptions()
  2. 10 core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTest::providerTestRenderAsLinkWithUrlAndOptions()
  3. 11.x core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTest::providerTestRenderAsLinkWithUrlAndOptions()

Data provider for ::testRenderAsLinkWithUrlAndOptions().

Return value

array Array of test data.

File

core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php, line 430

Class

FieldPluginBaseTest
@coversDefaultClass \Drupal\views\Plugin\views\field\FieldPluginBase @group views

Namespace

Drupal\Tests\views\Unit\Plugin\field

Code

public function providerTestRenderAsLinkWithUrlAndOptions() {
    $data = [];
    // Simple path with default options.
    $url = Url::fromRoute('test_route');
    $data[] = [
        $url,
        [],
        clone $url,
        '/test-path',
        clone $url,
        '<a href="/test-path">value</a>',
    ];
    // Simple URL with parameters.
    $url_parameters = Url::fromRoute('test_route', [
        'key' => 'value',
    ]);
    $data[] = [
        $url_parameters,
        [],
        clone $url_parameters,
        '/test-path/value',
        clone $url_parameters,
        '<a href="/test-path/value">value</a>',
    ];
    // Add a fragment.
    $url = Url::fromRoute('test_route');
    $url_with_fragment = Url::fromRoute('test_route');
    $options = [
        'fragment' => 'test',
    ] + $this->defaultUrlOptions;
    $url_with_fragment->setOptions($options);
    $data[] = [
        $url,
        [
            'fragment' => 'test',
        ],
        $url_with_fragment,
        '/test-path#test',
        clone $url_with_fragment,
        '<a href="/test-path#test">value</a>',
    ];
    // Rel attributes.
    $url = Url::fromRoute('test_route');
    $url_with_rel = Url::fromRoute('test_route');
    $options = [
        'attributes' => [
            'rel' => 'up',
        ],
    ] + $this->defaultUrlOptions;
    $url_with_rel->setOptions($options);
    $data[] = [
        $url,
        [
            'rel' => 'up',
        ],
        clone $url,
        '/test-path',
        $url_with_rel,
        '<a href="/test-path" rel="up">value</a>',
    ];
    // Target attributes.
    $url = Url::fromRoute('test_route');
    $url_with_target = Url::fromRoute('test_route');
    $options = [
        'attributes' => [
            'target' => '_blank',
        ],
    ] + $this->defaultUrlOptions;
    $url_with_target->setOptions($options);
    $data[] = [
        $url,
        [
            'target' => '_blank',
        ],
        $url_with_target,
        '/test-path',
        clone $url_with_target,
        '<a href="/test-path" target="_blank">value</a>',
    ];
    // Link attributes.
    $url = Url::fromRoute('test_route');
    $url_with_link_attributes = Url::fromRoute('test_route');
    $options = [
        'attributes' => [
            'foo' => 'bar',
        ],
    ] + $this->defaultUrlOptions;
    $url_with_link_attributes->setOptions($options);
    $data[] = [
        $url,
        [
            'link_attributes' => [
                'foo' => 'bar',
            ],
        ],
        clone $url,
        '/test-path',
        $url_with_link_attributes,
        '<a href="/test-path" foo="bar">value</a>',
    ];
    // Manual specified query.
    $url = Url::fromRoute('test_route');
    $url_with_query = Url::fromRoute('test_route');
    $options = [
        'query' => [
            'foo' => 'bar',
        ],
    ] + $this->defaultUrlOptions;
    $url_with_query->setOptions($options);
    $data[] = [
        $url,
        [
            'query' => [
                'foo' => 'bar',
            ],
        ],
        clone $url_with_query,
        '/test-path?foo=bar',
        $url_with_query,
        '<a href="/test-path?foo=bar">value</a>',
    ];
    // Query specified as part of the path.
    $url = Url::fromRoute('test_route')->setOption('query', [
        'foo' => 'bar',
    ]);
    $url_with_query = clone $url;
    $url_with_query->setOptions([
        'query' => [
            'foo' => 'bar',
        ],
    ] + $url_with_query->getOptions());
    $data[] = [
        $url,
        [],
        $url_with_query,
        '/test-path?foo=bar',
        clone $url,
        '<a href="/test-path?foo=bar">value</a>',
    ];
    // Query specified as option and path.
    $url = Url::fromRoute('test_route')->setOption('query', [
        'foo' => 'bar',
    ]);
    $url_with_query = Url::fromRoute('test_route');
    $options = [
        'query' => [
            'key' => 'value',
        ],
    ] + $this->defaultUrlOptions;
    $url_with_query->setOptions($options);
    $data[] = [
        $url,
        [
            'query' => [
                'key' => 'value',
            ],
        ],
        $url_with_query,
        '/test-path?key=value',
        clone $url_with_query,
        '<a href="/test-path?key=value">value</a>',
    ];
    // Alias flag.
    $url = Url::fromRoute('test_route');
    $url_without_alias = Url::fromRoute('test_route');
    $options = [
        'alias' => TRUE,
    ] + $this->defaultUrlOptions;
    $url_without_alias->setOptions($options);
    $data[] = [
        $url,
        [
            'alias' => TRUE,
        ],
        $url_without_alias,
        '/test-path',
        clone $url_without_alias,
        '<a href="/test-path">value</a>',
    ];
    // Language flag.
    $language = new Language([
        'id' => 'fr',
    ]);
    $url = Url::fromRoute('test_route');
    $url_with_language = Url::fromRoute('test_route');
    $options = [
        'language' => $language,
    ] + $this->defaultUrlOptions;
    $url_with_language->setOptions($options);
    $data[] = [
        $url,
        [
            'language' => $language,
        ],
        $url_with_language,
        '/fr/test-path',
        clone $url_with_language,
        '<a href="/fr/test-path" hreflang="fr">value</a>',
    ];
    // Entity flag.
    $entity = $this->createMock('Drupal\\Core\\Entity\\EntityInterface');
    $url = Url::fromRoute('test_route');
    $url_with_entity = Url::fromRoute('test_route');
    $options = [
        'entity' => $entity,
    ] + $this->defaultUrlOptions;
    $url_with_entity->setOptions($options);
    $data[] = [
        $url,
        [
            'entity' => $entity,
        ],
        $url_with_entity,
        '/test-path',
        clone $url_with_entity,
        '<a href="/test-path">value</a>',
    ];
    // Test entity_type flag.
    $entity_type_id = 'node';
    $url = Url::fromRoute('test_route');
    $url_with_entity_type = Url::fromRoute('test_route');
    $options = [
        'entity_type' => $entity_type_id,
    ] + $this->defaultUrlOptions;
    $url_with_entity_type->setOptions($options);
    $data[] = [
        $url,
        [
            'entity_type' => $entity_type_id,
        ],
        $url_with_entity_type,
        '/test-path',
        clone $url_with_entity_type,
        '<a href="/test-path">value</a>',
    ];
    // Test prefix.
    $url = Url::fromRoute('test_route');
    $data[] = [
        $url,
        [
            'prefix' => 'test_prefix',
        ],
        clone $url,
        '/test-path',
        clone $url,
        '<a href="/test-path">value</a>',
        'test_prefix<a href="/test-path">value</a>',
    ];
    // Test suffix.
    $url = Url::fromRoute('test_route');
    $data[] = [
        $url,
        [
            'suffix' => 'test_suffix',
        ],
        clone $url,
        '/test-path',
        clone $url,
        '<a href="/test-path">value</a>',
        '<a href="/test-path">value</a>test_suffix',
    ];
    return $data;
}

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