class TextWidgetTest

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

Test basic functionality of the widgets.

Create a content type with a example_field_rgb field, configure it with the field_example_text-widget, create a node and check for correct 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\TextWidgetTest extends \Drupal\Tests\field_example\Functional\FieldExampleBrowserTestBase

Expanded class hierarchy of TextWidgetTest

Related topics

File

field_example/tests/src/Functional/TextWidgetTest.php, line 18

Namespace

Drupal\Tests\field_example\Functional
View source
class TextWidgetTest extends FieldExampleBrowserTestBase {
    
    /**
     * Test basic functionality of the example field.
     *
     * - Creates a content type.
     * - Adds a single-valued field_example_rgb to it.
     * - Adds a multivalued field_example_rgb to it.
     * - Creates a node of the new type.
     * - Populates the single-valued field.
     * - Populates the multivalued field with two items.
     * - Tests the result.
     */
    public function testSingleValueField() {
        $assert = $this->assertSession();
        // Add a single field as administrator user.
        $this->drupalLogin($this->administratorAccount);
        $this->fieldName = $this->createField('field_example_rgb', 'field_example_text', '1');
        // Now that we have a content type with the desired field, switch to the
        // author user to create content with it.
        $this->drupalLogin($this->authorAccount);
        $this->drupalGet('node/add/' . $this->contentTypeName);
        // Add a node.
        $title = $this->randomMachineName(20);
        $edit = [
            'title[0][value]' => $title,
            'field_' . $this->fieldName . '[0][value]' => '#000001',
        ];
        // Create the content.
        $this->drupalPostForm(NULL, $edit, 'Save');
        $assert->pageTextContains((string) new FormattableMarkup('@type @title has been created', [
            '@type' => $this->contentTypeName,
            '@title' => $title,
        ]));
        // Verify the value is shown when viewing this node.
        $field_p = $this->xpath("//div[contains(@class,'field--type-field-example-rgb')]/div/p");
        $this->assertEquals('The color code in this field is #000001', (string) $field_p[0]->getText());
    }
    
    /**
     * Test basic functionality of the example field.
     *
     * - Creates a content type.
     * - Adds a single-valued field_example_rgb to it.
     * - Adds a multivalued field_example_rgb to it.
     * - Creates a node of the new type.
     * - Populates the single-valued field.
     * - Populates the multivalued field with two items.
     * - Tests the result.
     */
    public function testMultiValueField() {
        $assert = $this->assertSession();
        // Add a single field as administrator user.
        $this->drupalLogin($this->administratorAccount);
        $this->fieldName = $this->createField('field_example_rgb', 'field_example_text', '-1');
        // Now that we have a content type with the desired field, switch to the
        // author user to create content with it.
        $this->drupalLogin($this->authorAccount);
        $this->drupalGet('node/add/' . $this->contentTypeName);
        // Add a node.
        $title = $this->randomMachineName(20);
        $edit = [
            'title[0][value]' => $title,
            'field_' . $this->fieldName . '[0][value]' => '#00ff00',
        ];
        // We want to add a 2nd item to the multivalue field, so hit "add another".
        $this->drupalPostForm(NULL, $edit, 'Add another item');
        $edit = [
            'field_' . $this->fieldName . '[1][value]' => '#ffffff',
        ];
        // Now we can fill in the second item in the multivalue field and save.
        $this->drupalPostForm(NULL, $edit, 'Save');
        $assert->pageTextContains((string) new FormattableMarkup('@type @title has been created', [
            '@type' => $this->contentTypeName,
            '@title' => $title,
        ]));
        // Verify the value is shown when viewing this node.
        $field_p = $this->xpath("//div[contains(@class,'field--type-field-example-rgb')]/div/div/p");
        $this->assertEquals('The color code in this field is #00ff00', (string) $field_p[0]->getText());
        $this->assertEquals('The color code in this field is #ffffff', (string) $field_p[1]->getText());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
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
TextWidgetTest::testMultiValueField public function Test basic functionality of the example field.
TextWidgetTest::testSingleValueField public function Test basic functionality of the example field.