3 namespace Drupal\Tests\system\Functional\Form;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests building a form from an object.
12 class FormObjectTest extends BrowserTestBase {
19 public static $modules = ['form_test'];
22 * Tests using an object as the form callback.
24 * @see \Drupal\form_test\EventSubscriber\FormTestEventSubscriber::onKernelRequest()
26 public function testObjectFormCallback() {
27 $config_factory = $this->container->get('config.factory');
29 $this->drupalGet('form-test/object-builder');
30 $this->assertText('The FormTestObject::buildForm() method was used for this form.');
31 $elements = $this->xpath('//form[@id="form-test-form-test-object"]');
32 $this->assertTrue(!empty($elements), 'The correct form ID was used.');
33 $this->drupalPostForm(NULL, ['bananas' => 'green'], t('Save'));
34 $this->assertText('The FormTestObject::validateForm() method was used for this form.');
35 $this->assertText('The FormTestObject::submitForm() method was used for this form.');
36 $value = $config_factory->get('form_test.object')->get('bananas');
37 $this->assertIdentical('green', $value);
39 $this->drupalGet('form-test/object-arguments-builder/yellow');
40 $this->assertText('The FormTestArgumentsObject::buildForm() method was used for this form.');
41 $elements = $this->xpath('//form[@id="form-test-form-test-arguments-object"]');
42 $this->assertTrue(!empty($elements), 'The correct form ID was used.');
43 $this->drupalPostForm(NULL, NULL, t('Save'));
44 $this->assertText('The FormTestArgumentsObject::validateForm() method was used for this form.');
45 $this->assertText('The FormTestArgumentsObject::submitForm() method was used for this form.');
46 $value = $config_factory->get('form_test.object')->get('bananas');
47 $this->assertIdentical('yellow', $value);
49 $this->drupalGet('form-test/object-service-builder');
50 $this->assertText('The FormTestServiceObject::buildForm() method was used for this form.');
51 $elements = $this->xpath('//form[@id="form-test-form-test-service-object"]');
52 $this->assertTrue(!empty($elements), 'The correct form ID was used.');
53 $this->drupalPostForm(NULL, ['bananas' => 'brown'], t('Save'));
54 $this->assertText('The FormTestServiceObject::validateForm() method was used for this form.');
55 $this->assertText('The FormTestServiceObject::submitForm() method was used for this form.');
56 $value = $config_factory->get('form_test.object')->get('bananas');
57 $this->assertIdentical('brown', $value);
59 $this->drupalGet('form-test/object-controller-builder');
60 $this->assertText('The FormTestControllerObject::create() method was used for this form.');
61 $this->assertText('The FormTestControllerObject::buildForm() method was used for this form.');
62 $elements = $this->xpath('//form[@id="form-test-form-test-controller-object"]');
63 $this->assertTrue(!empty($elements), 'The correct form ID was used.');
64 $this->assertText('custom_value', 'Ensure parameters are injected from request attributes.');
65 $this->assertText('request_value', 'Ensure the request object is injected.');
66 $this->drupalPostForm(NULL, ['bananas' => 'black'], t('Save'));
67 $this->assertText('The FormTestControllerObject::validateForm() method was used for this form.');
68 $this->assertText('The FormTestControllerObject::submitForm() method was used for this form.');
69 $value = $config_factory->get('form_test.object')->get('bananas');
70 $this->assertIdentical('black', $value);