Issue #2503049 by phenaproxima, quietone: Migrate D7 blocked IPs to D8
parent
967b16fd77
commit
f9b5e68346
|
@ -0,0 +1,10 @@
|
||||||
|
id: d7_blocked_ips
|
||||||
|
label: Drupal 7 blocked IPs
|
||||||
|
migration_tags:
|
||||||
|
- Drupal 7
|
||||||
|
source:
|
||||||
|
plugin: d7_blocked_ips
|
||||||
|
process:
|
||||||
|
ip: ip
|
||||||
|
destination:
|
||||||
|
plugin: blocked_ip
|
|
@ -0,0 +1,88 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Contains \Drupal\ban\Plugin\migrate\destination\BlockedIP.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Drupal\ban\Plugin\migrate\destination;
|
||||||
|
|
||||||
|
use Drupal\ban\BanIpManagerInterface;
|
||||||
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||||
|
use Drupal\migrate\Entity\MigrationInterface;
|
||||||
|
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
|
||||||
|
use Drupal\migrate\Row;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destination for blocked IP addresses.
|
||||||
|
*
|
||||||
|
* @MigrateDestination(
|
||||||
|
* id = "blocked_ip"
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
class BlockedIP extends DestinationBase implements ContainerFactoryPluginInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The IP ban manager.
|
||||||
|
*
|
||||||
|
* @var \Drupal\ban\BanIpManagerInterface
|
||||||
|
*/
|
||||||
|
protected $banManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a BlockedIP object.
|
||||||
|
*
|
||||||
|
* @param array $configuration
|
||||||
|
* Plugin configuration.
|
||||||
|
* @param string $plugin_id
|
||||||
|
* The plugin ID.
|
||||||
|
* @param mixed $plugin_definition
|
||||||
|
* The plugin definiiton.
|
||||||
|
* @param \Drupal\migrate\Entity\MigrationInterface $migration
|
||||||
|
* The current migration.
|
||||||
|
* @param \Drupal\ban\BanIpManagerInterface $ban_manager
|
||||||
|
* The IP manager service.
|
||||||
|
*/
|
||||||
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, BanIpManagerInterface $ban_manager) {
|
||||||
|
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
|
||||||
|
$this->banManager = $ban_manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
|
||||||
|
return new static(
|
||||||
|
$configuration,
|
||||||
|
$plugin_id,
|
||||||
|
$plugin_definition,
|
||||||
|
$migration,
|
||||||
|
$container->get('ban.ip_manager')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getIds() {
|
||||||
|
return ['ip' => ['type' => 'string']];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function fields(MigrationInterface $migration = NULL) {
|
||||||
|
return [
|
||||||
|
'ip' => $this->t('The blocked IP address.'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function import(Row $row, array $old_destination_id_values = array()) {
|
||||||
|
$this->banManager->banIp($row->getDestinationProperty('ip'));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Contains \Drupal\ban\Plugin\migrate\source\d7\BlockedIps.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Drupal\ban\Plugin\migrate\source\d7;
|
||||||
|
|
||||||
|
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drupal 7 blocked IPs from database.
|
||||||
|
*
|
||||||
|
* @MigrateSource(
|
||||||
|
* id = "d7_blocked_ips",
|
||||||
|
* source_provider = "system"
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
class BlockedIps extends DrupalSqlBase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function query() {
|
||||||
|
return $this->select('blocked_ips', 'bi')->fields('bi', ['ip']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function fields() {
|
||||||
|
return [
|
||||||
|
'ip' => $this->t('The blocked IP address.'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getIds() {
|
||||||
|
return ['ip' => ['type' => 'string']];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Contains \Drupal\ban\Tests\d7\MigrateBlockedIPsTest.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Drupal\ban\Tests\d7;
|
||||||
|
|
||||||
|
use Drupal\config\Tests\SchemaCheckTestTrait;
|
||||||
|
use Drupal\migrate_drupal\Tests\d7\MigrateDrupal7TestBase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Migrate blocked IPs.
|
||||||
|
*
|
||||||
|
* @group ban
|
||||||
|
*/
|
||||||
|
class MigrateBlockedIPsTest extends MigrateDrupal7TestBase {
|
||||||
|
|
||||||
|
use SchemaCheckTestTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Modules to enable.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public static $modules = ['ban'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
$this->installSchema('ban', ['ban_ip']);
|
||||||
|
$this->loadDumps(['BlockedIps.php']);
|
||||||
|
$this->executeMigration('d7_blocked_ips');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests migration of blocked IPs.
|
||||||
|
*/
|
||||||
|
public function testBlockedIPs() {
|
||||||
|
$this->assertTrue(\Drupal::service('ban.ip_manager')->isBanned('111.111.111.111'));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Contains \Drupal\Tests\ban\Unit\Plugin\migrate\source\d7\BlockedIps.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Drupal\Tests\ban\Unit\Plugin\migrate\source\d7;
|
||||||
|
|
||||||
|
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests D7 blocked_ip source plugin.
|
||||||
|
*
|
||||||
|
* @coversDefaultClass \Drupal\ban\Plugin\migrate\source\d7\BlockedIps
|
||||||
|
* @group ban
|
||||||
|
*/
|
||||||
|
class BlockedIpsTest extends MigrateSqlSourceTestCase {
|
||||||
|
|
||||||
|
const PLUGIN_CLASS = 'Drupal\ban\Plugin\migrate\source\d7\BlockedIps';
|
||||||
|
|
||||||
|
protected $migrationConfiguration = [
|
||||||
|
'id' => 'test',
|
||||||
|
'idlist' => [],
|
||||||
|
'source' => [
|
||||||
|
'plugin' => 'd7_blocked_ips',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $expectedResults = [
|
||||||
|
[
|
||||||
|
'ip' => '127.0.0.1',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function setUp() {
|
||||||
|
$this->databaseContents['blocked_ips'] = [
|
||||||
|
[
|
||||||
|
'iid' => 1,
|
||||||
|
'ip' => '127.0.0.1',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
parent::setUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue