Skip to content
Snippets Groups Projects
Commit 851df445 authored by catch's avatar catch
Browse files

Issue #3343670 by tim-diels, shalini_jha, jecet4, mschudders, graber, enaznin,...

Issue #3343670 by tim-diels, shalini_jha, jecet4, mschudders, graber, enaznin, smustgrave: Allow to merge AjaxCommands inside of AjaxResponse
parent 11867a16
No related branches found
No related tags found
4 merge requests!12628#3524738 backport without deprecation,!12477#3532243: JSON support status during updates,!5423Draft: Resolve #3329907 "Test2",!213Issue #2906496: Give Media a menu item under Content
Pipeline #526542 passed with warnings
Pipeline: drupal

#526550

    Pipeline: drupal

    #526544

      Pipeline: drupal

      #526543

        ......@@ -55,6 +55,23 @@ public function addCommand(CommandInterface $command, $prepend = FALSE) {
        return $this;
        }
        /**
        * Merges other ajax response with this one.
        *
        * Adds commands and merges attachments from the other ajax response.
        *
        * @param \Drupal\Core\Ajax\AjaxResponse $other
        * An AJAX response to merge.
        *
        * @return $this
        * Returns this after merging.
        */
        public function mergeWith(AjaxResponse $other): AjaxResponse {
        $this->commands = array_merge($this->getCommands(), $other->getCommands());
        $this->attachments = BubbleableMetadata::mergeAttachments($this->getAttachments(), $other->getAttachments());
        return $this;
        }
        /**
        * Gets all AJAX commands.
        *
        ......
        ......@@ -5,6 +5,7 @@
        namespace Drupal\Tests\Core\Ajax;
        use Drupal\Core\Ajax\AjaxResponse;
        use Drupal\Core\Ajax\CommandInterface;
        use Drupal\Core\EventSubscriber\AjaxResponseSubscriber;
        use Drupal\Tests\UnitTestCase;
        use Symfony\Component\HttpFoundation\Request;
        ......@@ -98,4 +99,95 @@ public function testPrepareResponseForIeFormRequestsWithFileUpload(): void {
        $this->assertEquals('<textarea>[]</textarea>', $response->getContent());
        }
        /**
        * Tests the mergeWith() method.
        *
        * @see \Drupal\Core\Ajax\AjaxResponse::mergeWith()
        *
        * @throws \PHPUnit\Framework\MockObject\Exception
        */
        public function testMergeWithOtherAjaxResponse(): void {
        $response = new AjaxResponse([]);
        $command_one = $this->createCommandMock('one');
        $command_two = $this->createCommandMockWithSettingsAndLibrariesAttachments(
        'Drupal\Core\Ajax\HtmlCommand', [
        'setting1' => 'value1',
        'setting2' => 'value2',
        ], ['jquery', 'drupal'], 'two');
        $command_three = $this->createCommandMockWithSettingsAndLibrariesAttachments(
        'Drupal\Core\Ajax\InsertCommand', [
        'setting1' => 'overridden',
        'setting3' => 'value3',
        ], ['jquery', 'ajax'], 'three');
        $response->addCommand($command_one);
        $response->addCommand($command_two);
        $response2 = new AjaxResponse([]);
        $response2->addCommand($command_three);
        $response->mergeWith($response2);
        self::assertEquals([
        'library' => ['jquery', 'drupal', 'jquery', 'ajax'],
        'drupalSettings' => [
        'setting1' => 'overridden',
        'setting2' => 'value2',
        'setting3' => 'value3',
        ],
        ], $response->getAttachments());
        self::assertEquals([['command' => 'one'], ['command' => 'two'], ['command' => 'three']], $response->getCommands());
        }
        /**
        * Creates a mock of a provided subclass of CommandInterface.
        *
        * Adds given settings and libraries to assets mock
        * that is attached to the command mock.
        *
        * @param string $command_class_name
        * The command class name to create the mock for.
        * @param array|null $settings
        * The settings to attach.
        * @param array|null $libraries
        * The libraries to attach.
        * @param string $command_name
        * The command name to pass to the mock.
        */
        private function createCommandMockWithSettingsAndLibrariesAttachments(
        string $command_class_name,
        array|null $settings,
        array|null $libraries,
        string $command_name,
        ): CommandInterface {
        $command = $this->createMock($command_class_name);
        $command->expects($this->once())
        ->method('render')
        ->willReturn(['command' => $command_name]);
        $assets = $this->createMock('Drupal\Core\Asset\AttachedAssetsInterface');
        $assets->expects($this->once())->method('getLibraries')->willReturn($libraries);
        $assets->expects($this->once())->method('getSettings')->willReturn($settings);
        $command->expects($this->once())->method('getAttachedAssets')->willReturn($assets);
        return $command;
        }
        /**
        * Creates a mock of the Drupal\Core\Ajax\CommandInterface.
        *
        * @param string $command_name
        * The command name to pass to the mock.
        */
        private function createCommandMock(string $command_name): CommandInterface {
        $command = $this->createMock('Drupal\Core\Ajax\CommandInterface');
        $command->expects($this->once())
        ->method('render')
        ->willReturn(['command' => $command_name]);
        return $command;
        }
        }
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Please register or to comment