FormObjectTest.php in Drupal 9
File
core/modules/system/tests/src/Functional/Form/FormObjectTest.php
View source
<?php
namespace Drupal\Tests\system\Functional\Form;
use Drupal\Tests\BrowserTestBase;
class FormObjectTest extends BrowserTestBase {
protected static $modules = [
'form_test',
];
protected $defaultTheme = 'stark';
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.');
$elements = $this
->xpath('//form[@id="form-test-form-test-object"]');
$this
->assertTrue(!empty($elements), 'The correct form ID was used.');
$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.');
$elements = $this
->xpath('//form[@id="form-test-form-test-arguments-object"]');
$this
->assertTrue(!empty($elements), 'The correct form ID was used.');
$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.');
$elements = $this
->xpath('//form[@id="form-test-form-test-service-object"]');
$this
->assertTrue(!empty($elements), 'The correct form ID was used.');
$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.');
$elements = $this
->xpath('//form[@id="form-test-form-test-controller-object"]');
$this
->assertTrue(!empty($elements), 'The correct form ID was used.');
$this
->assertSession()
->pageTextContains('custom_value');
$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);
}
}