View source
<?php
namespace Drupal\Tests\color_field\Functional;
class ColorFieldFormatterTest extends ColorFieldFunctionalTestBase {
public function testColorFieldFormatterText() {
$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();
$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');
$edit = [
'title[0][value]' => $this
->randomMachineName(),
'field_color[0][color]' => "#E70000",
'field_color[0][opacity]' => 1,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertSession()
->responseContains('#E70000 1</div>');
$edit = [
'title[0][value]' => $this
->randomMachineName(),
'field_color[0][color]' => "FF8C00",
'field_color[0][opacity]' => 0.5,
];
$this->display
->setComponent('field_color', [
'type' => 'color_field_formatter_text',
'weight' => 1,
'settings' => [
'opacity' => FALSE,
],
])
->save();
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
$this
->assertSession()
->responseContains('#FF8C00</div>');
$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
->drupalPostForm('node/add/article', $edit, t('Save'));
$this
->assertSession()
->responseContains('rgba(255,239,0,0.9)');
$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
->drupalPostForm('node/add/article', $edit, t('Save'));
$this
->assertSession()
->responseContains('rgb(0,129,31)');
}
public function testColorFieldFormatterSwatch() {
$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_swatch',
'weight' => 1,
'label' => 'hidden',
])
->save();
$edit = [
'title[0][value]' => $this
->randomMachineName(),
'field_color[0][color]' => "#0044FF",
'field_color[0][opacity]' => 0.9,
];
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
$this
->assertSession()
->responseContains('background-color: rgba(0,68,255,0.9)');
$this
->assertSession()
->responseContains('color_field__swatch--square');
$edit = [
'title[0][value]' => $this
->randomMachineName(),
'field_color[0][color]' => "#760089",
'field_color[0][opacity]' => 1,
];
$this->display
->setComponent('field_color', [
'type' => 'color_field_formatter_swatch',
'weight' => 1,
'settings' => [
'shape' => 'circle',
'opacity' => FALSE,
],
])
->save();
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
$this
->assertSession()
->responseContains('background-color: rgb(118,0,137)');
$this
->assertSession()
->responseContains('color_field__swatch--circle');
}
public function testColorFieldFormatterCss() {
$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_css',
'weight' => 1,
'label' => 'hidden',
])
->save();
$edit = [
'title[0][value]' => $this
->randomMachineName(),
'field_color[0][color]' => "#FFF430",
'field_color[0][opacity]' => 0.9,
];
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
$this
->assertSession()
->responseContains('body { background-color: rgba(255,244,48,0.9) !important; }');
$edit = [
'title[0][value]' => $this
->randomMachineName(),
'field_color[0][color]' => "#FFFFFF",
'field_color[0][opacity]' => 1,
];
$this->display
->setComponent('field_color', [
'type' => 'color_field_formatter_css',
'weight' => 1,
'settings' => [
'selector' => 'body',
'property' => 'background-color',
'important' => FALSE,
'opacity' => FALSE,
],
'label' => 'hidden',
])
->save();
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
$this
->assertSession()
->responseContains('body { background-color: rgb(255,255,255); }');
}
}