View source
<?php
namespace Drupal\Tests\color_field\FunctionalJavascript;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class ColorFieldWidgetJavascriptTests extends WebDriverTestBase {
public static $modules = [
'field',
'node',
'color_field',
];
protected $display;
protected $form;
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'article',
]);
$user = $this
->drupalCreateUser([
'create article content',
'edit own article content',
]);
$this
->drupalLogin($user);
$entityTypeManager = $this->container
->get('entity_type.manager');
FieldStorageConfig::create([
'field_name' => 'field_color',
'entity_type' => 'node',
'type' => 'color_field_type',
])
->save();
FieldConfig::create([
'field_name' => 'field_color',
'label' => 'Freeform Color',
'description' => 'Color field description',
'entity_type' => 'node',
'bundle' => 'article',
'required' => TRUE,
'default_value' => [
[
'color' => 'ffb81c',
'opacity' => 0.5,
],
],
])
->save();
FieldStorageConfig::create([
'field_name' => 'field_color_repeat',
'entity_type' => 'node',
'type' => 'color_field_type',
])
->save();
FieldConfig::create([
'field_name' => 'field_color_repeat',
'label' => 'Repeat Color',
'description' => 'Color repeat description',
'entity_type' => 'node',
'bundle' => 'article',
'required' => FALSE,
])
->save();
$this->form = $entityTypeManager
->getStorage('entity_form_display')
->load('node.article.default');
$this->display = $entityTypeManager
->getStorage('entity_view_display')
->load('node.article.default');
}
public function testColorFieldWidgetBox() {
$this->form
->setComponent('field_color_repeat', [
'type' => 'color_field_widget_box',
'settings' => [
'default_colors' => '#FF0000,#FFFFFF',
],
])
->setComponent('field_color', [
'type' => 'color_field_widget_box',
'settings' => [
'default_colors' => '#007749,#000000,#FFFFFF,#FFB81C,#E03C31,#001489,#ffafc8,#74d7ee',
],
])
->save();
$session = $this
->getSession();
$web_assert = $this
->assertSession();
$this
->drupalGet('node/add/article');
$page = $session
->getPage();
$web_assert
->waitForElementVisible('css', '#color-field-field-color_repeat button');
$boxes = $page
->findAll('css', '#color-field-field-color-repeat button');
$this
->assertEquals(3, count($boxes));
$web_assert
->responseContains('Freeform Color');
$web_assert
->responseContains('Color field description');
$boxes = $page
->findAll('css', '#color-field-field-color button');
$this
->assertEquals(8, count($boxes));
$box = $boxes[0];
$this
->assertEquals('#007749', $box
->getAttribute('color'));
$active = $page
->findAll('css', 'button.color_field_widget_box__square.active');
$this
->assertEquals(1, count($active));
$box
->click();
$field = $page
->findField('field_color[0][color]');
$this
->assertEquals('#007749', $field
->getValue());
}
public function testColorFieldSpectrum() {
$this->form
->setComponent('field_color_repeat', [
'type' => 'color_field_widget_spectrum',
'settings' => [
'palette' => '["#0678BE","#53B0EB", "#96BC44"]',
'show_palette' => FALSE,
],
])
->setComponent('field_color', [
'type' => 'color_field_widget_spectrum',
'settings' => [
'palette' => '["#005493","#F5AA1C","#C63527","002754", hsv 0 100 100, "rgba(0,255,255,0.5)", green,hsl(0 100 50)]',
'show_palette' => TRUE,
],
])
->save();
FieldConfig::load('node.article.field_color_repeat')
->setSetting('opacity', 0)
->save();
$session = $this
->getSession();
$web_assert = $this
->assertSession();
$this
->drupalGet('node/add/article');
$page = $session
->getPage();
$web_assert
->responseContains('Freeform Color');
$web_assert
->responseContains('Color field description');
$web_assert
->waitForElementVisible('css', '.sp-preview');
$boxes = $page
->findAll('css', '.sp-thumb-el');
$this
->assertEquals(13, count($boxes));
$alpha = $page
->findAll('css', '.sp-alpha-enabled');
$this
->assertEquals(1, count($alpha));
}
public function testColorFieldWidgetGrid() {
$this->form
->setComponent('field_color_repeat', [
'type' => 'color_field_widget_grid',
'settings' => [
'cell_width' => '20',
'cell_height' => '20',
'cell_margin' => '1',
'box_width' => '250',
'box_height' => '100',
'columns' => '16',
],
])
->setComponent('field_color', [
'type' => 'color_field_widget_grid',
'settings' => [
'cell_width' => '10',
'cell_height' => '10',
'cell_margin' => '1',
'box_width' => '115',
'box_height' => '20',
'columns' => '16',
],
])
->save();
$session = $this
->getSession();
$web_assert = $this
->assertSession();
$this
->drupalGet('node/add/article');
$page = $session
->getPage();
$web_assert
->responseContains('Freeform Color');
$web_assert
->responseContains('Color field description');
$web_assert
->waitForElementVisible('css', '.simpleColorDisplay');
$boxes = $page
->findAll('css', '.simpleColorDisplay');
$this
->assertEquals(2, count($boxes));
$script = <<<HEREDOC
(function() {
fields = jQuery('.simpleColorDisplay');
return jQuery(fields[0]).width() == jQuery(fields[1]).width()
})()
HEREDOC;
$this
->assertFalse($session
->evaluateScript($script));
}
}