function FieldPluginBaseTest::testElementClassesWithTokens

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

@legacy-covers ::elementClasses @legacy-covers ::elementLabelClasses @legacy-covers ::elementWrapperClasses

File

core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php, line 956

Class

FieldPluginBaseTest
Tests Drupal\views\Plugin\views\field\FieldPluginBase.

Namespace

Drupal\Tests\views\Unit\Plugin\field

Code

public function testElementClassesWithTokens() : void {
  $this->setUpMockRenderer();
  $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,
  ];
  // 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->atLeastOnce())
    ->method('renderInIsolation')
    ->willReturnCallback(function (array $arg) use ($build, $expected_result) : string {
    $tmp = $arg;
    unset($tmp['#post_render']);
    if ($tmp == $build) {
      return $expected_result;
    }
    throw new \InvalidArgumentException('Unexpected argument value for renderInIsolation: ' . var_export($arg, TRUE));
  });
  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.