Same name and namespace in other branches
  1. 8.x-2.x tests/src/Functional/ColorFieldFormatterTest.php \Drupal\Tests\color_field\Functional\ColorFieldFormatterTest::testColorFieldFormatterText() 1 comment

Test color_field_formatter_text formatter.

File

tests/src/Functional/ColorFieldFormatterTest.php, line 17

Class

ColorFieldFormatterTest
Tests color field formatters.

Namespace

Drupal\Tests\color_field\Functional

Code

public function testColorFieldFormatterText() : void {
    $this->form
        ->setComponent('field_color', [
        'type' => 'color_field_widget_default',
        'settings' => [
            'placeholder_color' => '#ABC123',
            'placeholder_opacity' => '1.0',
        ],
    ])
        ->save();
    $this->display
        ->setComponent('field_color', [
        'type' => 'color_field_formatter_text',
        'weight' => 1,
        'label' => 'hidden',
    ])
        ->save();
    // Display creation form.
    $this->drupalGet('node/add/article');
    $session = $this->assertSession();
    $session->fieldExists("field_color[0][color]");
    $session->fieldExists("field_color[0][opacity]");
    $session->responseContains('placeholder="#ABC123"');
    $session->responseContains('placeholder="1.0"');
    $session->responseContains('Freeform Color');
    $session->responseContains('Color field description');
    // Test basic entry of color field.
    $edit = [
        'title[0][value]' => $this->randomMachineName(),
        'field_color[0][color]' => "#E70000",
        'field_color[0][opacity]' => 1,
    ];
    $this->submitForm($edit, t('Save'));
    $this->assertSession()
        ->responseContains('#E70000 1</div>');
    // Ensure alternate hex format works.
    $edit = [
        'title[0][value]' => $this->randomMachineName(),
        'field_color[0][color]' => "FF8C00",
        'field_color[0][opacity]' => 0.5,
    ];
    // Render without opacity value.
    $this->display
        ->setComponent('field_color', [
        'type' => 'color_field_formatter_text',
        'weight' => 1,
        'settings' => [
            'opacity' => FALSE,
        ],
    ])
        ->save();
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, t('Save'));
    $this->assertSession()
        ->responseContains('#FF8C00</div>');
    // Test rgba Render mode.
    $edit = [
        'title[0][value]' => $this->randomMachineName(),
        'field_color[0][color]' => "#FFEF00",
        'field_color[0][opacity]' => 0.9,
    ];
    $this->display
        ->setComponent('field_color', [
        'type' => 'color_field_formatter_text',
        'weight' => 1,
        'settings' => [
            'format' => 'rgb',
            'opacity' => TRUE,
        ],
    ])
        ->save();
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, t('Save'));
    $this->assertSession()
        ->responseContains('rgba(255,239,0,0.9)');
    // Test RGB render mode.
    $edit = [
        'title[0][value]' => $this->randomMachineName(),
        'field_color[0][color]' => "#00811F",
        'field_color[0][opacity]' => 0.8,
    ];
    $this->display
        ->setComponent('field_color', [
        'type' => 'color_field_formatter_text',
        'weight' => 1,
        'settings' => [
            'format' => 'rgb',
            'opacity' => FALSE,
        ],
    ])
        ->save();
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, t('Save'));
    $this->assertSession()
        ->responseContains('rgb(0,129,31)');
}