34ebb2e79ad1f4a0f38a007412c088f2ca5a89de
[yaffs-website] / web / core / modules / system / tests / src / Functional / Form / FormObjectTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Form;
4
5 use Drupal\system\Tests\System\SystemConfigFormTestBase;
6 use Drupal\form_test\FormTestObject;
7
8 /**
9  * Tests building a form from an object.
10  *
11  * @group Form
12  */
13 class FormObjectTest extends SystemConfigFormTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['form_test'];
21
22   protected function setUp() {
23     parent::setUp();
24
25     $this->form = new FormTestObject($this->container->get('config.factory'));
26     $this->values = [
27       'bananas' => [
28         '#value' => $this->randomString(10),
29         '#config_name' => 'form_test.object',
30         '#config_key' => 'bananas',
31       ],
32     ];
33   }
34
35   /**
36    * Tests using an object as the form callback.
37    *
38    * @see \Drupal\form_test\EventSubscriber\FormTestEventSubscriber::onKernelRequest()
39    */
40   public function testObjectFormCallback() {
41     $config_factory = $this->container->get('config.factory');
42
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);
52
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);
62
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);
72
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);
85   }
86
87 }