UnBanIpTest.php in Rules 8.3
File
tests/src/Unit/Integration/RulesAction/UnBanIpTest.php
View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\RulesAction;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\ban\BanIpManagerInterface;
use Drupal\Tests\rules\Unit\Integration\RulesIntegrationTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class UnBanIpTest extends RulesIntegrationTestBase {
protected $action;
protected $banManager;
protected $request;
protected $requestStack;
protected $logger;
protected function setUp() : void {
parent::setUp();
$this
->enableModule('ban');
$this->banManager = $this
->prophesize(BanIpManagerInterface::class);
$this->container
->set('ban.ip_manager', $this->banManager
->reveal());
$this->request = $this
->prophesize(Request::class);
$this->requestStack = $this
->prophesize(RequestStack::class);
$this->requestStack
->getCurrentRequest()
->willReturn($this->request
->reveal());
$this->container
->set('request_stack', $this->requestStack
->reveal());
$this->logger = $this
->prophesize(LoggerChannelInterface::class);
$logger_factory = $this
->prophesize(LoggerChannelFactoryInterface::class);
$logger_factory
->get('rules')
->willReturn($this->logger
->reveal());
$this->container
->set('logger.factory', $logger_factory
->reveal());
$this->action = $this->actionManager
->createInstance('rules_unban_ip');
}
public function testSummary() {
$this
->assertEquals('Remove the ban on an IP address', $this->action
->summary());
}
public function testActionExecutionWithContextIpv4() {
$ipv4 = '192.0.2.0';
$this->action
->setContextValue('ip', $ipv4);
$this->banManager
->unbanIp($ipv4)
->shouldBeCalledTimes(1);
$this->action
->execute();
$this->logger
->notice('Removed ban on IP address %ip', [
'%ip' => $ipv4,
])
->shouldHaveBeenCalled();
}
public function testActionExecutionWithContextIpv6() {
$ipv6 = '2002:0:0:0:0:0:c000:200';
$this->action
->setContextValue('ip', $ipv6);
$this->banManager
->unbanIp($ipv6)
->shouldBeCalledTimes(1);
$this->action
->execute();
$this->logger
->notice('Removed ban on IP address %ip', [
'%ip' => $ipv6,
])
->shouldHaveBeenCalled();
}
public function testActionExecutionWithoutContextIp() {
$ip = '192.0.2.0';
$this->request
->getClientIp()
->willReturn($ip)
->shouldBeCalledTimes(1);
$this->banManager
->unbanIp($ip)
->shouldBeCalledTimes(1);
$this->action
->execute();
$this->logger
->notice('Removed ban on IP address %ip', [
'%ip' => $ip,
])
->shouldHaveBeenCalled();
}
}