View source
<?php
namespace Drupal\Tests\system\Functional\Form;
use Drupal\Core\Database\Database;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Tests\BrowserTestBase;
class StorageTest extends BrowserTestBase {
public static $modules = [
'form_test',
'dblog',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser());
}
public function testForm() {
$this
->drupalGet('form_test/form-storage');
$assert_session = $this
->assertSession();
$assert_session
->pageTextContains('Form constructions: 1');
$edit = [
'title' => 'new',
'value' => 'value_is_set',
];
$this
->drupalPostForm(NULL, $edit, 'Continue submit');
$assert_session
->pageTextContains('Form constructions: 2');
$assert_session
->pageTextContains('Form constructions: 3');
$this
->drupalPostForm(NULL, [
'title' => 'changed',
], 'Reset');
$assert_session
->fieldValueEquals('title', 'new');
$assert_session
->pageTextContains('Form constructions: 4');
$this
->drupalPostForm(NULL, $edit, 'Save');
$assert_session
->pageTextContains('Form constructions: 4');
$assert_session
->pageTextContains('Title: new', 'The form storage has stored the values.');
}
public function testFormCached() {
$this
->drupalGet('form_test/form-storage', [
'query' => [
'cache' => 1,
],
]);
$this
->assertSession()
->pageTextContains('Form constructions: 1');
$edit = [
'title' => 'new',
'value' => 'value_is_set',
];
$this
->drupalPostForm(NULL, $edit, 'Continue submit');
$this
->assertSession()
->pageTextContains('Form constructions: 2');
$this
->assertSession()
->pageTextContains('Form constructions: 3');
$this
->drupalPostForm(NULL, [
'title' => 'changed',
], 'Reset');
$this
->assertSession()
->fieldValueEquals('title', 'new');
$this
->assertSession()
->pageTextContains('Form constructions: 4');
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertSession()
->pageTextContains('Form constructions: 4');
$this
->assertSession()
->pageTextContains('Title: new', 'The form storage has stored the values.');
}
public function testValidation() {
$this
->drupalPostForm('form_test/form-storage', [
'title' => '',
'value' => 'value_is_set',
], 'Continue submit');
$this
->assertPattern('/value_is_set/');
}
public function testCachedFormStorageValidation() {
$this
->drupalGet('form_test/form-storage', [
'query' => [
'cache' => 1,
],
]);
$edit = [
'title' => 'foo',
];
$this
->drupalPostForm(NULL, $edit, 'Continue submit');
$edit = [
'title' => '',
'value' => 'change_title',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->drupalPostForm(NULL, [
'title' => 'foo',
'value' => 'bar',
], 'Save');
$this
->assertSession()
->pageTextContains("The thing has been changed.", 'The altered form storage value was updated in cache and taken over.');
}
public function testImmutableForm() {
$this
->drupalGet('form_test/form-storage', [
'query' => [
'cache' => 1,
'immutable' => 1,
],
]);
$buildIdFields = $this
->xpath('//input[@name="form_build_id"]');
$this
->assertCount(1, $buildIdFields, 'One form build id field on the page');
$buildId = $buildIdFields[0]
->getValue();
$edit = [
'title' => '',
];
$this
->drupalPostForm(NULL, $edit, 'Continue submit');
$this
->assertSession()
->hiddenFieldValueNotEquals('form_build_id', $buildId);
$buildIdFields = $this
->xpath('//input[@name="form_build_id"]');
$this
->assertCount(1, $buildIdFields, 'One form build id field on the page');
$buildId = (string) $buildIdFields[0]
->getValue();
$edit = [
'title' => '',
];
$this
->drupalPostForm(NULL, $edit, 'Continue submit');
$this
->assertSession()
->hiddenFieldValueEquals('form_build_id', $buildId);
}
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();
$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');
$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');
$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');
}
}