class ColorPickerWidgetTest

Same name in other branches
  1. 3.x modules/field_example/tests/src/Functional/ColorPickerWidgetTest.php \Drupal\Tests\field_example\Functional\ColorPickerWidgetTest
  2. 4.0.x modules/field_example/tests/src/Functional/ColorPickerWidgetTest.php \Drupal\Tests\field_example\Functional\ColorPickerWidgetTest

Test the basic functionality of Color Picker Widget.

Create the content type with field_example_rgb field, and configure it with field_example_colorpicker, then create a node of content type and verify the values.

@group field_example @group examples

Hierarchy

  • class \Drupal\Tests\examples\Functional\ExamplesBrowserTestBase extends \Drupal\Tests\BrowserTestBase
    • class \Drupal\Tests\field_example\Functional\FieldExampleBrowserTestBase extends \Drupal\Tests\examples\Functional\ExamplesBrowserTestBase
      • class \Drupal\Tests\field_example\Functional\ColorPickerWidgetTest extends \Drupal\Tests\field_example\Functional\FieldExampleBrowserTestBase

Expanded class hierarchy of ColorPickerWidgetTest

Related topics

File

field_example/tests/src/Functional/ColorPickerWidgetTest.php, line 19

Namespace

Drupal\Tests\field_example\Functional
View source
class ColorPickerWidgetTest extends FieldExampleBrowserTestBase {
    
    /**
     * Field example scenario tests.
     *
     * The following scenarios:
     * - Creates a content type.
     * - Adds a single-valued field_example_rgb to it.
     * - Creates a node of the new type.
     * - Populates the single-valued field.
     * - Tests the result.
     */
    public function testSingleValueField() {
        $assert = $this->assertSession();
        // Login with Admin and create a field.
        $this->drupalLogin($this->administratorAccount);
        $this->fieldName = $this->createField('field_example_rgb', 'field_example_colorpicker', '1');
        // Login with Author user for content creation.
        $this->drupalLogin($this->authorAccount);
        $this->drupalGet('node/add/' . $this->contentTypeName);
        // Details to be submitted for content creation.
        $title = $this->randomMachineName(20);
        $edit = [
            'title[0][value]' => $title,
            'field_' . $this->fieldName . '[0][value]' => '#00ff00',
        ];
        // Submit the content creation form.
        $this->drupalPostForm(NULL, $edit, 'Save');
        $assert->pageTextContains((string) new FormattableMarkup('@type @title has been created', [
            '@type' => $this->contentTypeName,
            '@title' => $title,
        ]));
        // Verify color.
        $assert->pageTextContains('The color code in this field is #00ff00');
    }
    
    /**
     * Field example scenario tests.
     *
     * The following scenarios:
     * - Creates a content type.
     * - Adds a multivalued field_example_rgb to it.
     * - Creates a node of the new type.
     * - Populates the multivalued field with two items.
     * - Tests the result.
     */
    public function testMultiValueField() {
        $assert = $this->assertSession();
        // Login with Admin and create a field.
        $this->drupalLogin($this->administratorAccount);
        $this->fieldName = $this->createField('field_example_rgb', 'field_example_colorpicker', '-1');
        // Login with Author user for content creation.
        $this->drupalLogin($this->authorAccount);
        $this->drupalGet('node/add/' . $this->contentTypeName);
        // Details to be submitted for content creation.
        $title = $this->randomMachineName(20);
        $edit = [
            'title[0][value]' => $title,
            'field_' . $this->fieldName . '[0][value]' => '#00ff00',
        ];
        // Add another field value.
        $this->drupalPostForm(NULL, $edit, 'Add another item');
        // Set value for newly added item.
        $edit = [
            'field_' . $this->fieldName . '[1][value]' => '#ffffff',
        ];
        // Submit the content creation form.
        $this->drupalPostForm(NULL, $edit, 'Save');
        $assert->pageTextContains((string) new FormattableMarkup('@type @title has been created', [
            '@type' => $this->contentTypeName,
            '@title' => $title,
        ]));
        // Verify color.
        $assert->pageTextContains('The color code in this field is #00ff00');
        $assert->pageTextContains('The color code in this field is #ffffff');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ColorPickerWidgetTest::testMultiValueField public function Field example scenario tests.
ColorPickerWidgetTest::testSingleValueField public function Field example scenario tests.
ExamplesBrowserTestBase::setupExamplesMenus protected function Set up menus and tasks in their regions.
FieldExampleBrowserTestBase::$administratorAccount protected property The administrator account.
FieldExampleBrowserTestBase::$authorAccount protected property The author account.
FieldExampleBrowserTestBase::$contentTypeName protected property The content type name.
FieldExampleBrowserTestBase::$defaultTheme protected property
FieldExampleBrowserTestBase::$fieldName protected property The field name.
FieldExampleBrowserTestBase::$modules public static property Modules to enable. Overrides ExamplesBrowserTestBase::$modules 1
FieldExampleBrowserTestBase::createField protected function Create a field on the content type created during setUp().
FieldExampleBrowserTestBase::setUp protected function Once installed, a content type with the desired field is created. Overrides ExamplesBrowserTestBase::setUp