You are here

protected function AjaxFormTestBase::postAjaxForm in Purge 8.3

Submits a ajax form through http_kernel.

Parameters

array $edit: Field data in an associative array. Changes the current input fields (where possible) to the values indicated. A checkbox can be set to TRUE to be checked and should be set to FALSE to be unchecked.

string $submit: Value of the submit button whose click is to be emulated. For example, 'Save'. The processing of the request depends on this value. For example, a form may have one button with the value 'Save' and another button with the value 'Delete', and execute different code depending on which one is clicked.

array $route_parameters: (optional) An associative array of route parameter names and values.

Return value

\Drupal\Core\Ajax\AjaxResponse The undecoded AjaxResponse object returned by the http_kernel.

See also

\Drupal\Tests\UiHelperTrait::submitForm()

26 calls to AjaxFormTestBase::postAjaxForm()
PluginConfigFormTestBase::testCancelSubmit in modules/purge_ui/tests/src/Functional/Form/Config/PluginConfigFormTestBase.php
Tests cancel button form submit.
ProcessorAddFormTest::testAddSubmit in modules/purge_ui/tests/src/Functional/Form/ProcessorAddFormTest.php
Tests form submission results in the redirect command.
ProcessorAddFormTest::testCancelSubmit in modules/purge_ui/tests/src/Functional/Form/ProcessorAddFormTest.php
Tests that the cancel button closes the dialog.
ProcessorDeleteFormTest::testDeleteProcessor in modules/purge_ui/tests/src/Functional/Form/ProcessorDeleteFormTest.php
Tests that 'Yes, delete..', deletes the processor and closes the window.
ProcessorDeleteFormTest::testNoSubmit in modules/purge_ui/tests/src/Functional/Form/ProcessorDeleteFormTest.php
Tests "No" cancel button form submit.

... See full list

File

modules/purge_ui/tests/src/Functional/Form/AjaxFormTestBase.php, line 171

Class

AjaxFormTestBase
Testbase for Ajax-based purge_ui forms.

Namespace

Drupal\Tests\purge_ui\Functional\Form

Code

protected function postAjaxForm(array $edit, $submit, array $route_parameters = []) : AjaxResponse {
  $form_builder = $this
    ->formBuilder();
  $form_state = $this
    ->getFormStateInstance();
  $form = $this
    ->getFormInstance();

  // Get a path appended with ?ajax_form=1&_wrapper_format=drupal_ajax.
  $this
    ->propagateRouteParameters($route_parameters);
  $route_parameters[FormBuilderInterface::AJAX_FORM_REQUEST] = TRUE;
  $route_parameters[MainContentViewSubscriber::WRAPPER_FORMAT] = 'drupal_ajax';
  $path = $this
    ->getPath($route_parameters);

  // Instantiate a request which looks as if it is browser-initiated.
  $req = Request::create($path, 'POST');
  $req->headers
    ->set('X-Requested-With', 'XMLHttpRequest');
  $req->headers
    ->set('Accept', 'application/vnd.api+json');
  $edit['form_id'] = $form_builder
    ->getFormId($form, $form_state);
  $edit['op'] = $submit;
  $req->request
    ->add($edit);

  // Fetch the response from http_kernel and assert its sane.
  $response = $this->container
    ->get('http_kernel')
    ->handle($req, HttpKernelInterface::SUB_REQUEST);
  $this
    ->assertSame(200, $response
    ->getStatusCode(), (string) $response
    ->getContent());
  $this
    ->assertInstanceOf(AjaxResponse::class, $response);
  return $response;
}