annotate core/modules/ban/tests/src/Unit/BanMiddlewareTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\ban\Unit;
Chris@0 4
Chris@0 5 use Drupal\ban\BanMiddleware;
Chris@0 6 use Drupal\Tests\UnitTestCase;
Chris@0 7 use Symfony\Component\HttpFoundation\Request;
Chris@0 8 use Symfony\Component\HttpFoundation\Response;
Chris@0 9 use Symfony\Component\HttpKernel\HttpKernelInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * @coversDefaultClass \Drupal\ban\BanMiddleware
Chris@0 13 * @group ban
Chris@0 14 */
Chris@0 15 class BanMiddlewareTest extends UnitTestCase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * The mocked wrapped kernel.
Chris@0 19 *
Chris@0 20 * @var \Symfony\Component\HttpKernel\HttpKernelInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 21 */
Chris@0 22 protected $kernel;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * The mocked ban IP manager.
Chris@0 26 *
Chris@0 27 * @var \Drupal\ban\BanIpManagerInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 28 */
Chris@0 29 protected $banManager;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * The tested ban middleware.
Chris@0 33 *
Chris@0 34 * @var \Drupal\ban\BanMiddleware
Chris@0 35 */
Chris@0 36 protected $banMiddleware;
Chris@0 37
Chris@0 38 /**
Chris@0 39 * {@inheritdoc}
Chris@0 40 */
Chris@0 41 protected function setUp() {
Chris@0 42 parent::setUp();
Chris@0 43
Chris@0 44 $this->kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
Chris@0 45 $this->banManager = $this->getMock('Drupal\ban\BanIpManagerInterface');
Chris@0 46 $this->banMiddleware = new BanMiddleware($this->kernel, $this->banManager);
Chris@0 47 }
Chris@0 48
Chris@0 49 /**
Chris@0 50 * Tests a banned IP.
Chris@0 51 */
Chris@0 52 public function testBannedIp() {
Chris@0 53 $banned_ip = '17.0.0.2';
Chris@0 54 $this->banManager->expects($this->once())
Chris@0 55 ->method('isBanned')
Chris@0 56 ->with($banned_ip)
Chris@0 57 ->willReturn(TRUE);
Chris@0 58
Chris@0 59 $this->kernel->expects($this->never())
Chris@0 60 ->method('handle');
Chris@0 61
Chris@0 62 $request = Request::create('/test-path');
Chris@0 63 $request->server->set('REMOTE_ADDR', $banned_ip);
Chris@0 64 $response = $this->banMiddleware->handle($request);
Chris@0 65
Chris@0 66 $this->assertEquals(403, $response->getStatusCode());
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Tests an unbanned IP.
Chris@0 71 */
Chris@0 72 public function testUnbannedIp() {
Chris@0 73 $unbanned_ip = '18.0.0.2';
Chris@0 74 $this->banManager->expects($this->once())
Chris@0 75 ->method('isBanned')
Chris@0 76 ->with($unbanned_ip)
Chris@0 77 ->willReturn(FALSE);
Chris@0 78
Chris@0 79 $request = Request::create('/test-path');
Chris@0 80 $request->server->set('REMOTE_ADDR', $unbanned_ip);
Chris@0 81 $expected_response = new Response(200);
Chris@0 82 $this->kernel->expects($this->once())
Chris@0 83 ->method('handle')
Chris@0 84 ->with($request, HttpKernelInterface::MASTER_REQUEST, TRUE)
Chris@0 85 ->willReturn($expected_response);
Chris@0 86
Chris@0 87 $response = $this->banMiddleware->handle($request);
Chris@0 88
Chris@0 89 $this->assertSame($expected_response, $response);
Chris@0 90 }
Chris@0 91
Chris@0 92 }