You are here

public function FieldPluginBaseTest::testElementClassesWithTokens in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTest::testElementClassesWithTokens()

Ensures proper token replacement when generating CSS classes.

@covers ::elementClasses @covers ::elementLabelClasses @covers ::elementWrapperClasses

File

core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php, line 733
Contains \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTest.

Class

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

Namespace

Drupal\Tests\views\Unit\Plugin\field

Code

public function testElementClassesWithTokens() {
  $functions = [
    'elementClasses' => 'element_class',
    'elementLabelClasses' => 'element_label_class',
    'elementWrapperClasses' => 'element_wrapper_class',
  ];
  $tokens = [
    'test_token' => 'foo',
  ];
  $test_class = 'test-class-without-token test-class-with-{{ test_token }}-token';
  $expected_result = 'test-class-without-token test-class-with-foo-token';

  // Inline template to render the tokens.
  $build = [
    '#type' => 'inline_template',
    '#template' => $test_class,
    '#context' => $tokens,
    '#post_render' => [
      function () {
      },
    ],
  ];

  // We're not testing the token rendering itself, just that the function
  // being tested correctly handles tokens when generating the element's class
  // attribute.
  $this->renderer
    ->expects($this
    ->any())
    ->method('renderPlain')
    ->with($build)
    ->willReturn($expected_result);
  foreach ($functions as $callable => $option_name) {
    $field = $this
      ->setupTestField([
      $option_name => $test_class,
    ]);
    $field->view->style_plugin = new \stdClass();
    $field->view->style_plugin->render_tokens[] = $tokens;
    $result = $field
      ->{$callable}(0);
    $this
      ->assertEquals($expected_result, $result);
  }
}