Commit 14457237 authored by M Parker's avatar M Parker Committed by Jakob P
Browse files

Issue #3190531 by mparker17, omkar.podey: Migrate config from D7 to D8

parent 87672b76
Loading
Loading
Loading
Loading
+68 −0
Original line number Diff line number Diff line
id: d7_smtp_settings
label: SMTP Authentication Support configuration
migration_tags:
  - Drupal 7
  - Configuration
source:
  plugin: variable
  # Note that smtp_deliver, smtp_queue, and smtp_queue_fail were variables in
  # D7, but the corresponding features have been removed in D8.
  variables:
    - smtp_allowhtml
    - smtp_client_helo
    - smtp_client_hostname
    - smtp_debugging
    - smtp_from
    - smtp_fromname
    - smtp_host
    - smtp_hostbackup
    - smtp_on
    - smtp_password
    - smtp_port
    - smtp_protocol
    - smtp_queue
    - smtp_queue_fail
    - smtp_reroute_address
    - smtp_test_address
    - smtp_username
  source_module: smtp
process:
  smtp_on: smtp_on
  smtp_host: smtp_host
  smtp_hostbackup: smtp_hostbackup
  smtp_port: smtp_port
  smtp_protocol: smtp_protocol

  # AutoTLS was always on in D7.
  smtp_autotls:
    plugin: default_value
    default_value: true

  # Timeout defaulted to 10 in D7's smtp.phpmailer.php.
  smtp_timeout:
    plugin: default_value
    default_value: 10

  smtp_username: smtp_username
  smtp_password: smtp_password
  smtp_from: smtp_from
  smtp_fromname: smtp_fromname
  smtp_client_hostname: smtp_client_hostname
  smtp_client_helo: smtp_client_helo
  smtp_allowhtml: smtp_allowhtml
  smtp_reroute_address: smtp_reroute_address
  smtp_test_address: smtp_test_address
  smtp_debugging: smtp_debugging

  # Previous mail system was not recorded in D7, so we assume the D8 default.
  prev_mail_system:
    plugin: default_value
    default_value: 'php_mail'

  # Keep-alive was, by default, false in D7.
  smtp_keepalive:
    plugin: default_value
    default_value: false
destination:
  plugin: config
  config_name: smtp.settings
+3 −0
Original line number Diff line number Diff line
finished:
  7:
    smtp: smtp
+9 −0
Original line number Diff line number Diff line
@@ -127,3 +127,12 @@ function _smtp_mailer_send(array $variables) {
  }
  return TRUE;
}

/**
 * Implements hook_migration_plugins_alter().
 *
 * Adds mapping for SmtpMailSystem in d7_system_mail.
 */
function smtp_migration_plugins_alter(array &$migrations) {
  $migrations["d7_system_mail"]['process']['interface/default']['map']['SmtpMailSystem'] = 'SMTPMailSystem';
}
+135 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\smtp\Kernel;

use Drupal\Core\Database\Database;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;

/**
 * Tests migration of smtp settings.
 *
 * @group smtp
 */
class MigrateD7SettingsTest extends MigrateDrupal7TestBase {

  /**
   * The migration this test is testing.
   *
   * @var string
   */
  const MIGRATION_UNDER_TEST = 'd7_smtp_settings';

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['smtp'];

  /**
   * Test that we can migrate D7 SMTP settings.
   */
  public function testMigrateSettings() {
    // Generate test data.
    $allowHtml = '0';
    $clientHelo = $this->randomString();
    $clientHostname = $this->randomHostname();
    $debugging = TRUE;
    $from = $this->randomEmail();
    $fromName = $this->randomString();
    $host = $this->randomHostname();
    $hostBackup = $this->randomHostname();
    $smtpOn = TRUE;
    $password = $this->randomString();
    $port = strval(rand(1, 65535));
    $protocol = 'standard';
    $rerouteAddress = $this->randomEmail();
    $testAddress = $this->randomEmail();
    $username = $this->randomString();

    // Set D7 variables.
    $this->setUpD7Variable('smtp_allowhtml', $allowHtml);
    $this->setUpD7Variable('smtp_client_helo', $clientHelo);
    $this->setUpD7Variable('smtp_client_hostname', $clientHostname);
    $this->setUpD7Variable('smtp_debugging', $debugging);
    $this->setUpD7Variable('smtp_from', $from);
    $this->setUpD7Variable('smtp_fromname', $fromName);
    $this->setUpD7Variable('smtp_host', $host);
    $this->setUpD7Variable('smtp_hostbackup', $hostBackup);
    $this->setUpD7Variable('smtp_on', $smtpOn);
    $this->setUpD7Variable('smtp_password', $password);
    $this->setUpD7Variable('smtp_port', $port);
    $this->setUpD7Variable('smtp_protocol', $protocol);
    $this->setUpD7Variable('smtp_reroute_address', $rerouteAddress);
    $this->setUpD7Variable('smtp_test_address', $testAddress);
    $this->setUpD7Variable('smtp_username', $username);

    // Run the migration.
    $this->executeMigrations([self::MIGRATION_UNDER_TEST]);

    // Validate the D7 variable values made it into the destination structure.
    $destConfig = $this->config('smtp.settings');
    $this->assertSame($allowHtml, $destConfig->get('smtp_allowhtml'));
    $this->assertSame($clientHelo, $destConfig->get('smtp_client_helo'));
    $this->assertSame($clientHostname, $destConfig->get('smtp_client_hostname'));
    $this->assertSame($debugging, $destConfig->get('smtp_debugging'));
    $this->assertSame($from, $destConfig->get('smtp_from'));
    $this->assertSame($fromName, $destConfig->get('smtp_fromname'));
    $this->assertSame($host, $destConfig->get('smtp_host'));
    $this->assertSame($hostBackup, $destConfig->get('smtp_hostbackup'));
    $this->assertSame($smtpOn, $destConfig->get('smtp_on'));
    $this->assertSame($password, $destConfig->get('smtp_password'));
    $this->assertSame($port, $destConfig->get('smtp_port'));
    $this->assertSame($protocol, $destConfig->get('smtp_protocol'));
    $this->assertSame($rerouteAddress, $destConfig->get('smtp_reroute_address'));
    $this->assertSame($testAddress, $destConfig->get('smtp_test_address'));
    $this->assertSame($username, $destConfig->get('smtp_username'));

    // Validate default_value migrations.
    $this->assertSame(TRUE, $destConfig->get('smtp_autotls'));
    $this->assertSame(10, $destConfig->get('smtp_timeout'));
    $this->assertSame('php_mail', $destConfig->get('prev_mail_system'));
    $this->assertSame(FALSE, $destConfig->get('smtp_keepalive'));
  }

  /**
   * Generate a random email address.
   *
   * @return string
   *   A random email address at a random hostname.
   */
  protected function randomEmail() {
    return sprintf('%s@%s', $this->randomGenerator->word(8), $this->randomHostname());
  }

  /**
   * Generate a random hostname.
   *
   * @return string
   *   A random hostname.
   */
  protected function randomHostname() {
    return sprintf('%s.%s.com', $this->getRandomGenerator()->word(8), $this->getRandomGenerator()->word(8));
  }

  /**
   * Set up a D7 variable to be migrated.
   *
   * @param string $name
   *   The name of the variable to be set.
   * @param mixed $value
   *   The value of the variable to be set.
   */
  protected function setUpD7Variable($name, $value) {
    $this->assertIsString($name, 'Name must be a string');

    Database::getConnection('default', 'migrate')
      ->upsert('variable')
      ->key('name')
      ->fields(['name', 'value'])
      ->values([
        'name' => $name,
        'value' => serialize($value),
      ])
      ->execute();
  }

}
+26 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\smtp\Kernel;

use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
use Drupal\Tests\migrate_drupal\Traits\ValidateMigrationStateTestTrait;

/**
 * Tests that the smtp test has a declared migration status.
 *
 * ValidateMigrationStateTestTrait::testMigrationState() will succeed if the
 * modules enabled in \Drupal\Tests\KernelTestBase::bootKernel() have a valid
 * migration status (i.e.: finished or not_finished); but will fail if they do
 * not have a declared migration status.
 *
 * @group smtp
 */
class ValidateD7MigrationStateTest extends MigrateDrupal7TestBase {
  use ValidateMigrationStateTestTrait;

  /**
   * {@inheritdoc}
   */
  public static $modules = ['smtp'];

}