1<?php 2 3namespace Drupal\Tests\Core\Ajax; 4 5use Drupal\Core\Ajax\AjaxResponse; 6use Drupal\Core\EventSubscriber\AjaxResponseSubscriber; 7use Drupal\Tests\UnitTestCase; 8use Symfony\Component\HttpFoundation\Request; 9use Symfony\Component\HttpKernel\Event\ResponseEvent; 10use Symfony\Component\HttpKernel\HttpKernelInterface; 11 12/** 13 * @coversDefaultClass \Drupal\Core\Ajax\AjaxResponse 14 * @group Ajax 15 */ 16class AjaxResponseTest extends UnitTestCase { 17 18 /** 19 * The tested ajax response object. 20 * 21 * @var \Drupal\Core\Ajax\AjaxResponse 22 */ 23 protected $ajaxResponse; 24 25 protected function setUp(): void { 26 $this->ajaxResponse = new AjaxResponse(); 27 } 28 29 /** 30 * Tests the add and getCommands method. 31 * 32 * @see \Drupal\Core\Ajax\AjaxResponse::addCommand() 33 * @see \Drupal\Core\Ajax\AjaxResponse::getCommands() 34 */ 35 public function testCommands() { 36 $command_one = $this->createMock('Drupal\Core\Ajax\CommandInterface'); 37 $command_one->expects($this->once()) 38 ->method('render') 39 ->will($this->returnValue(['command' => 'one'])); 40 $command_two = $this->createMock('Drupal\Core\Ajax\CommandInterface'); 41 $command_two->expects($this->once()) 42 ->method('render') 43 ->will($this->returnValue(['command' => 'two'])); 44 $command_three = $this->createMock('Drupal\Core\Ajax\CommandInterface'); 45 $command_three->expects($this->once()) 46 ->method('render') 47 ->will($this->returnValue(['command' => 'three'])); 48 49 $this->ajaxResponse->addCommand($command_one); 50 $this->ajaxResponse->addCommand($command_two); 51 $this->ajaxResponse->addCommand($command_three, TRUE); 52 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]); 58 59 // Remove one and change one element from commands and ensure the reference 60 // worked as expected. 61 unset($commands[2]); 62 $commands[0]['class'] = 'test-class'; 63 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]); 68 } 69 70 /** 71 * Tests the support for IE specific headers in file uploads. 72 * 73 * @cover ::prepareResponse 74 */ 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'); 80 81 $ajax_response_attachments_processor = $this->createMock('\Drupal\Core\Render\AttachmentsResponseProcessorInterface'); 82 $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor); 83 $event = new ResponseEvent( 84 $this->createMock('\Symfony\Component\HttpKernel\HttpKernelInterface'), 85 $request, 86 HttpKernelInterface::MASTER_REQUEST, 87 $response 88 ); 89 $subscriber->onResponse($event); 90 $this->assertEquals('text/html; charset=utf-8', $response->headers->get('Content-Type')); 91 $this->assertEquals('<textarea>[]</textarea>', $response->getContent()); 92 } 93 94} 95