ArbitraryRebuildTest.php

Same filename in other branches
  1. 9 core/modules/system/tests/src/Functional/Form/ArbitraryRebuildTest.php
  2. 8.9.x core/modules/system/tests/src/Functional/Form/ArbitraryRebuildTest.php
  3. 11.x core/modules/system/tests/src/Functional/Form/ArbitraryRebuildTest.php

Namespace

Drupal\Tests\system\Functional\Form

File

core/modules/system/tests/src/Functional/Form/ArbitraryRebuildTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\system\Functional\Form;

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;

/**
 * Tests altering forms to be rebuilt so there are multiple steps.
 *
 * @group Form
 */
class ArbitraryRebuildTest extends BrowserTestBase {
    
    /**
     * {@inheritdoc}
     */
    protected static $modules = [
        'text',
        'form_test',
    ];
    
    /**
     * {@inheritdoc}
     */
    protected $defaultTheme = 'stark';
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        // Auto-create a field for testing.
        FieldStorageConfig::create([
            'entity_type' => 'user',
            'field_name' => 'test_multiple',
            'type' => 'text',
            'cardinality' => -1,
            'translatable' => FALSE,
        ])->save();
        FieldConfig::create([
            'entity_type' => 'user',
            'field_name' => 'test_multiple',
            'bundle' => 'user',
            'label' => 'Test a multiple valued field',
        ])->save();
        \Drupal::service('entity_display.repository')->getFormDisplay('user', 'user', 'register')
            ->setComponent('test_multiple', [
            'type' => 'text_textfield',
            'weight' => 0,
        ])
            ->save();
    }
    
    /**
     * Tests a basic rebuild with the user registration form.
     */
    public function testUserRegistrationRebuild() : void {
        $edit = [
            'name' => 'foo',
            'mail' => 'bar@example.com',
        ];
        $this->drupalGet('user/register');
        $this->submitForm($edit, 'Rebuild');
        $this->assertSession()
            ->pageTextContains('Form rebuilt.');
        $this->assertSession()
            ->fieldValueEquals('name', 'foo');
        $this->assertSession()
            ->fieldValueEquals('mail', 'bar@example.com');
    }
    
    /**
     * Tests a rebuild caused by a multiple value field.
     */
    public function testUserRegistrationMultipleField() : void {
        $edit = [
            'name' => 'foo',
            'mail' => 'bar@example.com',
        ];
        $this->drupalGet('user/register');
        $this->submitForm($edit, 'Add another item');
        $this->assertSession()
            ->pageTextContains('Test a multiple valued field');
        $this->assertSession()
            ->fieldValueEquals('name', 'foo');
        $this->assertSession()
            ->fieldValueEquals('mail', 'bar@example.com');
    }

}

Classes

Title Deprecated Summary
ArbitraryRebuildTest Tests altering forms to be rebuilt so there are multiple steps.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.