1<?php
2
3namespace Drupal\Tests\user\Kernel\Migrate\d7;
4
5use Drupal\Tests\SchemaCheckTestTrait;
6use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
7use Drupal\user\AccountSettingsForm;
8use Drupal\Core\Database\Database;
9use Drupal\user\UserInterface;
10
11/**
12 * Tests migration of user settings.
13 *
14 * @group migrate_drupal_7
15 */
16class MigrateUserSettingsTest extends MigrateDrupal7TestBase {
17
18  use SchemaCheckTestTrait;
19
20  /**
21   * {@inheritdoc}
22   */
23  protected function setUp(): void {
24    parent::setUp();
25    $this->executeMigrations(['d7_user_settings']);
26  }
27
28  /**
29   * Tests the migration.
30   */
31  public function testMigration() {
32    $config = $this->config('user.settings');
33    $this->assertTrue($config->get('notify.status_blocked'));
34    $this->assertTrue($config->get('notify.status_activated'));
35    $this->assertTrue($config->get('verify_mail'));
36    $this->assertSame(UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL, $config->get('register'));
37    $this->assertSame('Anonymous', $config->get('anonymous'));
38
39    // Tests migration of user_register using the AccountSettingsForm.
40    // Map source values to destination values.
41    $user_register_map = [
42      [0, UserInterface::REGISTER_ADMINISTRATORS_ONLY],
43      [1, UserInterface::REGISTER_VISITORS],
44      [2, UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL],
45    ];
46
47    foreach ($user_register_map as $map) {
48      // Tests migration of user_register = 1.
49      Database::getConnection('default', 'migrate')
50        ->update('variable')
51        ->fields(['value' => serialize($map[0])])
52        ->condition('name', 'user_register')
53        ->execute();
54
55      /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
56      $migration = $this->getMigration('d7_user_settings');
57      // Indicate we're rerunning a migration that's already run.
58      $migration->getIdMap()->prepareUpdate();
59      $this->executeMigration($migration);
60      $form = $this->container->get('form_builder')->getForm(AccountSettingsForm::create($this->container));
61      $this->assertSame($map[1], $form['registration_cancellation']['user_register']['#value']);
62    }
63  }
64
65}
66