View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\sms_user\Functional;
use Drupal\Tests\sms\Functional\SmsFrameworkBrowserTestBase;
use Drupal\Core\Url;
class SmsFrameworkUserSettingsTest extends SmsFrameworkBrowserTestBase {
public static $modules = [
'sms_user',
];
protected $defaultTheme = 'classy';
protected $days = [];
protected function setUp() {
parent::setUp();
$account = $this
->drupalCreateUser([
'administer smsframework',
]);
$this
->drupalLogin($account);
$timestamp = strtotime('next Sunday');
while (($day = strtolower(strftime('%A', $timestamp))) && !in_array($day, $this->days)) {
$this->days[] = $day;
$timestamp = strtotime('+1 day', $timestamp);
}
}
public function testSettingsForm() {
$this
->drupalGet(Url::fromRoute('sms_user.options'));
$this
->assertResponse(200);
$this
->assertFieldByName('active_hours[status]');
$this
->assertNoFieldChecked('edit-active-hours-status');
foreach ($this->days as $day) {
$this
->assertOptionSelected('edit-active-hours-days-' . $day . '-start', -1);
}
foreach ($this->days as $day) {
$this
->assertOptionSelected('edit-active-hours-days-' . $day . '-end', 24);
}
$edit = [
'active_hours[status]' => TRUE,
'active_hours[days][sunday][start]' => 2,
'active_hours[days][sunday][end]' => 22,
'active_hours[days][tuesday][start]' => 0,
'active_hours[days][tuesday][end]' => 24,
'active_hours[days][thursday][start]' => -1,
'active_hours[days][thursday][end]' => 18,
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('The configuration options have been saved.');
$this
->assertFieldChecked('edit-active-hours-status');
$this
->assertOptionSelected('edit-active-hours-days-sunday-start', 2);
$this
->assertOptionSelected('edit-active-hours-days-sunday-end', 22);
$this
->assertOptionSelected('edit-active-hours-days-tuesday-start', 0);
$this
->assertOptionSelected('edit-active-hours-days-tuesday-end', 24);
$this
->assertOptionSelected('edit-active-hours-days-thursday-start', -1);
$this
->assertOptionSelected('edit-active-hours-days-thursday-end', 24);
$ranges_expected = [
[
'start' => 'sunday 2:00',
'end' => 'sunday 22:00',
],
[
'start' => 'tuesday 0:00',
'end' => 'tuesday +1 day',
],
];
$ranges_actual = \Drupal::config('sms_user.settings')
->get('active_hours.ranges');
$this
->assertEquals($ranges_expected, $ranges_actual);
}
public function testSettingsFormValidationFail() {
$edit = [
'active_hours[days][wednesday][start]' => 10,
'active_hours[days][wednesday][end]' => 9,
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('End time must be greater than start time.');
$edit = [
'active_hours[status]' => TRUE,
'active_hours[days][wednesday][start]' => -1,
'active_hours[days][wednesday][end]' => 24,
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('If active hours hours are enabled there must be at least one enabled day.');
}
public function testAccountRegistrationOff() {
$edit = [
'account_registration[behaviour]' => 'none',
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('The configuration options have been saved.');
$settings = $this
->config('sms_user.settings')
->get('account_registration');
$this
->assertFalse($settings['unrecognized_sender']['status']);
$this
->assertFalse($settings['incoming_pattern']['status']);
}
public function testAccountRegistrationReplyTokens() {
$this
->drupalGet(Url::fromRoute('sms_user.options'));
$this
->assertResponse(200);
$this
->assertRaw('Available tokens include: [sms-message:*] [user:*]');
}
public function testAccountRegistrationUnrecognised() {
$this
->createPhoneNumberSettings('user', 'user');
$reply_message = $this
->randomString();
$edit = [
'account_registration[behaviour]' => 'all',
'account_registration[all_options][reply_status]' => TRUE,
'account_registration[all_options][reply][message]' => $reply_message,
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('The configuration options have been saved.');
$settings = $this
->config('sms_user.settings')
->get('account_registration');
$this
->assertTrue($settings['unrecognized_sender']['status']);
$this
->assertFalse($settings['incoming_pattern']['status']);
$this
->assertTrue($settings['unrecognized_sender']['reply']['status']);
$this
->assertEquals($reply_message, $settings['unrecognized_sender']['reply']['message']);
}
public function testAccountRegistrationIncomingPattern() {
$this
->createPhoneNumberSettings('user', 'user');
$incoming_message = '[email] ' . $this
->randomString();
$reply_message_success = $this
->randomString();
$reply_message_failure = $this
->randomString();
$edit = [
'account_registration[behaviour]' => 'incoming_pattern',
'account_registration[incoming_pattern_options][incoming_message]' => $incoming_message,
'account_registration[incoming_pattern_options][send_activation_email]' => TRUE,
'account_registration[incoming_pattern_options][reply_status]' => TRUE,
'account_registration[incoming_pattern_options][reply][message_success]' => $reply_message_success,
'account_registration[incoming_pattern_options][reply][message_failure]' => $reply_message_failure,
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('The configuration options have been saved.');
$settings = $this
->config('sms_user.settings')
->get('account_registration');
$this
->assertFalse($settings['unrecognized_sender']['status']);
$this
->assertTrue($settings['incoming_pattern']['status']);
$this
->assertEquals($incoming_message, $settings['incoming_pattern']['incoming_messages'][0]);
$this
->assertTrue($settings['incoming_pattern']['send_activation_email']);
$this
->assertTrue($settings['incoming_pattern']['reply']['status']);
$this
->assertEquals($reply_message_success, $settings['incoming_pattern']['reply']['message']);
$this
->assertEquals($reply_message_failure, $settings['incoming_pattern']['reply']['message_failure']);
}
public function testAccountRegistrationValidationEmptyReplies() {
$this
->createPhoneNumberSettings('user', 'user');
$edit = [
'account_registration[behaviour]' => 'all',
'account_registration[all_options][reply_status]' => TRUE,
'account_registration[all_options][reply][message]' => '',
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('Reply message must have a value if reply is enabled.', 'Validation failed for message on all unrecognised numbers when reply status is enabled.');
$edit = [
'account_registration[behaviour]' => 'incoming_pattern',
'account_registration[incoming_pattern_options][reply_status]' => TRUE,
'account_registration[incoming_pattern_options][reply][message_success]' => '',
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('Reply message must have a value if reply is enabled.', 'Validation failed for message_success on incoming_pattern when reply status is enabled.');
$edit = [
'account_registration[behaviour]' => 'incoming_pattern',
'account_registration[incoming_pattern_options][reply_status]' => TRUE,
'account_registration[incoming_pattern_options][reply][message_failure]' => '',
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('Reply message must have a value if reply is enabled.', 'Validation failed for message_failure on incoming_pattern when reply status is enabled.');
}
public function testAccountRegistrationValidationIncomingPattern() {
$this
->createPhoneNumberSettings('user', 'user');
$edit = [
'account_registration[behaviour]' => 'incoming_pattern',
'account_registration[incoming_pattern_options][incoming_message]' => '',
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('Incoming message must be filled if using pre-incoming_pattern option');
$edit = [
'account_registration[behaviour]' => 'incoming_pattern',
'account_registration[incoming_pattern_options][send_activation_email]' => TRUE,
'account_registration[incoming_pattern_options][incoming_message]' => $this
->randomString(),
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('Activation email cannot be sent if [email] placeholder is missing.');
$edit = [
'account_registration[behaviour]' => 'incoming_pattern',
'account_registration[incoming_pattern_options][send_activation_email]' => TRUE,
'account_registration[incoming_pattern_options][incoming_message]' => 'E [email] P [password]',
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('Activation email cannot be sent if [password] placeholder is present.');
$edit = [
'account_registration[behaviour]' => 'incoming_pattern',
'account_registration[incoming_pattern_options][incoming_message]' => 'Email [email][password]',
];
$this
->drupalPostForm(Url::fromRoute('sms_user.options'), $edit, 'Save configuration');
$this
->assertRaw('There must be a separator between placeholders.');
}
public function testFormNoUserPhoneNumberSettings() {
$this
->drupalGet(Url::fromRoute('sms_user.options'));
$this
->assertRaw('There are no phone number settings configured for the user entity type. Some features cannot operate without these settings. <a href="' . Url::fromRoute('entity.phone_number_settings.add')
->toString() . '">Add phone number settings</a>.', 'Warning message displayed for no phone number settings.');
$input = $this
->xpath('//input[@name="account_registration[behaviour]" and @disabled="disabled" and @value="all"]');
$this
->assertTrue(count($input) === 1, "The 'All unrecognised phone numbers' radio is disabled.");
$input = $this
->xpath('//input[@name="account_registration[behaviour]" and @disabled="disabled" and @value="incoming_pattern"]');
$this
->assertTrue(count($input) === 1, "The 'incoming_pattern' radio is disabled.");
}
public function testFormUserPhoneNumberSettings() {
$this
->createPhoneNumberSettings('user', 'user');
$this
->drupalGet(Url::fromRoute('sms_user.options'));
$this
->assertNoRaw('There are no phone number settings configured for the user entity type. Some features cannot operate without these settings.', 'Warning message displayed for no phone number settings.');
$input = $this
->xpath('//input[@name="account_registration[behaviour]" and @disabled="disabled" and @value="all"]');
$this
->assertTrue(count($input) === 0, "The 'All unrecognised phone numbers' radio is not disabled.");
$input = $this
->xpath('//input[@name="account_registration[behaviour]" and @disabled="disabled" and @value="incoming_pattern"]');
$this
->assertTrue(count($input) === 0, "The 'incoming_pattern' radio is not disabled.");
}
}