Chris@0: kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); Chris@0: $this->banManager = $this->getMock('Drupal\ban\BanIpManagerInterface'); Chris@0: $this->banMiddleware = new BanMiddleware($this->kernel, $this->banManager); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests a banned IP. Chris@0: */ Chris@0: public function testBannedIp() { Chris@0: $banned_ip = '17.0.0.2'; Chris@0: $this->banManager->expects($this->once()) Chris@0: ->method('isBanned') Chris@0: ->with($banned_ip) Chris@0: ->willReturn(TRUE); Chris@0: Chris@0: $this->kernel->expects($this->never()) Chris@0: ->method('handle'); Chris@0: Chris@0: $request = Request::create('/test-path'); Chris@0: $request->server->set('REMOTE_ADDR', $banned_ip); Chris@0: $response = $this->banMiddleware->handle($request); Chris@0: Chris@0: $this->assertEquals(403, $response->getStatusCode()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests an unbanned IP. Chris@0: */ Chris@0: public function testUnbannedIp() { Chris@0: $unbanned_ip = '18.0.0.2'; Chris@0: $this->banManager->expects($this->once()) Chris@0: ->method('isBanned') Chris@0: ->with($unbanned_ip) Chris@0: ->willReturn(FALSE); Chris@0: Chris@0: $request = Request::create('/test-path'); Chris@0: $request->server->set('REMOTE_ADDR', $unbanned_ip); Chris@0: $expected_response = new Response(200); Chris@0: $this->kernel->expects($this->once()) Chris@0: ->method('handle') Chris@0: ->with($request, HttpKernelInterface::MASTER_REQUEST, TRUE) Chris@0: ->willReturn($expected_response); Chris@0: Chris@0: $response = $this->banMiddleware->handle($request); Chris@0: Chris@0: $this->assertSame($expected_response, $response); Chris@0: } Chris@0: Chris@0: }