Commit 9a96be46 authored by NickWilde's avatar NickWilde

Issue #2985211 by NickWilde: Create a test suit

Test improvements.
parent ccd7318c
......@@ -5,3 +5,5 @@ core: 8.x
package: Field types
dependencies:
- drupal:field
test_dependencies:
- token:token
......@@ -8,6 +8,9 @@
"require": {
"drupal/core": "*"
},
"require-dev": {
"drupal/token": "~1.3"
},
"suggest": {
"bower-asset/jquery-simple-color": "^v1.2.2: Provides JavaScript library necessary for the Color Grid widget",
"bower-asset/spectrum": "^1.8: Provides JavaScript library necessary for Spectrum widget."
......
......@@ -2,74 +2,18 @@
namespace Drupal\Tests\color_field\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Tests color field formatters.
*
* @group color_field
*/
class ColorFieldFormatterTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'field',
'node',
'color_field',
];
/**
* The Entity Type Manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* A user with permission to create articles.
*
* @var \Drupal\user\UserInterface
*/
protected $webUser;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalCreateContentType(['type' => 'article']);
$this->webUser = $this->drupalCreateUser(['create article content', 'edit own article content']);
$this->drupalLogin($this->webUser);
$this->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',
'entity_type' => 'node',
'bundle' => 'article',
])->save();
}
class ColorFieldFormatterTest extends ColorFieldFunctionalTestBase {
/**
* Test color_field_formatter_text formatter.
*/
public function testColorFieldFormatterText() {
/** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $form */
$form = $this->entityTypeManager->getStorage('entity_form_display')
->load('node.article.default');
$form->setComponent('field_color', [
$this->form->setComponent('field_color', [
'type' => 'color_field_widget_default',
'settings' => [
'placeholder_color' => '#ABC123',
......@@ -77,12 +21,10 @@ class ColorFieldFormatterTest extends BrowserTestBase {
],
])->save();
/** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
$display = $this->entityTypeManager->getStorage('entity_view_display')
->load('node.article.default');
$display->setComponent('field_color', [
$this->display->setComponent('field_color', [
'type' => 'color_field_formatter_text',
'weight' => 1,
'label' => 'hidden',
])->save();
// Display creation form.
......@@ -111,7 +53,7 @@ class ColorFieldFormatterTest extends BrowserTestBase {
];
// Render without opacity value.
$display->setComponent('field_color', [
$this->display->setComponent('field_color', [
'type' => 'color_field_formatter_text',
'weight' => 1,
'settings' => [
......@@ -128,7 +70,7 @@ class ColorFieldFormatterTest extends BrowserTestBase {
'field_color[0][color]' => "#FFEF00",
'field_color[0][opacity]' => 0.9,
];
$display->setComponent('field_color', [
$this->display->setComponent('field_color', [
'type' => 'color_field_formatter_text',
'weight' => 1,
'settings' => [
......@@ -146,7 +88,7 @@ class ColorFieldFormatterTest extends BrowserTestBase {
'field_color[0][color]' => "#00811F",
'field_color[0][opacity]' => 0.8,
];
$display->setComponent('field_color', [
$this->display->setComponent('field_color', [
'type' => 'color_field_formatter_text',
'weight' => 1,
'settings' => [
......@@ -163,23 +105,17 @@ class ColorFieldFormatterTest extends BrowserTestBase {
* Test color_field_formatter_swatch formatter.
*/
public function testColorFieldFormatterSwatch() {
/** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $form */
$form = $this->entityTypeManager->getStorage('entity_form_display')
->load('node.article.default');
$form->setComponent('field_color', [
$this->form->setComponent('field_color', [
'type' => 'color_field_widget_default',
'settings' => [
'placeholder_color' => '#ABC123',
'placeholder_opacity' => '1.0',
],
])->save();
/** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
$display = $this->entityTypeManager->getStorage('entity_view_display')
->load('node.article.default');
$display->setComponent('field_color', [
$this->display->setComponent('field_color', [
'type' => 'color_field_formatter_swatch',
'weight' => 1,
'label' => 'hidden',
])->save();
// Test square with opacity.
......@@ -199,7 +135,7 @@ class ColorFieldFormatterTest extends BrowserTestBase {
'field_color[0][color]' => "#760089",
'field_color[0][opacity]' => 1,
];
$display->setComponent('field_color', [
$this->display->setComponent('field_color', [
'type' => 'color_field_formatter_swatch',
'weight' => 1,
'settings' => [
......@@ -217,23 +153,17 @@ class ColorFieldFormatterTest extends BrowserTestBase {
* Test color_field_formatter_css formatter.
*/
public function testColorFieldFormatterCss() {
/** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $form */
$form = $this->entityTypeManager->getStorage('entity_form_display')
->load('node.article.default');
$form->setComponent('field_color', [
$this->form->setComponent('field_color', [
'type' => 'color_field_widget_default',
'settings' => [
'placeholder_color' => '#ABC123',
'placeholder_opacity' => '1.0',
],
])->save();
/** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
$display = $this->entityTypeManager->getStorage('entity_view_display')
->load('node.article.default');
$display->setComponent('field_color', [
$this->display->setComponent('field_color', [
'type' => 'color_field_formatter_css',
'weight' => 1,
'label' => 'hidden',
])->save();
// Test default options.
......@@ -252,7 +182,7 @@ class ColorFieldFormatterTest extends BrowserTestBase {
'field_color[0][color]' => "#FFFFFF",
'field_color[0][opacity]' => 1,
];
$display->setComponent('field_color', [
$this->display->setComponent('field_color', [
'type' => 'color_field_formatter_css',
'weight' => 1,
'settings' => [
......@@ -261,64 +191,11 @@ class ColorFieldFormatterTest extends BrowserTestBase {
'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); }');
// Test with token selector.
$edit = [
'title[0][value]' => $this->randomMachineName(),
'field_color[0][color]' => "#9C59D1",
'field_color[0][opacity]' => 0.95,
];
$display->setComponent('field_color', [
'type' => 'color_field_formatter_css',
'weight' => 1,
'settings' => [
'selector' => '.node-[node:content-type]',
'property' => 'background-color',
'important' => FALSE,
'opacity' => TRUE,
],
])->save();
$this->drupalPostForm('node/add/article', $edit, t('Save'));
$this->assertSession()->responseContains('.node-article { background-color: RGBA(156,89,209,0.95); }');
// Ensure 2 fields on the same entity are both rendered properly.
FieldStorageConfig::create([
'field_name' => 'field_text_color',
'entity_type' => 'node',
'type' => 'color_field_type',
])->save();
FieldConfig::create([
'field_name' => 'field_text_color',
'label' => 'Text Color',
'entity_type' => 'node',
'bundle' => 'article',
])->save();
$display->setComponent('field_text_color', [
'type' => 'color_field_formatter_css',
'weight' => 1,
'settings' => [
'selector' => '.node-[node:content-type]',
'property' => 'color',
'important' => FALSE,
'opacity' => TRUE,
],
])->save();
$edit = [
'title[0][value]' => $this->randomMachineName(),
'field_color[0][color]' => "#000000",
'field_color[0][opacity]' => 0.1,
'field_text_color[0][color]' => "#000000",
'field_text_color[0][opacity]' => 1,
];
$this->drupalPostForm('node/add/article', $edit, t('Save'));
$this->assertSession()->responseContains('.node-article { background-color: RGBA(0,0,0,0.1); }');
$this->assertSession()->responseContains('.node-article { color: RGBA(0,0,0,1); }');
}
}
<?php
namespace Drupal\Tests\color_field\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Tests color field formatters.
*
* @group color_field
*/
class ColorFieldFormatterTokenTest extends ColorFieldFunctionalTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'field',
'node',
'color_field',
'token',
];
/**
* Test color_field_formatter_css formatter.
*/
public function testTokens() {
$this->form->setComponent('field_color', [
'type' => 'color_field_widget_default',
])->save();
$edit = [
'title[0][value]' => $this->randomMachineName(),
'field_color[0][color]' => "#9C59D1",
'field_color[0][opacity]' => 0.95,
];
$this->display->setComponent('field_color', [
'type' => 'color_field_formatter_css',
'weight' => 1,
'settings' => [
'selector' => '.node-[node:content-type]',
'property' => 'background-color',
'important' => FALSE,
'opacity' => TRUE,
],
'label' => 'hidden',
])->save();
$this->drupalPostForm('node/add/article', $edit, t('Save'));
$this->assertSession()->responseContains('.node-article { background-color: RGBA(156,89,209,0.95); }');
// Ensure 2 fields on the same entity are both rendered properly.
FieldStorageConfig::create([
'field_name' => 'field_text_color',
'entity_type' => 'node',
'type' => 'color_field_type',
])->save();
FieldConfig::create([
'field_name' => 'field_text_color',
'label' => 'Text Color',
'entity_type' => 'node',
'bundle' => 'article',
])->save();
$this->display->setComponent('field_text_color', [
'type' => 'color_field_formatter_css',
'weight' => 1,
'settings' => [
'selector' => '.node-[node:content-type]',
'property' => 'color',
'important' => FALSE,
'opacity' => TRUE,
],
'label' => 'hidden',
])->save();
$this->form->setComponent('field_text_color', [
'type' => 'color_field_widget_default',
])->save();
$edit = [
'title[0][value]' => $this->randomMachineName(),
'field_color[0][color]' => "#000000",
'field_color[0][opacity]' => 0.1,
'field_text_color[0][color]' => "#000000",
'field_text_color[0][opacity]' => 1,
];
$this->drupalPostForm('node/add/article', $edit, t('Save'));
$this->assertSession()->responseContains('.node-article { background-color: RGBA(0,0,0,0.1); }');
$this->assertSession()->responseContains('.node-article { color: RGBA(0,0,0,1); }');
}
}
<?php
namespace Drupal\Tests\color_field\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Provide basic setup for all color field functional tests.
*
* @group color_field
*/
abstract class ColorFieldFunctionalTestBase extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'field',
'node',
'color_field',
];
/**
* The Entity View Display for the article node type.
*
* @var \Drupal\Core\Entity\Entity\EntityViewDisplay
*/
protected $display;
/**
* The Entity Form Display for the article node type.
*
* @var \Drupal\Core\Entity\Entity\EntityFormDisplay
*/
protected $form;
/**
* {@inheritdoc}
*/
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',
])->save();
$this->form = $entityTypeManager->getStorage('entity_form_display')
->load('node.article.default');
$this->display = $entityTypeManager->getStorage('entity_view_display')
->load('node.article.default');
}
}
......@@ -2,82 +2,21 @@
namespace Drupal\Tests\color_field\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Tests color field widgets.
*
* @group color_field
*/
class ColorFieldWidgetTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'field',
'node',
'color_field',
];
/**
* The Entity Type Manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* A user with permission to create articles.
*
* @var \Drupal\user\UserInterface
*/
protected $webUser;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalCreateContentType(['type' => 'article']);
$this->webUser = $this->drupalCreateUser(['create article content', 'edit own article content']);
$this->drupalLogin($this->webUser);
$this->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',
])->save();
}
class ColorFieldWidgetTest extends ColorFieldFunctionalTestBase {
/**
* Test color_field_widget_html5.
*/
public function testColorFieldWidgetHtml5() {
/** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $form */
$form = $this->entityTypeManager->getStorage('entity_form_display')
->load('node.article.default');
$form->setComponent('field_color', [
$this->form->setComponent('field_color', [
'type' => 'color_field_widget_html5',
])->save();
/** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
$display = $this->entityTypeManager->getStorage('entity_view_display')
->load('node.article.default');
$display->setComponent('field_color', [
$this->display->setComponent('field_color', [
'type' => 'color_field_formatter_text',
'weight' => 1,
])->save();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment