From 42ae4eb3b809533d9ed96aa9aaeb4128e4b32bde Mon Sep 17 00:00:00 2001 From: tr Date: Thu, 14 Dec 2017 17:06:41 +0100 Subject: [PATCH] Issue #2922804 by TR, jonathan1055: BanIP action causes PHP Exception --- src/Plugin/RulesAction/BanIP.php | 20 +++++++++---------- .../src/Unit/Integration/Action/BanIPTest.php | 15 ++++++++++++-- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Plugin/RulesAction/BanIP.php b/src/Plugin/RulesAction/BanIP.php index bf54cc40..57f9ff47 100644 --- a/src/Plugin/RulesAction/BanIP.php +++ b/src/Plugin/RulesAction/BanIP.php @@ -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; /** @@ -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} @@ -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') ); } @@ -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; } /** @@ -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); diff --git a/tests/src/Unit/Integration/Action/BanIPTest.php b/tests/src/Unit/Integration/Action/BanIPTest.php index 4a7dd689..cd2098e5 100644 --- a/tests/src/Unit/Integration/Action/BanIPTest.php +++ b/tests/src/Unit/Integration/Action/BanIPTest.php @@ -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 @@ -29,6 +30,11 @@ class BanIPTest extends RulesIntegrationTestBase { */ protected $request; + /** + * @var \Symfony\Component\HttpFoundation\RequestStack|\Prophecy\Prophecy\ProphecyInterface + */ + protected $requestStack; + /** * {@inheritdoc} */ @@ -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'); } @@ -116,7 +128,6 @@ public function testActionExecutionWithoutContextIp() { $this->banManager->banIp($ip)->shouldBeCalledTimes(1); $this->action->execute(); - } }