FieldPluginBaseTest.php 21.5 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Contains \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTest.
 */

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 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
use Drupal\Core\Url;
use Drupal\Core\Utility\LinkGenerator;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\Core\Utility\UnroutedUrlAssembler;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Route;

/**
 * @coversDefaultClass \Drupal\views\Plugin\views\field\FieldPluginBase
 * @group views
 */
class FieldPluginBaseTest extends UnitTestCase {

  /**
   * The configuration of the plugin under test.
   *
   * @var array
   */
  protected $configuration = [];

  /**
   * The ID plugin of the plugin under test.
   *
   * @var string
   */
  protected $pluginId = 'field_test';

  /**
   * The definition of the plugin under test.
   *
   * @var array
   */
  protected $pluginDefinition = [];

  /**
   * Default configuration for URL output.
   *
   * @var array
   */
  protected $defaultUrlOptions = [
    'absolute' => FALSE,
    'alias' => FALSE,
    'entity' => NULL,
    'entity_type' => NULL,
    'language' => NULL,
    'query' => [],
    'set_active_class' => FALSE,
  ];

  /**
   * The mocked link generator.
   *
   * @var \Drupal\Core\Utility\LinkGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $linkGenerator;

  /**
   * The mocked view executable.
   *
   * @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $executable;

  /**
   * The mocked display plugin instance.
   *
   * @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $display;

  /**
   * The mocked url generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $urlGenerator;

  /**
   * The mocked path validator.
   *
   * @var \Drupal\Core\Path\PathValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $pathValidator;

  /**
   * The unrouted url assembler service.
   *
   * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $unroutedUrlAssembler;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The mocked path processor.
   *
   * @var \Drupal\Core\PathProcessor\OutboundPathProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $pathProcessor;

123 124 125 126 127 128 129
  /**
   * The mocked path renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $renderer;

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
      ->disableOriginalConstructor()
      ->getMock();
    $this->display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
      ->disableOriginalConstructor()
      ->getMock();

    $route_provider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
    $route_provider->expects($this->any())
      ->method('getRouteByName')
      ->with('test_route')
      ->willReturn(new Route('/test-path'));

    $this->urlGenerator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
    $this->pathValidator = $this->getMock('Drupal\Core\Path\PathValidatorInterface');

    $this->requestStack = new RequestStack();
    $this->requestStack->push(new Request());

    $this->unroutedUrlAssembler = $this->getMock('Drupal\Core\Utility\UnroutedUrlAssemblerInterface');
    $this->linkGenerator = $this->getMock('Drupal\Core\Utility\LinkGeneratorInterface');

158 159
    $this->renderer = $this->getMock('Drupal\Core\Render\RendererInterface');

160 161 162 163 164
    $container_builder = new ContainerBuilder();
    $container_builder->set('url_generator', $this->urlGenerator);
    $container_builder->set('path.validator', $this->pathValidator);
    $container_builder->set('unrouted_url_assembler', $this->unroutedUrlAssembler);
    $container_builder->set('request_stack', $this->requestStack);
165
    $container_builder->set('renderer', $this->renderer);
166 167 168 169 170 171 172 173
    \Drupal::setContainer($container_builder);
  }

  /**
   * Sets up the unrouted url assembler and the link generator.
   */
  protected function setUpUrlIntegrationServices() {
    $this->pathProcessor = $this->getMock('Drupal\Core\PathProcessor\OutboundPathProcessorInterface');
174
    $this->unroutedUrlAssembler = new UnroutedUrlAssembler($this->requestStack, $this->pathProcessor);
175 176 177

    \Drupal::getContainer()->set('unrouted_url_assembler', $this->unroutedUrlAssembler);

178
    $this->linkGenerator = new LinkGenerator($this->urlGenerator, $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'), $this->renderer);
179 180 181 182 183 184 185 186 187 188 189 190 191
    $this->renderer
      ->method('render')
      ->willReturnCallback(
        // Pretend to do a render.
        function (&$elements, $is_root_call = FALSE) {
          // Mock the ability to theme links
          $link = $this->linkGenerator->generate($elements['#title'], $elements['#url']);
          if (isset($elements['#prefix'])) {
            $link = $elements['#prefix'] . $link;
          }
          if (isset($elements['#suffix'])) {
            $link = $link . $elements['#suffix'];
          }
192
          return Markup::create($link);
193 194
        }
      );
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  }

  /**
   * Sets up a display with empty arguments and fields.
   */
  protected function setupDisplayWithEmptyArgumentsAndFields() {
    $this->display->expects($this->any())
      ->method('getHandlers')
      ->willReturnMap([
        ['argument', []],
        ['field', []],
      ]);
  }

  /**
   * Test rendering as a link without a path.
   *
   * @covers ::renderAsLink
   */
  public function testRenderAsLinkWithoutPath() {
    $alter = [
      'make_link' => TRUE,
    ];

    $this->setUpUrlIntegrationServices();
    $field = $this->setupTestField(['alter' => $alter]);
    $field->field_alias = 'key';
    $row = new ResultRow(['key' => 'value']);

    $expected_result = 'value';
    $result = $field->advancedRender($row);
    $this->assertEquals($expected_result, $result);
  }

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  /**
   * Test rendering as a link without a path.
   *
   * @covers ::renderText
   */
  public function testRenderTrimmedWithMoreLink() {
    $alter = [
      'trim' => TRUE,
      'max_length' => 7,
      'more_link' => TRUE,
      // Don't invoke translation.
      'ellipsis' => FALSE,
      'more_link_text' => 'more link',
    ];

    $this->display->expects($this->any())
      ->method('getHandlers')
      ->willReturnMap([
        ['argument', []],
        ['field', []],
      ]);

    $this->setUpUrlIntegrationServices();
    $field = $this->setupTestField(['alter' => $alter]);
    $field->field_alias = 'key';
    $row = new ResultRow(['key' => 'a long value']);

    $expected_result = 'a long <a href="/%3Cfront%3E" class="views-more-link">more link</a>';
    $result = $field->advancedRender($row);
    $this->assertEquals($expected_result, $result);
  }

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  /**
   * Test rendering of a link with a path and options.
   *
   * @dataProvider providerTestRenderAsLinkWithPathAndOptions
   * @covers ::renderAsLink
   */
  public function testRenderAsLinkWithPathAndOptions($path, $alter, $link_html, $final_html = NULL) {
    $alter += [
      'make_link' => TRUE,
      'path' => $path,
    ];

    $final_html = isset($final_html) ? $final_html : $link_html;

    $this->setUpUrlIntegrationServices();
    $this->setupDisplayWithEmptyArgumentsAndFields();
    $field = $this->setupTestField(['alter' => $alter]);
    $field->field_alias = 'key';
    $row = new ResultRow(['key' => 'value']);

    $result = $field->advancedRender($row);
282
    $this->assertEquals($final_html, (string) $result);
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  }

  /**
   * Data provider for ::testRenderAsLinkWithPathAndOptions().
   *
   * @return array
   *   Test data.
   */
  public function providerTestRenderAsLinkWithPathAndOptions() {
    $data = [];
    // Simple path with default options.
    $data[] = ['test-path', [], [], '<a href="/test-path">value</a>'];
    // Add a fragment.
    $data[] = ['test-path', ['fragment' => 'test'], '<a href="/test-path#test">value</a>'];
    // Rel attributes.
    $data[] = ['test-path', ['rel' => 'up'], '<a href="/test-path" rel="up">value</a>'];
    // Target attributes.
    $data[] = ['test-path', ['target' => '_blank'], '<a href="/test-path" target="_blank">value</a>'];
    // Link attributes.
    $data[] = ['test-path', ['link_attributes' => ['foo' => 'bar']], '<a href="/test-path" foo="bar">value</a>'];
    // Manual specified query.
    $data[] = ['test-path', ['query' => ['foo' => 'bar']], '<a href="/test-path?foo=bar">value</a>'];
    // Query specified as part of the path.
    $data[] = ['test-path?foo=bar', [], '<a href="/test-path?foo=bar">value</a>'];
    // Query specified as option and path.
    // @todo Do we expect that options override all existing ones?
    $data[] = ['test-path?foo=bar', ['query' => ['key' => 'value']], '<a href="/test-path?key=value">value</a>'];
    // Alias flag.
    $data[] = ['test-path', ['alias' => TRUE], '<a href="/test-path">value</a>'];
    // Note: In contrast to the testRenderAsLinkWithUrlAndOptions test we don't
    // test the language, because the path processor for the language won't be
    // executed for paths which aren't routed.

    // Entity flag.
    $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
    $data[] = ['test-path', ['entity' => $entity], '<a href="/test-path">value</a>'];
    // entity_type flag.
    $entity_type_id = 'node';
    $data[] = ['test-path', ['entity_type' => $entity_type_id], '<a href="/test-path">value</a>'];
    // prefix
    $data[] = ['test-path', ['prefix' => 'test_prefix'], '<a href="/test-path">value</a>', 'test_prefix<a href="/test-path">value</a>'];
    // suffix.
    $data[] = ['test-path', ['suffix' => 'test_suffix'], '<a href="/test-path">value</a>', '<a href="/test-path">value</a>test_suffix'];

    // External URL.
    $data[] = ['https://www.drupal.org', [], [], '<a href="https://www.drupal.org">value</a>'];

    return $data;
  }

  /**
   * Tests link rendering with a URL and options.
   *
   * @dataProvider providerTestRenderAsLinkWithUrlAndOptions
   * @covers ::renderAsLink
   */
  public function testRenderAsLinkWithUrlAndOptions(Url $url, $alter, Url $expected_url, $url_path, Url $expected_link_url, $link_html, $final_html = NULL) {
    $alter += [
      'make_link' => TRUE,
      'url' => $url,
    ];

    $final_html = isset($final_html) ? $final_html : $link_html;

    $this->setUpUrlIntegrationServices();
    $this->setupDisplayWithEmptyArgumentsAndFields();
    $field = $this->setupTestField(['alter' => $alter]);
    $field->field_alias = 'key';
    $row = new ResultRow(['key' => 'value']);

    $expected_url->setOptions($expected_url->getOptions() + $this->defaultUrlOptions);
    $expected_link_url->setUrlGenerator($this->urlGenerator);

    $expected_url_options = $expected_url->getOptions();
    unset($expected_url_options['attributes']);

    $this->urlGenerator->expects($this->once())
      ->method('generateFromRoute')
361 362
      ->with($expected_url->getRouteName(), $expected_url->getRouteParameters(), $expected_url_options, TRUE)
      ->willReturn((new GeneratedUrl())->setGeneratedUrl($url_path));
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

    $result = $field->advancedRender($row);
    $this->assertEquals($final_html, $result);
  }

  /**
   * Data provider for ::testRenderAsLinkWithUrlAndOptions().
   *
   * @return array
   *   Array of test data.
   */
  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->getMock('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;
  }

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  /**
   * Test rendering of a link with a path and options.
   *
   * @dataProvider providerTestRenderAsLinkWithPathAndTokens
   * @covers ::renderAsLink
   */
  public function testRenderAsLinkWithPathAndTokens($path, $tokens, $link_html) {
    $alter = [
      'make_link' => TRUE,
      'path' => $path,
    ];

    $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' => 'base:test-path/' . explode('/', $path)[1],
      '#context' => ['foo' => 123],
498
      '#post_render' => [function() {}],
499 500 501
    ];

    $this->renderer->expects($this->once())
502 503
      ->method('renderPlain')
      ->with($build)
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
      ->willReturn('base:test-path/123');

    $result = $field->advancedRender($row);
    $this->assertEquals($link_html, $result);
  }

  /**
   * Data provider for ::testRenderAsLinkWithPathAndTokens().
   *
   * @return array
   *   Test data.
   */
  public function providerTestRenderAsLinkWithPathAndTokens() {
    $tokens = ['{{ foo }}' => 123];
    $link_html = '<a href="/test-path/123">value</a>';

    $data = [];

    $data[] = ['test-path/{{foo}}', $tokens, $link_html];
    $data[] = ['test-path/{{ foo}}', $tokens, $link_html];
    $data[] = ['test-path/{{  foo}}', $tokens, $link_html];
    $data[] = ['test-path/{{foo }}', $tokens, $link_html];
    $data[] = ['test-path/{{foo  }}', $tokens, $link_html];
    $data[] = ['test-path/{{ foo }}', $tokens, $link_html];
    $data[] = ['test-path/{{  foo }}', $tokens, $link_html];
    $data[] = ['test-path/{{ foo  }}', $tokens, $link_html];
    $data[] = ['test-path/{{  foo  }}', $tokens, $link_html];

    return $data;
  }

535 536 537
  /**
   * Sets up a test field.
   *
538
   * @return \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTestField|\PHPUnit_Framework_MockObject_MockObject
539 540 541
   *   The test field.
   */
  protected function setupTestField(array $options = []) {
542 543
    /** @var \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTestField $field */
    $field = $this->getMock('Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTestField', ['l'], [$this->configuration, $this->pluginId, $this->pluginDefinition]);
544 545 546 547 548 549
    $field->init($this->executable, $this->display, $options);
    $field->setLinkGenerator($this->linkGenerator);

    return $field;
  }

550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
  /**
   * @covers ::getRenderTokens
   */
  public function testGetRenderTokensWithoutFieldsAndArguments() {
    $field = $this->setupTestField();

    $this->display->expects($this->any())
      ->method('getHandlers')
      ->willReturnMap([
        ['argument', []],
        ['field', []],
      ]);

    $this->assertEquals([], $field->getRenderTokens([]));
  }

  /**
   * @covers ::getRenderTokens
   */
  public function testGetRenderTokensWithoutArguments() {
    $field = $this->setupTestField(['id' => 'id']);

    $field->last_render = 'last rendered output';
    $this->display->expects($this->any())
      ->method('getHandlers')
      ->willReturnMap([
        ['argument', []],
        ['field', ['id' => $field]],
      ]);

    $this->assertEquals(['{{ id }}' => 'last rendered output'], $field->getRenderTokens([]));
  }

  /**
   * @covers ::getRenderTokens
   */
  public function testGetRenderTokensWithArguments() {
    $field = $this->setupTestField(['id' => 'id']);
    $field->view->args = ['argument value'];
    $field->view->build_info['substitutions']['{{ arguments.name }}'] = 'argument value';

    $argument = $this->getMockBuilder('\Drupal\views\Plugin\views\argument\ArgumentPluginBase')
      ->disableOriginalConstructor()
      ->getMock();

    $field->last_render = 'last rendered output';
    $this->display->expects($this->any())
      ->method('getHandlers')
      ->willReturnMap([
        ['argument', ['name' => $argument]],
        ['field', ['id' => $field]],
      ]);

    $expected = [
      '{{ id }}' => 'last rendered output',
      '{{ arguments.name }}' => 'argument value',
      '{{ raw_arguments.name }}' => 'argument value',
    ];
    $this->assertEquals($expected, $field->getRenderTokens([]));
  }

611 612
}

613
class FieldPluginBaseTestField extends FieldPluginBase {
614 615 616 617 618 619

  public function setLinkGenerator(LinkGeneratorInterface $link_generator) {
    $this->linkGenerator = $link_generator;
  }

}
620 621 622 623 624 625 626 627 628
}
// @todo Remove as part of https://www.drupal.org/node/2529170.
namespace {
  if (!function_exists('base_path')) {
    function base_path() {
      return '/';
    }
  }
}