You are here

public function FormAjaxResponseBuilderTest::testBuildResponseWithUpdateCommand in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Form/FormAjaxResponseBuilderTest.php \Drupal\Tests\Core\Form\FormAjaxResponseBuilderTest::testBuildResponseWithUpdateCommand()
  2. 9 core/tests/Drupal/Tests/Core/Form/FormAjaxResponseBuilderTest.php \Drupal\Tests\Core\Form\FormAjaxResponseBuilderTest::testBuildResponseWithUpdateCommand()

@covers ::buildResponse

File

core/tests/Drupal/Tests/Core/Form/FormAjaxResponseBuilderTest.php, line 171

Class

FormAjaxResponseBuilderTest
@coversDefaultClass \Drupal\Core\Form\FormAjaxResponseBuilder @group Form

Namespace

Drupal\Tests\Core\Form

Code

public function testBuildResponseWithUpdateCommand() {
  $triggering_element = [
    '#ajax' => [
      'callback' => function (array $form, FormStateInterface $form_state) {
        return new AjaxResponse([]);
      },
    ],
  ];
  $request = new Request();
  $form = [
    '#build_id' => 'the_build_id',
    '#build_id_old' => 'a_new_build_id',
    'test' => [
      '#type' => 'textfield',
    ],
  ];
  $form_state = new FormState();
  $form_state
    ->setTriggeringElement($triggering_element);
  $commands = [
    new AlertCommand('alert!'),
  ];
  $commands_expected = [];
  $commands_expected[] = [
    'command' => 'update_build_id',
    'old' => 'a_new_build_id',
    'new' => 'the_build_id',
  ];
  $commands_expected[] = [
    'command' => 'alert',
    'text' => 'alert!',
  ];
  $this->renderer
    ->expects($this
    ->never())
    ->method('renderResponse');
  $result = $this->formAjaxResponseBuilder
    ->buildResponse($request, $form, $form_state, $commands);
  $this
    ->assertInstanceOf('\\Drupal\\Core\\Ajax\\AjaxResponse', $result);
  $this
    ->assertSame($commands_expected, $result
    ->getCommands());
}