3 namespace Drupal\Tests\system\Functional\Form;
5 use Drupal\system\Tests\System\SystemConfigFormTestBase;
6 use Drupal\form_test\FormTestObject;
9 * Tests building a form from an object.
13 class FormObjectTest extends SystemConfigFormTestBase {
20 public static $modules = ['form_test'];
22 protected function setUp() {
25 $this->form = new FormTestObject($this->container->get('config.factory'));
28 '#value' => $this->randomString(10),
29 '#config_name' => 'form_test.object',
30 '#config_key' => 'bananas',
36 * Tests using an object as the form callback.
38 * @see \Drupal\form_test\EventSubscriber\FormTestEventSubscriber::onKernelRequest()
40 public function testObjectFormCallback() {
41 $config_factory = $this->container->get('config.factory');
43 $this->drupalGet('form-test/object-builder');
44 $this->assertText('The FormTestObject::buildForm() method was used for this form.');
45 $elements = $this->xpath('//form[@id="form-test-form-test-object"]');
46 $this->assertTrue(!empty($elements), 'The correct form ID was used.');
47 $this->drupalPostForm(NULL, ['bananas' => 'green'], t('Save'));
48 $this->assertText('The FormTestObject::validateForm() method was used for this form.');
49 $this->assertText('The FormTestObject::submitForm() method was used for this form.');
50 $value = $config_factory->get('form_test.object')->get('bananas');
51 $this->assertIdentical('green', $value);
53 $this->drupalGet('form-test/object-arguments-builder/yellow');
54 $this->assertText('The FormTestArgumentsObject::buildForm() method was used for this form.');
55 $elements = $this->xpath('//form[@id="form-test-form-test-arguments-object"]');
56 $this->assertTrue(!empty($elements), 'The correct form ID was used.');
57 $this->drupalPostForm(NULL, NULL, t('Save'));
58 $this->assertText('The FormTestArgumentsObject::validateForm() method was used for this form.');
59 $this->assertText('The FormTestArgumentsObject::submitForm() method was used for this form.');
60 $value = $config_factory->get('form_test.object')->get('bananas');
61 $this->assertIdentical('yellow', $value);
63 $this->drupalGet('form-test/object-service-builder');
64 $this->assertText('The FormTestServiceObject::buildForm() method was used for this form.');
65 $elements = $this->xpath('//form[@id="form-test-form-test-service-object"]');
66 $this->assertTrue(!empty($elements), 'The correct form ID was used.');
67 $this->drupalPostForm(NULL, ['bananas' => 'brown'], t('Save'));
68 $this->assertText('The FormTestServiceObject::validateForm() method was used for this form.');
69 $this->assertText('The FormTestServiceObject::submitForm() method was used for this form.');
70 $value = $config_factory->get('form_test.object')->get('bananas');
71 $this->assertIdentical('brown', $value);
73 $this->drupalGet('form-test/object-controller-builder');
74 $this->assertText('The FormTestControllerObject::create() method was used for this form.');
75 $this->assertText('The FormTestControllerObject::buildForm() method was used for this form.');
76 $elements = $this->xpath('//form[@id="form-test-form-test-controller-object"]');
77 $this->assertTrue(!empty($elements), 'The correct form ID was used.');
78 $this->assertText('custom_value', 'Ensure parameters are injected from request attributes.');
79 $this->assertText('request_value', 'Ensure the request object is injected.');
80 $this->drupalPostForm(NULL, ['bananas' => 'black'], t('Save'));
81 $this->assertText('The FormTestControllerObject::validateForm() method was used for this form.');
82 $this->assertText('The FormTestControllerObject::submitForm() method was used for this form.');
83 $value = $config_factory->get('form_test.object')->get('bananas');
84 $this->assertIdentical('black', $value);