function FieldPluginBaseTest::testElementClassesWithTokens

Same name in other branches
  1. 9 core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTest::testElementClassesWithTokens()
  2. 8.9.x core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTest::testElementClassesWithTokens()
  3. 11.x 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 820

Class

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

Namespace

Drupal\Tests\views\Unit\Plugin\field

Code

public function testElementClassesWithTokens() : void {
    $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('renderInIsolation')
        ->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);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.