class BanMiddlewareTest

Same name in other branches
  1. 9 core/modules/ban/tests/src/Unit/BanMiddlewareTest.php \Drupal\Tests\ban\Unit\BanMiddlewareTest
  2. 8.9.x core/modules/ban/tests/src/Unit/BanMiddlewareTest.php \Drupal\Tests\ban\Unit\BanMiddlewareTest
  3. 10 core/modules/ban/tests/src/Unit/BanMiddlewareTest.php \Drupal\Tests\ban\Unit\BanMiddlewareTest

@coversDefaultClass \Drupal\ban\BanMiddleware @group ban

Hierarchy

Expanded class hierarchy of BanMiddlewareTest

File

core/modules/ban/tests/src/Unit/BanMiddlewareTest.php, line 17

Namespace

Drupal\Tests\ban\Unit
View source
class BanMiddlewareTest extends UnitTestCase {
    
    /**
     * The mocked wrapped kernel.
     *
     * @var \Symfony\Component\HttpKernel\HttpKernelInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $kernel;
    
    /**
     * The mocked ban IP manager.
     *
     * @var \Drupal\ban\BanIpManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $banManager;
    
    /**
     * The tested ban middleware.
     *
     * @var \Drupal\ban\BanMiddleware
     */
    protected $banMiddleware;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->kernel = $this->createMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
        $this->banManager = $this->createMock('Drupal\\ban\\BanIpManagerInterface');
        $this->banMiddleware = new BanMiddleware($this->kernel, $this->banManager);
    }
    
    /**
     * Tests a banned IP.
     */
    public function testBannedIp() : void {
        $banned_ip = '17.0.0.2';
        $this->banManager
            ->expects($this->once())
            ->method('isBanned')
            ->with($banned_ip)
            ->willReturn(TRUE);
        $this->kernel
            ->expects($this->never())
            ->method('handle');
        $request = Request::create('/test-path');
        $request->server
            ->set('REMOTE_ADDR', $banned_ip);
        $response = $this->banMiddleware
            ->handle($request);
        $this->assertEquals(403, $response->getStatusCode());
    }
    
    /**
     * Tests an unbanned IP.
     */
    public function testUnbannedIp() : void {
        $unbanned_ip = '18.0.0.2';
        $this->banManager
            ->expects($this->once())
            ->method('isBanned')
            ->with($unbanned_ip)
            ->willReturn(FALSE);
        $request = Request::create('/test-path');
        $request->server
            ->set('REMOTE_ADDR', $unbanned_ip);
        $expected_response = new Response(status: 200);
        $this->kernel
            ->expects($this->once())
            ->method('handle')
            ->with($request, HttpKernelInterface::MAIN_REQUEST, TRUE)
            ->willReturn($expected_response);
        $response = $this->banMiddleware
            ->handle($request);
        $this->assertSame($expected_response, $response);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
BanMiddlewareTest::$banManager protected property The mocked ban IP manager.
BanMiddlewareTest::$banMiddleware protected property The tested ban middleware.
BanMiddlewareTest::$kernel protected property The mocked wrapped kernel.
BanMiddlewareTest::setUp protected function Overrides UnitTestCase::setUp
BanMiddlewareTest::testBannedIp public function Tests a banned IP.
BanMiddlewareTest::testUnbannedIp public function Tests an unbanned IP.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.