View source
<?php
namespace Drupal\Tests\sms\Kernel\Migrate;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\migrate\MigrateExecutable;
use Drupal\sms\Entity\PhoneNumberSettings;
use Drupal\sms\Entity\PhoneNumberVerification;
use Drupal\sms\Plugin\Migrate\process\PhoneNumberSettings as PhoneNumberSettingsPlugin;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
trait MigratePhoneNumberTestTrait {
public function testPhoneSettingsMigration() {
$settings = PhoneNumberSettings::loadMultiple();
$this
->assertEquals([], $settings);
$this
->executeMigration('phone_number_settings');
$settings = PhoneNumberSettings::loadMultiple();
$this
->assertEquals(1, count($settings));
$setting = reset($settings);
$this
->assertEquals(PhoneNumberSettingsPlugin::DEFAULT_VERIFICATION_MESSAGE, $setting
->getVerificationMessage());
$this
->assertEquals('phone_number', $setting
->getFieldName('phone_number'));
$this
->assertEquals(TRUE, $setting
->getPurgeVerificationPhoneNumber());
$this
->assertEquals('user', $setting
->getPhoneNumberBundle());
$this
->assertEquals('user', $setting
->getPhoneNumberEntityTypeId());
$this
->assertEquals(600, $setting
->getVerificationCodeLifetime());
$field_storage = FieldStorageConfig::load('user.phone_number');
$this
->assertEquals('user.phone_number', $field_storage
->id());
$this
->assertEquals('phone_number', $field_storage
->getName());
$this
->assertEquals('user', $field_storage
->getTargetEntityTypeId());
$this
->assertEquals('telephone', $field_storage
->getType());
$field_config = FieldConfig::load('user.user.phone_number');
$this
->assertEquals('user', $field_config
->getTargetEntityTypeId());
$this
->assertEquals('user', $field_config
->getTargetBundle());
}
public function testPhoneSettingsMigrationWithCustomVerificationMessage() {
$this
->loadFixture($this
->confirmationMessageFixturePath());
$this
->executeMigration('phone_number_settings');
$settings = PhoneNumberSettings::loadMultiple();
$this
->assertEquals(1, count($settings));
$setting = reset($settings);
$expected_message = 'This is a custom confirmation message from [site:name]. Confirmation code: [sms-message:verification-code]';
$this
->assertEquals($expected_message, $setting
->getVerificationMessage());
}
public function testPhoneNumberMigration() {
$this
->loadFixture($this
->smsUserFixtureFilePath());
$this
->installEntitySchema('sms');
$this
->installEntitySchema('sms_phone_number_verification');
$this
->executeMigrations($this
->getMigrationsToTest());
$user = User::load(40);
$this
->assertEquals('1234567890', $user
->get('phone_number')->value);
$this
->assertVerifiedPhoneNumber($user, '1234567890');
$user = User::load(41);
$this
->assertEquals('87654321190', $user
->get('phone_number')->value);
$this
->assertUnVerifiedPhoneNumber($user, '87654321190');
$this
->assertVerificationCode('87654321190', '8002');
$user = User::load(42);
$this
->assertEquals('', $user
->get('phone_number')->value);
$this
->assertNoVerifiedPhoneNumber($user);
}
public function testRollBack() {
$this
->loadFixture($this
->smsUserFixtureFilePath());
$this
->installEntitySchema('sms');
$this
->installEntitySchema('sms_phone_number_verification');
EntityFormDisplay::create([
'targetEntityType' => 'user',
'bundle' => 'user',
'mode' => 'default',
])
->setStatus(TRUE)
->save();
$this
->executeMigrations($this
->getMigrationsToTest());
$this
->assertVerifiedPhoneNumber(User::load(40), '1234567890');
$entity_form_display = EntityFormDisplay::load('user.user.default');
$this
->assertNotNull($entity_form_display
->getComponent('phone_number'));
$this
->rollBackMigrations($this
->getMigrationsToRollBack());
$this
->assertEquals([], PhoneNumberVerification::loadMultiple());
$this
->assertEquals([], PhoneNumberSettings::loadMultiple());
$this
->assertNull(FieldConfig::loadByName('user', 'user', 'phone_number'));
$this
->assertNull(FieldStorageConfig::loadByName('user', 'phone_number'));
$entity_form_display = EntityFormDisplay::load('user.user.default');
$this
->assertNull($entity_form_display
->getComponent('phone_number'));
}
protected function assertVerifiedPhoneNumber(UserInterface $user, $number) {
$phone_numbers = $this->container
->get('sms.phone_number')
->getPhoneNumbers($user, TRUE);
$phone_number = reset($phone_numbers);
return $this
->assertEquals($number, $phone_number, "Phone number '{$number}' is verified.");
}
protected function assertUnVerifiedPhoneNumber(UserInterface $user, $number) {
$phone_numbers = $this->container
->get('sms.phone_number')
->getPhoneNumbers($user, FALSE);
$phone_number = reset($phone_numbers);
return $this
->assertEquals($number, $phone_number, "Phone number '{$number}' is unverified.");
}
protected function assertNoVerifiedPhoneNumber(UserInterface $user) {
$phone_numbers = $this->container
->get('sms.phone_number')
->getPhoneNumbers($user);
return $this
->assertEquals([], $phone_numbers, "No phone numbers for user {$user->id()}.");
}
protected function assertVerificationCode($number, $code) {
$verification = $this->container
->get('sms.phone_number.verification')
->getPhoneVerificationByPhoneNumber($number, FALSE);
$verification = reset($verification);
return $this
->assertEquals($code, $verification
->getCode());
}
protected function rollBackMigrations(array $ids) {
foreach ($ids as $id) {
$this->migration = $this
->getMigration($id);
$this
->prepareMigration($this->migration);
(new MigrateExecutable($this->migration, $this))
->rollback();
}
}
protected abstract function smsUserFixtureFilePath();
protected abstract function confirmationMessageFixturePath();
protected abstract function getMigrationsToTest();
protected abstract function getMigrationsToRollback();
}