View source
<?php
namespace Drupal\Tests\sms\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\sms\Exception\NoPhoneNumberException;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\sms\Entity\PhoneNumberSettings;
use Drupal\Component\Utility\Unicode;
use Drupal\sms\Message\SmsMessage;
class SmsFrameworkPhoneNumberProviderTest extends SmsFrameworkKernelBase {
public static $modules = [
'sms',
'entity_test',
'user',
'field',
'telephone',
'dynamic_entity_reference',
'sms_test_gateway',
];
protected $phoneNumberProvider;
protected $phoneNumberVerificationProvider;
protected $phoneField;
protected $phoneNumberSettings;
protected $gateway;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('sms_phone_number_verification');
$this->gateway = $this
->createMemoryGateway([
'skip_queue' => TRUE,
]);
$this
->setFallbackGateway($this->gateway);
$this->phoneNumberProvider = $this->container
->get('sms.phone_number');
$this->phoneField = FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => Unicode::strtolower($this
->randomMachineName()),
'type' => 'telephone',
]);
$this->phoneField
->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'field_name' => $this->phoneField
->getName(),
])
->save();
$this->phoneNumberSettings = PhoneNumberSettings::create();
$this->phoneNumberSettings
->setPhoneNumberEntityTypeId('entity_test')
->setPhoneNumberBundle('entity_test')
->setFieldName('phone_number', $this->phoneField
->getName())
->setVerificationMessage($this
->randomString())
->save();
}
public function testGetPhoneNumbersUnverified() {
$phone_numbers_all = [
'+123123123',
'+456456456',
];
for ($i = 0; $i < 3; $i++) {
$phone_numbers = array_slice($phone_numbers_all, 0, $i);
$entity = $this
->createEntityWithPhoneNumber($this->phoneNumberSettings, $phone_numbers);
$return = $this->phoneNumberProvider
->getPhoneNumbers($entity, NULL);
$this
->assertEquals($phone_numbers, $return);
$return = $this->phoneNumberProvider
->getPhoneNumbers($entity, FALSE);
$this
->assertEquals($phone_numbers, $return);
$return = $this->phoneNumberProvider
->getPhoneNumbers($entity, TRUE);
$this
->assertEquals([], $return);
}
}
public function testGetPhoneNumbersVerified() {
$phone_numbers_all = [
'+123123123',
'+456456456',
];
for ($i = 0; $i < 3; $i++) {
$phone_numbers = array_slice($phone_numbers_all, 0, $i);
$entity = $this
->createEntityWithPhoneNumber($this->phoneNumberSettings, $phone_numbers);
$phone_number_verified = array_values(array_slice($phone_numbers, 0, 1, TRUE));
$phone_number_unverified = array_values(array_slice($phone_numbers, 1, $i, TRUE));
if (!empty($phone_number_verified)) {
$this
->verifyPhoneNumber($entity, reset($phone_number_verified));
}
$return = $this->phoneNumberProvider
->getPhoneNumbers($entity, NULL);
$this
->assertEquals($phone_numbers, $return);
$return = $this->phoneNumberProvider
->getPhoneNumbers($entity, FALSE);
$this
->assertEquals($phone_number_unverified, $return, $entity
->id());
$return = $this->phoneNumberProvider
->getPhoneNumbers($entity, TRUE);
$this
->assertEquals($phone_number_verified, $return);
}
}
public function testGetPhoneNumbersNoVerification() {
$phoneNumberSettings = $this->phoneNumberSettings;
$this->phoneNumberSettings
->delete();
$entity = EntityTest::create([
$this->phoneField
->getName() => '+123123123',
]);
$entity
->save();
$phoneNumberSettings
->save();
$this
->assertEquals([], $this->phoneNumberProvider
->getPhoneNumbers($entity, TRUE));
}
public function testSendMessageUnverified() {
$phone_numbers = [
'+123123123',
];
$entity = $this
->createEntityWithPhoneNumber($this->phoneNumberSettings, $phone_numbers);
$this
->resetTestMessages();
$sms_message = new SmsMessage();
$sms_message
->setSenderNumber('+999888777')
->setMessage($this
->randomString());
$this
->setExpectedException(NoPhoneNumberException::class);
$this->phoneNumberProvider
->sendMessage($entity, $sms_message);
}
public function testSendMessageVerified() {
$phone_numbers = [
'+123123123',
];
$entity = $this
->createEntityWithPhoneNumber($this->phoneNumberSettings, $phone_numbers);
$this
->resetTestMessages();
$this
->verifyPhoneNumber($entity, $phone_numbers[0]);
$sms_message = new SmsMessage();
$sms_message
->setSenderNumber('+999888777')
->setMessage($this
->randomString());
$this->phoneNumberProvider
->sendMessage($entity, $sms_message);
$this
->assertEquals(1, count($this
->getTestMessages($this->gateway)));
}
public function testSendMessageOneMessage() {
$phone_numbers = [
'+123123123',
'+456456456',
];
$entity = $this
->createEntityWithPhoneNumber($this->phoneNumberSettings, $phone_numbers);
$this
->resetTestMessages();
$this
->verifyPhoneNumber($entity, $phone_numbers[0]);
$this
->verifyPhoneNumber($entity, $phone_numbers[1]);
$sms_message = new SmsMessage();
$sms_message
->setMessage($this
->randomString());
$this->phoneNumberProvider
->sendMessage($entity, $sms_message);
$message = $this
->getLastTestMessage($this->gateway);
$this
->assertEquals([
$phone_numbers[0],
], $message
->getRecipients(), 'The SMS message is using the first phone number from the entity.');
}
}