class FormObjectTest
Same name in this branch
- 9 core/modules/system/tests/src/Kernel/Form/FormObjectTest.php \Drupal\Tests\system\Kernel\Form\FormObjectTest
Same name and namespace in other branches
- 11.x core/modules/system/tests/src/Kernel/Form/FormObjectTest.php \Drupal\Tests\system\Kernel\Form\FormObjectTest
- 11.x core/modules/system/tests/src/Functional/Form/FormObjectTest.php \Drupal\Tests\system\Functional\Form\FormObjectTest
- 10 core/modules/system/tests/src/Kernel/Form/FormObjectTest.php \Drupal\Tests\system\Kernel\Form\FormObjectTest
- 10 core/modules/system/tests/src/Functional/Form/FormObjectTest.php \Drupal\Tests\system\Functional\Form\FormObjectTest
- 8.9.x core/modules/system/tests/src/Kernel/Form/FormObjectTest.php \Drupal\Tests\system\Kernel\Form\FormObjectTest
- 8.9.x core/modules/system/tests/src/Functional/Form/FormObjectTest.php \Drupal\Tests\system\Functional\Form\FormObjectTest
Tests building a form from an object.
@group Form
Hierarchy
- class \Drupal\Tests\BrowserTestBase uses \Drupal\Core\Test\FunctionalTestSetupTrait, \Drupal\Tests\UiHelperTrait, \Drupal\Core\Test\TestSetupTrait, \Drupal\Tests\block\Traits\BlockCreationTrait, \Drupal\FunctionalTests\AssertLegacyTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\node\Traits\NodeCreationTrait, \Drupal\Tests\node\Traits\ContentTypeCreationTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\user\Traits\UserCreationTrait, \Drupal\Tests\XdebugRequestTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\ExtensionListTestTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\system\Functional\Form\FormObjectTest extends \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of FormObjectTest
File
-
core/
modules/ system/ tests/ src/ Functional/ Form/ FormObjectTest.php, line 12
Namespace
Drupal\Tests\system\Functional\FormView source
class FormObjectTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'form_test',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests using an object as the form callback.
*
* @see \Drupal\form_test\EventSubscriber\FormTestEventSubscriber::onKernelRequest()
*/
public function testObjectFormCallback() {
$config_factory = $this->container
->get('config.factory');
$this->drupalGet('form-test/object-builder');
$this->assertSession()
->pageTextContains('The FormTestObject::buildForm() method was used for this form.');
$this->assertSession()
->elementExists('xpath', '//form[@id="form-test-form-test-object"]');
$this->submitForm([
'bananas' => 'green',
], 'Save');
$this->assertSession()
->pageTextContains('The FormTestObject::validateForm() method was used for this form.');
$this->assertSession()
->pageTextContains('The FormTestObject::submitForm() method was used for this form.');
$value = $config_factory->get('form_test.object')
->get('bananas');
$this->assertSame('green', $value);
$this->drupalGet('form-test/object-arguments-builder/yellow');
$this->assertSession()
->pageTextContains('The FormTestArgumentsObject::buildForm() method was used for this form.');
$this->assertSession()
->elementExists('xpath', '//form[@id="form-test-form-test-arguments-object"]');
$this->submitForm([], 'Save');
$this->assertSession()
->pageTextContains('The FormTestArgumentsObject::validateForm() method was used for this form.');
$this->assertSession()
->pageTextContains('The FormTestArgumentsObject::submitForm() method was used for this form.');
$value = $config_factory->get('form_test.object')
->get('bananas');
$this->assertSame('yellow', $value);
$this->drupalGet('form-test/object-service-builder');
$this->assertSession()
->pageTextContains('The FormTestServiceObject::buildForm() method was used for this form.');
$this->assertSession()
->elementExists('xpath', '//form[@id="form-test-form-test-service-object"]');
$this->submitForm([
'bananas' => 'brown',
], 'Save');
$this->assertSession()
->pageTextContains('The FormTestServiceObject::validateForm() method was used for this form.');
$this->assertSession()
->pageTextContains('The FormTestServiceObject::submitForm() method was used for this form.');
$value = $config_factory->get('form_test.object')
->get('bananas');
$this->assertSame('brown', $value);
$this->drupalGet('form-test/object-controller-builder');
$this->assertSession()
->pageTextContains('The FormTestControllerObject::create() method was used for this form.');
$this->assertSession()
->pageTextContains('The FormTestControllerObject::buildForm() method was used for this form.');
$this->assertSession()
->elementExists('xpath', '//form[@id="form-test-form-test-controller-object"]');
// Ensure parameters are injected from request attributes.
$this->assertSession()
->pageTextContains('custom_value');
// Ensure the request object is injected.
$this->assertSession()
->pageTextContains('request_value');
$this->submitForm([
'bananas' => 'black',
], 'Save');
$this->assertSession()
->pageTextContains('The FormTestControllerObject::validateForm() method was used for this form.');
$this->assertSession()
->pageTextContains('The FormTestControllerObject::submitForm() method was used for this form.');
$value = $config_factory->get('form_test.object')
->get('bananas');
$this->assertSame('black', $value);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.