Initial Drupal 11 with DDEV setup
This commit is contained in:
		
							
								
								
									
										14
									
								
								web/core/modules/ban/tests/src/Functional/GenericTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								web/core/modules/ban/tests/src/Functional/GenericTest.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,14 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Drupal\Tests\ban\Functional;
 | 
			
		||||
 | 
			
		||||
use Drupal\Tests\system\Functional\Module\GenericModuleTestBase;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Generic module test for ban.
 | 
			
		||||
 *
 | 
			
		||||
 * @group ban
 | 
			
		||||
 */
 | 
			
		||||
class GenericTest extends GenericModuleTestBase {}
 | 
			
		||||
							
								
								
									
										66
									
								
								web/core/modules/ban/tests/src/Kernel/BanIpTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								web/core/modules/ban/tests/src/Kernel/BanIpTest.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,66 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Drupal\Tests\ban\Kernel;
 | 
			
		||||
 | 
			
		||||
use Drupal\ban\BanIpManagerInterface;
 | 
			
		||||
use Drupal\Core\Database\Connection;
 | 
			
		||||
use Drupal\KernelTests\KernelTestBase;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @group ban
 | 
			
		||||
 */
 | 
			
		||||
class BanIpTest extends KernelTestBase {
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * {@inheritdoc}
 | 
			
		||||
   */
 | 
			
		||||
  protected static $modules = ['ban'];
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * {@inheritdoc}
 | 
			
		||||
   */
 | 
			
		||||
  protected function setUp(): void {
 | 
			
		||||
    parent::setUp();
 | 
			
		||||
    $this->installSchema('ban', ['ban_ip']);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Test banning IPs.
 | 
			
		||||
   */
 | 
			
		||||
  public function testBanIp(): void {
 | 
			
		||||
    $banIp = $this->container->get(BanIpManagerInterface::class);
 | 
			
		||||
 | 
			
		||||
    // Test valid IP addresses.
 | 
			
		||||
    $ip = '1.2.3.3';
 | 
			
		||||
    $this->assertCount(0, $this->getIpBans($ip));
 | 
			
		||||
    $banIp->banIp($ip);
 | 
			
		||||
    $this->assertCount(1, $this->getIpBans($ip));
 | 
			
		||||
 | 
			
		||||
    // Test duplicate ip address are not present in the 'blocked_ips' table
 | 
			
		||||
    // when they are entered programmatically.
 | 
			
		||||
    $ip = '1.0.0.0';
 | 
			
		||||
    $banIp->banIp($ip);
 | 
			
		||||
    $banIp->banIp($ip);
 | 
			
		||||
    $banIp->banIp($ip);
 | 
			
		||||
    $this->assertCount(1, $this->getIpBans($ip));
 | 
			
		||||
 | 
			
		||||
    $ip = '';
 | 
			
		||||
    $banIp->banIp($ip);
 | 
			
		||||
    $banIp->banIp($ip);
 | 
			
		||||
    $this->assertCount(1, $this->getIpBans($ip));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Gets the IP bans.
 | 
			
		||||
   */
 | 
			
		||||
  protected function getIpBans(string $ip): array {
 | 
			
		||||
    $connection = $this->container->get(Connection::class);
 | 
			
		||||
    $query = $connection->select('ban_ip', 'bip');
 | 
			
		||||
    $query->fields('bip', ['iid']);
 | 
			
		||||
    $query->condition('bip.ip', $ip);
 | 
			
		||||
    return $query->execute()->fetchAll();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,42 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Drupal\Tests\ban\Kernel\Migrate\d7;
 | 
			
		||||
 | 
			
		||||
use Drupal\Tests\SchemaCheckTestTrait;
 | 
			
		||||
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Migrate blocked IPs.
 | 
			
		||||
 *
 | 
			
		||||
 * @group ban
 | 
			
		||||
 */
 | 
			
		||||
class MigrateBlockedIpsTest extends MigrateDrupal7TestBase {
 | 
			
		||||
 | 
			
		||||
  use SchemaCheckTestTrait;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * {@inheritdoc}
 | 
			
		||||
   */
 | 
			
		||||
  protected static $modules = ['ban'];
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * {@inheritdoc}
 | 
			
		||||
   */
 | 
			
		||||
  protected function setUp(): void {
 | 
			
		||||
    parent::setUp();
 | 
			
		||||
    $this->installSchema('ban', ['ban_ip']);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Tests migration of blocked IPs.
 | 
			
		||||
   */
 | 
			
		||||
  public function testBlockedIps(): void {
 | 
			
		||||
    $this->startCollectingMessages();
 | 
			
		||||
    $this->executeMigration('d7_blocked_ips');
 | 
			
		||||
    $this->assertEmpty($this->migrateMessages);
 | 
			
		||||
    $this->assertTrue(\Drupal::service('ban.ip_manager')->isBanned('111.111.111.111'));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,42 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Drupal\Tests\ban\Kernel\Plugin\migrate\source\d7;
 | 
			
		||||
 | 
			
		||||
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Tests D7 blocked_ip source plugin.
 | 
			
		||||
 *
 | 
			
		||||
 * @covers \Drupal\ban\Plugin\migrate\source\d7\BlockedIps
 | 
			
		||||
 * @group ban
 | 
			
		||||
 */
 | 
			
		||||
class BlockedIpsTest extends MigrateSqlSourceTestBase {
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * {@inheritdoc}
 | 
			
		||||
   */
 | 
			
		||||
  protected static $modules = ['ban', 'migrate_drupal'];
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * {@inheritdoc}
 | 
			
		||||
   */
 | 
			
		||||
  public static function providerSource() {
 | 
			
		||||
    $tests = [];
 | 
			
		||||
 | 
			
		||||
    $tests[0]['source_data']['blocked_ips'] = [
 | 
			
		||||
      [
 | 
			
		||||
        'iid' => 1,
 | 
			
		||||
        'ip' => '127.0.0.1',
 | 
			
		||||
      ],
 | 
			
		||||
    ];
 | 
			
		||||
    $tests[0]['expected_data'] = [
 | 
			
		||||
      [
 | 
			
		||||
        'ip' => '127.0.0.1',
 | 
			
		||||
      ],
 | 
			
		||||
    ];
 | 
			
		||||
    return $tests;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										144
									
								
								web/core/modules/ban/tests/src/Unit/BanAdminTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										144
									
								
								web/core/modules/ban/tests/src/Unit/BanAdminTest.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,144 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Drupal\Tests\ban\Unit;
 | 
			
		||||
 | 
			
		||||
use Drupal\ban\BanIpManagerInterface;
 | 
			
		||||
use Drupal\ban\Form\BanAdmin;
 | 
			
		||||
use Drupal\Core\Form\FormStateInterface;
 | 
			
		||||
use Drupal\Core\Messenger\MessengerInterface;
 | 
			
		||||
use Drupal\Tests\UnitTestCase;
 | 
			
		||||
use Symfony\Component\HttpFoundation\Request;
 | 
			
		||||
use Symfony\Component\HttpFoundation\RequestStack;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Tests the BanAdmin form.
 | 
			
		||||
 *
 | 
			
		||||
 * @coversDefaultClass \Drupal\ban\Form\BanAdmin
 | 
			
		||||
 * @group ban
 | 
			
		||||
 */
 | 
			
		||||
class BanAdminTest extends UnitTestCase {
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Tests various user input to confirm correct validation.
 | 
			
		||||
   *
 | 
			
		||||
   * @covers ::validateForm
 | 
			
		||||
   * @dataProvider providerIpValidation
 | 
			
		||||
   */
 | 
			
		||||
  public function testIpValidation(string $ip, bool $isBanned, ?string $error): void {
 | 
			
		||||
    $manager = $this->getIpManagerMock();
 | 
			
		||||
    $manager->expects($this->once())
 | 
			
		||||
      ->method('isBanned')
 | 
			
		||||
      ->with($ip)
 | 
			
		||||
      ->willReturn($isBanned);
 | 
			
		||||
 | 
			
		||||
    $formObject = new BanAdmin($manager);
 | 
			
		||||
    $formObject->setStringTranslation($this->getStringTranslationStub());
 | 
			
		||||
    $formObject->setRequestStack($this->getRequestStackMock());
 | 
			
		||||
 | 
			
		||||
    $formState = $this->createMock(FormStateInterface::class);
 | 
			
		||||
    $formState->expects($this->any())
 | 
			
		||||
      ->method('getValue')
 | 
			
		||||
      ->with('ip')
 | 
			
		||||
      ->willReturn($ip);
 | 
			
		||||
 | 
			
		||||
    if ($error === NULL) {
 | 
			
		||||
      $formState->expects($this->never())
 | 
			
		||||
        ->method('setErrorByName');
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
      $formState->expects($this->once())
 | 
			
		||||
        ->method('setErrorByName')
 | 
			
		||||
        ->with('ip', $error);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    $form = [];
 | 
			
		||||
    $formObject->validateForm($form, $formState);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Test form submission.
 | 
			
		||||
   */
 | 
			
		||||
  public function testSubmit(): void {
 | 
			
		||||
    $ip = '1.2.3.4';
 | 
			
		||||
 | 
			
		||||
    $manager = $this->getIpManagerMock();
 | 
			
		||||
    $manager->expects($this->once())
 | 
			
		||||
      ->method('banIp')
 | 
			
		||||
      ->with($ip);
 | 
			
		||||
 | 
			
		||||
    $messenger = $this->createMock(MessengerInterface::class);
 | 
			
		||||
    $messenger->expects($this->once())->method('addStatus');
 | 
			
		||||
 | 
			
		||||
    $formObject = new BanAdmin($manager);
 | 
			
		||||
    $formObject->setStringTranslation($this->getStringTranslationStub());
 | 
			
		||||
    $formObject->setMessenger($messenger);
 | 
			
		||||
 | 
			
		||||
    $formState = $this->createMock(FormStateInterface::class);
 | 
			
		||||
    $formState->expects($this->any())
 | 
			
		||||
      ->method('getValue')
 | 
			
		||||
      ->with('ip')
 | 
			
		||||
      ->willReturn($ip);
 | 
			
		||||
 | 
			
		||||
    $form = [];
 | 
			
		||||
    $formObject->submitForm($form, $formState);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Test passing an IP address as a route parameter.
 | 
			
		||||
   *
 | 
			
		||||
   * @covers ::buildForm
 | 
			
		||||
   */
 | 
			
		||||
  public function testRouteParameter(): void {
 | 
			
		||||
    $ip = '1.2.3.4';
 | 
			
		||||
    $formObject = new BanAdmin($this->getIpManagerMock());
 | 
			
		||||
    $formObject->setStringTranslation($this->getStringTranslationStub());
 | 
			
		||||
    $formState = $this->createMock(FormStateInterface::class);
 | 
			
		||||
    $form = $formObject->buildForm([], $formState, $ip);
 | 
			
		||||
    $this->assertSame($ip, $form['ip']['#default_value']);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Data provider for testIpValidation().
 | 
			
		||||
   */
 | 
			
		||||
  public static function providerIpValidation(): array {
 | 
			
		||||
    return [
 | 
			
		||||
      'valid ip' => ['1.2.3.3', FALSE, NULL],
 | 
			
		||||
      'already blocked' => ['1.2.3.3', TRUE, 'This IP address is already banned.'],
 | 
			
		||||
      'reserved ip' => ['255.255.255.255', FALSE, 'Enter a valid IP address.'],
 | 
			
		||||
      'fqdn' => ['test.example.com', FALSE, 'Enter a valid IP address.'],
 | 
			
		||||
      'empty' => ['', FALSE, 'Enter a valid IP address.'],
 | 
			
		||||
      'client ip' => ['127.0.0.1', FALSE, 'You may not ban your own IP address.'],
 | 
			
		||||
    ];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Get a request stack with a dummy IP.
 | 
			
		||||
   */
 | 
			
		||||
  protected function getRequestStackMock(): RequestStack {
 | 
			
		||||
    $request = $this->createMock(Request::class);
 | 
			
		||||
    $request->expects($this->any())
 | 
			
		||||
      ->method('getClientIp')
 | 
			
		||||
      ->willReturn('127.0.0.1');
 | 
			
		||||
 | 
			
		||||
    $requestStack = $this->createMock(RequestStack::class);
 | 
			
		||||
    $requestStack->expects($this->any())
 | 
			
		||||
      ->method('getCurrentRequest')
 | 
			
		||||
      ->willReturn($request);
 | 
			
		||||
 | 
			
		||||
    return $requestStack;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Get the mocked IP manager service.
 | 
			
		||||
   */
 | 
			
		||||
  protected function getIpManagerMock(): BanIpManagerInterface {
 | 
			
		||||
    $manager = $this->createMock(BanIpManagerInterface::class);
 | 
			
		||||
    $manager->expects($this->any())
 | 
			
		||||
      ->method('findAll')
 | 
			
		||||
      ->willReturn([]);
 | 
			
		||||
    return $manager;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										94
									
								
								web/core/modules/ban/tests/src/Unit/BanMiddlewareTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								web/core/modules/ban/tests/src/Unit/BanMiddlewareTest.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,94 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace Drupal\Tests\ban\Unit;
 | 
			
		||||
 | 
			
		||||
use Drupal\ban\BanMiddleware;
 | 
			
		||||
use Drupal\Tests\UnitTestCase;
 | 
			
		||||
use Symfony\Component\HttpFoundation\Request;
 | 
			
		||||
use Symfony\Component\HttpFoundation\Response;
 | 
			
		||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @coversDefaultClass \Drupal\ban\BanMiddleware
 | 
			
		||||
 * @group ban
 | 
			
		||||
 */
 | 
			
		||||
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);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user