getFormBuildId example

protected function getFormBuildId() {
    // Ensure the hidden 'form_build_id' field is unique.     $this->assertSession()->elementsCount('xpath', '//input[@name="form_build_id"]', 1);
    return $this->assertSession()->hiddenFieldExists('form_build_id')->getValue();
  }

  /** * Create a simple form, then submit the form via AJAX to change to it. */
  public function testSimpleAJAXFormValue() {
    $this->drupalGet('ajax_forms_test_get_form');
    $build_id_initial = $this->getFormBuildId();

    // Changing the value of a select input element, triggers an AJAX     // request/response. The callback on the form responds with three AJAX     // commands:     // - UpdateBuildIdCommand     // - HtmlCommand     // - DataCommand     $session = $this->getSession();
    $session->getPage()->selectFieldOption('select', 'green');

    // Wait for the DOM to update. The HtmlCommand will update
    $this->assertSession()->elementsCount('xpath', '//input[@name="form_build_id"]', 1);
    return (string) $this->assertSession()->hiddenFieldExists('form_build_id')->getAttribute('value');
  }

  /** * Build-id is regenerated when validating cached form. */
  public function testValidateFormStorageOnCachedPage() {
    $this->drupalGet('form-test/form-storage-page-cache');
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertSession()->pageTextContains('No old build id');
    $build_id_initial = $this->getFormBuildId();

    // Trigger validation error by submitting an empty title.     $edit = ['title' => ''];
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains('No old build id');
    $build_id_first_validation = $this->getFormBuildId();
    $this->assertNotEquals($build_id_initial$build_id_first_validation, 'Build id changes when form validation fails');

    // Trigger validation error by again submitting an empty title.     $edit = ['title' => ''];
    $this->submitForm($edit, 'Save');
    
Home | Imprint | This part of the site doesn't use cookies.