public function StorageTest::testImmutableFormLegacyProtection in Drupal 8
Same name and namespace in other branches
- 9 core/modules/system/tests/src/Functional/Form/StorageTest.php \Drupal\Tests\system\Functional\Form\StorageTest::testImmutableFormLegacyProtection()
- 10 core/modules/system/tests/src/Functional/Form/StorageTest.php \Drupal\Tests\system\Functional\Form\StorageTest::testImmutableFormLegacyProtection()
Verify that existing contrib code cannot overwrite immutable form state.
File
- core/
modules/ system/ tests/ src/ Functional/ Form/ StorageTest.php, line 177
Class
- StorageTest
- Tests a multistep form using form storage and makes sure validation and caching works right.
Namespace
Drupal\Tests\system\Functional\FormCode
public function testImmutableFormLegacyProtection() {
$this
->drupalGet('form_test/form-storage', [
'query' => [
'cache' => 1,
'immutable' => 1,
],
]);
$build_id_fields = $this
->xpath('//input[@name="form_build_id"]');
$this
->assertCount(1, $build_id_fields, 'One form build id field on the page');
$build_id = $build_id_fields[0]
->getValue();
// Try to poison the form cache.
$response = $this
->drupalGet('form-test/form-storage-legacy/' . $build_id, [
'query' => [
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
],
], [
'X-Requested-With: XMLHttpRequest',
]);
$original = json_decode($response, TRUE);
$this
->assertEquals($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded');
$this
->assertNotEquals($original['form']['#build_id'], $build_id, 'New build_id was generated');
// Assert that a watchdog message was logged by
// \Drupal::formBuilder()->setCache().
$status = (bool) Database::getConnection()
->select('watchdog')
->condition('message', 'Form build-id mismatch detected while attempting to store a form in the cache.')
->range(0, 1)
->countQuery()
->execute()
->fetchField();
$this
->assertTrue($status, 'A watchdog message was logged by \\Drupal::formBuilder()->setCache');
// Ensure that the form state was not poisoned by the preceding call.
$response = $this
->drupalGet('form-test/form-storage-legacy/' . $build_id, [
'query' => [
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
],
], [
'X-Requested-With: XMLHttpRequest',
]);
$original = json_decode($response, TRUE);
$this
->assertEquals($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded');
$this
->assertNotEquals($original['form']['#build_id'], $build_id, 'New build_id was generated');
$this
->assertTrue(empty($original['form']['#poisoned']), 'Original form structure was preserved');
$this
->assertTrue(empty($original['form_state']['poisoned']), 'Original form state was preserved');
}