0b33a58917af55c1de468f83e2d73639aec93234
[yaffs-website] / web / core / modules / views / tests / src / Unit / Plugin / field / FieldPluginBaseTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTest.
6  */
7
8 namespace Drupal\Tests\views\Unit\Plugin\field;
9
10 use Drupal\Core\GeneratedUrl;
11 use Drupal\Core\Language\Language;
12 use Drupal\Core\Render\Markup;
13 use Drupal\Core\Url;
14 use Drupal\Core\Utility\LinkGenerator;
15 use Drupal\Core\Utility\LinkGeneratorInterface;
16 use Drupal\Core\Utility\UnroutedUrlAssembler;
17 use Drupal\Tests\UnitTestCase;
18 use Drupal\views\Plugin\views\field\FieldPluginBase;
19 use Drupal\views\ResultRow;
20 use Symfony\Component\DependencyInjection\ContainerBuilder;
21 use Symfony\Component\HttpFoundation\Request;
22 use Symfony\Component\HttpFoundation\RequestStack;
23 use Symfony\Component\Routing\Route;
24
25 /**
26  * @coversDefaultClass \Drupal\views\Plugin\views\field\FieldPluginBase
27  * @group views
28  */
29 class FieldPluginBaseTest extends UnitTestCase {
30
31   /**
32    * The configuration of the plugin under test.
33    *
34    * @var array
35    */
36   protected $configuration = [];
37
38   /**
39    * The ID plugin of the plugin under test.
40    *
41    * @var string
42    */
43   protected $pluginId = 'field_test';
44
45   /**
46    * The definition of the plugin under test.
47    *
48    * @var array
49    */
50   protected $pluginDefinition = [];
51
52   /**
53    * Default configuration for URL output.
54    *
55    * @var array
56    */
57   protected $defaultUrlOptions = [
58     'absolute' => FALSE,
59     'alias' => FALSE,
60     'entity' => NULL,
61     'entity_type' => NULL,
62     'language' => NULL,
63     'query' => [],
64     'set_active_class' => FALSE,
65   ];
66
67   /**
68    * The mocked link generator.
69    *
70    * @var \Drupal\Core\Utility\LinkGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
71    */
72   protected $linkGenerator;
73
74   /**
75    * The mocked view executable.
76    *
77    * @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject
78    */
79   protected $executable;
80
81   /**
82    * The mocked display plugin instance.
83    *
84    * @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject
85    */
86   protected $display;
87
88   /**
89    * The mocked url generator.
90    *
91    * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
92    */
93   protected $urlGenerator;
94
95   /**
96    * The mocked path validator.
97    *
98    * @var \Drupal\Core\Path\PathValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
99    */
100   protected $pathValidator;
101
102   /**
103    * The unrouted url assembler service.
104    *
105    * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface|\PHPUnit_Framework_MockObject_MockObject
106    */
107   protected $unroutedUrlAssembler;
108
109   /**
110    * The request stack.
111    *
112    * @var \Symfony\Component\HttpFoundation\RequestStack
113    */
114   protected $requestStack;
115
116   /**
117    * The mocked path processor.
118    *
119    * @var \Drupal\Core\PathProcessor\OutboundPathProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
120    */
121   protected $pathProcessor;
122
123   /**
124    * The mocked path renderer.
125    *
126    * @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
127    */
128   protected $renderer;
129
130   /**
131    * {@inheritdoc}
132    */
133   protected function setUp() {
134     parent::setUp();
135
136     $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
137       ->disableOriginalConstructor()
138       ->getMock();
139     $this->display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
140       ->disableOriginalConstructor()
141       ->getMock();
142
143     $route_provider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
144     $route_provider->expects($this->any())
145       ->method('getRouteByName')
146       ->with('test_route')
147       ->willReturn(new Route('/test-path'));
148
149     $this->urlGenerator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
150     $this->pathValidator = $this->getMock('Drupal\Core\Path\PathValidatorInterface');
151
152     $this->requestStack = new RequestStack();
153     $this->requestStack->push(new Request());
154
155     $this->unroutedUrlAssembler = $this->getMock('Drupal\Core\Utility\UnroutedUrlAssemblerInterface');
156     $this->linkGenerator = $this->getMock('Drupal\Core\Utility\LinkGeneratorInterface');
157
158     $this->renderer = $this->getMock('Drupal\Core\Render\RendererInterface');
159
160     $container_builder = new ContainerBuilder();
161     $container_builder->set('url_generator', $this->urlGenerator);
162     $container_builder->set('path.validator', $this->pathValidator);
163     $container_builder->set('unrouted_url_assembler', $this->unroutedUrlAssembler);
164     $container_builder->set('request_stack', $this->requestStack);
165     $container_builder->set('renderer', $this->renderer);
166     \Drupal::setContainer($container_builder);
167   }
168
169   /**
170    * Sets up the unrouted url assembler and the link generator.
171    */
172   protected function setUpUrlIntegrationServices() {
173     $this->pathProcessor = $this->getMock('Drupal\Core\PathProcessor\OutboundPathProcessorInterface');
174     $this->unroutedUrlAssembler = new UnroutedUrlAssembler($this->requestStack, $this->pathProcessor);
175
176     \Drupal::getContainer()->set('unrouted_url_assembler', $this->unroutedUrlAssembler);
177
178     $this->linkGenerator = new LinkGenerator($this->urlGenerator, $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'), $this->renderer);
179     $this->renderer
180       ->method('render')
181       ->willReturnCallback(
182         // Pretend to do a render.
183         function (&$elements, $is_root_call = FALSE) {
184           // Mock the ability to theme links
185           $link = $this->linkGenerator->generate($elements['#title'], $elements['#url']);
186           if (isset($elements['#prefix'])) {
187             $link = $elements['#prefix'] . $link;
188           }
189           if (isset($elements['#suffix'])) {
190             $link = $link . $elements['#suffix'];
191           }
192           return Markup::create($link);
193         }
194       );
195   }
196
197   /**
198    * Sets up a display with empty arguments and fields.
199    */
200   protected function setupDisplayWithEmptyArgumentsAndFields() {
201     $this->display->expects($this->any())
202       ->method('getHandlers')
203       ->willReturnMap([
204         ['argument', []],
205         ['field', []],
206       ]);
207   }
208
209   /**
210    * Test rendering as a link without a path.
211    *
212    * @covers ::renderAsLink
213    */
214   public function testRenderAsLinkWithoutPath() {
215     $alter = [
216       'make_link' => TRUE,
217     ];
218
219     $this->setUpUrlIntegrationServices();
220     $field = $this->setupTestField(['alter' => $alter]);
221     $field->field_alias = 'key';
222     $row = new ResultRow(['key' => 'value']);
223
224     $expected_result = 'value';
225     $result = $field->advancedRender($row);
226     $this->assertEquals($expected_result, $result);
227   }
228
229   /**
230    * Test rendering with a more link.
231    *
232    * @param string $path
233    *   An internal or external path.
234    * @param string $url
235    *   The final url used by the more link.
236    *
237    * @dataProvider providerTestRenderTrimmedWithMoreLinkAndPath
238    * @covers ::renderText
239    */
240   public function testRenderTrimmedWithMoreLinkAndPath($path, $url) {
241     $alter = [
242       'trim' => TRUE,
243       'max_length' => 7,
244       'more_link' => TRUE,
245       // Don't invoke translation.
246       'ellipsis' => FALSE,
247       'more_link_text' => 'more link',
248       'more_link_path' => $path,
249     ];
250
251     $this->display->expects($this->any())
252       ->method('getHandlers')
253       ->willReturnMap([
254         ['argument', []],
255         ['field', []],
256       ]);
257
258     $this->setUpUrlIntegrationServices();
259     $field = $this->setupTestField(['alter' => $alter]);
260     $field->field_alias = 'key';
261     $row = new ResultRow(['key' => 'a long value']);
262
263     $expected_result = 'a long <a href="' . $url . '" class="views-more-link">more link</a>';
264     $result = $field->advancedRender($row);
265     $this->assertEquals($expected_result, $result);
266   }
267
268   /**
269    * Data provider for ::testRenderTrimmedWithMoreLinkAndPath().
270    *
271    * @return array
272    *   Test data.
273    */
274   public function providerTestRenderTrimmedWithMoreLinkAndPath() {
275     $data = [];
276     // Simple path with default options.
277     $data[] = ['test-path', '/test-path'];
278     // Add a fragment.
279     $data[] = ['test-path#test', '/test-path#test'];
280     // Query specified as part of the path.
281     $data[] = ['test-path?foo=bar', '/test-path?foo=bar'];
282     // Empty path.
283     $data[] = ['', '/%3Cfront%3E'];
284     // Front page path.
285     $data[] = ['<front>', '/%3Cfront%3E'];
286
287     // External URL.
288     $data[] = ['https://www.drupal.org', 'https://www.drupal.org'];
289     $data[] = ['http://www.drupal.org', 'http://www.drupal.org'];
290     $data[] = ['www.drupal.org', '/www.drupal.org'];
291
292     return $data;
293   }
294
295   /**
296    * Tests the "No results text" rendering.
297    *
298    * @covers ::renderText
299    */
300   public function testRenderNoResult() {
301     $this->setupDisplayWithEmptyArgumentsAndFields();
302     $field = $this->setupTestField(['empty' => 'This <strong>should work</strong>.']);
303     $field->field_alias = 'key';
304     $row = new ResultRow(['key' => '']);
305
306     $expected_result = 'This <strong>should work</strong>.';
307     $result = $field->advancedRender($row);
308     $this->assertEquals($expected_result, $result);
309     $this->assertInstanceOf('\Drupal\views\Render\ViewsRenderPipelineMarkup', $result);
310   }
311
312   /**
313    * Test rendering of a link with a path and options.
314    *
315    * @dataProvider providerTestRenderAsLinkWithPathAndOptions
316    * @covers ::renderAsLink
317    */
318   public function testRenderAsLinkWithPathAndOptions($path, $alter, $link_html, $final_html = NULL) {
319     $alter += [
320       'make_link' => TRUE,
321       'path' => $path,
322     ];
323
324     $final_html = isset($final_html) ? $final_html : $link_html;
325
326     $this->setUpUrlIntegrationServices();
327     $this->setupDisplayWithEmptyArgumentsAndFields();
328     $field = $this->setupTestField(['alter' => $alter]);
329     $field->field_alias = 'key';
330     $row = new ResultRow(['key' => 'value']);
331
332     $result = $field->advancedRender($row);
333     $this->assertEquals($final_html, (string) $result);
334   }
335
336   /**
337    * Data provider for ::testRenderAsLinkWithPathAndOptions().
338    *
339    * @return array
340    *   Test data.
341    */
342   public function providerTestRenderAsLinkWithPathAndOptions() {
343     $data = [];
344     // Simple path with default options.
345     $data[] = ['test-path', [], [], '<a href="/test-path">value</a>'];
346     // Add a fragment.
347     $data[] = ['test-path', ['fragment' => 'test'], '<a href="/test-path#test">value</a>'];
348     // Rel attributes.
349     $data[] = ['test-path', ['rel' => 'up'], '<a href="/test-path" rel="up">value</a>'];
350     // Target attributes.
351     $data[] = ['test-path', ['target' => '_blank'], '<a href="/test-path" target="_blank">value</a>'];
352     // Link attributes.
353     $data[] = ['test-path', ['link_attributes' => ['foo' => 'bar']], '<a href="/test-path" foo="bar">value</a>'];
354     // Manual specified query.
355     $data[] = ['test-path', ['query' => ['foo' => 'bar']], '<a href="/test-path?foo=bar">value</a>'];
356     // Query specified as part of the path.
357     $data[] = ['test-path?foo=bar', [], '<a href="/test-path?foo=bar">value</a>'];
358     // Query specified as option and path.
359     // @todo Do we expect that options override all existing ones?
360     $data[] = ['test-path?foo=bar', ['query' => ['key' => 'value']], '<a href="/test-path?key=value">value</a>'];
361     // Alias flag.
362     $data[] = ['test-path', ['alias' => TRUE], '<a href="/test-path">value</a>'];
363     // Note: In contrast to the testRenderAsLinkWithUrlAndOptions test we don't
364     // test the language, because the path processor for the language won't be
365     // executed for paths which aren't routed.
366
367     // Entity flag.
368     $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
369     $data[] = ['test-path', ['entity' => $entity], '<a href="/test-path">value</a>'];
370     // entity_type flag.
371     $entity_type_id = 'node';
372     $data[] = ['test-path', ['entity_type' => $entity_type_id], '<a href="/test-path">value</a>'];
373     // prefix
374     $data[] = ['test-path', ['prefix' => 'test_prefix'], '<a href="/test-path">value</a>', 'test_prefix<a href="/test-path">value</a>'];
375     // suffix.
376     $data[] = ['test-path', ['suffix' => 'test_suffix'], '<a href="/test-path">value</a>', '<a href="/test-path">value</a>test_suffix'];
377
378     // External URL.
379     $data[] = ['https://www.drupal.org', [], [], '<a href="https://www.drupal.org">value</a>'];
380     $data[] = ['www.drupal.org', ['external' => TRUE], [], '<a href="http://www.drupal.org">value</a>'];
381     $data[] = ['', ['external' => TRUE], [], 'value'];
382
383     return $data;
384   }
385
386   /**
387    * Tests link rendering with a URL and options.
388    *
389    * @dataProvider providerTestRenderAsLinkWithUrlAndOptions
390    * @covers ::renderAsLink
391    */
392   public function testRenderAsLinkWithUrlAndOptions(Url $url, $alter, Url $expected_url, $url_path, Url $expected_link_url, $link_html, $final_html = NULL) {
393     $alter += [
394       'make_link' => TRUE,
395       'url' => $url,
396     ];
397
398     $final_html = isset($final_html) ? $final_html : $link_html;
399
400     $this->setUpUrlIntegrationServices();
401     $this->setupDisplayWithEmptyArgumentsAndFields();
402     $field = $this->setupTestField(['alter' => $alter]);
403     $field->field_alias = 'key';
404     $row = new ResultRow(['key' => 'value']);
405
406     $expected_url->setOptions($expected_url->getOptions() + $this->defaultUrlOptions);
407     $expected_link_url->setUrlGenerator($this->urlGenerator);
408
409     $expected_url_options = $expected_url->getOptions();
410     unset($expected_url_options['attributes']);
411
412     $this->urlGenerator->expects($this->once())
413       ->method('generateFromRoute')
414       ->with($expected_url->getRouteName(), $expected_url->getRouteParameters(), $expected_url_options, TRUE)
415       ->willReturn((new GeneratedUrl())->setGeneratedUrl($url_path));
416
417     $result = $field->advancedRender($row);
418     $this->assertEquals($final_html, $result);
419   }
420
421   /**
422    * Data provider for ::testRenderAsLinkWithUrlAndOptions().
423    *
424    * @return array
425    *   Array of test data.
426    */
427   public function providerTestRenderAsLinkWithUrlAndOptions() {
428     $data = [];
429
430     // Simple path with default options.
431     $url = Url::fromRoute('test_route');
432     $data[] = [$url, [], clone $url, '/test-path', clone $url, '<a href="/test-path">value</a>'];
433
434     // Simple url with parameters.
435     $url_parameters = Url::fromRoute('test_route', ['key' => 'value']);
436     $data[] = [$url_parameters, [], clone $url_parameters, '/test-path/value', clone $url_parameters, '<a href="/test-path/value">value</a>'];
437
438     // Add a fragment.
439     $url = Url::fromRoute('test_route');
440     $url_with_fragment = Url::fromRoute('test_route');
441     $options = ['fragment' => 'test'] + $this->defaultUrlOptions;
442     $url_with_fragment->setOptions($options);
443     $data[] = [$url, ['fragment' => 'test'], $url_with_fragment, '/test-path#test', clone $url_with_fragment, '<a href="/test-path#test">value</a>'];
444
445     // Rel attributes.
446     $url = Url::fromRoute('test_route');
447     $url_with_rel = Url::fromRoute('test_route');
448     $options = ['attributes' => ['rel' => 'up']] + $this->defaultUrlOptions;
449     $url_with_rel->setOptions($options);
450     $data[] = [$url, ['rel' => 'up'], clone $url, '/test-path', $url_with_rel, '<a href="/test-path" rel="up">value</a>'];
451
452     // Target attributes.
453     $url = Url::fromRoute('test_route');
454     $url_with_target = Url::fromRoute('test_route');
455     $options = ['attributes' => ['target' => '_blank']] + $this->defaultUrlOptions;
456     $url_with_target->setOptions($options);
457     $data[] = [$url, ['target' => '_blank'], $url_with_target, '/test-path', clone $url_with_target, '<a href="/test-path" target="_blank">value</a>'];
458
459     // Link attributes.
460     $url = Url::fromRoute('test_route');
461     $url_with_link_attributes = Url::fromRoute('test_route');
462     $options = ['attributes' => ['foo' => 'bar']] + $this->defaultUrlOptions;
463     $url_with_link_attributes->setOptions($options);
464     $data[] = [$url, ['link_attributes' => ['foo' => 'bar']], clone $url, '/test-path', $url_with_link_attributes, '<a href="/test-path" foo="bar">value</a>'];
465
466     // Manual specified query.
467     $url = Url::fromRoute('test_route');
468     $url_with_query = Url::fromRoute('test_route');
469     $options = ['query' => ['foo' => 'bar']] + $this->defaultUrlOptions;
470     $url_with_query->setOptions($options);
471     $data[] = [$url, ['query' => ['foo' => 'bar']], clone $url_with_query, '/test-path?foo=bar', $url_with_query, '<a href="/test-path?foo=bar">value</a>'];
472
473     // Query specified as part of the path.
474     $url = Url::fromRoute('test_route')->setOption('query', ['foo' => 'bar']);
475     $url_with_query = clone $url;
476     $url_with_query->setOptions(['query' => ['foo' => 'bar']] + $url_with_query->getOptions());
477     $data[] = [$url, [], $url_with_query, '/test-path?foo=bar', clone $url, '<a href="/test-path?foo=bar">value</a>'];
478
479     // Query specified as option and path.
480     $url = Url::fromRoute('test_route')->setOption('query', ['foo' => 'bar']);
481     $url_with_query = Url::fromRoute('test_route');
482     $options = ['query' => ['key' => 'value']] + $this->defaultUrlOptions;
483     $url_with_query->setOptions($options);
484     $data[] = [$url, ['query' => ['key' => 'value']], $url_with_query, '/test-path?key=value', clone $url_with_query, '<a href="/test-path?key=value">value</a>'];
485
486     // Alias flag.
487     $url = Url::fromRoute('test_route');
488     $url_without_alias = Url::fromRoute('test_route');
489     $options = ['alias' => TRUE] + $this->defaultUrlOptions;
490     $url_without_alias->setOptions($options);
491     $data[] = [$url, ['alias' => TRUE], $url_without_alias, '/test-path', clone $url_without_alias, '<a href="/test-path">value</a>'];
492
493     // Language flag.
494     $language = new Language(['id' => 'fr']);
495     $url = Url::fromRoute('test_route');
496     $url_with_language = Url::fromRoute('test_route');
497     $options = ['language' => $language] + $this->defaultUrlOptions;
498     $url_with_language->setOptions($options);
499     $data[] = [$url, ['language' => $language], $url_with_language, '/fr/test-path', clone $url_with_language, '<a href="/fr/test-path" hreflang="fr">value</a>'];
500
501     // Entity flag.
502     $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
503     $url = Url::fromRoute('test_route');
504     $url_with_entity = Url::fromRoute('test_route');
505     $options = ['entity' => $entity] + $this->defaultUrlOptions;
506     $url_with_entity->setOptions($options);
507     $data[] = [$url, ['entity' => $entity], $url_with_entity, '/test-path', clone $url_with_entity, '<a href="/test-path">value</a>'];
508
509     // Test entity_type flag.
510     $entity_type_id = 'node';
511     $url = Url::fromRoute('test_route');
512     $url_with_entity_type = Url::fromRoute('test_route');
513     $options = ['entity_type' => $entity_type_id] + $this->defaultUrlOptions;
514     $url_with_entity_type->setOptions($options);
515     $data[] = [$url, ['entity_type' => $entity_type_id], $url_with_entity_type, '/test-path', clone $url_with_entity_type, '<a href="/test-path">value</a>'];
516
517     // Test prefix.
518     $url = Url::fromRoute('test_route');
519     $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>'];
520
521     // Test suffix.
522     $url = Url::fromRoute('test_route');
523     $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'];
524
525     return $data;
526   }
527
528   /**
529    * Test rendering of a link with a path and options.
530    *
531    * @dataProvider providerTestRenderAsLinkWithPathAndTokens
532    * @covers ::renderAsLink
533    */
534   public function testRenderAsLinkWithPathAndTokens($path, $tokens, $link_html) {
535     $alter = [
536       'make_link' => TRUE,
537       'path' => $path,
538     ];
539
540     $this->setUpUrlIntegrationServices();
541     $this->setupDisplayWithEmptyArgumentsAndFields();
542     $this->executable->build_info['substitutions'] = $tokens;
543     $field = $this->setupTestField(['alter' => $alter]);
544     $field->field_alias = 'key';
545     $row = new ResultRow(['key' => 'value']);
546
547     $build = [
548       '#type' => 'inline_template',
549       '#template' => 'test-path/' . explode('/', $path)[1],
550       '#context' => ['foo' => 123],
551       '#post_render' => [function() {}],
552     ];
553
554     $this->renderer->expects($this->once())
555       ->method('renderPlain')
556       ->with($build)
557       ->willReturn('base:test-path/123');
558
559     $result = $field->advancedRender($row);
560     $this->assertEquals($link_html, $result);
561   }
562
563   /**
564    * Data provider for ::testRenderAsLinkWithPathAndTokens().
565    *
566    * @return array
567    *   Test data.
568    */
569   public function providerTestRenderAsLinkWithPathAndTokens() {
570     $tokens = ['{{ foo }}' => 123];
571     $link_html = '<a href="/test-path/123">value</a>';
572
573     $data = [];
574
575     $data[] = ['test-path/{{foo}}', $tokens, $link_html];
576     $data[] = ['test-path/{{ foo}}', $tokens, $link_html];
577     $data[] = ['test-path/{{  foo}}', $tokens, $link_html];
578     $data[] = ['test-path/{{foo }}', $tokens, $link_html];
579     $data[] = ['test-path/{{foo  }}', $tokens, $link_html];
580     $data[] = ['test-path/{{ foo }}', $tokens, $link_html];
581     $data[] = ['test-path/{{  foo }}', $tokens, $link_html];
582     $data[] = ['test-path/{{ foo  }}', $tokens, $link_html];
583     $data[] = ['test-path/{{  foo  }}', $tokens, $link_html];
584
585     return $data;
586   }
587
588   /**
589    * Test rendering of a link with a path and options.
590    *
591    * @dataProvider providerTestRenderAsExternalLinkWithPathAndTokens
592    * @covers ::renderAsLink
593    */
594   public function testRenderAsExternalLinkWithPathAndTokens($path, $tokens, $link_html, $context) {
595     $alter = [
596       'make_link' => TRUE,
597       'path' => $path,
598       'url' => '',
599     ];
600     if (isset($context['alter'])) {
601       $alter += $context['alter'];
602     }
603
604     $this->setUpUrlIntegrationServices();
605     $this->setupDisplayWithEmptyArgumentsAndFields();
606     $this->executable->build_info['substitutions'] = $tokens;
607     $field = $this->setupTestField(['alter' => $alter]);
608     $field->field_alias = 'key';
609     $row = new ResultRow(['key' => 'value']);
610
611     $build = [
612       '#type' => 'inline_template',
613       '#template' => $path,
614       '#context' => ['foo' => $context['context_path']],
615       '#post_render' => [function() {}],
616     ];
617
618     $this->renderer->expects($this->once())
619       ->method('renderPlain')
620       ->with($build)
621       ->willReturn($context['context_path']);
622
623     $result = $field->advancedRender($row);
624     $this->assertEquals($link_html, $result);
625   }
626
627   /**
628    * Data provider for ::testRenderAsExternalLinkWithPathAndTokens().
629    *
630    * @return array
631    *   Test data.
632    */
633   public function providerTestRenderAsExternalLinkWithPathAndTokens() {
634     $data = [];
635
636     $data[] = ['{{ foo }}', ['{{ foo }}' => 'http://www.drupal.org'], '<a href="http://www.drupal.org">value</a>', ['context_path' => 'http://www.drupal.org']];
637     $data[] = ['{{ foo }}', ['{{ foo }}' => ''], 'value', ['context_path' => '']];
638     $data[] = ['{{ foo }}', ['{{ foo }}' => ''], 'value', ['context_path' => '', 'alter' => ['external' => TRUE]]];
639     $data[] = ['{{ foo }}', ['{{ foo }}' => '/test-path/123'], '<a href="/test-path/123">value</a>', ['context_path' => '/test-path/123']];
640
641     return $data;
642   }
643
644   /**
645    * Sets up a test field.
646    *
647    * @return \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTestField|\PHPUnit_Framework_MockObject_MockObject
648    *   The test field.
649    */
650   protected function setupTestField(array $options = []) {
651     /** @var \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTestField $field */
652     $field = $this->getMock('Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTestField', ['l'], [$this->configuration, $this->pluginId, $this->pluginDefinition]);
653     $field->init($this->executable, $this->display, $options);
654     $field->setLinkGenerator($this->linkGenerator);
655
656     return $field;
657   }
658
659   /**
660    * @covers ::getRenderTokens
661    */
662   public function testGetRenderTokensWithoutFieldsAndArguments() {
663     $field = $this->setupTestField();
664
665     $this->display->expects($this->any())
666       ->method('getHandlers')
667       ->willReturnMap([
668         ['argument', []],
669         ['field', []],
670       ]);
671
672     $this->assertEquals([], $field->getRenderTokens([]));
673   }
674
675   /**
676    * @covers ::getRenderTokens
677    */
678   public function testGetRenderTokensWithoutArguments() {
679     $field = $this->setupTestField(['id' => 'id']);
680
681     $field->last_render = 'last rendered output';
682     $this->display->expects($this->any())
683       ->method('getHandlers')
684       ->willReturnMap([
685         ['argument', []],
686         ['field', ['id' => $field]],
687       ]);
688
689     $this->assertEquals(['{{ id }}' => 'last rendered output'], $field->getRenderTokens([]));
690   }
691
692   /**
693    * @covers ::getRenderTokens
694    */
695   public function testGetRenderTokensWithArguments() {
696     $field = $this->setupTestField(['id' => 'id']);
697     $field->view->args = ['argument value'];
698     $field->view->build_info['substitutions']['{{ arguments.name }}'] = 'argument value';
699
700     $argument = $this->getMockBuilder('\Drupal\views\Plugin\views\argument\ArgumentPluginBase')
701       ->disableOriginalConstructor()
702       ->getMock();
703
704     $field->last_render = 'last rendered output';
705     $this->display->expects($this->any())
706       ->method('getHandlers')
707       ->willReturnMap([
708         ['argument', ['name' => $argument]],
709         ['field', ['id' => $field]],
710       ]);
711
712     $expected = [
713       '{{ id }}' => 'last rendered output',
714       '{{ arguments.name }}' => 'argument value',
715       '{{ raw_arguments.name }}' => 'argument value',
716     ];
717     $this->assertEquals($expected, $field->getRenderTokens([]));
718   }
719
720 }
721
722 class FieldPluginBaseTestField extends FieldPluginBase {
723
724   public function setLinkGenerator(LinkGeneratorInterface $link_generator) {
725     $this->linkGenerator = $link_generator;
726   }
727
728 }
729
730 // @todo Remove as part of https://www.drupal.org/node/2529170.
731 namespace Drupal\views\Plugin\views\field;
732
733 if (!function_exists('base_path')) {
734   function base_path() {
735     return '/';
736   }
737 }