diff --git a/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php index 3f100c6..04b2c00 100644 --- a/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php +++ b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php @@ -455,12 +455,7 @@ public function testRenderAsLinkWithPathAndTokens($path, $tokens, $link_html) { $alter = [ 'make_link' => TRUE, 'path' => $path, - 'url' => '', ]; - if ($link_html == 'external') { - $alter['external'] = TRUE; - $link_html = 'value'; - } $this->setUpUrlIntegrationServices(); $this->setupDisplayWithEmptyArgumentsAndFields(); @@ -469,45 +464,17 @@ public function testRenderAsLinkWithPathAndTokens($path, $tokens, $link_html) { $field->field_alias = 'key'; $row = new ResultRow(['key' => 'value']); - if (!empty(explode('/', $path)[1])) { - $build = [ - '#type' => 'inline_template', - '#template' => 'test-path/' . explode('/', $path)[1], - '#context' => ['foo' => 123], - '#post_render' => [function() {}], - ]; - - $this->renderer->expects($this->once()) - ->method('renderPlain') - ->with($build) - ->willReturn('test-path/123'); - } - elseif ($link_html == 'value') { - $build = [ - '#type' => 'inline_template', - '#template' => $path, - '#context' => ['foo' => ''], - '#post_render' => [function() {}], - ]; - - $this->renderer->expects($this->once()) - ->method('renderPlain') - ->with($build) - ->willReturn(''); - } - else { - $build = [ - '#type' => 'inline_template', - '#template' => $path, - '#context' => ['foo' => 'http://www.drupal.org'], - '#post_render' => [function() {}], - ]; - - $this->renderer->expects($this->once()) - ->method('renderPlain') - ->with($build) - ->willReturn('http://www.drupal.org'); - } + $build =[ + '#type' => 'inline_template', + '#template' => 'test-path/' . explode('/', $path)[1], + '#context' => ['foo' => 123], + '#post_render' => [function() {}], + ]; + + $this->renderer->expects($this->once()) + ->method('renderPlain') + ->with($build) + ->willReturn('base:test-path/123'); $result = $field->advancedRender($row); $this->assertEquals($link_html, $result); @@ -534,9 +501,61 @@ public function providerTestRenderAsLinkWithPathAndTokens() { $data[] = ['test-path/{{ foo }}', $tokens, $link_html]; $data[] = ['test-path/{{ foo }}', $tokens, $link_html]; $data[] = ['test-path/{{ foo }}', $tokens, $link_html]; - $data[] = ['{{ foo }}', ['{{ foo }}' => 'http://www.drupal.org'], 'value']; - $data[] = ['{{ foo }}', ['{{ foo }}' => ''], 'value']; - $data[] = ['{{ foo }}', ['{{ foo }}' => ''], 'external']; + + return $data; + } + + /** + * Test rendering of a link with a path and options. + * + * @dataProvider providerTestRenderAsExternalLinkWithPathAndTokens + * @covers ::renderAsLink + */ + public function testRenderAsExternalLinkWithPathAndTokens($path, $tokens, $link_html, $context) { + $alter = [ + 'make_link' => TRUE, + 'path' => $path, + 'url' => '', + ]; + if (isset($context['alter'])) { + $alter += $context['alter']; + } + + $this->setUpUrlIntegrationServices(); + $this->setupDisplayWithEmptyArgumentsAndFields(); + $this->executable->build_info['substitutions'] = $tokens; + $field = $this->setupTestField(['alter' => $alter]); + $field->field_alias = 'key'; + $row = new ResultRow(['key' => 'value']); + + $build = [ + '#type' => 'inline_template', + '#template' => $path, + '#context' => ['foo' => $context['context_path']], + '#post_render' => [function() {}], + ]; + + $this->renderer->expects($this->once()) + ->method('renderPlain') + ->with($build) + ->willReturn($context['context_path']); + + $result = $field->advancedRender($row); + $this->assertEquals($link_html, $result); + } + + /** + * Data provider for ::testRenderAsExternalLinkWithPathAndTokens(). + * + * @return array + * Test data. + */ + public function providerTestRenderAsExternalLinkWithPathAndTokens() { + $data = []; + + $data[] = ['{{ foo }}', ['{{ foo }}' => 'http://www.drupal.org'], 'value', ['context_path' => 'http://www.drupal.org']]; + $data[] = ['{{ foo }}', ['{{ foo }}' => ''], 'value', ['context_path' => '']]; + $data[] = ['{{ foo }}', ['{{ foo }}' => ''], 'value', ['context_path' => '', 'alter' => ['external' => TRUE]]]; return $data; }