View source
<?php
namespace Drupal\Tests\system\Functional\Form;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\BrowserTestBase;
class StateValuesCleanTest extends BrowserTestBase {
protected static $modules = [
'form_test',
];
protected $defaultTheme = 'stark';
public function testFormStateValuesClean() {
$this
->drupalGet('form_test/form-state-values-clean');
$this
->submitForm([], 'Submit');
$values = Json::decode($this
->getSession()
->getPage()
->getContent());
$result = [
'beer' => 1000,
'baz' => [
'beer' => 2000,
],
];
$this
->assertFalse(isset($values['form_id']), new FormattableMarkup('%element was removed.', [
'%element' => 'form_id',
]));
$this
->assertFalse(isset($values['form_token']), new FormattableMarkup('%element was removed.', [
'%element' => 'form_token',
]));
$this
->assertFalse(isset($values['form_build_id']), new FormattableMarkup('%element was removed.', [
'%element' => 'form_build_id',
]));
$this
->assertFalse(isset($values['op']), new FormattableMarkup('%element was removed.', [
'%element' => 'op',
]));
$this
->assertFalse(isset($values['foo']), new FormattableMarkup('%element was removed.', [
'%element' => 'foo',
]));
$this
->assertFalse(isset($values['bar']), new FormattableMarkup('%element was removed.', [
'%element' => 'bar',
]));
$this
->assertFalse(isset($values['baz']['foo']), new FormattableMarkup('%element was removed.', [
'%element' => 'foo',
]));
$this
->assertFalse(isset($values['baz']['baz']), new FormattableMarkup('%element was removed.', [
'%element' => 'baz',
]));
$this
->assertFalse(isset($values['wine']), new FormattableMarkup('%element was removed.', [
'%element' => 'wine',
]));
$this
->assertTrue(isset($values['baz']['beer']), 'Nested form value still exists.');
$this
->assertEquals($result, $values, 'Expected form values equal actual form values.');
}
}