Commit b28f5b81 authored by catch's avatar catch
Browse files

Issue #2071145 by dawehner: Regression: Allow to change the commands of an ajax response.

parent 1b5f414a
......@@ -26,7 +26,7 @@ class AjaxResponse extends JsonResponse {
/**
* Add an AJAX command to the response.
*
* @param object $command
* @param \Drupal\Core\Ajax\CommandInterface $command
* An AJAX command object implementing CommandInterface.
* @param boolean $prepend
* A boolean which determines whether the new command should be executed
......@@ -35,7 +35,7 @@ class AjaxResponse extends JsonResponse {
* @return AjaxResponse
* The current AjaxResponse.
*/
public function addCommand($command, $prepend = FALSE) {
public function addCommand(CommandInterface $command, $prepend = FALSE) {
if ($prepend) {
array_unshift($this->commands, $command->render());
}
......@@ -46,6 +46,16 @@ public function addCommand($command, $prepend = FALSE) {
return $this;
}
/**
* Gets all AJAX commands.
*
* @return \Drupal\Core\Ajax\CommandInterface[]
* Returns all previously added AJAX commands.
*/
public function &getCommands() {
return $this->commands;
}
/**
* {@inheritdoc}
*
......
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Ajax\AjaxResponseTest.
*/
namespace Drupal\Tests\Core\Ajax;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Tests\UnitTestCase;
/**
* Tests the AJAX response object.
*
* @see \Drupal\Core\Ajax\AjaxResponse
*/
class AjaxResponseTest extends UnitTestCase {
/**
* The tested ajax response object.
*
* @var \Drupal\Core\Ajax\AjaxResponse
*/
protected $ajaxResponse;
public static function getInfo() {
return array(
'name' => 'Ajax Response Object',
'description' => 'Tests the AJAX response object.',
'group' => 'Ajax',
);
}
protected function setUp() {
$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->getMock('Drupal\Core\Ajax\CommandInterface');
$command_one->expects($this->once())
->method('render')
->will($this->returnValue(array('command' => 'one')));
$command_two = $this->getMock('Drupal\Core\Ajax\CommandInterface');
$command_two->expects($this->once())
->method('render')
->will($this->returnValue(array('command' => 'two')));
$command_three = $this->getMock('Drupal\Core\Ajax\CommandInterface');
$command_three->expects($this->once())
->method('render')
->will($this->returnValue(array('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($commands[1], array('command' => 'one'));
$this->assertSame($commands[2], array('command' => 'two'));
$this->assertSame($commands[0], array('command' => 'three'));
// 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($commands[1], array('command' => 'one'));
$this->assertFalse(isset($commands[2]));
$this->assertSame($commands[0], array('command' => 'three', 'class' => 'test-class'));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment