Skip to content

Commit

Permalink
Issue #2922804 by TR, jonathan1055: BanIP action causes PHP Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
rohaly authored and fago committed Dec 14, 2017
1 parent afff2bd commit 42ae4eb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
20 changes: 10 additions & 10 deletions src/Plugin/RulesAction/BanIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Drupal\ban\BanIpManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\rules\Core\RulesActionBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand Down Expand Up @@ -41,11 +41,11 @@ class BanIP extends RulesActionBase implements ContainerFactoryPluginInterface {
protected $banManager;

/**
* The corresponding request.
* The corresponding request stack.
*
* @var \Symfony\Component\HttpFoundation\Request
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $request;
protected $requestStack;

/**
* {@inheritdoc}
Expand All @@ -56,7 +56,7 @@ public static function create(ContainerInterface $container, array $configuratio
$plugin_id,
$plugin_definition,
$container->get('ban.ip_manager'),
$container->get('request')
$container->get('request_stack')
);
}

Expand All @@ -71,13 +71,13 @@ public static function create(ContainerInterface $container, array $configuratio
* The plugin implementation definition.
* @param \Drupal\ban\BanIpManagerInterface $ban_manager
* The ban manager.
* @param \Symfony\Component\HttpFoundation\Request $request
* The corresponding request.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The corresponding request stack.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, BanIpManagerInterface $ban_manager, Request $request) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, BanIpManagerInterface $ban_manager, RequestStack $request_stack) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->banManager = $ban_manager;
$this->request = $request;
$this->requestStack = $request_stack;
}

/**
Expand All @@ -88,7 +88,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
*/
protected function doExecute($ip = NULL) {
if (!isset($ip)) {
$ip = $this->request->getClientIp();
$ip = $this->requestStack->getCurrentRequest()->getClientIp();
}

$this->banManager->banIp($ip);
Expand Down
15 changes: 13 additions & 2 deletions tests/src/Unit/Integration/Action/BanIPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\ban\BanIpManagerInterface;
use Drupal\Tests\rules\Unit\Integration\RulesIntegrationTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* @coversDefaultClass \Drupal\rules\Plugin\RulesAction\BanIP
Expand All @@ -29,6 +30,11 @@ class BanIPTest extends RulesIntegrationTestBase {
*/
protected $request;

/**
* @var \Symfony\Component\HttpFoundation\RequestStack|\Prophecy\Prophecy\ProphecyInterface
*/
protected $requestStack;

/**
* {@inheritdoc}
*/
Expand All @@ -39,8 +45,14 @@ public function setUp() {
$this->banManager = $this->prophesize(BanIpManagerInterface::class);
$this->container->set('ban.ip_manager', $this->banManager->reveal());

// Mock a request.
$this->request = $this->prophesize(Request::class);
$this->container->set('request', $this->request->reveal());

// Mock the request_stack service, make it return our mocked request,
// and register it in the container.
$this->requestStack = $this->prophesize(RequestStack::class);
$this->requestStack->getCurrentRequest()->willReturn($this->request->reveal());
$this->container->set('request_stack', $this->requestStack->reveal());

$this->action = $this->actionManager->createInstance('rules_ban_ip');
}
Expand Down Expand Up @@ -116,7 +128,6 @@ public function testActionExecutionWithoutContextIp() {
$this->banManager->banIp($ip)->shouldBeCalledTimes(1);

$this->action->execute();

}

}

0 comments on commit 42ae4eb

Please sign in to comment.