class AjaxResponseTest

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php \Drupal\Tests\Core\Ajax\AjaxResponseTest
  2. 10 core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php \Drupal\Tests\Core\Ajax\AjaxResponseTest
  3. 11.x core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php \Drupal\Tests\Core\Ajax\AjaxResponseTest

@coversDefaultClass \Drupal\Core\Ajax\AjaxResponse @group Ajax

Hierarchy

Expanded class hierarchy of AjaxResponseTest

File

core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php, line 16

Namespace

Drupal\Tests\Core\Ajax
View source
class AjaxResponseTest extends UnitTestCase {
    
    /**
     * The tested ajax response object.
     *
     * @var \Drupal\Core\Ajax\AjaxResponse
     */
    protected $ajaxResponse;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        $this->ajaxResponse = new AjaxResponse();
    }
    
    /**
     * Tests the add and getCommands method.
     *
     * @see \Drupal\Core\Ajax\AjaxResponse::addCommand()
     * @see \Drupal\Core\Ajax\AjaxResponse::getCommands()
     */
    public function testCommands() {
        $command_one = $this->createMock('Drupal\\Core\\Ajax\\CommandInterface');
        $command_one->expects($this->once())
            ->method('render')
            ->willReturn([
            'command' => 'one',
        ]);
        $command_two = $this->createMock('Drupal\\Core\\Ajax\\CommandInterface');
        $command_two->expects($this->once())
            ->method('render')
            ->willReturn([
            'command' => 'two',
        ]);
        $command_three = $this->createMock('Drupal\\Core\\Ajax\\CommandInterface');
        $command_three->expects($this->once())
            ->method('render')
            ->willReturn([
            'command' => 'three',
        ]);
        $this->ajaxResponse
            ->addCommand($command_one);
        $this->ajaxResponse
            ->addCommand($command_two);
        $this->ajaxResponse
            ->addCommand($command_three, TRUE);
        // Ensure that the added commands are in the right order.
        $commands =& $this->ajaxResponse
            ->getCommands();
        $this->assertSame([
            'command' => 'one',
        ], $commands[1]);
        $this->assertSame([
            'command' => 'two',
        ], $commands[2]);
        $this->assertSame([
            'command' => 'three',
        ], $commands[0]);
        // Remove one and change one element from commands and ensure the reference
        // worked as expected.
        unset($commands[2]);
        $commands[0]['class'] = 'test-class';
        $commands = $this->ajaxResponse
            ->getCommands();
        $this->assertSame([
            'command' => 'one',
        ], $commands[1]);
        $this->assertFalse(isset($commands[2]));
        $this->assertSame([
            'command' => 'three',
            'class' => 'test-class',
        ], $commands[0]);
    }
    
    /**
     * Tests the support for IE specific headers in file uploads.
     *
     * @cover ::prepareResponse
     */
    public function testPrepareResponseForIeFormRequestsWithFileUpload() {
        $request = Request::create('/example', 'POST');
        $request->headers
            ->set('Accept', 'text/html');
        $response = new AjaxResponse([]);
        $response->headers
            ->set('Content-Type', 'application/json; charset=utf-8');
        $ajax_response_attachments_processor = $this->createMock('\\Drupal\\Core\\Render\\AttachmentsResponseProcessorInterface');
        $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor);
        $event = new ResponseEvent($this->createMock('\\Symfony\\Component\\HttpKernel\\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST, $response);
        $subscriber->onResponse($event);
        $this->assertEquals('text/html; charset=utf-8', $response->headers
            ->get('Content-Type'));
        $this->assertEquals('<textarea>[]</textarea>', $response->getContent());
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
AjaxResponseTest::$ajaxResponse protected property The tested ajax response object.
AjaxResponseTest::setUp protected function Overrides UnitTestCase::setUp
AjaxResponseTest::testCommands public function Tests the add and getCommands method.
AjaxResponseTest::testPrepareResponseForIeFormRequestsWithFileUpload public function Tests the support for IE specific headers in file uploads.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.