sessionless-1.x-dev/tests/src/Functional/SessionlessFormsFormTest.php
tests/src/Functional/SessionlessFormsFormTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\sessionless\Functional;
use Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface;
use Drupal\Tests\BrowserTestBase;
/**
* @group sessionless
*/
final class SessionlessFormsFormTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'sessionless_forms_test',
];
private KeyValueExpirableFactoryInterface $keyValueExpirableFactory;
protected function setUp(): void {
parent::setUp();
$this->keyValueExpirableFactory = \Drupal::service('keyvalue.expirable');
}
public function testForm(): void {
$this->drupalGet('sessionless_forms_test_form');
$this->submitForm(['text' => 'JohnDoe'], 'Submit');
$this->assertSession()->statusCodeEquals(200);
$this->submitForm([], 'Submit');
$this->assertSession()->statusCodeEquals(200);
$this->assertSame([NULL, NULL], $this->getCoreCachedFormAndFormState());
// Verify form state persists via SessionlessForm.
$this->assertSession()->pageTextContainsOnce('TestValue:AliceInWonderland');
}
public function getCoreCachedFormAndFormState(): array {
$formBuildIdElement = $this->assertSession()
->hiddenFieldExists('form_build_id');
$formBuildId = $formBuildIdElement->getAttribute('value');
$cachedForm = $this->keyValueExpirableFactory
->get('form')->get($formBuildId);
$cachedFormState = $this->keyValueExpirableFactory
->get('form_state')->get($formBuildId);
return [$cachedForm, $cachedFormState];
}
}
