function BooleanFormatterTest::testBooleanFormatter

Same name in other branches
  1. 8.9.x core/modules/field/tests/src/Kernel/Boolean/BooleanFormatterTest.php \Drupal\Tests\field\Kernel\Boolean\BooleanFormatterTest::testBooleanFormatter()
  2. 10 core/modules/field/tests/src/Kernel/Boolean/BooleanFormatterTest.php \Drupal\Tests\field\Kernel\Boolean\BooleanFormatterTest::testBooleanFormatter()
  3. 11.x core/modules/field/tests/src/Kernel/Boolean/BooleanFormatterTest.php \Drupal\Tests\field\Kernel\Boolean\BooleanFormatterTest::testBooleanFormatter()

Tests boolean formatter output.

File

core/modules/field/tests/src/Kernel/Boolean/BooleanFormatterTest.php, line 108

Class

BooleanFormatterTest
Tests the boolean formatter.

Namespace

Drupal\Tests\field\Kernel\Boolean

Code

public function testBooleanFormatter() {
    $data = [];
    $data[] = [
        0,
        [],
        'Off',
    ];
    $data[] = [
        1,
        [],
        'On',
    ];
    $format = [
        'format' => 'enabled-disabled',
    ];
    $data[] = [
        0,
        $format,
        'Disabled',
    ];
    $data[] = [
        1,
        $format,
        'Enabled',
    ];
    $format = [
        'format' => 'unicode-yes-no',
    ];
    $data[] = [
        1,
        $format,
        '✔',
    ];
    $data[] = [
        0,
        $format,
        '✖',
    ];
    $format = [
        'format' => 'custom',
        'format_custom_false' => 'FALSE',
        'format_custom_true' => 'TRUE',
    ];
    $data[] = [
        0,
        $format,
        'FALSE',
    ];
    $data[] = [
        1,
        $format,
        'TRUE',
    ];
    foreach ($data as $test_data) {
        [
            $value,
            $settings,
            $expected,
        ] = $test_data;
        $component = $this->display
            ->getComponent($this->fieldName);
        $component['settings'] = $settings;
        $this->display
            ->setComponent($this->fieldName, $component);
        $entity = EntityTest::create([]);
        $entity->{$this->fieldName}->value = $value;
        // Verify that all HTML is escaped and newlines are retained.
        $this->renderEntityFields($entity, $this->display);
        $this->assertRaw($expected);
    }
}

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