3 namespace Drupal\Tests\Core\Ajax;
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\EventSubscriber\AjaxResponseSubscriber;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
10 use Symfony\Component\HttpKernel\HttpKernelInterface;
13 * @coversDefaultClass \Drupal\Core\Ajax\AjaxResponse
16 class AjaxResponseTest extends UnitTestCase {
19 * The tested ajax response object.
21 * @var \Drupal\Core\Ajax\AjaxResponse
23 protected $ajaxResponse;
25 protected function setUp() {
26 $this->ajaxResponse = new AjaxResponse();
30 * Tests the add and getCommands method.
32 * @see \Drupal\Core\Ajax\AjaxResponse::addCommand()
33 * @see \Drupal\Core\Ajax\AjaxResponse::getCommands()
35 public function testCommands() {
36 $command_one = $this->getMock('Drupal\Core\Ajax\CommandInterface');
37 $command_one->expects($this->once())
39 ->will($this->returnValue(['command' => 'one']));
40 $command_two = $this->getMock('Drupal\Core\Ajax\CommandInterface');
41 $command_two->expects($this->once())
43 ->will($this->returnValue(['command' => 'two']));
44 $command_three = $this->getMock('Drupal\Core\Ajax\CommandInterface');
45 $command_three->expects($this->once())
47 ->will($this->returnValue(['command' => 'three']));
49 $this->ajaxResponse->addCommand($command_one);
50 $this->ajaxResponse->addCommand($command_two);
51 $this->ajaxResponse->addCommand($command_three, TRUE);
53 // Ensure that the added commands are in the right order.
54 $commands =& $this->ajaxResponse->getCommands();
55 $this->assertSame(['command' => 'one'], $commands[1]);
56 $this->assertSame(['command' => 'two'], $commands[2]);
57 $this->assertSame(['command' => 'three'], $commands[0]);
59 // Remove one and change one element from commands and ensure the reference
60 // worked as expected.
62 $commands[0]['class'] = 'test-class';
64 $commands = $this->ajaxResponse->getCommands();
65 $this->assertSame(['command' => 'one'], $commands[1]);
66 $this->assertFalse(isset($commands[2]));
67 $this->assertSame(['command' => 'three', 'class' => 'test-class'], $commands[0]);
71 * Tests the support for IE specific headers in file uploads.
73 * @cover ::prepareResponse
75 public function testPrepareResponseForIeFormRequestsWithFileUpload() {
76 $request = Request::create('/example', 'POST');
77 $request->headers->set('Accept', 'text/html');
78 $response = new AjaxResponse([]);
79 $response->headers->set('Content-Type', 'application/json; charset=utf-8');
81 $ajax_response_attachments_processor = $this->getMock('\Drupal\Core\Render\AttachmentsResponseProcessorInterface');
82 $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor);
83 $event = new FilterResponseEvent(
84 $this->getMock('\Symfony\Component\HttpKernel\HttpKernelInterface'),
86 HttpKernelInterface::MASTER_REQUEST,
89 $subscriber->onResponse($event);
90 $this->assertEquals('text/html; charset=utf-8', $response->headers->get('Content-Type'));
91 $this->assertEquals($response->getContent(), '<textarea>[]</textarea>');