5 * Contains \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTest.
8 namespace Drupal\Tests\views\Unit\Plugin\field;
10 use Drupal\Core\GeneratedUrl;
11 use Drupal\Core\Language\Language;
12 use Drupal\Core\Render\Markup;
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;
26 * @coversDefaultClass \Drupal\views\Plugin\views\field\FieldPluginBase
29 class FieldPluginBaseTest extends UnitTestCase {
32 * The configuration of the plugin under test.
36 protected $configuration = [];
39 * The ID plugin of the plugin under test.
43 protected $pluginId = 'field_test';
46 * The definition of the plugin under test.
50 protected $pluginDefinition = [];
53 * Default configuration for URL output.
57 protected $defaultUrlOptions = [
61 'entity_type' => NULL,
64 'set_active_class' => FALSE,
68 * The mocked link generator.
70 * @var \Drupal\Core\Utility\LinkGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
72 protected $linkGenerator;
75 * The mocked view executable.
77 * @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject
79 protected $executable;
82 * The mocked display plugin instance.
84 * @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject
89 * The mocked url generator.
91 * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
93 protected $urlGenerator;
96 * The mocked path validator.
98 * @var \Drupal\Core\Path\PathValidatorInterface|\PHPUnit_Framework_MockObject_MockObject
100 protected $pathValidator;
103 * The unrouted url assembler service.
105 * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface|\PHPUnit_Framework_MockObject_MockObject
107 protected $unroutedUrlAssembler;
112 * @var \Symfony\Component\HttpFoundation\RequestStack
114 protected $requestStack;
117 * The mocked path processor.
119 * @var \Drupal\Core\PathProcessor\OutboundPathProcessorInterface|\PHPUnit_Framework_MockObject_MockObject
121 protected $pathProcessor;
124 * The mocked path renderer.
126 * @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
133 protected function setUp() {
136 $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
137 ->disableOriginalConstructor()
139 $this->display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
140 ->disableOriginalConstructor()
143 $route_provider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
144 $route_provider->expects($this->any())
145 ->method('getRouteByName')
147 ->willReturn(new Route('/test-path'));
149 $this->urlGenerator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
150 $this->pathValidator = $this->getMock('Drupal\Core\Path\PathValidatorInterface');
152 $this->requestStack = new RequestStack();
153 $this->requestStack->push(new Request());
155 $this->unroutedUrlAssembler = $this->getMock('Drupal\Core\Utility\UnroutedUrlAssemblerInterface');
156 $this->linkGenerator = $this->getMock('Drupal\Core\Utility\LinkGeneratorInterface');
158 $this->renderer = $this->getMock('Drupal\Core\Render\RendererInterface');
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);
170 * Sets up the unrouted url assembler and the link generator.
172 protected function setUpUrlIntegrationServices() {
173 $this->pathProcessor = $this->getMock('Drupal\Core\PathProcessor\OutboundPathProcessorInterface');
174 $this->unroutedUrlAssembler = new UnroutedUrlAssembler($this->requestStack, $this->pathProcessor);
176 \Drupal::getContainer()->set('unrouted_url_assembler', $this->unroutedUrlAssembler);
178 $this->linkGenerator = new LinkGenerator($this->urlGenerator, $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'), $this->renderer);
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;
189 if (isset($elements['#suffix'])) {
190 $link = $link . $elements['#suffix'];
192 return Markup::create($link);
198 * Sets up a display with empty arguments and fields.
200 protected function setupDisplayWithEmptyArgumentsAndFields() {
201 $this->display->expects($this->any())
202 ->method('getHandlers')
210 * Test rendering as a link without a path.
212 * @covers ::renderAsLink
214 public function testRenderAsLinkWithoutPath() {
219 $this->setUpUrlIntegrationServices();
220 $field = $this->setupTestField(['alter' => $alter]);
221 $field->field_alias = 'key';
222 $row = new ResultRow(['key' => 'value']);
224 $expected_result = 'value';
225 $result = $field->advancedRender($row);
226 $this->assertEquals($expected_result, $result);
230 * Test rendering with a more link.
232 * @param string $path
233 * An internal or external path.
235 * The final url used by the more link.
237 * @dataProvider providerTestRenderTrimmedWithMoreLinkAndPath
238 * @covers ::renderText
240 public function testRenderTrimmedWithMoreLinkAndPath($path, $url) {
245 // Don't invoke translation.
247 'more_link_text' => 'more link',
248 'more_link_path' => $path,
251 $this->display->expects($this->any())
252 ->method('getHandlers')
258 $this->setUpUrlIntegrationServices();
259 $field = $this->setupTestField(['alter' => $alter]);
260 $field->field_alias = 'key';
261 $row = new ResultRow(['key' => 'a long value']);
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);
269 * Data provider for ::testRenderTrimmedWithMoreLinkAndPath().
274 public function providerTestRenderTrimmedWithMoreLinkAndPath() {
276 // Simple path with default options.
277 $data[] = ['test-path', '/test-path'];
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'];
283 $data[] = ['', '/%3Cfront%3E'];
285 $data[] = ['<front>', '/%3Cfront%3E'];
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'];
296 * Tests the "No results text" rendering.
298 * @covers ::renderText
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' => '']);
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);
313 * Test rendering of a link with a path and options.
315 * @dataProvider providerTestRenderAsLinkWithPathAndOptions
316 * @covers ::renderAsLink
318 public function testRenderAsLinkWithPathAndOptions($path, $alter, $link_html, $final_html = NULL) {
324 $final_html = isset($final_html) ? $final_html : $link_html;
326 $this->setUpUrlIntegrationServices();
327 $this->setupDisplayWithEmptyArgumentsAndFields();
328 $field = $this->setupTestField(['alter' => $alter]);
329 $field->field_alias = 'key';
330 $row = new ResultRow(['key' => 'value']);
332 $result = $field->advancedRender($row);
333 $this->assertEquals($final_html, (string) $result);
337 * Data provider for ::testRenderAsLinkWithPathAndOptions().
342 public function providerTestRenderAsLinkWithPathAndOptions() {
344 // Simple path with default options.
345 $data[] = ['test-path', [], [], '<a href="/test-path">value</a>'];
347 $data[] = ['test-path', ['fragment' => 'test'], '<a href="/test-path#test">value</a>'];
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>'];
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>'];
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.
368 $entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
369 $data[] = ['test-path', ['entity' => $entity], '<a href="/test-path">value</a>'];
371 $entity_type_id = 'node';
372 $data[] = ['test-path', ['entity_type' => $entity_type_id], '<a href="/test-path">value</a>'];
374 $data[] = ['test-path', ['prefix' => 'test_prefix'], '<a href="/test-path">value</a>', 'test_prefix<a href="/test-path">value</a>'];
376 $data[] = ['test-path', ['suffix' => 'test_suffix'], '<a href="/test-path">value</a>', '<a href="/test-path">value</a>test_suffix'];
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'];
387 * Tests link rendering with a URL and options.
389 * @dataProvider providerTestRenderAsLinkWithUrlAndOptions
390 * @covers ::renderAsLink
392 public function testRenderAsLinkWithUrlAndOptions(Url $url, $alter, Url $expected_url, $url_path, Url $expected_link_url, $link_html, $final_html = NULL) {
398 $final_html = isset($final_html) ? $final_html : $link_html;
400 $this->setUpUrlIntegrationServices();
401 $this->setupDisplayWithEmptyArgumentsAndFields();
402 $field = $this->setupTestField(['alter' => $alter]);
403 $field->field_alias = 'key';
404 $row = new ResultRow(['key' => 'value']);
406 $expected_url->setOptions($expected_url->getOptions() + $this->defaultUrlOptions);
407 $expected_link_url->setUrlGenerator($this->urlGenerator);
409 $expected_url_options = $expected_url->getOptions();
410 unset($expected_url_options['attributes']);
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));
417 $result = $field->advancedRender($row);
418 $this->assertEquals($final_html, $result);
422 * Data provider for ::testRenderAsLinkWithUrlAndOptions().
425 * Array of test data.
427 public function providerTestRenderAsLinkWithUrlAndOptions() {
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>'];
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>'];
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>'];
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>'];
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>'];
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>'];
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>'];
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>'];
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>'];
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>'];
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>'];
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>'];
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>'];
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>'];
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'];
529 * Test rendering of a link with a path and options.
531 * @dataProvider providerTestRenderAsLinkWithPathAndTokens
532 * @covers ::renderAsLink
534 public function testRenderAsLinkWithPathAndTokens($path, $tokens, $link_html) {
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']);
548 '#type' => 'inline_template',
549 '#template' => 'test-path/' . explode('/', $path)[1],
550 '#context' => ['foo' => 123],
551 '#post_render' => [function() {}],
554 $this->renderer->expects($this->once())
555 ->method('renderPlain')
557 ->willReturn('base:test-path/123');
559 $result = $field->advancedRender($row);
560 $this->assertEquals($link_html, $result);
564 * Data provider for ::testRenderAsLinkWithPathAndTokens().
569 public function providerTestRenderAsLinkWithPathAndTokens() {
570 $tokens = ['{{ foo }}' => 123];
571 $link_html = '<a href="/test-path/123">value</a>';
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];
589 * Test rendering of a link with a path and options.
591 * @dataProvider providerTestRenderAsExternalLinkWithPathAndTokens
592 * @covers ::renderAsLink
594 public function testRenderAsExternalLinkWithPathAndTokens($path, $tokens, $link_html, $context) {
600 if (isset($context['alter'])) {
601 $alter += $context['alter'];
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']);
612 '#type' => 'inline_template',
613 '#template' => $path,
614 '#context' => ['foo' => $context['context_path']],
615 '#post_render' => [function() {}],
618 $this->renderer->expects($this->once())
619 ->method('renderPlain')
621 ->willReturn($context['context_path']);
623 $result = $field->advancedRender($row);
624 $this->assertEquals($link_html, $result);
628 * Data provider for ::testRenderAsExternalLinkWithPathAndTokens().
633 public function providerTestRenderAsExternalLinkWithPathAndTokens() {
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']];
645 * Sets up a test field.
647 * @return \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTestField|\PHPUnit_Framework_MockObject_MockObject
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);
660 * @covers ::getRenderTokens
662 public function testGetRenderTokensWithoutFieldsAndArguments() {
663 $field = $this->setupTestField();
665 $this->display->expects($this->any())
666 ->method('getHandlers')
672 $this->assertEquals([], $field->getRenderTokens([]));
676 * @covers ::getRenderTokens
678 public function testGetRenderTokensWithoutArguments() {
679 $field = $this->setupTestField(['id' => 'id']);
681 $field->last_render = 'last rendered output';
682 $this->display->expects($this->any())
683 ->method('getHandlers')
686 ['field', ['id' => $field]],
689 $this->assertEquals(['{{ id }}' => 'last rendered output'], $field->getRenderTokens([]));
693 * @covers ::getRenderTokens
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';
700 $argument = $this->getMockBuilder('\Drupal\views\Plugin\views\argument\ArgumentPluginBase')
701 ->disableOriginalConstructor()
704 $field->last_render = 'last rendered output';
705 $this->display->expects($this->any())
706 ->method('getHandlers')
708 ['argument', ['name' => $argument]],
709 ['field', ['id' => $field]],
713 '{{ id }}' => 'last rendered output',
714 '{{ arguments.name }}' => 'argument value',
715 '{{ raw_arguments.name }}' => 'argument value',
717 $this->assertEquals($expected, $field->getRenderTokens([]));
721 * Ensures proper token replacement when generating CSS classes.
723 * @covers ::elementClasses
724 * @covers ::elementLabelClasses
725 * @covers ::elementWrapperClasses
727 public function testElementClassesWithTokens() {
729 'elementClasses' => 'element_class',
730 'elementLabelClasses' => 'element_label_class',
731 'elementWrapperClasses' => 'element_wrapper_class',
734 $tokens = ['test_token' => 'foo'];
735 $test_class = 'test-class-without-token test-class-with-{{ test_token }}-token';
736 $expected_result = 'test-class-without-token test-class-with-foo-token';
738 // Inline template to render the tokens.
740 '#type' => 'inline_template',
741 '#template' => $test_class,
742 '#context' => $tokens,
743 '#post_render' => [function() {}],
746 // We're not testing the token rendering itself, just that the function
747 // being tested correctly handles tokens when generating the element's class
749 $this->renderer->expects($this->any())
750 ->method('renderPlain')
752 ->willReturn($expected_result);
754 foreach ($functions as $callable => $option_name) {
755 $field = $this->setupTestField([$option_name => $test_class]);
756 $field->view->style_plugin = new \stdClass();
757 $field->view->style_plugin->render_tokens[] = $tokens;
759 $result = $field->{$callable}(0);
760 $this->assertEquals($expected_result, $result);
766 class FieldPluginBaseTestField extends FieldPluginBase {
768 public function setLinkGenerator(LinkGeneratorInterface $link_generator) {
769 $this->linkGenerator = $link_generator;
774 // @todo Remove as part of https://www.drupal.org/node/2529170.
775 namespace Drupal\views\Plugin\views\field;
777 if (!function_exists('base_path')) {
778 function base_path() {