ColorBackgroundFormatterTest.php
Namespace
Drupal\Tests\field_example\Functional
File
-
modules/field_example/tests/src/Functional/ColorBackgroundFormatterTest.php
View source
<?php
namespace Drupal\Tests\field_example\Functional;
use Drupal\Component\Render\FormattableMarkup;
class ColorBackgroundFormatterTest extends FieldExampleBrowserTestBase {
public function testSingleValueField() {
$assert = $this->assertSession();
$this->drupalLogin($this->administratorAccount);
$this->fieldName = $this->createField('field_example_rgb', 'field_example_colorpicker', '1', 'field_example_color_background');
$this->drupalLogin($this->authorAccount);
$this->drupalGet('node/add/' . $this->contentTypeName);
$title = $this->randomMachineName(20);
$color = '#00ff00';
$edit = [
'title[0][value]' => $title,
'field_' . $this->fieldName . '[0][value]' => $color,
];
$this->submitForm($edit, 'Save');
$assert->pageTextContains((string) new FormattableMarkup('@type @title has been created', [
'@type' => $this->contentTypeName,
'@title' => $title,
]));
$assert->pageTextContains('The content area color has been changed to ' . $color);
$assert->elementAttributeNotContains('css', 'p[style*="background-color: ' . $color . '"]', 'style', 'color: inherit');
$assert->elementAttributeContains('css', 'p[style*="background-color: ' . $color . '"]', 'style', 'color: black');
\Drupal::service('entity_display.repository')->getViewDisplay('node', $this->contentTypeName)
->setComponent('field_' . $this->fieldName, [
'label' => 'inline',
'weight' => 20,
'type' => 'field_example_color_background',
'settings' => [
'adjust_text_color' => 0,
],
])
->save();
$this->getSession()
->reload();
$assert = $this->assertSession();
$assert->elementAttributeContains('css', 'p[style*="background-color: ' . $color . '"]', 'style', 'color: inherit');
$assert->elementAttributeNotContains('css', 'p[style*="background-color: ' . $color . '"]', 'style', 'color: black');
}
public function testMultiValueField() {
$assert = $this->assertSession();
$this->drupalLogin($this->administratorAccount);
$this->fieldName = $this->createField('field_example_rgb', 'field_example_colorpicker', '-1', 'field_example_color_background');
$this->drupalLogin($this->authorAccount);
$this->drupalGet('node/add/' . $this->contentTypeName);
$title = $this->randomMachineName(20);
$edit = [
'title[0][value]' => $title,
'field_' . $this->fieldName . '[0][value]' => '#00ff00',
];
$this->submitForm($edit, 'Add another item');
$edit = [
'field_' . $this->fieldName . '[1][value]' => '#ffff4f',
];
$this->submitForm($edit, 'Save');
$assert->pageTextContains((string) new FormattableMarkup('@type @title has been created', [
'@type' => $this->contentTypeName,
'@title' => $title,
]));
$assert->pageTextContains('The content area color has been changed to #00ff00');
$assert->pageTextContains('The content area color has been changed to #ffff4f');
}
}
Classes