View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\sms\Functional;
use Drupal\Core\Url;
use Drupal\Tests\Traits\Core\CronRunTrait;
class SmsFrameworkPhoneNumberWidgetTest extends SmsFrameworkBrowserTestBase {
use CronRunTrait;
public static $modules = [
'entity_test',
];
protected function setUp() {
parent::setUp();
$account = $this
->drupalCreateUser([
'sms verify phone number',
'administer entity_test content',
]);
$this
->drupalLogin($account);
}
public function testPhoneNumberWidget() {
$phone_number_settings = $this
->createPhoneNumberSettings('entity_test', 'entity_test');
$field_phone_number = $phone_number_settings
->getFieldName('phone_number');
$form_field_phone_number = $field_phone_number . '[0][value]';
$test_entity = $this
->createEntityWithPhoneNumber($phone_number_settings);
$this
->drupalGet($test_entity
->toUrl('edit-form'));
$t_args = [
'@url' => Url::fromRoute('sms.phone.verify')
->toString(),
'@time' => '1 hour',
];
$this
->assertRaw(t('Enter a phone number. A verification code will be sent as an SMS message, you must enter the code into the <a href="@url">verification form</a> within @time.', $t_args));
$edit = [
$form_field_phone_number => '+123123123',
];
$this
->drupalPostForm($test_entity
->toUrl('edit-form'), $edit, t('Save'));
$this
->assertRaw(t('A verification code has been sent to this phone number. Go to the <a href="@url">verification form</a> and enter the code. The code will expire if it is not verified in', $t_args));
$input = $this
->xpath('//input[@name="' . $form_field_phone_number . '" and @disabled="disabled"]');
$this
->assertTrue(count($input) === 1, 'The phone number text field is disabled.');
$phone_verification = $this
->getLastVerification();
$phone_verification
->setStatus(TRUE)
->save();
$this
->drupalGet($test_entity
->toUrl('edit-form'));
$this
->assertRaw(t('This phone number is verified. <strong>Warning:</strong> Modifying this phone number will remove verification.'));
$input = $this
->xpath('//input[@name="' . $form_field_phone_number . '" and @disabled="disabled"]');
$this
->assertTrue(count($input) === 0, 'The phone number text field is enabled.');
}
public function testPhoneNumberWidgetWithExpiredVerificationCode() {
$phone_number_settings = $this
->createPhoneNumberSettings('entity_test', 'entity_test');
$test_entity = $this
->createEntityWithPhoneNumber($phone_number_settings, [
'+123123123',
]);
$phone_verification = $this
->getLastVerification();
$phone_verification
->set('created', time() - ($phone_number_settings
->getVerificationCodeLifetime() + 1))
->save();
$this
->drupalGet($test_entity
->toUrl('edit-form'));
$this
->assertRaw(t('Verification code expired. Try again later.'));
$this
->cronRun();
$this
->drupalGet($test_entity
->toUrl('edit-form'));
$t_args = [
'@url' => Url::fromRoute('sms.phone.verify')
->toString(),
'@time' => '1 hour',
];
$field_phone_number = $phone_number_settings
->getFieldName('phone_number');
$this
->assertFieldByName($field_phone_number . '[0][value]', '');
$this
->assertRaw(t('Enter a phone number. A verification code will be sent as an SMS message, you must enter the code into the <a href="@url">verification form</a> within @time.', $t_args));
}
public function testPhoneNumberPurgedFieldValueOnExpiration() {
$phone_number_settings = $this
->createPhoneNumberSettings('entity_test', 'entity_test');
$phone_number_settings
->setPurgeVerificationPhoneNumber(TRUE)
->save();
$test_entity = $this
->createEntityWithPhoneNumber($phone_number_settings, [
'+123123123',
]);
$this
->getLastVerification()
->set('created', time() - ($phone_number_settings
->getVerificationCodeLifetime() + 1))
->save();
$this
->cronRun();
$this
->drupalGet($test_entity
->toUrl('edit-form'));
$field_phone_number = $phone_number_settings
->getFieldName('phone_number');
$this
->assertFieldByName($field_phone_number . '[0][value]', '');
}
public function testPhoneNumberNotPurgedFieldValueOnExpiration() {
$phone_number_settings = $this
->createPhoneNumberSettings('entity_test', 'entity_test');
$phone_number_settings
->setPurgeVerificationPhoneNumber(FALSE)
->save();
$test_entity = $this
->createEntityWithPhoneNumber($phone_number_settings, [
'+123123123',
]);
$this
->getLastVerification()
->set('created', time() - ($phone_number_settings
->getVerificationCodeLifetime() + 1))
->save();
$this
->cronRun();
$this
->drupalGet($test_entity
->toUrl('edit-form'));
$t_args = [
'@url' => Url::fromRoute('sms.phone.verify')
->toString(),
'@time' => '1 hour',
];
$this
->assertRaw(t('Save this form to send a new verification code as an SMS message, you must enter the code into the <a href="@url">verification form</a> within @time.', $t_args));
$field_phone_number = $phone_number_settings
->getFieldName('phone_number');
$this
->assertFieldByName($field_phone_number . '[0][value]', '+123123123');
}
}