FormAjaxResponseBuilderTest.php 5.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

namespace Drupal\Tests\Core\Form;

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Form\FormAjaxResponseBuilder;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpKernel\Exception\HttpException;
13 14 15 16 17 18 19 20

/**
 * @coversDefaultClass \Drupal\Core\Form\FormAjaxResponseBuilder
 * @group Form
 */
class FormAjaxResponseBuilderTest extends UnitTestCase {

  /**
21
   * @var \Drupal\Core\Render\MainContent\MainContentRendererInterface|\PHPUnit\Framework\MockObject\MockObject
22 23 24 25
   */
  protected $renderer;

  /**
26
   * @var \Drupal\Core\Routing\RouteMatchInterface|\PHPUnit\Framework\MockObject\MockObject
27 28 29 30 31 32 33 34 35 36 37
   */
  protected $routeMatch;

  /**
   * @var \Drupal\Core\Form\FormAjaxResponseBuilder
   */
  protected $formAjaxResponseBuilder;

  /**
   * {@inheritdoc}
   */
38
  protected function setUp(): void {
39
    parent::setUp();
40 41
    $this->renderer = $this->createMock('Drupal\Core\Render\MainContent\MainContentRendererInterface');
    $this->routeMatch = $this->createMock('Drupal\Core\Routing\RouteMatchInterface');
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    $this->formAjaxResponseBuilder = new FormAjaxResponseBuilder($this->renderer, $this->routeMatch);
  }

  /**
   * @covers ::buildResponse
   */
  public function testBuildResponseNoTriggeringElement() {
    $this->renderer->expects($this->never())
      ->method('renderResponse');

    $request = new Request();
    $form = [];
    $form_state = new FormState();
    $commands = [];

57
    $this->expectException(HttpException::class);
58
    $this->formAjaxResponseBuilder->buildResponse($request, $form, $form_state, $commands);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  }

  /**
   * @covers ::buildResponse
   */
  public function testBuildResponseNoCallable() {
    $this->renderer->expects($this->never())
      ->method('renderResponse');

    $request = new Request();
    $form = [];
    $form_state = new FormState();
    $triggering_element = [];
    $form_state->setTriggeringElement($triggering_element);
    $commands = [];

75
    $this->expectException(HttpException::class);
76
    $this->formAjaxResponseBuilder->buildResponse($request, $form, $form_state, $commands);
77 78 79 80 81 82 83 84 85 86
  }

  /**
   * @covers ::buildResponse
   */
  public function testBuildResponseRenderArray() {
    $triggering_element = [
      '#ajax' => [
        'callback' => function (array $form, FormStateInterface $form_state) {
          return $form['test'];
87
        },
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
      ],
    ];
    $request = new Request();
    $form = [
      'test' => [
        '#type' => 'textfield',
      ],
    ];
    $form_state = new FormState();
    $form_state->setTriggeringElement($triggering_element);
    $commands = [];

    $this->renderer->expects($this->once())
      ->method('renderResponse')
      ->with($form['test'], $request, $this->routeMatch)
      ->willReturn(new AjaxResponse([]));

    $result = $this->formAjaxResponseBuilder->buildResponse($request, $form, $form_state, $commands);
    $this->assertInstanceOf('\Drupal\Core\Ajax\AjaxResponse', $result);
    $this->assertSame($commands, $result->getCommands());
  }

  /**
   * @covers ::buildResponse
   */
  public function testBuildResponseResponse() {
    $triggering_element = [
      '#ajax' => [
        'callback' => function (array $form, FormStateInterface $form_state) {
          return new AjaxResponse([]);
118
        },
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
      ],
    ];
    $request = new Request();
    $form = [];
    $form_state = new FormState();
    $form_state->setTriggeringElement($triggering_element);
    $commands = [];

    $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, $result->getCommands());
  }

  /**
   * @covers ::buildResponse
   */
  public function testBuildResponseWithCommands() {
    $triggering_element = [
      '#ajax' => [
        'callback' => function (array $form, FormStateInterface $form_state) {
          return new AjaxResponse([]);
143
        },
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
      ],
    ];
    $request = new Request();
    $form = [
      'test' => [
        '#type' => 'textfield',
      ],
    ];
    $form_state = new FormState();
    $form_state->setTriggeringElement($triggering_element);
    $commands = [
      new AlertCommand('alert!'),
    ];
    $commands_expected = [];
    $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());
  }

  /**
   * @covers ::buildResponse
   */
  public function testBuildResponseWithUpdateCommand() {
    $triggering_element = [
      '#ajax' => [
        'callback' => function (array $form, FormStateInterface $form_state) {
          return new AjaxResponse([]);
176
        },
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
      ],
    ];
    $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());
  }

}